-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeometry.py
269 lines (205 loc) · 8.95 KB
/
geometry.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from PyQt5.QtWidgets import QGraphicsEllipseItem, QGraphicsPathItem, QGraphicsLineItem
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtGui import QPen, QPainter, QPainterPath, QColor, QPolygonF
import numpy as np
from scipy.interpolate import splprep, splev
class Marker(QGraphicsLineItem):
"""Class that describes a line in the longitudinal view"""
def __init__(self, pos, display_height, display_length, color=[173, 216, 230], dashed=False):
super(Marker, self).__init__()
self.setZValue(3)
self.display_height = display_height
self.display_length = display_length
self.defaultColor = QPen(QColor(color[0], color[1], color[2]), 2)
if dashed:
self.defaultColor.setDashPattern([10,10,10])
self.setLine(pos, 0, pos, self.display_height)
self.setPen(self.defaultColor)
self.pos = self.display_length
def update(self, pos):
"""Updates the Point position"""
self.pos = pos
# point must be constrained to bottom and top of scene
if pos < 0:
pos = 0
elif pos > self.display_length:
pos = self.display_length
self.setLine(pos, 0, pos, self.display_height)
return pos
class Arrowhead(QGraphicsPathItem):
def __init__(self, source: QPointF = None, direction: str = "left"):
super(Arrowhead, self).__init__()
self._sourcePoint = source
self.defaultColor = QPen(QColor(255, 255, 255), 2)
self.setPen(self.defaultColor)
self.direction = direction
#self.setZValue(3)
def directPath(self):
p1 = QPointF(self._sourcePoint.x(), self._sourcePoint.y() + 5.0)
p2 = QPointF(self._sourcePoint.x(), self._sourcePoint.y() - 5.0)
if self.direction == "right":
p3 = QPointF(self._sourcePoint.x() + 5.0, self._sourcePoint.y())
else:
p3 = QPointF(self._sourcePoint.x() - 5.0, self._sourcePoint.y())
path = QPainterPath(p1)
path.lineTo(p2)
path.lineTo(p3)
path.closeSubpath()
return path
def draw(self):
path = self.directPath()
self.setPath(path)
class Arrowbody(QGraphicsPathItem):
def __init__(self, source: QPointF = None, destination: QPointF = None, *args, **kwargs):
super(Arrowbody, self).__init__(*args, **kwargs)
self._sourcePoint = source
self._destinationPoint = destination
self.defaultColor = QPen(QColor(255, 255, 255), 2)
self.setPen(self.defaultColor)
#self.setZValue(3)
self._arrow_height = 5
self._arrow_width = 4
def setSource(self, point: QPointF):
self._sourcePoint = point
def setDestination(self, point: QPointF):
self._destinationPoint = point
def directPath(self):
path = QPainterPath(self._sourcePoint)
path.lineTo(self._destinationPoint)
return path
def arrowCalc(self, start_point=None, end_point=None): # calculates the point where the arrow should be drawn
try:
startPoint, endPoint = start_point, end_point
if start_point is None:
startPoint = self._sourcePoint
if endPoint is None:
endPoint = self._destinationPoint
dx, dy = startPoint.x() - endPoint.x(), startPoint.y() - endPoint.y()
leng = math.sqrt(dx ** 2 + dy ** 2)
normX, normY = dx / leng, dy / leng # normalize
# perpendicular vector
perpX = -normY
perpY = normX
leftX = endPoint.x() + self._arrow_height * normX + self._arrow_width * perpX
leftY = endPoint.y() + self._arrow_height * normY + self._arrow_width * perpY
rightX = endPoint.x() + self._arrow_height * normX - self._arrow_width * perpX
rightY = endPoint.y() + self._arrow_height * normY - self._arrow_width * perpY
point2 = QPointF(leftX, leftY)
point3 = QPointF(rightX, rightY)
return QPolygonF([point2, endPoint, point3])
except (ZeroDivisionError, Exception):
return None
def draw(self):
path = self.directPath()
self.setPath(path)
#def paint(self, painter: QPainter, option, widget=None) -> None:
# painter.setRenderHint(painter.Antialiasing)
# painter.pen().setWidth(2)
# painter.setBrush(Qt.NoBrush)
# path = self.directPath()
# painter.drawPath(path)
# self.setPath(path)
# triangle_source = self.arrowCalc(path.pointAtPercent(0.1), self._sourcePoint) # change path.PointAtPercent() value to move arrow on the line
# if triangle_source is not None:
# painter.drawPolyline(triangle_source)
class Line(QGraphicsLineItem):
"""Class that describes a line in the cross-section view"""
def __init__(self, pos, display_size):
super(Line, self).__init__()
self.setZValue(3)
self.display_size = display_size
image_radius = display_size//2
self.defaultColor = QPen(QColor(173, 216, 230), 5)
self.setLine(pos[0], pos[1], pos[2], pos[3])
self.setPen(self.defaultColor)
theta = np.linspace(0, 2*np.pi, 180)
self.points = [[image_radius*np.cos(val) + image_radius, image_radius*np.sin(val) + image_radius] for val in theta]
def update(self, pos):
"""Updates the Point position"""
dist = [np.sqrt((pt[0] - pos.x())**2 + (pt[1] - pos.y())**2) for pt in self.points]
idx = [i for i, val in enumerate(dist) if val == min(dist)]
# point must be constrained to circular path
new_x = self.points[idx[0]][0]
new_y = self.points[idx[0]][1]
if new_x > self.display_size//2:
new_x1 = self.display_size - new_x
else:
new_x1 = self.display_size - new_x
if new_y > self.display_size//2:
new_y1 = self.display_size - new_y
else:
new_y1 = self.display_size - new_y
self.setLine(new_x1, new_y1, new_x, new_y)
return [new_x1, new_y1, new_x, new_y]
class Point(QGraphicsEllipseItem):
"""Class that describes a spline point"""
def __init__(self, pos, color):
super(Point, self).__init__()
self.setZValue(3)
if color =='y':
self.defaultColor = QPen(Qt.yellow, 5)
elif color =='r':
self.defaultColor = QPen(Qt.red, 5)
else:
self.defaultColor = QPen(Qt.blue, 5)
self.setPen(self.defaultColor)
self.setRect(pos[0], pos[1], 3, 3)
def select_point(self, pos):
"""Identifies what point has been selected with the mouse"""
dist = np.sqrt((self.rect().x() - pos.x())**2 + (self.rect().y() - pos.y())**2)
return dist
def getPoint(self):
return self.rect().x(), self.rect().y()
def updateColor(self):
self.setPen(QPen(Qt.blue, 2))
def resetColor(self):
self.setPen(self.defaultColor)
def update(self, pos):
"""Updates the Point position"""
self.setRect(pos.x(), pos.y(), 3, 3)
return self.rect()
class Spline(QGraphicsPathItem):
"""Class that describes a spline"""
def __init__(self, points, color):
self.setKnotPoints(points)
self.setZValue(3)
if color =='y':
self.setPen(QPen(Qt.yellow, 2))
elif color == "r":
self.setPen(QPen(Qt.red, 2))
else:
self.setPen(QPen(Qt.blue, 2))
def setKnotPoints(self, knotPoints):
"""KnotPoints is a list of points"""
p1 = QPointF(knotPoints[0][0], knotPoints[1][0])
self.path = QPainterPath(p1)
super(Spline, self).__init__(self.path)
self.points = self.interpolate(knotPoints)
for i in range(0, len(self.points[0])):
self.path.lineTo(self.points[0][i], self.points[1][i])
self.setPath(self.path)
self.path.closeSubpath()
self.knotPoints = knotPoints
def interpolate(self, pts):
"""Interpolates the spline points at 500 points along spline"""
pts = np.array(pts)
tck, u = splprep(pts, u=None, s=0.0, per=1)
u_new = np.linspace(u.min(), u.max(), 500)
x_new, y_new = splev(u_new, tck, der=0)
return (x_new, y_new)
def update(self, pos, idx):
"""Updates the stored spline everytime it is moved
Args:
pos: new points coordinates
idx: index on spline of updated point
"""
if idx == len(self.knotPoints[0]) + 1:
self.knotPoints[0].append(pos.x())
self.knotPoints[1].append(pos.y())
else:
self.knotPoints[0][idx] = pos.x()
self.knotPoints[1][idx] = pos.y()
self.points = self.interpolate(self.knotPoints)
for i in range(0, len(self.points[0])):
self.path.setElementPositionAt(i, self.points[0][i], self.points[1][i])
self.setPath(self.path)