forked from highfestiva/maya-ascii-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat3.py
834 lines (682 loc) · 24.1 KB
/
mat3.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
# coding: latin1
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is the Python Computer Graphics Kit.
#
# The Initial Developer of the Original Code is Matthias Baas.
# Portions created by the Initial Developer are Copyright (C) 2004
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
# $Id: mat3.py,v 1.2 2005/08/17 19:38:29 mbaas Exp $
import types, math, copy
from vec3 import vec3 as _vec3
# [ 0 1 2 ]
# [ 3 4 5 ]
# [ 6 7 8 ]
# Comparison threshold
_epsilon = 1E-12
# mat3
class mat3:
"""Matrix class (3x3).
This class represents a 3x3 matrix that can be used to store
linear transformations.
"""
def __init__(self, *args):
"""Constructor.
There are several possibilities how to initialize a matrix,
depending on the number of arguments you provide to the constructor.
- 0 arguments: Every component is zero.
- 1 number argument: The diagonal is initialized to that number,
all the other elements are zero.
- 1 sequence argument: The elements are initialized with the numbers
in the sequence (the sequence must contain 9 numbers).
- 1 mat3 argument: The matrix is copied.
- 3 sequence arguments: The columns are initialized with the
respective sequence (each sequence must contain 3 numbers).
- 9 number arguments: The matrix is initialized with those values
(row-major order).
"""
# No arguments
if len(args)==0:
self.mlist = 9*[0.0]
# 1 argument (list, scalar or mat3)
elif len(args)==1:
T = type(args[0])
# Scalar
if T==float or T==int or T==int:
f = float(args[0])
self.mlist = [f,0.0,0.0,
0.0,f,0.0,
0.0,0.0,f]
# mat3
elif isinstance(args[0], mat3):
self.mlist = copy.copy(args[0].mlist)
# String
elif T==bytes:
s=args[0].replace(","," ").replace(" "," ").strip().split(" ")
self.mlist=[float(x) for x in s]
else:
self.mlist = mat3(*args[0]).mlist
# 3 arguments (sequences)
elif len(args)==3:
a,b,c=args
self.mlist = [a[0], b[0], c[0],
a[1], b[1], c[1],
a[2], b[2], c[2]]
self.mlist = [float(x) for x in self.mlist]
# 9 arguments
elif len(args)==9:
self.mlist = [float(x) for x in args]
else:
raise TypeError("mat3() arg can't be converted to mat3")
# Check if there are really 9 elements in the list
if len(self.mlist)!=9:
raise TypeError("mat4(): Wrong number of matrix elements ("+repr(len(self.mlist))+" instead of 9)")
def __repr__(self):
return 'mat3('+repr(self.mlist)[1:-1]+')'
def __str__(self):
fmt="%9.4f"
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return ('['+fmt%m11+', '+fmt%m12+', '+fmt%m13+']\n'+
'['+fmt%m21+', '+fmt%m22+', '+fmt%m23+']\n'+
'['+fmt%m31+', '+fmt%m32+', '+fmt%m33+']')
def __eq__(self, other):
"""== operator"""
global _epsilon
if isinstance(other, mat3):
# return self.mlist==other.mlist
lst = [a_b for a_b in list(zip(self.mlist, other.mlist)) if abs(a_b[0]-a_b[1])>_epsilon]
return len(lst)==0
else:
return False
def __ne__(self, other):
"""!= operator"""
return not (self==other)
def __add__(self, other):
if isinstance(other, mat3):
return mat3(list(map(lambda x,y: x+y, self.mlist, other.mlist)))
else:
raise TypeError("unsupported operand type for +")
def __sub__(self, other):
if isinstance(other, mat3):
return mat3(list(map(lambda x,y: x-y, self.mlist, other.mlist)))
else:
raise TypeError("unsupported operand type for -")
def __mul__(self, other):
T = type(other)
# mat3*scalar
if T==float or T==int or T==int:
return mat3(list(map(lambda x,other=other: x*other, self.mlist)))
# mat3*vec3
if isinstance(other, _vec3):
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return _vec3(m11*other.x + m12*other.y + m13*other.z,
m21*other.x + m22*other.y + m23*other.z,
m31*other.x + m32*other.y + m33*other.z)
# mat3*mat3
if isinstance(other, mat3):
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
n11,n12,n13,n21,n22,n23,n31,n32,n33 = other.mlist
return mat3( m11*n11+m12*n21+m13*n31,
m11*n12+m12*n22+m13*n32,
m11*n13+m12*n23+m13*n33,
m21*n11+m22*n21+m23*n31,
m21*n12+m22*n22+m23*n32,
m21*n13+m22*n23+m23*n33,
m31*n11+m32*n21+m33*n31,
m31*n12+m32*n22+m33*n32,
m31*n13+m32*n23+m33*n33)
# unsupported
else:
raise TypeError("unsupported operand type for *")
def __rmul__(self, other):
T = type(other)
# scalar*mat3
if T==float or T==int or T==int:
return mat3(list(map(lambda x,other=other: other*x, self.mlist)))
# vec3*mat3
if isinstance(other, _vec3):
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return _vec3(other.x*m11 + other.y*m21 + other.z*m31,
other.x*m12 + other.y*m22 + other.z*m32,
other.x*m13 + other.y*m23 + other.z*m33)
# mat3*mat3
if isinstance(other, mat3):
return self.__mul__(other)
# unsupported
else:
raise TypeError("unsupported operand type for *")
def __div__(self, other):
T = type(other)
# mat3/scalar
if T==float or T==int or T==int:
return mat3(list(map(lambda x,other=other: x/other, self.mlist)))
# unsupported
else:
raise TypeError("unsupported operand type for /")
def __mod__(self, other):
T = type(other)
# mat3%scalar
if T==float or T==int or T==int:
return mat3(list(map(lambda x,other=other: x%other, self.mlist)))
# mat3%mat3
if isinstance(other, mat3):
return mat3([a_b1[0]%a_b1[1] for a_b1 in list(zip(self.mlist, other.mlist))])
# unsupported
else:
raise TypeError("unsupported operand type for %")
def __neg__(self):
return mat3([-x for x in self.mlist])
def __pos__(self):
return mat3([+x for x in self.mlist])
def __len__(self):
return 3
def __getitem__(self, key):
"""Return a column or an individual element."""
if type(key)==int:
if key==0:
return _vec3(self.mlist[0],self.mlist[3],self.mlist[6])
elif key==1:
return _vec3(self.mlist[1],self.mlist[4],self.mlist[7])
elif key==2:
return _vec3(self.mlist[2],self.mlist[5],self.mlist[8])
else:
raise IndexError("index out of range")
elif type(key)==tuple:
i,j=key
if i<0 or i>2 or j<0 or j>2:
raise IndexError("index out of range")
return self.mlist[i*3+j]
else:
raise TypeError("index must be integer or 2-tuple")
def __setitem__(self, key, value):
"""Set a column or an individual element."""
if type(key)==int:
value = [float(x) for x in value]
if key==0: self.mlist[0],self.mlist[3],self.mlist[6] = value
elif key==1: self.mlist[1],self.mlist[4],self.mlist[7] = value
elif key==2: self.mlist[2],self.mlist[5],self.mlist[8] = value
else:
raise IndexError("index out of range")
elif type(key)==tuple:
i,j=key
if i<0 or i>2 or j<0 or j>2:
raise IndexError("index out of range")
self.mlist[i*3+j] = float(value)
else:
raise TypeError("index must be integer or 2-tuple")
def getRow(self, index):
"""Return a row (as vec3)."""
if type(index)==int:
if index==0:
return _vec3(self.mlist[0], self.mlist[1], self.mlist[2])
elif index==1:
return _vec3(self.mlist[3], self.mlist[4], self.mlist[5])
elif index==2:
return _vec3(self.mlist[6], self.mlist[7], self.mlist[8])
else:
raise IndexError("index out of range")
else:
raise TypeError("index must be an integer")
def setRow(self, index, value):
"""Set a row (as vec3)."""
if type(index)==int:
value = [float(x) for x in value]
if index==0: self.mlist[0], self.mlist[1], self.mlist[2] = value
elif index==1: self.mlist[3], self.mlist[4], self.mlist[5] = value
elif index==2: self.mlist[6], self.mlist[7], self.mlist[8] = value
else:
raise IndexError("index out of range")
else:
raise TypeError("index must be an integer")
def getColumn(self, index):
"""Return a column (as vec3)."""
if type(index)==int:
if index==0:
return _vec3(self.mlist[0], self.mlist[3], self.mlist[6])
elif index==1:
return _vec3(self.mlist[1], self.mlist[4], self.mlist[7])
elif index==2:
return _vec3(self.mlist[2], self.mlist[5], self.mlist[8])
else:
raise IndexError("index out of range")
else:
raise TypeError("index must be an integer")
def setColumn(self, index, value):
"""Set a column."""
if type(index)==int:
value = [float(x) for x in value]
if index==0: self.mlist[0], self.mlist[3], self.mlist[6] = value
elif index==1: self.mlist[1], self.mlist[4], self.mlist[7] = value
elif index==2: self.mlist[2], self.mlist[5], self.mlist[8] = value
else:
raise IndexError("index out of range")
else:
raise TypeError("index must be an integer")
def getDiag(self):
"""Return the diagonal."""
return _vec3(self.mlist[0], self.mlist[4], self.mlist[8])
def setDiag(self, value):
"""Set diagonal."""
a,b,c = value
self.mlist[0] = float(a)
self.mlist[4] = float(b)
self.mlist[8] = float(c)
def toList(self, rowmajor=0):
"""Create a list containing the matrix elements.
By default the list is in column-major order. If you set the
optional argument rowmajor to 1, you'll get the list in row-major
order.
"""
if rowmajor:
return copy.copy(self.mlist)
else:
return self.transpose().mlist
def identity():
"""Return the identity matrix."""
return mat3(1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0)
identity = staticmethod(identity)
def transpose(self):
"""Return the transposed matrix."""
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return mat3(m11,m21,m31,
m12,m22,m32,
m13,m23,m33)
def determinant(self):
"""Return determinant."""
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
return m11*m22*m33+ \
m12*m23*m31+ \
m13*m21*m32- \
m31*m22*m13- \
m32*m23*m11- \
m33*m21*m12
def inverse(self):
"""Return inverse matrix."""
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
d = 1.0/self.determinant()
return mat3( m22*m33-m23*m32, m32*m13-m12*m33, m12*m23-m22*m13,
m23*m31-m21*m33, m11*m33-m31*m13, m21*m13-m11*m23,
m21*m32-m31*m22, m31*m12-m11*m32, m11*m22-m12*m21 )*d
def scaling(s):
"""Return a scale transformation."""
sx,sy,sz = s
return mat3(sx, 0.0, 0.0,
0.0, sy, 0.0,
0.0, 0.0, sz)
scaling = staticmethod(scaling)
def rotation(angle, axis):
"""Return a rotation matrix."""
axis = _vec3(axis)
sqr_a = axis.x*axis.x
sqr_b = axis.y*axis.y
sqr_c = axis.z*axis.z
len2 = sqr_a+sqr_b+sqr_c
k2 = math.cos(angle)
k1 = (1.0-k2)/len2
k3 = math.sin(angle)/math.sqrt(len2)
k1ab = k1*axis.x*axis.y
k1ac = k1*axis.x*axis.z
k1bc = k1*axis.y*axis.z
k3a = k3*axis.x
k3b = k3*axis.y
k3c = k3*axis.z
return mat3( k1*sqr_a+k2, k1ab-k3c, k1ac+k3b,
k1ab+k3c, k1*sqr_b+k2, k1bc-k3a,
k1ac-k3b, k1bc+k3a, k1*sqr_c+k2)
rotation = staticmethod(rotation)
def scale(self, s):
sx = float(s[0])
sy = float(s[1])
sz = float(s[2])
self.mlist[0] *= sx
self.mlist[1] *= sy
self.mlist[2] *= sz
self.mlist[3] *= sx
self.mlist[4] *= sy
self.mlist[5] *= sz
self.mlist[6] *= sx
self.mlist[7] *= sy
self.mlist[8] *= sz
return self
def rotate(self, angle, axis):
R=self.rotation(angle, axis)
self.mlist = (self*R).mlist
return self
def ortho(self):
"""Return a matrix with orthogonal base vectors.
"""
m11,m12,m13,m21,m22,m23,m31,m32,m33 = self.mlist
x = _vec3(m11, m21, m31)
y = _vec3(m12, m22, m32)
z = _vec3(m13, m23, m33)
xl = x.length()
xl*=xl
y = y - ((x*y)/xl)*x
z = z - ((x*z)/xl)*x
yl = y.length()
yl*=yl
z = z - ((y*z)/yl)*y
return mat3( x.x, y.x, z.x,
x.y, y.y, z.y,
x.z, y.z, z.z)
def decompose(self):
"""Decomposes the matrix into a rotation and scaling part.
Returns a tuple (rotation, scaling). The scaling part is given
as a vec3, the rotation is still a mat3.
"""
dummy = self.ortho()
x = dummy.getColumn(0)
y = dummy.getColumn(1)
z = dummy.getColumn(2)
xl = x.length()
yl = y.length()
zl = z.length()
scale = _vec3(xl,yl,zl)
x/=xl
y/=yl
z/=zl
dummy.setColumn(0,x)
dummy.setColumn(1,y)
dummy.setColumn(2,z)
if dummy.determinant()<0.0:
dummy.setColumn(0,-x)
scale.x=-scale.x
return (dummy, scale)
def fromEulerYXZ(x, y, z):
"""Initializes self from Euler angles."""
A = math.cos(x)
B = math.sin(x)
C = math.cos(y)
D = math.sin(y)
E = math.cos(z)
F = math.sin(z)
CE = C*E
CF = C*F
DE = D*E
DF = D*F
return mat3( CE+DF*B, DE*B-CF, A*D,
A*F, A*E, -B,
CF*B-DE, DF+CE*B, A*C )
fromEulerYXZ = staticmethod(fromEulerYXZ)
def fromEulerZXY(x, y, z):
"""Initializes self from Euler angles."""
A = math.cos(x)
B = math.sin(x)
C = math.cos(y)
D = math.sin(y)
E = math.cos(z)
F = math.sin(z)
CE = C*E
CF = C*F
DE = D*E
DF = D*F
return mat3( CE-DF*B, -A*F, DE+CF*B,
CF+DE*B, A*E, DF-CE*B,
-A*D, B, A*C )
fromEulerZXY = staticmethod(fromEulerZXY)
def fromEulerZYX(x, y, z):
"""Initializes self from Euler angles."""
A = math.cos(x)
B = math.sin(x)
C = math.cos(y)
D = math.sin(y)
E = math.cos(z)
F = math.sin(z)
AE = A*E
AF = A*F
BE = B*E
BF = B*F
return mat3( C*E, BE*D-AF, AE*D+BF,
C*F, BF*D+AE, AF*D-BE,
-D, B*C, A*C )
fromEulerZYX = staticmethod(fromEulerZYX)
def fromEulerYZX(x, y, z):
"""Initializes self from Euler angles."""
A = math.cos(x)
B = math.sin(x)
C = math.cos(y)
D = math.sin(y)
E = math.cos(z)
F = math.sin(z)
AC = A*C
AD = A*D
BC = B*C
BD = B*D
return mat3( C*E, BD-AC*F, BC*F+AD,
F, A*E, -B*E,
-D*E, AD*F+BC, AC-BD*F )
fromEulerYZX = staticmethod(fromEulerYZX)
def fromEulerXZY(x, y, z):
"""Initializes self from Euler angles."""
A = math.cos(x)
B = math.sin(x)
C = math.cos(y)
D = math.sin(y)
E = math.cos(z)
F = math.sin(z)
AC = A*C
AD = A*D
BC = B*C
BD = B*D
return mat3( C*E, -F, D*E,
AC*F+BD, A*E, AD*F-BC,
BC*F-AD, B*E, BD*F+AC )
fromEulerXZY = staticmethod(fromEulerXZY)
def fromEulerXYZ(x, y, z):
"""Initializes self from Euler angles."""
A = math.cos(x)
B = math.sin(x)
C = math.cos(y)
D = math.sin(y)
E = math.cos(z)
F = math.sin(z)
AE = A*E
AF = A*F
BE = B*E
BF = B*F
return mat3( C*E, -C*F, D,
AF+BE*D, AE-BF*D, -B*C,
BF-AE*D, BE+AF*D, A*C )
fromEulerXYZ = staticmethod(fromEulerXYZ)
def toEulerYXZ(self):
"""Return the Euler angles of a rotation matrix."""
global _epsilon
r1 = self.getRow(0)
r2 = self.getRow(1)
r3 = self.getRow(2)
B = -r2.z
x = math.asin(B)
A = math.cos(x)
if (A>_epsilon):
y = mat3.acos(r3.z/A)
z = mat3.acos(r2.y/A)
else:
z = 0.0
y = mat3.acos(r1.x)
return (x,y,z)
def toEulerZXY(self):
"""Return the Euler angles of a rotation matrix."""
global _epsilon
r1 = self.getRow(0)
r2 = self.getRow(1)
r3 = self.getRow(2)
B = r3.y
x = math.asin(B)
A = math.cos(x)
if (A>_epsilon):
y = mat3.acos(r3.z/A)
z = mat3.acos(r2.y/A)
else:
z = 0.0
y = mat3.acos(r1.x)
return (x,y,z)
def toEulerZYX(self):
"""Return the Euler angles of a rotation matrix."""
global _epsilon
r1 = self.getRow(0)
r2 = self.getRow(1)
r3 = self.getRow(2)
D = -r3.x
y = math.asin(D)
C = math.cos(y)
if (C>_epsilon):
x = mat3.acos(r3.z/C)
z = mat3.acos(r1.x/C)
else:
z = 0.0
x = mat3.acos(-r2.y)
return (x,y,z)
def toEulerYZX(self):
"""Return the Euler angles of a rotation matrix."""
global _epsilon
r1 = self.getRow(0)
r2 = self.getRow(1)
r3 = self.getRow(2)
F = r2.x
z = math.asin(F)
E = math.cos(z)
if (E>_epsilon):
x = mat3.acos(r2.y/E)
y = mat3.acos(r1.x/E)
else:
y = 0.0
x = math.asin(r3.y)
return (x,y,z)
def toEulerXZY(self):
"""Return the Euler angles of a rotation matrix."""
global _epsilon
r1 = self.getRow(0)
r2 = self.getRow(1)
r3 = self.getRow(2)
F = -r1.y
z = math.asin(F)
E = math.cos(z)
if (E>_epsilon):
x = mat3.acos(r2.y/E)
y = mat3.acos(r1.x/E)
else:
y = 0.0
x = mat3.acos(r3.z)
return (x,y,z)
def toEulerXYZ(self):
"""Return the Euler angles of a rotation matrix."""
global _epsilon
r1 = self.getRow(0)
r2 = self.getRow(1)
r3 = self.getRow(2)
D = r1.z
y = math.asin(D)
C = math.cos(y)
if (C>_epsilon):
x = mat3.acos(r3.z/C)
z = mat3.acos(r1.x/C)
else:
z = 0.0
x = mat3.acos(r2.y)
return (x,y,z)
def fromToRotation(_from, to):
"""Returns a rotation matrix that rotates one vector into another.
The generated rotation matrix will rotate the vector _from into
the vector to. _from and to must be unit vectors!
This method is based on the code from:
Tomas Möller, John Hughes
Efficiently Building a Matrix to Rotate One Vector to Another
Journal of Graphics Tools, 4(4):1-4, 1999
http://www.acm.org/jgt/papers/MollerHughes99/
"""
_from = _vec3(_from)
to = _vec3(to)
EPSILON = 0.000001
e = _from*to
f = abs(e)
if (f>1.0-EPSILON): # "from" and "to"-vector almost parallel
# vector most nearly orthogonal to "from"
fx = abs(_from.x)
fy = abs(_from.y)
fz = abs(_from.z)
if (fx<fy):
if (fx<fz):
x = _vec3(1.0, 0.0, 0.0)
else:
x = _vec3(0.0, 0.0, 1.0)
else:
if (fy<fz):
x = _vec3(0.0, 1.0, 0.0)
else:
x = _vec3(0.0, 0.0, 1.0)
u = x-_from
v = x-to
c1 = 2.0/(u*u)
c2 = 2.0/(v*v)
c3 = c1*c2*u*v
res = mat3()
for i in range(3):
for j in range(3):
res[i,j] = - c1*u[i]*u[j] - c2*v[i]*v[j] + c3*v[i]*u[j]
res[i,i] += 1.0
return res
else: # the most common case, unless "from"="to", or "from"=-"to"
v = _from.cross(to)
h = 1.0/(1.0 + e) # optimization by Gottfried Chen
hvx = h*v.x
hvz = h*v.z
hvxy = hvx*v.y
hvxz = hvx*v.z
hvyz = hvz*v.y
m11 = e + hvx*v.x
m12 = hvxy - v.z
m13 = hvxz + v.y
m21 = hvxy + v.z
m22 = e + h*v.y*v.y
m23 = hvyz - v.x
m31 = hvxz - v.y
m32 = hvyz + v.x
m33 = e + hvz*v.z
return mat3(m11,m12,m13,m21,m22,m23,m31,m32,m33)
fromToRotation = staticmethod(fromToRotation)
@staticmethod
def acos(angle):
global _epsilon
if angle+_epsilon >= 1:
return math.pi
if angle-_epsilon < -1:
return 0
return math.acos(angle)
######################################################################
if __name__=="__main__":
vec3 = _vec3
a=vec3(1,2,3)
M = mat3("2,4,5,6")
a=mat3(M)
a[0,0]=17
print(M)
print(a)