/
_algebra.py
2825 lines (2189 loc) · 70.7 KB
/
_algebra.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
from copy import deepcopy
from math import acos, atan2, asin, cos, fabs, pi, sin, sqrt, tan
from compas.tolerance import TOL
_SPEC2TUPLE = {
"sxyz": (0, 0, 0, 0),
"sxyx": (0, 0, 1, 0),
"sxzy": (0, 1, 0, 0),
"sxzx": (0, 1, 1, 0),
"syzx": (1, 0, 0, 0),
"syzy": (1, 0, 1, 0),
"syxz": (1, 1, 0, 0),
"syxy": (1, 1, 1, 0),
"szxy": (2, 0, 0, 0),
"szxz": (2, 0, 1, 0),
"szyx": (2, 1, 0, 0),
"szyz": (2, 1, 1, 0),
"rzyx": (0, 0, 0, 1),
"rxyx": (0, 0, 1, 1),
"ryzx": (0, 1, 0, 1),
"rxzx": (0, 1, 1, 1),
"rxzy": (1, 0, 0, 1),
"ryzy": (1, 0, 1, 1),
"rzxy": (1, 1, 0, 1),
"ryxy": (1, 1, 1, 1),
"ryxz": (2, 0, 0, 1),
"rzxz": (2, 0, 1, 1),
"rxyz": (2, 1, 0, 1),
"rzyz": (2, 1, 1, 1),
}
"""used for Euler angles: to map rotation type and axes to tuples of inner axis, parity, repetition, frame"""
_NEXT_SPEC = [1, 2, 0, 1]
def vector_average(vector):
"""Average of a vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
List of values.
Returns
-------
float
The mean value.
"""
return sum(vector) / float(len(vector))
def vector_variance(vector):
"""Variance of a vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
List of values.
Returns
-------
float
The variance value.
"""
m = vector_average(vector)
return (sum([(i - m) ** 2 for i in vector]) / float(len(vector))) ** 0.5
def vector_standard_deviation(vector):
"""Standard deviation of a vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
List of values.
Returns
-------
float
The standard deviation value.
"""
return vector_variance(vector) ** 0.5
def argmax(values):
"""Returns the index of the first maximum value within an array.
Parameters
----------
values : sequence[float]
A list of values.
Returns
-------
int
The index of the first maximum value within an array.
Notes
-----
NumPy's *argmax* function [1]_ is different, it returns an array of indices.
References
----------
.. [1] https://numpy.org/doc/stable/reference/generated/numpy.argmax.html
Examples
--------
>>> argmax([2, 4, 4, 3])
1
"""
return max(range(len(values)), key=lambda i: values[i]) # type: ignore
def argmin(values):
"""Returns the index of the first minimum value within an array.
Parameters
----------
values : sequence[float]
A list of values.
Returns
-------
int
The index of the first minimum value within an array.
Notes
-----
NumPy's *argmin* function [1]_ is different, it returns an array of indices.
References
----------
.. [1] https://numpy.org/doc/stable/reference/generated/numpy.argmin.html
Examples
--------
>>> argmin([4, 2, 2, 3])
1
"""
return min(range(len(values)), key=lambda i: values[i])
# ==============================================================================
# these return something of smaller dimension/length/...
# something_(of)vector/s
# ==============================================================================
def sum_vectors(vectors, axis=0):
"""Calculate the sum of a series of vectors along the specified axis.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
axis : int, optional
If ``axis == 0``, the sum is taken per column.
If ``axis == 1``, the sum is taken per row.
Returns
-------
list[float]
The length of the list is ``len(vectors[0])``, if ``axis == 0``.
The length is ``len(vectors)``, otherwise.
Examples
--------
>>> vectors = [[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]
>>> sum_vectors(vectors)
[3.0, 6.0, 9.0]
>>> sum_vectors(vectors, axis=1)
[6.0, 6.0, 6.0]
"""
if axis == 0:
vectors = zip(*vectors)
return [sum(vector) for vector in vectors]
def norm_vector(vector):
"""Calculate the length of a vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
Returns
-------
float
The L2 norm, or *length* of the vector.
Examples
--------
>>> norm_vector([2.0, 0.0, 0.0])
2.0
>>> norm_vector([1.0, 1.0, 0.0]) == sqrt(2.0)
True
"""
return sqrt(sum(axis**2 for axis in vector))
def norm_vectors(vectors):
"""
Calculate the norm of each vector in a list of vectors.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors
Returns
-------
list[float]
A list with the lengths of all vectors.
Examples
--------
>>> norm_vectors([[1.0, 0.0, 0.0], [2.0, 0.0, 0.0], [3.0, 0.0, 0.0]])
[1.0, 2.0, 3.0]
"""
return [norm_vector(vector) for vector in vectors]
def length_vector(vector):
"""Calculate the length of the vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
Returns
-------
float
The length of the vector.
Examples
--------
>>> length_vector([2.0, 0.0, 0.0])
2.0
>>> length_vector([1.0, 1.0, 0.0]) == sqrt(2.0)
True
"""
return sqrt(length_vector_sqrd(vector))
def length_vector_xy(vector):
"""Compute the length of a vector, assuming it lies in the XY plane.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) components of the vector.
Returns
-------
float
The length of the XY component of the vector.
Examples
--------
>>> length_vector_xy([2.0, 0.0])
2.0
>>> length_vector_xy([2.0, 0.0, 0.0])
2.0
>>> length_vector_xy([2.0, 0.0, 2.0])
2.0
"""
return sqrt(length_vector_sqrd_xy(vector))
def length_vector_sqrd(vector):
"""Compute the squared length of a vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
Returns
-------
float
The squared length.
Examples
--------
>>> length_vector_sqrd([1.0, 1.0, 0.0])
2.0
"""
return vector[0] ** 2 + vector[1] ** 2 + vector[2] ** 2
def length_vector_sqrd_xy(vector):
"""Compute the squared length of a vector, assuming it lies in the XY plane.
Parameters
----------
vector : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) components of the vector.
Returns
-------
float
The squared length.
Examples
--------
>>> length_vector_sqrd_xy([1.0, 1.0])
2.0
>>> length_vector_sqrd_xy([1.0, 1.0, 0.0])
2.0
>>> length_vector_sqrd_xy([1.0, 1.0, 1.0])
2.0
"""
return vector[0] ** 2 + vector[1] ** 2
# ==============================================================================
# these perform an operation on a vector and return a modified vector
# -> elementwise operations on 1 vector
# should this not bet ...ed_vector
# ... or else modify the vector in-place
# ==============================================================================
def scale_vector(vector, factor):
"""Scale a vector by a given factor.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
factor : float
The scaling factor.
Returns
-------
[float, float, float]
The scaled vector.
Examples
--------
>>> scale_vector([1.0, 2.0, 3.0], 2.0)
[2.0, 4.0, 6.0]
>>> v = [2.0, 0.0, 0.0]
>>> scale_vector(v, 1 / length_vector(v))
[1.0, 0.0, 0.0]
"""
return [axis * factor for axis in vector]
def scale_vector_xy(vector, factor):
"""Scale a vector by a given factor, assuming it lies in the XY plane.
Parameters
----------
vector : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) components of the vector.
scale : float
Scale factor.
Returns
-------
[float, float, 0.0]
The scaled vector in the XY-plane.
Examples
--------
>>> scale_vector_xy([1.0, 2.0, 3.0], 2.0)
[2.0, 4.0, 0.0]
"""
return [vector[0] * factor, vector[1] * factor, 0.0]
def scale_vectors(vectors, factor):
"""Scale multiple vectors by a given factor.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
factor : float
The scaling factor.
Returns
-------
list[[float, float, float]]
The scaled vectors.
Examples
--------
>>>
"""
return [scale_vector(vector, factor) for vector in vectors]
def scale_vectors_xy(vectors, factor):
"""Scale multiple vectors by a given factor, assuming they lie in the XY plane.
Parameters
----------
vectors : sequence[[float, float] or [float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
factor : float
The scaling factor.
Returns
-------
list[[float, float, 0.0]]
The scaled vectors in the XY plane.
Examples
--------
>>>
"""
return [scale_vector_xy(vector, factor) for vector in vectors]
def normalize_vector(vector):
"""Normalise a given vector.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
Returns
-------
[float, float, float]
The normalized vector.
Examples
--------
>>>
"""
length = length_vector(vector)
if not length:
return vector
return [vector[0] / length, vector[1] / length, vector[2] / length]
def normalize_vector_xy(vector):
"""Normalize a vector, assuming it lies in the XY-plane.
Parameters
----------
vector : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) components of the vector.
Returns
-------
[float, float, 0.0]
The normalized vector in the XY-plane.
Examples
--------
>>>
"""
length = length_vector_xy(vector)
if not length:
return vector
return [vector[0] / length, vector[1] / length, 0.0]
def normalize_vectors(vectors):
"""Normalise multiple vectors.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
Returns
-------
list[[float, float, float]]
The normalized vectors.
Examples
--------
>>>
"""
return [normalize_vector(vector) for vector in vectors]
def normalize_vectors_xy(vectors):
"""Normalise multiple vectors, assuming they lie in the XY plane.
Parameters
----------
vectors : sequence[[float, float] or [float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
Returns
-------
list[[float, float, 0.0]]
The normalized vectors in the XY plane.
Examples
--------
>>>
"""
return [normalize_vector_xy(vector) for vector in vectors]
def power_vector(vector, power):
"""Raise a vector to the given power.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
power : int, float
The power to which to raise the vector.
Returns
-------
[float, float, float]
The raised vector.
Examples
--------
>>>
"""
return [axis**power for axis in vector]
def power_vectors(vectors, power):
"""Raise a list of vectors to the given power.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
power : int, float
The power to which to raise the vectors.
Returns
-------
list[[float, float, float]]
The raised vectors.
Examples
--------
>>>
"""
return [power_vector(vector, power) for vector in vectors]
def square_vector(vector):
"""Raise a vector to the power 2.
Parameters
----------
vector : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
Returns
-------
[float, float, float]
The squared vector.
Examples
--------
>>>
"""
return power_vector(vector, 2)
def square_vectors(vectors):
"""Raise a multiple vectors to the power 2.
Parameters
----------
vectors : sequence[[float, float, float] | :class:`compas.geometry.Vector`]
A list of vectors.
Returns
-------
[float, float, float]]
The squared vectors.
Examples
--------
>>>
"""
return [square_vectors(vector) for vector in vectors]
# ==============================================================================
# these perform an operation with corresponding elements of the (2) input vectors as operands
# and return a vector with the results
# -> elementwise operations on two vectors
# ==============================================================================
def add_vectors(u, v):
"""Add two vectors.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the first vector.
v : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the second vector.
Returns
-------
[float, float, float]
The resulting vector.
"""
return [a + b for (a, b) in zip(u, v)]
def add_vectors_xy(u, v):
"""Add two vectors, assuming they lie in the XY-plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) components of the first vector.
v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) components of the second vector.
Returns
-------
[float, float, 0.0]
Resulting vector in the XY-plane.
Examples
--------
>>>
"""
return [u[0] + v[0], u[1] + v[1], 0.0]
def subtract_vectors(u, v):
"""Subtract one vector from another.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the first vector.
v : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the second vector.
Returns
-------
[float, float, float]
The resulting vector.
Examples
--------
>>>
"""
return [a - b for (a, b) in zip(u, v)]
def subtract_vectors_xy(u, v):
"""Subtract one vector from another, assuming they lie in the XY plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
The XY(Z) components of the first vector.
v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
The XY(Z) components of the second vector.
Returns
-------
[float, float, 0.0]
Resulting vector in the XY-plane.
Examples
--------
>>>
"""
return [u[0] - v[0], u[1] - v[1], 0.0]
def multiply_vectors(u, v):
"""Element-wise multiplication of two vectors.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
The XYZ components of the first vector.
v : l[float, float, float] | :class:`compas.geometry.Vector`
The XYZ components of the second vector.
Returns
-------
[float, float, float]
Resulting vector.
Examples
--------
>>>
"""
return [a * b for (a, b) in zip(u, v)]
def multiply_vectors_xy(u, v):
"""Element-wise multiplication of two vectors assumed to lie in the XY plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
The XY(Z) components of the first vector.
v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
The XY(Z) components of the second vector.
Returns
-------
[float, float, 0.0]
Resulting vector in the XY plane.
Examples
--------
>>>
"""
return [u[0] * v[0], u[1] * v[1], 0.0]
def divide_vectors(u, v):
"""Element-wise division of two vectors.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
The XYZ components of the first vector.
v : [float, float, float] | :class:`compas.geometry.Vector`
The XYZ components of the second vector.
Returns
-------
[float, float, float]
Resulting vector.
Examples
--------
>>>
"""
return [a / b for (a, b) in zip(u, v)]
def divide_vectors_xy(u, v):
"""Element-wise division of two vectors assumed to lie in the XY plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
The XY(Z) components of the first vector.
v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
The XY(Z) components of the second vector.
Returns
-------
[float, float, 0.0]
Resulting vector in the XY plane.
Examples
--------
>>>
"""
return [u[0] / v[0], u[1] / v[1], 0.0]
# ==============================================================================
# ...
# ==============================================================================
def cross_vectors(u, v):
r"""Compute the cross product of two vectors.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the first vector.
v : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the second vector.
Returns
-------
[float, float, float]
The cross product of the two vectors.
Notes
-----
The xyz components of the cross product of two vectors :math:`\mathbf{u}`
and :math:`\mathbf{v}` can be computed as the *minors* of the following matrix:
.. math::
:nowrap:
\begin{bmatrix}
x & y & z \\
u_{x} & u_{y} & u_{z} \\
v_{x} & v_{y} & v_{z}
\end{bmatrix}
Therefore, the cross product can be written as:
.. math::
:nowrap:
\begin{eqnarray}
\mathbf{u} \times \mathbf{v}
& =
\begin{bmatrix}
u_{y} * v_{z} - u_{z} * v_{y} \\
u_{z} * v_{x} - u_{x} * v_{z} \\
u_{x} * v_{y} - u_{y} * v_{x}
\end{bmatrix}
\end{eqnarray}
Examples
--------
>>> cross_vectors([1.0, 0.0, 0.0], [0.0, 1.0, 0.0])
[0.0, 0.0, 1.0]
"""
return [
u[1] * v[2] - u[2] * v[1],
u[2] * v[0] - u[0] * v[2],
u[0] * v[1] - u[1] * v[0],
]
def cross_vectors_xy(u, v):
"""Compute the cross product of two vectors, assuming they lie in the XY-plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) coordinates of the first vector.
v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) coordinates of the second vector.
Returns
-------
[float, float, float]
The cross product of the two vectors.
This vector will be perpendicular to the XY plane.
Examples
--------
>>> cross_vectors_xy([1.0, 0.0], [0.0, 1.0])
[0.0, 0.0, 1.0]
>>> cross_vectors_xy([1.0, 0.0, 0.0], [0.0, 1.0, 0.0])
[0.0, 0.0, 1.0]
>>> cross_vectors_xy([1.0, 0.0, 1.0], [0.0, 1.0, 1.0])
[0.0, 0.0, 1.0]
"""
return [0.0, 0.0, u[0] * v[1] - u[1] * v[0]]
def dot_vectors(u, v):
"""Compute the dot product of two vectors.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the first vector.
v : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the second vector.
Returns
-------
float
The dot product of the two vectors.
Examples
--------
>>> dot_vectors([1.0, 0, 0], [2.0, 0, 0])
2.0
"""
return sum(a * b for a, b in zip(u, v))
def dot_vectors_xy(u, v):
"""Compute the dot product of two vectors, assuming they lie in the XY-plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) coordinates of the first vector.
v : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`
XY(Z) coordinates of the second vector.
Returns
-------
float
The dot product of the XY components of the two vectors.
Examples
--------
>>> dot_vectors_xy([1.0, 0], [2.0, 0])
2.0
>>> dot_vectors_xy([1.0, 0, 0], [2.0, 0, 0])
2.0
>>> dot_vectors_xy([1.0, 0, 1], [2.0, 0, 1])
2.0
"""
return u[0] * v[0] + u[1] * v[1]
def vector_component(u, v):
"""Compute the component of u in the direction of v.
Parameters
----------
u : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the vector.
v : [float, float, float] | :class:`compas.geometry.Vector`
XYZ components of the direction.
Returns
-------
[float, float, float]
The component of u in the direction of v.
Notes
-----
This is similar to computing direction cosines, or to the projection of
a vector onto another vector. See the respective Wikipedia pages ([1]_, [2]_)
for more info.
References
----------
.. [1] *Direction cosine*. Available at https://en.wikipedia.org/wiki/Direction_cosine.
.. [2] *Vector projection*. Available at https://en.wikipedia.org/wiki/Vector_projection.
Examples
--------
>>> vector_component([1.0, 2.0, 3.0], [1.0, 0.0, 0.0])
[1.0, 0.0, 0.0]
"""
l2 = length_vector_sqrd(v)
if not l2:
return [0, 0, 0]
x = dot_vectors(u, v) / l2
return scale_vector(v, x)
def vector_component_xy(u, v):
"""Compute the component of u in the direction of v, assuming they lie in the XY-plane.
Parameters
----------
u : [float, float] or [float, float, float] | :class:`compas.geometry.Vector`