/
polyline.py
724 lines (595 loc) · 23 KB
/
polyline.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from compas.geometry import Frame
from compas.geometry import Line
from compas.geometry import Point
from compas.geometry import is_point_on_line
from compas.geometry import is_point_on_polyline
from compas.geometry import transform_points
from compas.itertools import pairwise
from compas.tolerance import TOL
from .curve import Curve
class Polyline(Curve):
"""A polyline is a curve defined by a sequence of points connected by line segments.
A Polyline can be open or closed.
It can be self-intersecting.
It does not have an interior.
The parameter space is defined along the consecutive direction vectors of the line segments of the polyline.
The coordinate system of the parametrisation is the world coordinate system.
Transformations of polylines are defined as transformations of the points defining the polyline.
Parameters
----------
points : list[[float, float, float] | :class:`compas.geometry.Point`]
An ordered list of points.
Each consecutive pair of points forms a segment of the polyline.
name : str, optional
The name of the polyline.
Attributes
----------
frame : :class:`compas.geometry.Frame`, read-only
The frame of the spatial coordinates of the polyline.
This is always the world XY frame.
points : list[:class:`compas.geometry.Point`]
The points of the polyline.
lines : list[:class:`compas.geometry.Line`], read-only
The lines of the polyline.
length : float, read-only
The length of the polyline.
start : :class:`compas.geometry.Point`, read-only
The start point of the polyline.
end : :class:`compas.geometry.Point`, read-only
The end point of the polyline.
is_selfintersecting : bool, read-only
True if the polyline is self-intersecting.
is_closed : bool, read-only
True if the polyline is closed.
Examples
--------
>>> polyline = Polyline([[0,0,0], [1,0,0], [2,0,0], [3,0,0]])
>>> polyline.length
3.0
>>> type(polyline.points[0]) == Point
True
>>> polyline.points[0].x
0.0
>>> type(polyline.lines[0]) == Line
True
>>> polyline.lines[0].length
1.0
"""
# overwriting the __new__ method is necessary
# to avoid triggering the plugin mechanism of the base curve class
def __new__(cls, *args, **kwargs):
curve = object.__new__(cls)
curve.__init__(*args, **kwargs)
return curve
DATASCHEMA = {
"type": "object",
"properties": {
"points": {"type": "array", "minItems": 2, "items": Point.DATASCHEMA},
},
"required": ["points"],
}
@property
def __data__(self):
return {"points": [point.__data__ for point in self.points]}
def __init__(self, points, name=None):
super(Polyline, self).__init__(name=name)
self._points = []
self._lines = []
self.points = points
def __repr__(self):
return "{0}({1!r})".format(
type(self).__name__,
self.points,
)
def __getitem__(self, key):
return self.points[key]
def __setitem__(self, key, value):
self.points[key] = Point(*value)
self._lines = None
def __iter__(self):
return iter(self.points)
def __len__(self):
return len(self.points)
def __eq__(self, other):
if not hasattr(other, "__iter__") or not hasattr(other, "__len__") or len(self) != len(other):
return False
return TOL.is_allclose(self, other)
# ==========================================================================
# properties
# ==========================================================================
@property
def frame(self):
return Frame.worldXY()
@frame.setter
def frame(self, frame):
raise AttributeError("Setting the coordinate frame of a polyline is not supported.")
@property
def points(self):
return self._points
@points.setter
def points(self, points):
self._points = [Point(*xyz) for xyz in points]
self._lines = None
@property
def lines(self):
if self._lines is None:
self._lines = [Line(a, b) for a, b in pairwise(self.points)]
return self._lines
@property
def length(self):
return sum([line.length for line in self.lines])
@property
def start(self):
return self.points[0]
@property
def end(self):
return self.points[-1]
@property
def is_selfintersecting(self):
raise NotImplementedError
@property
def is_closed(self):
return self.points[0] == self.points[-1]
# ==========================================================================
# Constructors
# ==========================================================================
# ==========================================================================
# Transformations
# ==========================================================================
def transform(self, T):
"""Transform this polyline.
Parameters
----------
T : :class:`compas.geometry.Transformation` | list[list[float]]
The transformation.
Examples
--------
>>> from math import radians
>>> from compas.geometry import Rotation
>>> polyline = Polyline([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]])
>>> R = Rotation.from_axis_and_angle([0.0, 0.0, 1.0], radians(90))
>>> polyline.transform(R)
"""
for index, point in enumerate(transform_points(self.points, T)):
self.points[index].x = point[0]
self.points[index].y = point[1]
self.points[index].z = point[2]
# ==========================================================================
# Methods
# ==========================================================================
def append(self, point):
"""Append a point to the end of the polyline.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
The point to append.
"""
self.points.append(Point(*point))
self._lines = None
def insert(self, i, point):
"""Insert a point at the specified index.
Parameters
----------
i : int
The index of the insertion point.
point : [float, float, float] | :class:`compas.geometry.Point`
The point to insert.
"""
self.points.insert(i, Point(*point))
self._lines = None
def point_at(self, t, snap=False):
"""Point on the polyline at a specific normalized parameter.
Parameters
----------
t : float
The parameter value.
snap : bool, optional
If True, return the closest polyline point.
Returns
-------
:class:`compas.geometry.Point`
The point on the polyline.
Examples
--------
>>> polyline = Polyline([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0]])
>>> polyline.point(0.75)
Point(1.000, 0.500, 0.000)
"""
if t < 0 or t > 1:
return None
points = self.points
if t == 0:
return points[0]
if t == 1:
return points[-1]
polyline_length = self.length
x = 0
i = 0
while x <= t:
line = Line(points[i], points[i + 1])
line_length = line.length
dx = line_length / polyline_length
if x + dx > t:
if snap:
if t - x < x + dx - t:
return line.start
else:
return line.end
return line.point_at((t - x) * polyline_length / line_length)
x += dx
i += 1
def parameter_at(self, point, tol=None):
"""Parameter of the polyline at a specific point.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
The point on the polyline.
tol : float, optional
A tolerance value for verifying that the point is on the polyline.
Default is :attr:`TOL.absolute`.
Returns
-------
float
The parameter of the polyline.
Examples
--------
>>> from compas.geometry import Point
>>> polyline = Polyline([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0]])
>>> polyline.parameter_at(Point(0.1, 0.0, 0.0))
0.5
"""
if not is_point_on_polyline(point, self, tol):
raise Exception("{} not found!".format(point))
dx = 0
for line in self.lines:
if not is_point_on_line(point, line, tol):
dx += line.length
continue
dx += line.start.distance_to_point(point)
break
return dx / self.length
def tangent_at(self, t):
"""Tangent vector at a specific normalized parameter.
Parameters
----------
t : float
The parameter value.
Returns
-------
:class:`compas.geometry.Vector`
The tangent vector at the specified parameter.
Examples
--------
>>> polyline = Polyline([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0]])
>>> polyline.tangent(0.75)
Vector(0.000, 1.000, 0.000)
"""
if t < 0 or t > 1:
return None
points = self.points
if t == 0:
return points[1] - points[0]
if t == 1:
return points[-1] - points[-2]
polyline_length = self.length
x = 0
i = 0
while x <= t:
line = Line(points[i], points[i + 1])
line_length = line.length
dx = line_length / polyline_length
if x + dx > t:
return line.direction
x += dx
i += 1
def tangent_at_point(self, point):
"""Calculates the tangent vector of a point on a polyline
Parameters
----------
point: [float, float, float] | :class:`compas.geometry.Point`
Returns
-------
:class:`compas.geometry.Vector`
"""
for line in self.lines:
if is_point_on_line(point, line):
return line.direction
raise Exception("{} not found!".format(point))
def split_at_corners(self, angle_threshold):
"""Splits a polyline at corners larger than the given angle_threshold
Parameters
----------
angle_threshold : float
In radians.
Returns
-------
list[:class:`compas.geometry.Polyline`]
"""
corner_ids = []
split_polylines = []
points = self.points
seg_ids = list(range(len(self.lines)))
if self.is_closed:
seg_ids.append(0)
for seg1, seg2 in pairwise(seg_ids):
angle = self.lines[seg2].vector.angle(-self.lines[seg1].vector)
if angle >= angle_threshold:
corner_ids.append(seg1 + 1)
if self.is_closed and len(corner_ids) > 0:
if corner_ids[-1] != len(points):
corner_ids = [corner_ids[-1]] + corner_ids
else:
corner_ids = [0] + corner_ids + [len(points)]
for id1, id2 in pairwise(corner_ids):
if id1 < id2:
split_polylines.append(Polyline(points[id1 : id2 + 1]))
else:
looped_pts = [points[i] for i in range(id1, len(points))] + points[1 : id2 + 1]
split_polylines.append(Polyline(looped_pts))
if self.is_closed and not corner_ids:
return [Polyline(self.points)]
return split_polylines
def divide_at_corners(self, angle_threshold):
"""Divides a polyline at corners larger than the given angle_threshold
Parameters
----------
angle_threshold : float
In radians.
Returns
-------
list[:class:`compas.geometry.Point`]
"""
corner_ids = []
seg_ids = list(range(len(self.lines)))
if self.is_closed:
seg_ids.insert(0, seg_ids[-1])
for seg1, seg2 in pairwise(seg_ids):
angle = self.lines[seg2].vector.angle(-self.lines[seg1].vector)
if angle >= angle_threshold:
corner_ids.append(seg1 + 1)
return [self.points[i] for i in corner_ids]
def divide(self, num_segments):
"""Divide a polyline in equal segments.
Parameters
----------
num_segments : int
Returns
-------
list
list[:class:`compas.geometry.Point`]
Examples
--------
>>> polyline = Polyline([(0, 0, 0), (1, 1, 0), (2, 3, 0), (4, 4, 0), (5, 2, 0)])
>>> divided_polylines = polyline.divide(3)
>>> divided_polyline
[Point(0.000, 0.000, 0.000), Point(1.578, 2.157, 0.000), Point(3.578, 3.789, 0.000), Point(5.000, 2.000, 0.000)]
"""
segment_length = self.length / num_segments
return self.divide_by_length(segment_length, False)
def divide_by_length(self, length, strict=True, tol=1e-06):
"""Divide a polyline in segments of a given length.
Parameters
----------
length : float
Length of the segments.
strict : bool, optional
If False, the remainder segment will be added even if it is smaller than the desired length
tol : float, optional
Floating point error tolerance.
Returns
-------
list[:class:`compas.geometry.Point`]
Notes
-----
The points of the new polyline are constrained to the segments of the old polyline.
However, since the old points are not part of the new set of points, the geometry of the polyline will change.
Examples
--------
>>> polyline = Polyline([(0, 0, 0), (1, 1, 0), (2, 3, 0), (4, 4, 0), (5, 2, 0)])
>>> divided_polylines = polyline.divide_by_length(3)
>>> divided_polyline
[Point(0.000, 0.000, 0.000), Point(1.709, 2.418, 0.000), Point(4.051, 3.898, 0.000)]
>>> polyline = Polyline([(0, 0, 0), (1, 1, 0), (2, 3, 0), (4, 4, 0), (5, 2, 0)])
>>> divided_polylines = polyline.divide_by_length(3, strict=False)
>>> divided_polyline
[Point(0.000, 0.000, 0.000), Point(1.709, 2.418, 0.000), Point(4.051, 3.898, 0.000), Point(5.000, 2.000, 0.000)]
"""
num_pts = int(self.length / length)
total_length = [0, 0]
division_pts = [self.points[0]]
new_polyline = self
for i in range(num_pts):
for i_ln, line in enumerate(new_polyline.lines):
total_length.append(total_length[-1] + line.length) # type: ignore
if total_length[-1] > length:
amp = (length - total_length[-2]) / line.length
new_pt = line.start + line.vector.scaled(amp)
division_pts.append(new_pt)
total_length = [0, 0]
remaining_pts = new_polyline.points[i_ln + 2 :]
new_polyline = Polyline([new_pt, line.end] + remaining_pts)
break
elif total_length[-1] == length:
total_length = [0, 0]
division_pts.append(line.end)
if len(division_pts) == num_pts + 1:
break
if strict is False and not self.is_closed and len(division_pts) < num_pts + 1:
division_pts.append(new_polyline.points[-1])
elif strict is False and division_pts[-1].distance_to_point(self.points[-1]) > tol:
division_pts.append(self.points[-1])
return division_pts
def split_by_length(self, length, strict=True):
"""Split a polyline in segments of a given length.
Parameters
----------
length : float
Length of the segments.
strict : bool, optional
If False, the remainder segment will be added even if it is smaller than the desired length
tol : float, optional
Floating point error tolerance.
Returns
-------
list[:class:`compas.geometry.Polyline`]
Examples
--------
>>> from compas.geometry import Polyline
>>> polyline = Polyline([(0, 0, 0), (1, 1, 0), (2, 3, 0), (4, 4, 0), (5, 2, 0)])
>>> split_polylines = polyline.split_polyline_by_length(3)
>>> split_polylines
[Polyline([Point(0.000, 0.000, 0.000), Point(1.000, 1.000, 0.000), Point(1.709, 2.418, 0.000)]),\
Polyline([Point(1.709, 2.418, 0.000), Point(2.000, 3.000, 0.000), Point(4.000, 4.000, 0.000),\
Point(4.051, 3.898, 0.000)])]
>>> from compas.geometry import Polyline
>>> polyline = Polyline([(0, 0, 0), (1, 1, 0), (2, 3, 0), (4, 4, 0), (5, 2, 0)])
>>> split_polylines = polyline.split_polyline_by_length(3, strict=False)
>>> split_polylines
[Polyline([Point(0.000, 0.000, 0.000), Point(1.000, 1.000, 0.000), Point(1.709, 2.418, 0.000)]),\
Polyline([Point(1.709, 2.418, 0.000), Point(2.000, 3.000, 0.000), Point(4.000, 4.000, 0.000),\
Point(4.051, 3.898, 0.000)]), Polyline([Point(4.051, 3.898, 0.000), Point(5.000, 2.000, 0.000)])]
"""
if length <= 0:
raise ValueError("Length should be bigger than 0.")
elif length > self.length:
raise ValueError("Polyline length {0} is smaller than input length {1}.".format(self.length, length))
divided_polylines = []
polyline_copy = self.copy()
segment = Polyline([self[0]]) # Start a new segment
i, current_length = 0, 0
polyline_points_num = len(polyline_copy)
while i < polyline_points_num - 1:
pt1, pt2 = polyline_copy.points[i : i + 2]
line_length = pt1.distance_to_point(pt2)
current_length += line_length
if current_length <= length:
segment.points.append(pt2)
i += 1
else:
amp = 1 - ((current_length - length) / line_length)
new_pt = pt1 + (pt2 - pt1).scaled(amp)
polyline_copy.points.insert(i + 1, new_pt)
segment.points.append(new_pt)
divided_polylines.append(segment)
segment = Polyline([new_pt]) # Start a new segment
current_length = 0
i += 1
polyline_points_num = len(polyline_copy)
if not strict and len(divided_polylines):
divided_polylines.append(segment) # Add the last segment
return divided_polylines
def split(self, num_segments):
"""Split a polyline in equal segments.
Parameters
----------
num_segments : int
Returns
-------
list
list[:class:`compas.geometry.Polyline`]
Examples
--------
>>> from compas.geometry import Polyline
>>> polyline = Polyline([(0, 0, 0), (1, 1, 0), (2, 3, 0), (4, 4, 0), (5, 2, 0)])
>>> split_polylines = polyline.split_polyline(3)
>>> split_polylines
[Polyline([Point(0.000, 0.000, 0.000), Point(1.000, 1.000, 0.000), Point(1.578, 2.157, 0.000)]),\
Polyline([Point(1.578, 2.157, 0.000), Point(2.000, 3.000, 0.000), Point(3.578, 3.789, 0.000)]),\
Polyline([Point(3.578, 3.789, 0.000), Point(4.000, 4.000, 0.000), Point(5.000, 2.000, 0.000)])]
"""
if num_segments < 1:
raise ValueError("Number of segments must be greater than or equal to 1.")
elif num_segments == 1:
return [self]
total_length = self.length
segment_length = total_length / num_segments
return self.split_by_length(segment_length, False)
def extend(self, length):
"""Extends a polyline by a given length, by modifying the first and/or last point tangentially.
Parameters
----------
length: float or tuple[float, float]
A single length value to extend the polyline only at the end,
or two length values to extend at both ends.
Returns
-------
None
"""
try:
start, end = length
self.points[0] = self.points[0] + self.lines[0].vector.unitized().scaled(-start)
self._lines = None
except TypeError:
start = end = length
self.points[-1] = self.points[-1] + self.lines[-1].vector.unitized().scaled(end)
self._lines = None
def extended(self, length):
"""Returns a copy of this polyline extended by a given length.
Parameters
----------
length: float or tuple[float, float]
A single length value to extend the polyline only at the end,
or two length values to extend at both ends.
Returns
-------
:class:`compas.geometry.Polyline`
"""
crv = self.copy()
crv.extend(length)
return crv
def shorten(self, length):
"""Shortens a polyline by a given length.
Parameters
----------
length: float or tuple[float, float]
A single length value to shorten the polyline only at the end,
or two length values to shorten at both ends.
Returns
-------
None
"""
try:
start, end = length
total_length = 0
for line in self.lines:
total_length += line.length
if total_length < start:
del self.points[0]
elif total_length == start:
del self.points[0]
break
else:
self.points[0] = line.end + line.vector.unitized().scaled(-(total_length - start))
break
except TypeError:
start = end = length
total_length = 0
for i in range(len(self.lines)):
line = self.lines[-(i + 1)]
total_length += line.length
if total_length < end:
del self.points[-1]
elif total_length == end:
del self.points[-1]
break
else:
self.points[-1] = line.start + line.vector.unitized().scaled(total_length - end)
break
self._lines = None
def shortened(self, length):
"""Returns a copy of this polyline shortened by a given length.
Parameters
----------
length: float or tuple[float, float]
A single length value to shorten the polyline only at the end,
or two length values to shorten at both ends.
Returns
-------
:class:`compas.geometry.Polyline`
"""
crv = self.copy()
crv.shorten(length)
return crv