/
transformations.py
1122 lines (858 loc) · 31.2 KB
/
transformations.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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
import math
from ._algebra import scale_vector
from ._algebra import scale_vector_xy
from ._algebra import normalize_vector
from ._algebra import add_vectors
from ._algebra import add_vectors_xy
from ._algebra import subtract_vectors
from ._algebra import subtract_vectors_xy
from ._algebra import dot_vectors
from ._algebra import cross_vectors
from ._algebra import vector_component
from ._algebra import vector_component_xy
from ._algebra import multiply_matrix_vector
from ._algebra import multiply_matrices
from ._algebra import transpose_matrix
from ._algebra import norm_vector
from ._algebra import matrix_from_axis_and_angle
from ._algebra import matrix_from_scale_factors
from ._algebra import matrix_from_change_of_basis
from .angles import angle_vectors
from .distance import closest_point_on_plane
from .distance import closest_point_on_line
from .distance import closest_point_on_line_xy
# this function will not always work
# it is also a duplicate of stuff found in matrices and frame
def local_axes(a, b, c):
u = b - a
v = c - a
w = cross_vectors(u, v)
v = cross_vectors(w, u)
return normalize_vector(u), normalize_vector(v), normalize_vector(w)
def orthonormalize_axes(xaxis, yaxis):
"""Corrects xaxis and yaxis to be unit vectors and orthonormal.
Parameters
----------
xaxis: [float, float, float] | :class:`compas.geometry.Vector`
The first axis.
yaxis: [float, float, float] | :class:`compas.geometry.Vector`
The second axis.
Returns
-------
[float, float, float]
The corrected x axis.
[float, float, float]
The corrected y axis.
Raises
------
ValueError
If xaxis and yaxis cannot span a plane.
Examples
--------
>>> from compas.geometry import allclose
>>> xaxis = [1, 4, 5]
>>> yaxis = [1, 0, -2]
>>> xaxis, yaxis = orthonormalize_axes(xaxis, yaxis)
>>> allclose(xaxis, [0.1543, 0.6172, 0.7715], tol=0.001)
True
>>> allclose(yaxis, [0.6929, 0.4891, -0.5298], tol=0.001)
True
"""
xaxis = normalize_vector(xaxis)
yaxis = normalize_vector(yaxis)
zaxis = cross_vectors(xaxis, yaxis)
if not norm_vector(zaxis):
raise ValueError("Xaxis and yaxis cannot span a plane.")
yaxis = cross_vectors(normalize_vector(zaxis), xaxis)
return xaxis, yaxis
def homogenize(xyz, w=1.0):
"""Homogenise a list of vectors.
Parameters
----------
xyz : sequence[[float, float, float] | :class:`compas.geometry.Point`] | sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of points or vectors.
w : float, optional
Homogenisation parameter.
Use ``1.0`` for points, and ``0.0`` for vectors.
Returns
-------
list[[float, float, float, `w`]]
Homogenised data.
Notes
-----
Vectors described by XYZ components are homogenised by appending a homogenisation
parameter to the components, and by dividing each component by that parameter.
Homogenisatioon of vectors is often used in relation to transformations.
Examples
--------
>>> vectors = [[1.0, 0.0, 0.0]]
>>> homogenize(vectors)
[[1.0, 0.0, 0.0, 1.0]]
"""
return [[x * w, y * w, z * w, w] if w else [x, y, z, 0.0] for x, y, z in xyz]
def dehomogenize(xyzw):
"""Dehomogenise a list of vectors.
Parameters
----------
xyzw : sequence[[float, float, float, `w`]]
A list of vectors.
Returns
-------
list[[float, float, float]]
Dehomogenised vectors.
Examples
--------
>>>
"""
return [[x / w, y / w, z / w] if w else [x, y, z] for x, y, z, w in xyzw]
def homogenize_and_flatten_frames(frames):
"""Homogenize a list of frames and flatten the 3D list into a 2D list.
Parameters
----------
frames : sequence[[point, vector, vector] | :class:`compas.geometry.Frame`]
Returns
-------
list[[float, float, float, `w`]]
A list with 3 entries per frame: a homogenized point, and two homogenized vectors.
Examples
--------
>>> from compas.geometry import Frame
>>> frames = [Frame((1, 1, 1), (0, 1, 0), (1, 0, 0))]
>>> homogenize_and_flatten_frames(frames)
[[1.0, 1.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0], [1.0, -0.0, 0.0, 0.0]]
"""
def homogenize_frame(frame):
return homogenize([frame[0]], w=1.0) + homogenize([frame[1], frame[2]], w=0.0)
return [v for frame in frames for v in homogenize_frame(frame)]
def dehomogenize_and_unflatten_frames(points_and_vectors):
"""Dehomogenize a list of vectors and unflatten the 2D list into a 3D list.
Parameters
----------
points_and_vectors : sequence[[float, float, float, `w`]]
List of homogenized frames with 3 entries per frame: a homogenized point, and two homogenized vectors.
Returns
-------
list[[point, vector, vector]]
The dehmogenized frame data.
Examples
--------
>>> points_and_vectors = [(1., 1., 1., 1.), (0., 1., 0., 0.), (1., 0., 0., 0.)]
>>> dehomogenize_and_unflatten_frames(points_and_vectors)
[[[1.0, 1.0, 1.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]]
"""
frames = dehomogenize(points_and_vectors)
return [frames[i : i + 3] for i in range(0, len(frames), 3)]
# ==============================================================================
# transform
# ==============================================================================
def transform_points(points, T):
"""Transform multiple points with one transformation matrix.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points to be transformed.
T : list[list[float]] | :class:`compas.geometry.Transformation`
The transformation to apply.
Returns
-------
list[[float, float, float]]
Transformed points.
Examples
--------
>>> points = [[1, 0, 0], [1, 2, 4], [4, 7, 1]]
>>> T = matrix_from_axis_and_angle([0, 2, 0], math.radians(45), point=[4, 5, 6])
>>> points_transformed = transform_points(points, T)
"""
return dehomogenize(multiply_matrices(homogenize(points, w=1.0), transpose_matrix(T)))
def transform_vectors(vectors, T):
"""Transform multiple vectors with one transformation matrix.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors to be transformed.
T : list[list[float]] | :class:`compas.geometry.Transformation`
The transformation to apply.
Returns
-------
list[[float, float, float]]
Transformed vectors.
Examples
--------
>>> vectors = [[1, 0, 0], [1, 2, 4], [4, 7, 1]]
>>> T = matrix_from_axis_and_angle([0, 2, 0], math.radians(45), point=[4, 5, 6])
>>> vectors_transformed = transform_vectors(vectors, T)
"""
return dehomogenize(multiply_matrices(homogenize(vectors, w=0.0), transpose_matrix(T)))
def transform_frames(frames, T):
"""Transform multiple frames with one transformation matrix.
Parameters
----------
frames : sequence[[point, vector, vector] | :class:`compas.geometry.Frame`]
A list of frames to be transformed.
T : list[list[float]] | :class:`compas.geometry.Transformation`
The transformation to apply on the frames.
Returns
-------
list[[point, vector, vector]]
Transformed frames.
Examples
--------
>>> from compas.geometry import Frame, matrix_from_axis_and_angle
>>> frames = [Frame([1, 0, 0], [1, 2, 4], [4, 7, 1]), Frame([0, 2, 0], [5, 2, 1], [0, 2, 1])]
>>> T = matrix_from_axis_and_angle([0, 2, 0], math.radians(45), point=[4, 5, 6])
>>> transformed_frames = transform_frames(frames, T)
"""
points_and_vectors = homogenize_and_flatten_frames(frames)
return dehomogenize_and_unflatten_frames(multiply_matrices(points_and_vectors, transpose_matrix(T)))
def world_to_local_coordinates(frame, xyz):
"""Convert global coordinates to local coordinates.
Parameters
----------
frame : [point, vector, vector] | :class:`compas.geometry.Frame`
The local coordinate system.
xyz : array-like[[float, float, float] | :class:`compas.geometry.Point`]
The global coordinates of the points to convert.
Returns
-------
list[[float, float, float]]
The coordinates of the given points in the local coordinate system.
Examples
--------
>>> from compas.geometry import Point, Frame
>>> f = Frame([0, 1, 0], [3, 4, 1], [1, 5, 9])
>>> xyz = [Point(2, 3, 5)]
>>> Point(*world_to_local_coordinates(f, xyz)[0])
Point(3.726, 4.088, 1.550)
"""
from compas.geometry import Frame # noqa: F811
T = matrix_from_change_of_basis(Frame.worldXY(), frame)
return transform_points(xyz, T)
def local_to_world_coordinates(frame, xyz):
"""Convert local coordinates to global coordinates.
Parameters
----------
frame : [point, vector, vector] | :class:`compas.geometry.Frame`
The local coordinate system.
xyz : array-like[[float, float, float] | :class:`compas.geometry.Point`]
The global coordinates of the points to convert.
Returns
-------
list[[float, float, float]]
The coordinates of the given points in the local coordinate system.
Examples
--------
>>> from compas.geometry import Point, Frame
>>> f = Frame([0, 1, 0], [3, 4, 1], [1, 5, 9])
>>> xyz = [Point(3.726, 4.088, 1.550)]
>>> Point(*local_to_world_coordinates(f, xyz)[0])
Point(2.000, 3.000, 5.000)
"""
from compas.geometry import Frame # noqa: F811
T = matrix_from_change_of_basis(frame, Frame.worldXY())
return transform_points(xyz, T)
# ==============================================================================
# translate
# ==============================================================================
def translate_points(points, vector):
"""Translate points.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points.
vector : [float, float, float] | :class:`compas.geometry.Vector`
A translation vector.
Returns
-------
list[[float, float, float]]
The translated points.
Examples
--------
>>>
"""
return [add_vectors(point, vector) for point in points]
def translate_points_xy(points, vector):
"""Translate points and in the XY plane.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points.
vector : [float, float, float] | :class:`compas.geometry.Vector`
A translation vector.
Returns
-------
list[[float, float, float]]
The translated points in the XY plane (Z=0).
Examples
--------
>>>
"""
return [add_vectors_xy(point, vector) for point in points]
# ==============================================================================
# scale
# ==============================================================================
def scale_points(points, scale):
"""Scale points.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points.
scale : float
A scaling factor.
Returns
-------
list[[float, float, float]]
The scaled points.
Examples
--------
>>>
"""
T = matrix_from_scale_factors([scale, scale, scale])
return transform_points(points, T)
def scale_points_xy(points, scale):
"""Scale points in the XY plane.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points.
scale : float
A scaling factor.
Returns
-------
list[[float, float, float]]
The scaled points in the XY plane (Z=0).
Examples
--------
>>>
"""
T = matrix_from_scale_factors([scale, scale, 0])
return transform_points(points, T)
# ==============================================================================
# rotate
# ==============================================================================
def rotate_points(points, angle, axis=None, origin=None):
"""Rotates points around an arbitrary axis in 3D.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points.
angle : float
The angle of rotation in radians.
axis : [float, float, float] | :class:`compas.geometry.Vector`, optional
The rotation axis.
Default is ``[0.0, 0.0, 1.0]``
origin : [float, float, float] | :class:`compas.geometry.Point`, optional
The origin of the rotation axis.
Default is ``[0.0, 0.0, 0.0]``.
Returns
-------
list[[float, float, float]]
The rotated points
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wikipedia. *Rotation matrix*.
Available at: https://en.wikipedia.org/wiki/Rotation_matrix.
Examples
--------
>>>
"""
if axis is None:
axis = [0.0, 0.0, 1.0]
if origin is None:
origin = [0.0, 0.0, 0.0]
R = matrix_from_axis_and_angle(axis, angle, origin)
points = transform_points(points, R)
return points
def rotate_points_xy(points, angle, origin=None):
"""Rotates points in the XY plane around the Z axis at a specific origin.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
A list of points.
angle : float
The angle of rotation in radians.
origin : [float, float, float] | :class:`compas.geometry.Point`, optional
The origin of the rotation axis.
Default is ``[0.0, 0.0, 0.0]``.
Returns
-------
list[[float, float, 0.0]]
The rotated points in the XY plane (Z=0).
Examples
--------
>>>
"""
if not origin:
origin = [0.0, 0.0, 0.0]
cosa = math.cos(angle)
sina = math.sin(angle)
R = [[cosa, -sina, 0.0], [sina, cosa, 0.0], [0.0, 0.0, 1.0]]
# translate points
points = translate_points_xy(points, scale_vector_xy(origin, -1.0))
# rotate points
points = [multiply_matrix_vector(R, point) for point in points]
# translate points back
points = translate_points_xy(points, origin)
return points
# ==============================================================================
# mirror
# ==============================================================================
def mirror_vector_vector(v1, v2):
"""Mirrors vector about vector.
Parameters
----------
v1 : [float, float, float] | :class:`compas.geometry.Vector`
The vector.
v2 : [float, float, float] | :class:`compas.geometry.Vector`
The normalized vector as mirror axis
Returns
-------
[float, float, float]
The mirrored vector.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Math Stack Exchange. *How to get a reflection vector?*
Available at: https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector.
"""
return subtract_vectors(v1, scale_vector(v2, 2 * dot_vectors(v1, v2)))
def mirror_point_point(point, mirror):
"""Mirror a point about a point.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XYZ coordinates of the point to mirror.
mirror : [float, float, float] | :class:`compas.geometry.Point`
XYZ coordinates of the mirror point.
Returns
-------
[float, float, float]
The mirrored point.
"""
return add_vectors(mirror, subtract_vectors(mirror, point))
def mirror_point_point_xy(point, mirror):
"""Mirror a point about a point.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of the point to mirror.
mirror : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of the mirror point.
Returns
-------
[float, float, float]
The mirrored point, with Z=0.
"""
return add_vectors_xy(mirror, subtract_vectors_xy(mirror, point))
def mirror_points_point(points, mirror):
"""Mirror multiple points about a point.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
List of points.
mirror : [float, float, float] | :class:`compas.geometry.Point`
The mirror point.
Returns
-------
list[[float, float, float]]
The mirrored points, with Z=0.
"""
return [mirror_point_point(point, mirror) for point in points]
def mirror_points_point_xy(points, mirror):
"""Mirror multiple points about a point.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
List of points with XY(Z) coordinates.
mirror : [float, float, float] | :class:`compas.geometry.Point`
The XY(Z) coordinates of the mirror point.
Returns
-------
list[[float, float, float]]
The mirrored points, with Z=0.
"""
return [mirror_point_point_xy(point, mirror) for point in points]
def mirror_point_line(point, line):
"""Mirror a point about a line.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XYZ coordinates of the point to mirror.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the mirror line.
Returns
-------
[float, float, float]
The mirrored point.
"""
closest = closest_point_on_line(point, line)
return add_vectors(closest, subtract_vectors(closest, point))
def mirror_point_line_xy(point, line):
"""Mirror a point about a line.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of the point to mirror.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the line.
XY(Z) coordinates of the two points defining the mirror line.
Returns
-------
[float, float, float]
The mirrored point, with Z=0.
"""
closest = closest_point_on_line_xy(point, line)
return add_vectors_xy(closest, subtract_vectors_xy(closest, point))
def mirror_points_line(points, line):
"""Mirror a point about a line.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
List of points to mirror.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the mirror line.
Returns
-------
list[[float, float, float]]
The mirrored points.
"""
return [mirror_point_line(point, line) for point in points]
def mirror_points_line_xy(points, line):
"""Mirror a point about a line.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
List of points to mirror.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the mirror line.
Returns
-------
list[[float, float, float]]
The mirrored points.
"""
return [mirror_point_line_xy(point, line) for point in points]
def mirror_point_plane(point, plane):
"""Mirror a point about a plane.
Parameters
----------
point : list[float]
XYZ coordinates of mirror point.
plane : [point, vector] | :class:`compas.geometry.Plane`
Base point and normal defining the mirror plane.
Returns
-------
[float, float, float]
XYZ coordinates of the mirrored point.
"""
closest = closest_point_on_plane(point, plane)
return add_vectors(closest, subtract_vectors(closest, point))
def mirror_points_plane(points, plane):
"""Mirror a point about a plane.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
List of points to mirror.
plane : [point, vector] | :class:`compas.geometry.Plane`
Base point and normal defining the mirror plane.
Returns
-------
list[[float, float, float]]
The mirrored points.
"""
return [mirror_point_plane(point, plane) for point in points]
# ==============================================================================
# project
# specify orhtogonal
# add perspective
# ==============================================================================
def project_point_plane(point, plane):
"""Project a point onto a plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XYZ coordinates of the point.
plane : [point, vector] | :class:`compas.geometry.Plane`
Base point and normal vector defining the projection plane.
Returns
-------
[float, float, float]
XYZ coordinates of the projected point.
Notes
-----
The projection is in the direction perpendicular to the plane.
The projected point is thus the closest point on the plane to the original
point [1]_.
References
----------
.. [1] Math Stack Exchange. *Project a point in 3D on a given plane*.
Available at: https://math.stackexchange.com/questions/444968/project-a-point-in-3d-on-a-given-plane.
Examples
--------
>>> from compas.geometry import project_point_plane
>>> point = [3.0, 3.0, 3.0]
>>> plane = ([0.0, 0.0, 0.0], [0.0, 0.0, 1.0]) # the XY plane
>>> project_point_plane(point, plane)
[3.0, 3.0, 0.0]
"""
base, normal = plane
normal = normalize_vector(normal)
vector = subtract_vectors(point, base)
snormal = scale_vector(normal, dot_vectors(vector, normal))
return subtract_vectors(point, snormal)
def project_points_plane(points, plane):
"""Project multiple points onto a plane.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
List of points.
plane : [point, vector] | :class:`compas.geometry.Plane`
Base point and normal vector defining the projection plane.
Returns
-------
list[[float, float, float]]
The projected points.
See Also
--------
project_point_plane
"""
return [project_point_plane(point, plane) for point in points]
def project_point_line(point, line):
"""Project a point onto a line.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XYZ coordinates of the point.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the projection line.
Returns
-------
[float, float, float]
XYZ coordinates of the projected point.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wiki Books. *Linear Algebra/Orthogonal Projection Onto a Line*.
Available at: https://en.wikibooks.org/wiki/Linear_Algebra/Orthogonal_Projection_Onto_a_Line.
"""
a, b = line
ab = subtract_vectors(b, a)
ap = subtract_vectors(point, a)
c = vector_component(ap, ab)
return add_vectors(a, c)
def project_point_line_xy(point, line):
"""Project a point onto a line in the XY plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of the point.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the projection line.
Returns
-------
[float, float, float]
XYZ coordinates of the projected point, with Z=0.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wiki Books. *Linear Algebra/Orthogonal Projection Onto a Line*.
Available at: https://en.wikibooks.org/wiki/Linear_Algebra/Orthogonal_Projection_Onto_a_Line.
"""
a, b = line
ab = subtract_vectors_xy(b, a)
ap = subtract_vectors_xy(point, a)
c = vector_component_xy(ap, ab)
return add_vectors_xy(a, c)
def project_points_line(points, line):
"""Project points onto a line.
Parameters
----------
points : sequence[[float, float, float] | :class:`compas.geometry.Point`]
XYZ coordinates of the points.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the projection line.
Returns
-------
list[[float, float, float]]
XYZ coordinates of the projected points.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wiki Books. *Linear Algebra/Orthogonal Projection Onto a Line*.
Available at: https://en.wikibooks.org/wiki/Linear_Algebra/Orthogonal_Projection_Onto_a_Line.
"""
return [project_point_line(point, line) for point in points]
def project_points_line_xy(points, line):
"""Project points onto a line in the XY plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of the point.
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the projection line.
Returns
-------
[float, float, float]
XYZ coordinates of the projected point, with Z=0.
Notes
-----
For more info, see [1]_.
References
----------
.. [1] Wiki Books. *Linear Algebra/Orthogonal Projection Onto a Line*.
Available at: https://en.wikibooks.org/wiki/Linear_Algebra/Orthogonal_Projection_Onto_a_Line.
"""
return [project_point_line_xy(point, line) for point in points]
# ==============================================================================
# reflection
# ==============================================================================
def reflect_line_plane(line, plane, tol=None):
"""Bounce a line of a reflection plane.
Parameters
----------
line : [point, point] | :class:`compas.geometry.Line`
Two points defining the line.
plane : [point, vector] | :class:`compas.geometry.Plane`
Base point and normal vector of the plane.
tol : float, optional
A tolerance for finding the intersection between the line and the plane.
Default is :func:`TOL.absolute`.
Returns
-------
tuple[[float, float, float], [float, float, float]]
The reflected line defined by the intersection point of the line and plane
and the mirrored start point of the line with respect to a line perpendicular
to the plane through the intersection.
Notes
-----
The direction of the line and plane are important.
The line is only reflected if it points towards the front of the plane.
This is true if the dot product of the direction vector of the line and the
normal vector of the plane is smaller than zero.
Examples
--------
>>> plane = [0, 0, 0], [0, 1, 0]
>>> line = [-1, 1, 0], [-0.5, 0.5, 0]
>>> reflect_line_plane(line, plane)
([0.0, 0.0, 0.0], [1.0, 1.0, 0.0])
"""
from compas.geometry import intersection_line_plane
x = intersection_line_plane(line, plane, tol=tol)
if not x:
return
a, b = line
o, n = plane
ab = subtract_vectors(b, a)
if dot_vectors(ab, n) > 0:
# the line does not point towards the front of the plane
return
mirror = x, add_vectors(x, n)
return x, mirror_point_line(a, mirror)