-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrectangle.py
More file actions
325 lines (277 loc) · 10.8 KB
/
rectangle.py
File metadata and controls
325 lines (277 loc) · 10.8 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
from guidata.utils.misc import assert_interfaces_valid
from plotpy.items.shape.polygon import PolygonShape
from plotpy.mathutils.geometry import (
compute_center,
vector_norm,
vector_projection,
vector_rotation,
)
if TYPE_CHECKING:
from plotpy.styles.shape import ShapeParam
def _no_null_vector(x0, y0, x1, y1, x2, y2, x3, y3):
return (
vector_norm(x0, y0, x1, y1)
and vector_norm(x0, y0, x2, y2)
and vector_norm(x0, y0, x3, y3)
and vector_norm(x1, y1, x2, y2)
and vector_norm(x1, y1, x3, y3)
and vector_norm(x2, y2, x3, y3)
)
class RectangleShape(PolygonShape):
"""Rectangle shape
Args:
x1: X coordinate of the top-left corner
y1: Y coordinate of the top-left corner
x2: X coordinate of the bottom-right corner
y2: Y coordinate of the bottom-right corner
shapeparam: Shape parameters
"""
CLOSED = True
_icon_name = "rectangle.png"
def __init__(
self,
x1: float = 0.0,
y1: float = 0.0,
x2: float = 0.0,
y2: float = 0.0,
shapeparam: ShapeParam | None = None,
) -> None:
super().__init__(shapeparam=shapeparam)
self.set_rect(x1, y1, x2, y2)
def set_rect(self, x1: float, y1: float, x2: float, y2: float) -> None:
"""Set the coordinates of the rectangle
Args:
x1: X coordinate of the top-left corner
y1: Y coordinate of the top-left corner
x2: X coordinate of the bottom-right corner
y2: Y coordinate of the bottom-right corner
"""
self.set_points([(x1, y1), (x2, y1), (x2, y2), (x1, y2)])
def get_rect(self) -> tuple[float, float, float, float]:
"""Return the coordinates of the rectangle
Returns:
Coordinates of the rectangle
"""
return tuple(self.points[0]) + tuple(self.points[2])
def get_center(self) -> tuple[float, float]:
"""Return center coordinates
Returns:
Center coordinates
"""
return compute_center(*self.get_rect())
def move_point_to(
self, handle: int, pos: tuple[float, float], ctrl: bool = False
) -> None:
"""Move a handle as returned by hit_test to the new position
Args:
handle: Handle
pos: Position
ctrl: True if <Ctrl> button is being pressed, False otherwise
"""
nx, ny = pos
x1, y1, x2, y2 = self.get_rect()
if handle == 0:
self.set_rect(nx, ny, x2, y2)
elif handle == 1:
self.set_rect(x1, ny, nx, y2)
elif handle == 2:
self.set_rect(x1, y1, nx, ny)
elif handle == 3:
self.set_rect(nx, y1, x2, ny)
elif handle == -1:
delta = (nx, ny) - self.points.mean(axis=0)
self.points += delta
def __reduce__(self) -> tuple:
"""Return the state of the object for pickling"""
state = (self.shapeparam, self.points, self.z())
return (self.__class__, (), state)
def __setstate__(self, state: tuple) -> None:
"""Set the state of the object from pickling"""
self.shapeparam, self.points, z = state
self.setZ(z)
self.shapeparam.update_item(self)
assert_interfaces_valid(RectangleShape)
class ObliqueRectangleShape(PolygonShape):
"""Oblique rectangle shape
Args:
x0: X coordinate of the top-left corner
y0: Y coordinate of the top-left corner
x1: X coordinate of the top-right corner
y1: Y coordinate of the top-right corner
x2: X coordinate of the bottom-right corner
y2: Y coordinate of the bottom-right corner
x3: X coordinate of the bottom-left corner
y3: Y coordinate of the bottom-left corner
shapeparam: Shape parameters
"""
CLOSED = True
ADDITIONNAL_POINTS = 2 # Number of points which are not part of the shape
LINK_ADDITIONNAL_POINTS = True # Link additionnal points with dotted lines
_icon_name = "oblique_rectangle.png"
def __init__(
self,
x0: float = 0.0,
y0: float = 0.0,
x1: float = 0.0,
y1: float = 0.0,
x2: float = 0.0,
y2: float = 0.0,
x3: float = 0.0,
y3: float = 0.0,
shapeparam: ShapeParam | None = None,
) -> None:
super().__init__(shapeparam=shapeparam)
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
def set_rect(
self,
x0: float,
y0: float,
x1: float,
y1: float,
x2: float,
y2: float,
x3: float,
y3: float,
) -> None:
"""Set the coordinates of the rectangle
Args:
x0: X coordinate of the top-left corner
y0: Y coordinate of the top-left corner
x1: X coordinate of the top-right corner
y1: Y coordinate of the top-right corner
x2: X coordinate of the bottom-right corner
y2: Y coordinate of the bottom-right corner
x3: X coordinate of the bottom-left corner
y3: Y coordinate of the bottom-left corner
::
x: additionnal points (handles used for rotation -- other handles
being used for rectangle resizing)
(x0, y0)------>(x1, y1)
↑ |
| |
x x
| |
| ↓
(x3, y3)<------(x2, y2)
"""
self.set_points(
[
(x0, y0),
(x1, y1),
(x2, y2),
(x3, y3),
(0.5 * (x0 + x3), 0.5 * (y0 + y3)),
(0.5 * (x1 + x2), 0.5 * (y1 + y2)),
]
)
def get_rect(self) -> tuple[float, float, float, float, float, float, float, float]:
"""Return the coordinates of the rectangle
Returns:
Coordinates of the rectangle
"""
return self.points.ravel()[: -self.ADDITIONNAL_POINTS * 2]
def get_center(self) -> tuple[float, float]:
"""Return center coordinates
Returns:
Center coordinates
"""
rect = tuple(self.points[0]) + tuple(self.points[2])
return compute_center(*rect)
def move_point_to(
self, handle: int, pos: tuple[float, float], ctrl: bool = False
) -> None:
"""Move a handle as returned by hit_test to the new position
Args:
handle: Handle
pos: Position
ctrl: True if <Ctrl> button is being pressed, False otherwise
"""
nx, ny = pos
x0, y0, x1, y1, x2, y2, x3, y3 = self.get_rect()
if handle == 0:
if vector_norm(x2, y2, x3, y3) and vector_norm(x2, y2, x1, y1):
v0n = np.array((nx - x0, ny - y0))
x3, y3 = vector_projection(v0n, x2, y2, x3, y3)
x1, y1 = vector_projection(v0n, x2, y2, x1, y1)
x0, y0 = nx, ny
if _no_null_vector(x0, y0, x1, y1, x2, y2, x3, y3):
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
elif handle == 1:
if vector_norm(x3, y3, x0, y0) and vector_norm(x3, y3, x2, y2):
v1n = np.array((nx - x1, ny - y1))
x0, y0 = vector_projection(v1n, x3, y3, x0, y0)
x2, y2 = vector_projection(v1n, x3, y3, x2, y2)
x1, y1 = nx, ny
if _no_null_vector(x0, y0, x1, y1, x2, y2, x3, y3):
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
elif handle == 2:
if vector_norm(x0, y0, x1, y1) and vector_norm(x0, y0, x3, y3):
v2n = np.array((nx - x2, ny - y2))
x1, y1 = vector_projection(v2n, x0, y0, x1, y1)
x3, y3 = vector_projection(v2n, x0, y0, x3, y3)
x2, y2 = nx, ny
if _no_null_vector(x0, y0, x1, y1, x2, y2, x3, y3):
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
elif handle == 3:
if vector_norm(x1, y1, x0, y0) and vector_norm(x1, y1, x2, y2):
v3n = np.array((nx - x3, ny - y3))
x0, y0 = vector_projection(v3n, x1, y1, x0, y0)
x2, y2 = vector_projection(v3n, x1, y1, x2, y2)
x3, y3 = nx, ny
if _no_null_vector(x0, y0, x1, y1, x2, y2, x3, y3):
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
elif handle == 4:
x4, y4 = 0.5 * (x0 + x3), 0.5 * (y0 + y3)
x5, y5 = 0.5 * (x1 + x2), 0.5 * (y1 + y2)
nx, ny = x0 + nx - x4, y0 + ny - y4 # moving handle #4 to handle #0
v10 = np.array((x0 - x1, y0 - y1))
v12 = np.array((x2 - x1, y2 - y1))
v10n = np.array((nx - x1, ny - y1))
k = np.linalg.norm(v12) / np.linalg.norm(v10)
v12n = vector_rotation(-np.pi / 2, *v10n) * k
x2, y2 = v12n + np.array([x1, y1])
x3, y3 = v12n + v10n + np.array([x1, y1])
x0, y0 = nx, ny
dx = x5 - 0.5 * (x1 + x2)
dy = y5 - 0.5 * (y1 + y2)
x0, y0 = x0 + dx, y0 + dy
x1, y1 = x1 + dx, y1 + dy
x2, y2 = x2 + dx, y2 + dy
x3, y3 = x3 + dx, y3 + dy
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
elif handle == 5:
x4, y4 = 0.5 * (x0 + x3), 0.5 * (y0 + y3)
x5, y5 = 0.5 * (x1 + x2), 0.5 * (y1 + y2)
nx, ny = x1 + nx - x5, y1 + ny - y5 # moving handle #5 to handle #1
v01 = np.array((x1 - x0, y1 - y0))
v03 = np.array((x3 - x0, y3 - y0))
v01n = np.array((nx - x0, ny - y0))
k = np.linalg.norm(v03) / np.linalg.norm(v01)
v03n = vector_rotation(np.pi / 2, *v01n) * k
x3, y3 = v03n + np.array([x0, y0])
x2, y2 = v03n + v01n + np.array([x0, y0])
x1, y1 = nx, ny
dx = x4 - 0.5 * (x0 + x3)
dy = y4 - 0.5 * (y0 + y3)
x0, y0 = x0 + dx, y0 + dy
x1, y1 = x1 + dx, y1 + dy
x2, y2 = x2 + dx, y2 + dy
x3, y3 = x3 + dx, y3 + dy
self.set_rect(x0, y0, x1, y1, x2, y2, x3, y3)
elif handle == -1:
delta = (nx, ny) - self.points.mean(axis=0)
self.points += delta
def __reduce__(self) -> tuple:
"""Return the state of the object for pickling"""
state = (self.shapeparam, self.points, self.z())
return (self.__class__, (), state)
def __setstate__(self, state: tuple) -> None:
"""Set the state of the object from pickling"""
self.shapeparam, self.points, z = state
self.setZ(z)
self.shapeparam.update_item(self)
assert_interfaces_valid(ObliqueRectangleShape)