-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvmath.py
983 lines (806 loc) · 27.9 KB
/
vmath.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
import math
__all__ = [
'Vector3', 'Matrix4', 'Quaternion', 'Transform',
'Vector2', 'Vector4', 'Ray',
]
class Vector3(object):
__slots__ = ('x', 'y', 'z')
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def length_squared(self):
return self.x ** 2 + self.y ** 2 + self.z ** 2
def dot(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z
def cross(self, other):
ax = self.x
ay = self.y
az = self.z
bx = other.x
by = other.y
bz = other.z
return Vector3(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx)
def normalize_self(self):
l = self.length()
if l == 0.0:
self.x = 1.0
self.y = 0.0
self.z = 0.0
else:
self *= 1.0 / l
def normalize(self):
v = self.copy()
v.normalize_self()
return v
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
def __add__(self, other):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
self.z += other.z
return self
def __mul__(self, n):
return Vector3(self.x * n, self.y * n, self.z * n)
def __imul__(self, n):
self.x *= n
self.y *= n
self.z *= n
return self
__rmul__ = __mul__
def __sub__(self, other):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
def __isub__(self, other):
self.x -= other.x
self.y -= other.y
self.z -= other.z
return self
def __neg__(self):
return Vector3(-self.x, -self.y, -self.z)
def __copy__(self):
return Vector3(self.x, self.y, self.z)
copy = __copy__
def __repr__(self):
return 'Vector3({:0.4f}, {:0.4f}, {:0.4f})'.format(self.x, self.y, self.z)
class Matrix4(object):
"""Column major matrix4.
m00, m01, m02, m03 is the first column.
"""
__slots__ = ('m00', 'm01', 'm02', 'm03', 'm10', 'm11', 'm12', 'm13',
'm20', 'm21', 'm22', 'm23', 'm30', 'm31', 'm32', 'm33')
def __init__(self,
m00=1.0, m01=0.0, m02=0.0, m03=0.0, m10=0.0, m11=1.0, m12=0.0, m13=0.0,
m20=0.0, m21=0.0, m22=1.0, m23=0.0, m30=0.0, m31=0.0, m32=0.0, m33=1.0):
self.m00 = m00
self.m01 = m01
self.m02 = m02
self.m03 = m03
self.m10 = m10
self.m11 = m11
self.m12 = m12
self.m13 = m13
self.m20 = m20
self.m21 = m21
self.m22 = m22
self.m23 = m23
self.m30 = m30
self.m31 = m31
self.m32 = m32
self.m33 = m33
def transform_point(self, p):
# ignore last row
x = p.x
y = p.y
z = p.z
nx = self.m00 * x + self.m10 * y + self.m20 * z + self.m30
ny = self.m01 * x + self.m11 * y + self.m21 * z + self.m31
nz = self.m02 * x + self.m12 * y + self.m22 * z + self.m32
return Vector3(nx, ny, nz)
def transform_vector(self, v):
# ignore last row
x = v.x
y = v.y
z = v.z
nx = self.m00 * x + self.m10 * y + self.m20 * z
ny = self.m01 * x + self.m11 * y + self.m21 * z
nz = self.m02 * x + self.m12 * y + self.m22 * z
return Vector3(nx, ny, nz)
def project_point(self, p):
x = p.x
y = p.y
z = p.z
nx = self.m00 * x + self.m10 * y + self.m20 * z + self.m30
ny = self.m01 * x + self.m11 * y + self.m21 * z + self.m31
nz = self.m02 * x + self.m12 * y + self.m22 * z + self.m32
nw = self.m03 * x + self.m13 * y + self.m23 * z + self.m33
return Vector3(nx, ny, nz) * (1.0 / nw)
def inverse(self):
# TODO TRS only for now
return self.to_transform().inverse().to_matrix4()
def transpose(self):
m00 = self.m00
m01 = self.m01
m02 = self.m02
m03 = self.m03
m10 = self.m10
m11 = self.m11
m12 = self.m12
m13 = self.m13
m20 = self.m20
m21 = self.m21
m22 = self.m22
m23 = self.m23
m30 = self.m30
m31 = self.m31
m32 = self.m32
m33 = self.m33
return Matrix4(
m00, m10, m20, m30,
m01, m11, m21, m31,
m02, m12, m22, m32,
m03, m13, m23, m33)
def to_transform(self):
translation = Vector3(self.m30, self.m31, self.m32)
axis_x = Vector3(self.m00, self.m01, self.m02)
axis_y = Vector3(self.m10, self.m11, self.m12)
axis_z = Vector3(self.m20, self.m21, self.m22)
scale = Vector3(axis_x.length(), axis_y.length(), axis_z.length())
axis_x.normalize_self()
axis_y.normalize_self()
axis_z.normalize_self()
rotation = Quaternion.from_matrix3((axis_x, axis_y, axis_z))
return Transform(translation, rotation, scale)
def __mul__(self, other):
am00 = self.m00
am01 = self.m01
am02 = self.m02
am03 = self.m03
am10 = self.m10
am11 = self.m11
am12 = self.m12
am13 = self.m13
am20 = self.m20
am21 = self.m21
am22 = self.m22
am23 = self.m23
am30 = self.m30
am31 = self.m31
am32 = self.m32
am33 = self.m33
bm00 = other.m00
bm01 = other.m01
bm02 = other.m02
bm03 = other.m03
bm10 = other.m10
bm11 = other.m11
bm12 = other.m12
bm13 = other.m13
bm20 = other.m20
bm21 = other.m21
bm22 = other.m22
bm23 = other.m23
bm30 = other.m30
bm31 = other.m31
bm32 = other.m32
bm33 = other.m33
cm00 = am00 * bm00 + am10 * bm01 + am20 * bm02 + am30 * bm03
cm01 = am01 * bm00 + am11 * bm01 + am21 * bm02 + am31 * bm03
cm02 = am02 * bm00 + am12 * bm01 + am22 * bm02 + am32 * bm03
cm03 = am03 * bm00 + am13 * bm01 + am23 * bm02 + am33 * bm03
cm10 = am00 * bm10 + am10 * bm11 + am20 * bm12 + am30 * bm13
cm11 = am01 * bm10 + am11 * bm11 + am21 * bm12 + am31 * bm13
cm12 = am02 * bm10 + am12 * bm11 + am22 * bm12 + am32 * bm13
cm13 = am03 * bm10 + am13 * bm11 + am23 * bm12 + am33 * bm13
cm20 = am00 * bm20 + am10 * bm21 + am20 * bm22 + am30 * bm23
cm21 = am01 * bm20 + am11 * bm21 + am21 * bm22 + am31 * bm23
cm22 = am02 * bm20 + am12 * bm21 + am22 * bm22 + am32 * bm23
cm23 = am03 * bm20 + am13 * bm21 + am23 * bm22 + am33 * bm23
cm30 = am00 * bm30 + am10 * bm31 + am20 * bm32 + am30 * bm33
cm31 = am01 * bm30 + am11 * bm31 + am21 * bm32 + am31 * bm33
cm32 = am02 * bm30 + am12 * bm31 + am22 * bm32 + am32 * bm33
cm33 = am03 * bm30 + am13 * bm31 + am23 * bm32 + am33 * bm33
return Matrix4(cm00, cm01, cm02, cm03, cm10, cm11, cm12, cm13, cm20, cm21, cm22, cm23, cm30, cm31, cm32, cm33)
def __add__(self, other):
return Matrix4(
self.m00 + other.m00, self.m01 + other.m01, self.m02 + other.m02, self.m03 + other.m03,
self.m10 + other.m10, self.m11 + other.m11, self.m12 + other.m12, self.m13 + other.m13,
self.m20 + other.m20, self.m21 + other.m21, self.m22 + other.m22, self.m23 + other.m23,
self.m30 + other.m30, self.m31 + other.m31, self.m32 + other.m32, self.m33 + other.m33,
)
def __iadd__(self, other):
self.m00 += other.m00
self.m01 += other.m01
self.m02 += other.m02
self.m03 += other.m03
self.m10 += other.m10
self.m11 += other.m11
self.m12 += other.m12
self.m13 += other.m13
self.m20 += other.m20
self.m21 += other.m21
self.m22 += other.m22
self.m23 += other.m23
self.m30 += other.m30
self.m31 += other.m31
self.m32 += other.m32
self.m33 += other.m33
def __sub__(self, other):
return Matrix4(
self.m00 - other.m00, self.m01 - other.m01, self.m02 - other.m02, self.m03 - other.m03,
self.m10 - other.m10, self.m11 - other.m11, self.m12 - other.m12, self.m13 - other.m13,
self.m20 - other.m20, self.m21 - other.m21, self.m22 - other.m22, self.m23 - other.m23,
self.m30 - other.m30, self.m31 - other.m31, self.m32 - other.m32, self.m33 - other.m33,
)
def __isub__(self, other):
self.m00 -= other.m00
self.m01 -= other.m01
self.m02 -= other.m02
self.m03 -= other.m03
self.m10 -= other.m10
self.m11 -= other.m11
self.m12 -= other.m12
self.m13 -= other.m13
self.m20 -= other.m20
self.m21 -= other.m21
self.m22 -= other.m22
self.m23 -= other.m23
self.m30 -= other.m30
self.m31 -= other.m31
self.m32 -= other.m32
self.m33 -= other.m33
def mul_scalar(self, s):
return Matrix4(
self.m00 * s, self.m01 * s, self.m02 * s, self.m03 * s,
self.m10 * s, self.m11 * s, self.m12 * s, self.m13 * s,
self.m20 * s, self.m21 * s, self.m22 * s, self.m23 * s,
self.m30 * s, self.m31 * s, self.m32 * s, self.m33 * s,
)
@staticmethod
def from_orthographic(left, right, bottom, top, near, far):
m00 = 2.0 / (right - left)
m11 = 2.0 / (top - bottom)
m22 = -2.0 / (far - near)
m30 = -(right + left) / (right - left)
m31 = -(top + bottom) / (top - bottom)
m32 = -near / (far - near)
return Matrix4(m00=m00, m11=m11, m22=m22, m30=m30, m31=m31, m32=m32)
@staticmethod
def from_perspective(fov, aspect, near, far):
tan_half_fov = math.tan(fov / 2.0)
m00 = 1.0 / (aspect * tan_half_fov)
m11 = 1.0 / tan_half_fov
m22 = -(far + near) / (far - near)
m23 = -1.0
m32 = -(2.0 * far * near) / (far - near)
m33 = 0.0
return Matrix4(m00=m00, m11=m11, m22=m22, m23=m23, m32=m32, m33=m33)
def __copy__(self):
return Matrix4(
self.m00, self.m01, self.m02, self.m03,
self.m10, self.m11, self.m12, self.m13,
self.m20, self.m21, self.m22, self.m23,
self.m30, self.m31, self.m32, self.m33,
)
copy = __copy__
def __repr__(self):
return 'Matrix4({:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f}, {:0.4f})'.format(
self.m00, self.m01, self.m02, self.m03, self.m10, self.m11, self.m12, self.m13,
self.m20, self.m21, self.m22, self.m23, self.m30, self.m31, self.m32, self.m33)
# TODO methods below not in C++
def set_translation(self, translation):
self.m30 = translation.x
self.m31 = translation.y
self.m32 = translation.z
def set_look_rotation(self, forward, up):
# forward becomes negative z, reset scale
axis_z = -forward
axis_x = up.cross(axis_z).normalize()
axis_y = axis_z.cross(axis_x)
self.m00 = axis_x.x
self.m01 = axis_x.y
self.m02 = axis_x.z
self.m10 = axis_y.x
self.m11 = axis_y.y
self.m12 = axis_y.z
self.m20 = axis_z.x
self.m21 = axis_z.y
self.m22 = axis_z.z
@staticmethod
def from_translation(translation):
return Matrix4(m30=translation.x, m31=translation.y, m32=translation.z)
@staticmethod
def from_rotation(rotation):
return rotation.to_matrix4()
@staticmethod
def from_angle_axis(angle, axis):
return Matrix4.from_rotation(Quaternion.from_angle_axis(angle, axis))
@staticmethod
def from_scale(scale):
return Matrix4(m00=scale.x, m11=scale.y, m22=scale.z)
@staticmethod
def from_look_at(eye, center, up):
forward = center - eye
forward.normalize_self()
side = forward.cross(up)
side.normalize_self()
camup = side.cross(forward)
m00 = side.x
m10 = side.y
m20 = side.z
m01 = camup.x
m11 = camup.y
m21 = camup.z
m02 = -forward.x
m12 = -forward.y
m22 = -forward.z
m30 = -side.dot(eye)
m31 = -camup.dot(eye)
m32 = forward.dot(eye)
return Matrix4(m00, m01, m02, 0.0, m10, m11, m12, 0.0, m20, m21, m22, 0.0, m30, m31, m32, 1.0)
class Quaternion(object):
__slots__ = ('w', 'x', 'y', 'z')
def __init__(self, w=1.0, x=0.0, y=0.0, z=0.0):
self.w = w
self.x = x
self.y = y
self.z = z
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2 + self.w ** 2)
def length_squared(self):
return self.x ** 2 + self.y ** 2 + self.z ** 2 + self.w ** 2
def normalize_self(self):
l = self.length()
if l == 0.0:
self.w = 1.0
self.x = 0.0
self.y = 0.0
self.z = 0.0
else:
m = 1.0 / l
self.w *= m
self.x *= m
self.y *= m
self.z *= m
def normalize(self):
q = self.copy()
q.normalize_self()
return q
def slerp(self, other, t):
px = self.x
py = self.y
pz = self.z
pw = self.w
qx = other.x
qy = other.y
qz = other.z
qw = other.w
cos_theta = px * qx + py * qy + pz * qz + pw * qw
if cos_theta < 0.0:
qx = -qx
qy = -qy
qz = -qz
qw = -qw
cos_theta = -cos_theta
if cos_theta > 0.999999:
t0 = 1.0 - t
t1 = t
else:
angle = math.acos(cos_theta)
norm = 1.0 / math.sin(angle)
t0 = math.sin((1.0 - t) * angle) * norm
t1 = math.sin(t * angle) * norm
x = px * t0 + qx * t1
y = py * t0 + qy * t1
z = pz * t0 + qz * t1
w = pw * t0 + qw * t1
return Quaternion(w, x, y, z)
def conjugate(self):
return Quaternion(self.w, -self.x, -self.y, -self.z)
def inverse(self):
x = self.x
y = self.y
z = self.z
w = self.w
rmagsqr = 1.0 / (x * x + y * y + z * z + w * w)
return Quaternion(self.w * rmagsqr, -self.x * rmagsqr, -self.y * rmagsqr, -self.z * rmagsqr)
def angle_axis(self):
x = self.x
y = self.y
z = self.z
w = self.w
t1 = 1.0 - w * w
if t1 <= 0.0:
return (0.0, Vector3(0.0, 0.0, 1.0))
angle = math.atan2(x * x + y * y + z * z, w) * 2.0
t2 = 1.0 / math.sqrt(t1)
return (angle, Vector3(x * t2, y * t2, z * t2))
def transform_point(self, p):
return self.transform_vector(p)
def transform_vector(self, v):
vq = Quaternion(0.0, v.x, v.y, v.z)
q_v_qi = self * vq * self.inverse()
return Vector3(q_v_qi.x, q_v_qi.y, q_v_qi.z)
def to_matrix3(self):
x = self.x
y = self.y
z = self.z
w = self.w
qxx = x * x
qyy = y * y
qzz = z * z
qxz = x * z
qxy = x * y
qyz = y * z
qwx = w * x
qwy = w * y
qwz = w * z
m00 = 1.0 - 2.0 * (qyy + qzz)
m01 = 2.0 * (qxy + qwz)
m02 = 2.0 * (qxz - qwy)
m10 = 2.0 * (qxy - qwz)
m11 = 1.0 - 2.0 * (qxx + qzz)
m12 = 2.0 * (qyz + qwx)
m20 = 2.0 * (qxz + qwy)
m21 = 2.0 * (qyz - qwx)
m22 = 1.0 - 2.0 * (qxx + qyy)
axis_x = Vector3(m00, m01, m02)
axis_y = Vector3(m10, m11, m12)
axis_z = Vector3(m20, m21, m22)
return (axis_x, axis_y, axis_z)
def to_matrix4(self):
axis_x, axis_y, axis_z = self.to_matrix3()
return Matrix4(axis_x.x, axis_x.y, axis_x.z, 0.0, axis_y.x, axis_y.y, axis_y.z, 0.0,
axis_z.x, axis_z.y, axis_z.z, 0.0, 0.0, 0.0, 0.0, 1.0)
@staticmethod
def from_angle_axis(angle, axis):
ha = angle * 0.5
s = math.sin(ha)
c = math.cos(ha)
return Quaternion(c, axis.x * s, axis.y * s, axis.z * s)
@staticmethod
def from_matrix3(matrix3):
axis_x, axis_y, axis_z = matrix3
m00 = axis_x.x
m01 = axis_x.y
m02 = axis_x.z
m10 = axis_y.x
m11 = axis_y.y
m12 = axis_y.z
m20 = axis_z.x
m21 = axis_z.y
m22 = axis_z.z
fx = m00 - m11 - m22
fy = m11 - m00 - m22
fz = m22 - m00 - m11
fw = m00 + m11 + m22
bigi = 0
bigf = fw
if fx > bigf:
bigf = fx
bigi = 1
if fy > bigf:
bigf = fy
bigi = 2
if fz > bigf:
bigf = fz
bigi = 3
bigv = math.sqrt(bigf + 1.0) * 0.5
mult = 0.25 / bigv
if bigi == 0:
return Quaternion(bigv, (m12 - m21) * mult, (m20 - m02) * mult, (m01 - m10) * mult)
if bigi == 1:
return Quaternion((m12 - m21) * mult, bigv, (m01 + m10) * mult, (m20 + m02) * mult)
if bigi == 2:
return Quaternion((m20 - m02) * mult, (m01 + m10) * mult, bigv, (m12 + m21) * mult)
if bigi == 3:
return Quaternion((m01 - m10) * mult, (m20 + m02) * mult, (m12 + m21) * mult, bigv)
return Quaternion()
def euler_angles(self):
# (pitch, yaw, roll), euler order y-x-z
# swap ZYX <-> YXZ from Wikipedia Conversion_between_quaternions_and_Euler_angles
x = self.z
y = self.x
z = self.y
w = self.w
xx = x * x
yy = y * y
zz = z * z
sinr_cosp = 2.0 * (w * x + y * z)
cosr_cosp = 1.0 - 2.0 * (xx + yy)
roll = math.atan2(sinr_cosp, cosr_cosp)
sinp = 2.0 * (w * y - z * x)
if abs(sinp) >= 1.0:
pitch = math.copysign(math.pi / 2.0, sinp)
else:
pitch = math.asin(sinp)
siny_cosp = 2.0 * (w * z + x * y)
cosy_cosp = 1.0 - 2.0 * (yy + zz)
yaw = math.atan2(siny_cosp, cosy_cosp)
return Vector3(pitch, yaw, roll)
@staticmethod
def from_euler_angles(euler_angles):
# (pitch, yaw, roll), euler order y-x-z
hpitch = euler_angles.x * 0.5
hyaw = euler_angles.y * 0.5
hroll = euler_angles.z * 0.5
cy = math.cos(hyaw)
sy = math.sin(hyaw)
cp = math.cos(hpitch)
sp = math.sin(hpitch)
cr = math.cos(hroll)
sr = math.sin(hroll)
# swap ZYX <-> YXZ from Wikipedia Conversion_between_quaternions_and_Euler_angles
w = cy * cp * cr + sy * sp * sr
x = cy * cp * sr - sy * sp * cr
y = sy * cp * sr + cy * sp * cr
z = sy * cp * cr - cy * sp * sr
return Quaternion(w, y, z, x)
@staticmethod
def from_from_to_rotation(va, vb):
va = va.normalize()
vb = vb.normalize()
axis = va.cross(vb).normalize()
angle = math.acos(va.dot(vb))
return Quaternion.from_angle_axis(angle, axis)
@staticmethod
def from_look_rotation(forward, up):
# forward becomes negative z
axis_z = -forward
axis_x = up.cross(axis_z).normalize()
axis_y = axis_z.cross(axis_x)
return Quaternion.from_matrix3((axis_x, axis_y, axis_z))
def __mul__(self, other):
res = self.copy()
res *= other
return res
def __imul__(self, other):
px = self.x
py = self.y
pz = self.z
pw = self.w
qx = other.x
qy = other.y
qz = other.z
qw = other.w
self.w = pw * qw - px * qx - py * qy - pz * qz
self.x = pw * qx + px * qw + py * qz - pz * qy
self.y = pw * qy + py * qw + pz * qx - px * qz
self.z = pw * qz + pz * qw + px * qy - py * qx
return self
def __copy__(self):
return Quaternion(self.w, self.x, self.y, self.z)
copy = __copy__
def __repr__(self):
return 'Quaternion({:0.4f}, {:0.4f}, {:0.4f}, {:0.4f})'.format(self.w, self.x, self.y, self.z)
class Transform(object):
__slots__ = ('translation', 'rotation', 'scale')
def __init__(self, translation=Vector3(0.0, 0.0, 0.0), rotation=Quaternion(), scale=Vector3(1.0, 1.0, 1.0)):
self.translation = translation.copy()
self.rotation = rotation.copy()
self.scale = scale.copy()
def transform_point(self, p):
return self.to_matrix4().transform_point(p)
def transform_vector(self, v):
return self.to_matrix4().transform_vector(v)
def inverse(self):
inv_translation = -self.translation
inv_rotation = self.rotation.inverse()
inv_scale = Vector3(1.0 / self.scale.x, 1.0 / self.scale.y, 1.0 / self.scale.z)
inv_mT = Matrix4.from_translation(inv_translation)
inv_mR = Matrix4.from_rotation(inv_rotation)
inv_mS = Matrix4.from_scale(inv_scale)
inv_m = inv_mS * inv_mR * inv_mT
return inv_m.to_transform()
def to_matrix4(self):
# T * R * S
axis_x, axis_y, axis_z = self.rotation.to_axes()
axis_x *= self.scale.x
axis_y *= self.scale.y
axis_z *= self.scale.z
translation = self.translation
return Matrix4(axis_x.x, axis_x.y, axis_x.z, 0.0, axis_y.x, axis_y.y, axis_y.z, 0.0,
axis_z.x, axis_z.y, axis_z.z, 0.0, translation.x, translation.y, translation.z, 1.0)
def __mul__(self, other):
t1 = self.translation
r1 = self.rotation
s1 = self.scale
t2 = other.translation
r2 = other.rotation
s2 = other.scale
t3 = r1.transform_vector(Vector3(s1.x * t2.x, s1.y * t2.y, s1.z * t2.z)) + t1
r3 = r1 * r2
s3 = Vector3(s1.x * s2.x, s1.y * s2.y, s1.z * s2.z)
return Transform(t3, r3, s3)
def __copy__(self):
return Transform(self.translation, self.rotation, self.scale)
copy = __copy__
def __repr__(self):
return 'Transform({}, {}, {})'.format(self.translation, self.rotation, self.scale)
class Vector2:
__slots__ = ('x', 'y')
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
def length_squared(self):
return self.x ** 2 + self.y ** 2
def dot(self, other):
return self.x * other.x + self.y * other.y
def normalize_self(self):
l = self.length()
if l == 0.0:
self.x = 1.0
self.y = 0.0
else:
self *= 1.0 / l
def normalize(self):
v = self.copy()
v.normalize_self()
return v
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __add__(self, other):
return Vector3(self.x + other.x, self.y + other.y)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
return self
def __mul__(self, n):
return Vector3(self.x * n, self.y * n)
def __imul__(self, n):
self.x *= n
self.y *= n
return self
__rmul__ = __mul__
def __sub__(self, other):
return Vector3(self.x - other.x, self.y - other.y)
def __isub__(self, other):
self.x -= other.x
self.y -= other.y
return self
def __neg__(self):
return Vector3(-self.x, -self.y)
def __copy__(self):
return Vector3(self.x, self.y)
copy = __copy__
def __repr__(self):
return 'Vector2({:0.4f}, {:0.4f})'.format(self.x, self.y)
class Vector4:
__slots__ = ('x', 'y', 'z', 'w')
def __init__(self, x=0.0, y=0.0, z=0.0, w=0.0):
self.x = x
self.y = y
self.z = z
self.w = w
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2 + self.w ** 2)
def length_squared(self):
return self.x ** 2 + self.y ** 2 + self.z ** 2 + self.w ** 2
def dot(self, other):
return self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
def normalize_self(self):
l = self.length()
if l == 0.0:
self.x = 1.0
self.y = 0.0
self.z = 0.0
self.w = 0.0
else:
self *= 1.0 / l
def normalize(self):
v = self.copy()
v.normalize_self()
return v
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z and self.w == other.w
def __add__(self, other):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z, self.w + other.w)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
self.z += other.z
self.w += other.w
return self
def __mul__(self, n):
return Vector3(self.x * n, self.y * n, self.z * n, self.w * n)
def __imul__(self, n):
self.x *= n
self.y *= n
self.z *= n
self.w *= n
return self
__rmul__ = __mul__
def __sub__(self, other):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z, self.w - other.w)
def __isub__(self, other):
self.x -= other.x
self.y -= other.y
self.z -= other.z
self.w -= other.w
return self
def __neg__(self):
return Vector3(-self.x, -self.y, -self.z, -self.w)
def __copy__(self):
return Vector3(self.x, self.y, self.z, self.w)
copy = __copy__
def __repr__(self):
return 'Vector4({:0.4f}, {:0.4f}, {:0.4f}, {:0.4f})'.format(self.x, self.y, self.z, self.w)
class Ray:
__slots__ = ('position', 'direction')
def __init__(self, position=Vector3(0.0, 0.0, 0.0), direction=Vector3(0.0, 0.0, 1.0)):
self.position = position.copy()
self.direction = direction.copy()
def __copy__(self):
return Ray(self.position, self.direction)
copy = __copy__
def __repr__(self):
return 'Ray({}, {})'.format(self.position, self.direction)
_py_vmath_types = {typename: globals()[typename] for typename in __all__}
_have_ext = False
try:
from _vmath import *
_have_ext = True
_c_vmath_types = {typename: globals()[typename] for typename in __all__}
except ImportError:
pass
def have_ext():
return _have_ext
def _use_types(types):
global Vector3, Matrix4, Transform, Quaternion, Vector2, Vector4, Ray
d = globals()
for typename in __all__:
type_ = types[typename]
d[typename] = type_
def use_py_types():
_use_types(_py_vmath_types)
def use_c_types():
_use_types(_c_vmath_types)
def _test_euler(euler):
q = Quaternion.from_euler_angles(euler)
print('euler', euler, 'q', q)
print('out', q.euler_angles())
def _test():
v1 = Vector3(1.0, 1.0, 1.0)
v2 = Vector3(1.0, 2.0, 3.0)
print(v1, v2)
print(v1 * 10)
print(10 * v1)
print(-v1 + v2)
m = Matrix4()
print(m)
print(Matrix4.from_orthographic(0, 800, 0, 600, -1.0, 1.0))
print(Matrix4.from_perspective(1.0, 1.6, 0.1, 1000))
_test_euler(Vector3(0.1, 0.2, 0.3))
_test_euler(Vector3(1.1, 6.0, 2.0))
q = Quaternion.from_euler_angles(Vector3(0.2, 4.0, 3.0))
q = Quaternion.from_angle_axis(0.235, Vector3(0.0, 1.0, 0.0))
print(q.euler_angles())
m = q.to_matrix4()
print(m)
q2 = m.to_transform().rotation
print(q, q.euler_angles())
print(q2, q2.euler_angles())
print(q * q.inverse())
print(q2 * q2.conjugate())
t = Transform(Vector3(1.0, 2.0, 3.0), Quaternion.from_euler_angles(Vector3(0.3, 0.5, 0.9)), Vector3(2.0, 2.0, 2.0))
print(t)
print(t.inverse())
print(t * t.inverse())
eye = Vector3(0.0, 0.0, -10.0)
at = Vector3(1.0, 2.0, 3.0)
up = Vector3(0.0, 1.0, 0.0)
m = Matrix4()
#m.set_translation(eye)
#m.set_look_rotation((at - eye).normalize(), up)
print(m)
print(Transform(eye, Quaternion.from_look_rotation((at - eye).normalize(), up), Vector3(1.0, 1.0, 1.0)).to_matrix4())
print('---')
print(m.inverse())
#print(Matrix4.from_look_at(eye, at, up))
if __name__ == '__main__':
_test()