-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirBend.py
181 lines (147 loc) · 6.72 KB
/
DirBend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import numpy as np
from numpy import sin, cos
import shapely
#from shapely import LineString, Point, Polygon
from shapely.geometry import Point, Polygon
import shapely.geometry.polygon
from shapely import affinity
from shapely.ops import nearest_points
import vedo as v
from enum import Enum
import copy
from Transformation import *
from LinearTransformation import *
DIR = Enum('DIR', 'NEGY POSY NEGX POSX')#
global MAX_EDGE_LENGTH
class DirBend(Transformation):
def __init__(self, data, prio=0, addResidual=True, name=None):
self.boundaries_rot = None
self.prio = prio
self.addResidual = addResidual
self.name = name
self.parent = None
if "points" not in data: # found point data; prioritize those
raise ValueError("Not enough data for Transformation {}".format(self.name))
if len(data["points"]) < 4:
raise ValueError("Not enough points in points array of transformation {}: {} (expecting 4+)".format(data["name"], len(data["points"])))
poly = Polygon([[p["x"], p["y"]] for p in data["points"]])
baseline = LineString(((data["points"][0]["x"], data["points"][0]["y"]), (data["points"][1]["x"], data["points"][1]["y"])))
self.boundaries = poly
super().__init__(self.boundaries, prio, addResidual, name)
minx, miny, maxx, maxy = poly.bounds
bounding_box = shapely.geometry.box(minx, miny, maxx, maxy)
ax, ay, bx, by = baseline.bounds
self.pivot = list(poly.exterior.coords[0])
self.pivot.append(0)
if ax == bx: # vertical line
extended_line = LineString([(ax, miny), (ax, maxy)])
elif ay == by: # horizonthal line
extended_line = LineString([(minx, ay), (maxx, ay)])
else:
# linear equation: y = k*x + m
m = -(ay - by) / (ax - bx)
n = ay - m * ax + (by-ay)
y0 = m * minx + n
y1 = m * maxx + n
x0 = (miny - n) / m
x1 = (maxy - n) / m
print ("m: {}; n: {}; P1({}, {}), 2({}, {})".format(m, n, x0, y0, x1, y1))
print("Angle is {}".format(np.arctan(m)/(2*np.pi)*360))
self.extLine = LineString((Point(minx, y0), Point(maxx, y1)))
dists = [self.extLine.distance(Point(p)) for p in poly.exterior.coords]
distPoints = [(dists[i], poly.exterior.coords[i]) for i in range(len(poly.exterior.coords)) if dists[i] > 0]
points_sorted_by_distance = sorted(distPoints, key=lambda x: x[0])
length = points_sorted_by_distance[0][0]
print(">>min: {}".format(length))
self.projPoint, _ = nearest_points(self.extLine, Point(points_sorted_by_distance[0][1]))
self.ortho = LineString([self.projPoint, Point(points_sorted_by_distance[0][1])])
print(self.projPoint)
self.angle = np.deg2rad(data["angle"])
self.z_angle = np.arctan((bx-ax)/(ay-by))
print("Z_ANGLE: {}".format(np.rad2deg(self.z_angle)))
self.boundaries_rot = affinity.rotate(poly, self.z_angle, origin=self.pivot, use_radians=True)
r = length / self.angle
a = self.angle
self.newTr = np.zeros((3, 4), dtype=float)
self.newTr[0] = cos(a), 0, -sin(a), -(self.pivot[0]+length) * cos(a) + self.pivot[0] + r * (sin(a))
self.newTr[1] = 0, 1, 0, 0
self.newTr[2] = sin(a), 0, cos(a), -(self.pivot[0]+length) * sin(a) + r * (1 - cos(a))
self.baseline = baseline
self.length = length
# self.points = []
self.meshes = []
self.mel = []
self.transformWholeMesh = True
def __repr__(self):
return "Tr.DirBend: [P={}; Res={}; angle={}; len={}; baseline={}; bounds={}]".format(self.prio, self.addResidual, self.angle, self.length, self.baseline, self.boundaries)
def __str__(self):
return "Tr.DirBend: [P={}; Res={}; angle={}; len={}; baseline={}; bounds={}]".format(self.prio, self.addResidual, self.angle, self.length, self.baseline, self.boundaries)
def debugShow(self):
def getPoints(obj):
x = obj.coords.xy[0]
y = obj.coords.xy[1]
z = [0] * len(x)
pts = list(zip(x, y, z))
return pts
ext = v.Line(getPoints(self.extLine), closed=False).c("Blue")
ortho = v.Line(getPoints(self.ortho), closed=False).c("Yellow")
perp = v.Point((self.projPoint.x, self.projPoint.y, 0), r=12, c="Red")
print(self.extLine)
return v.merge(ext, perp, ortho)
def isInScope(self, point):
pt = Point(point[0], point[1])
if not pt.disjoint(self.boundaries):
return True
def getOutlinePts(self):
poly = shapely.geometry.polygon.orient(self.boundaries)
x = poly.exterior.coords.xy[0][:-1]
y = poly.exterior.coords.xy[1][:-1]
z = [0] * len(x)
pts = list(zip(x, y, z))
return pts
def getOutline(self):
return v.Line(self.getOutlinePts(), closed=True)
def getBorderlinePts(self):
p0 = self.baseline.coords[0]
p1 = self.baseline.coords[1]
pts = [(p0[0], p0[1], 0), (p1[0], p1[1], 0)]
return pts
def getBorderline(self):
return Line(self.getBorderlinePts())
def getResidualTransformation(self, mesh=None): #TODO
newBounds = shapely.geometry.box(self.pivot[0], self.pivot[1], self.pivot[0]+100, self.pivot[1]+100)
ret = LinearTransformation(self.newTr, newBounds, self.prio, residual=True, angle=self.z_angle, pivot=self.pivot)
ret.name = self.name + "-Res"
return ret
def transformMesh(self, mesh):
print("--> Transforming a whole mesh now")
mesh.rotate_z(self.z_angle, rad=True, around=self.pivot)
points = mesh.points()
for pid, pt in enumerate(points):
self.parent.update_progress(pid, len(points))
vec = np.array([pt[0], pt[1], pt[2], 1])
vec = np.dot(self.getMatrixAt(pt), vec)
points[pid][0] = vec[0]
points[pid][1] = vec[1]
points[pid][2] = vec[2]
mesh.points(points)
mesh.rotate_z(-self.z_angle, rad=True, around=self.pivot)
return mesh
def getMatrixAt(self, pt): #TODO
x = pt[0]
y = pt[1]
if x > (self.pivot[0]+self.length):
return self.newTr
mat = np.zeros((3, 4), dtype=float)
if self.boundaries_rot.disjoint(Point(x, y)):
mat[0] = 1, 0, 0, 0
mat[1] = 0, 1, 0, 0
mat[2] = 0, 0, 1, 0
return mat
r = abs(self.length) / self.angle
t = abs(x - self.pivot[0]) / self.length
a = t * self.angle
mat[0] = 0, 0, -sin(a), self.pivot[0] + r * sin(a)
mat[1] = 0, 1, 0, 0
mat[2] = 0, 0, cos(a), (1 - cos(a)) * r
return mat