-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix44.py
1109 lines (746 loc) · 34.2 KB
/
matrix44.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 util import format_number
from vector3 import Vector3
from math import sin, cos, tan, sqrt, pi, radians
#import psyco
#psyco.full()
class Matrix44Error(Exception):
"""Matrix44 Exception class"""
def __init__(self, description):
Exception.__init__(self)
self.description = description
def __str__(self):
return self.description
class Matrix44(object):
_identity = ( (1.0 ,0.0 ,0.0 ,0.0),
(0.0 ,1.0 ,0.0 ,0.0),
(0.0 ,0.0 ,1.0 ,0.0),
(0.0 ,0.0 ,0.0 ,1.0) )
__slots__ = ('_m',)
def __init__(self, *args):
"""If no parameteres are given, the Matrix44 is initialised to the identity Matrix44.
If 1 parameter is given it should be an iterable with the 16 values of the Matrix44.
If 4 parameters are given they should be 4 sequences of up to 4 values.
Missing values in each row are padded out with values from the identity matix
(so you can use Vector3's or tuples of 3 values).
"""
if not args:
self._m = [1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1.]
return
elif len(args) == 4:
self._m = [1.,0.,0.,0., 0.,1.,0.,0., 0.,0.,1.,0., 0.,0.,0.,1.]
row_0, row_1, row_2, row_3 = self._setters
r1, r2, r3, r4 = args
row_0(r1)
row_1(r2)
row_2(r3)
row_3(r4)
else:
raise TypeError("Matrix44.__init__() takes 0, or 4 arguments (%i given)"%len(args))
def _get_row_0(self):
return tuple(self._m[0:4])
def _get_row_1(self):
return tuple(self._m[4:8])
def _get_row_2(self):
return tuple(self._m[8:12])
def _get_row_3(self):
return tuple(self._m[12:16])
def _set_row_0(self, values):
values = tuple(values)[:4]
self._m[0:len(values)] = map(float, values)
def _set_row_1(self, values):
values = tuple(values)[:4]
self._m[4:4+len(values)] = map(float, values)
def _set_row_2(self, values):
values = tuple(values)[:4]
self._m[8:8+len(values)] = map(float, values)
def _set_row_3(self, values):
values = tuple(values)[:4]
self._m[12:12+len(values)] = map(float, values)
_getters = (_get_row_0, _get_row_1, _get_row_2, _get_row_3)
_setters = (_set_row_0, _set_row_1, _set_row_2, _set_row_3)
_row0 = property(_get_row_0, _set_row_0, None, "Row 0")
_row1 = property(_get_row_1, _set_row_1, None, "Row 1")
_row2 = property(_get_row_2, _set_row_2, None, "Row 2")
_row3 = property(_get_row_3, _set_row_3, None, "Row 3")
x_axis = _row0
right = _row0
y_axis = _row1
up = _row1
z_axis = _row2
forward = _row2
translate = _row3
def to_opengl(self):
"""Converts the matrix in to a list of values, suitable for using
with glLoadMatrix*"""
return self._m[:]
def set(self, row1, row2, row3, row4):
"""Sets all four rows of the matrix, rows must be a sequence of 4
floats
row1 -- first row
row2 -- second row
row3 -- third row
row4 -- fourth row
"""
m = self._m
m[0:4] = row1
m[4:8] = row2
m[8:12] = row3
m[12:16] = row4
def get_row(self, row_no):
"""Gets a row of the matrix as a tuple
row_no -- Index of row
"""
try:
return self._getters[row_no](self)
except IndexError:
raise IndexError("Row must be 0, 1, 2 or 3")
@classmethod
def from_iter(cls, iterable):
"""Creates a Matrix44 from an iterable of 16 values."""
m = cls.__new__(cls, object)
m._m = map(float, iterable)
if len(m._m) != 16:
raise Matrix44Error("Iterable must have 16 values")
return m
@classmethod
def clone(cls, copy_Matrix44):
"""Creates a Matrix44 that is a copy of another."""
m = cls.__new__(cls, object)
m._m = copy_Matrix44._m[:]
return m
@classmethod
def blank(cls):
"""Creates a blank Matrix44 (with no information). This is rarely
required, you may want to use an identity Matrix44,
see Matrix44.identity()
"""
m = cls.__new__(cls, object)
m._m = [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,]
return m
@classmethod
def identity(cls):
"""Creates and identity Matrix44."""
m = cls.__new__(cls, object)
m._m = [1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.]
return m
@classmethod
def scale(cls, scale_x, scale_y= None, scale_z= None):
"""Creates a scale Matrix44.
If one parameter is given the scale is uniform,
if three parameters are give the scale is different (potentialy) on each x axis.
"""
m = cls.__new__(cls, object)
return m.make_scale(scale_x, scale_y, scale_z)
@classmethod
def translation(cls, x, y, z):
"""Creates a translation Matrix44 to (x, y, z).
x -- X Coordinate
y -- Y Coordinate
z -- Z Coordinate
"""
m = cls.__new__(cls, object)
return m.make_translation(x, y, z)
@classmethod
def x_rotation(cls, angle):
"""Creates a Matrix44 that does a rotation about the x axis.
angle -- Angle of rotation (in radians)
"""
m = cls.__new__(cls, object)
return m.make_x_rotation(angle)
@classmethod
def y_rotation(cls, angle):
"""Creates a Matrix44 that does a rotation about the y axis.
angle -- Angle of rotation (in radians)
"""
m = cls.__new__(cls, object)
return m.make_y_rotation(angle)
@classmethod
def z_rotation(cls, angle):
"""Creates a Matrix44 that does a rotation about the z axis.
angle -- Angle of rotation (in radians)
"""
m = cls.__new__(cls, object)
return m.make_z_rotation(angle)
@classmethod
def rotation_about_axis(cls, axis, angle):
"""Creates a Matrix44 that does a rotation about an axis.
axis -- A vector of the axis
angle -- Angle of rotation
"""
m = cls.__new__(cls, object)
return m.make_rotation_about_axis(axis, angle)
@classmethod
def xyz_rotation(cls, angle_x, angle_y, angle_z):
"""Creates a Matrix44 that does a rotation about each axis.
angle_x -- Angle of rotation, about x
angle_y -- Angle of rotation, about y
angle_z -- Angle of rotation, about z
"""
m = cls.__new__(cls, object)
return m.make_xyz_rotation(angle_x, angle_y, angle_z)
@classmethod
def perspective_projection(cls, left, right, top, bottom, near, far):
"""Creates a Matrix44 that projects points in to 2d space.
left -- Coordinate of left of screen
right -- Coordination of right of screen
top -- Coordination of the top of the screen
bottom -- Coordination of the borrom of the screen
near -- Coordination of the near clipping plane
far -- Coordinate of the far clipping plane
"""
m = cls.__new__(cls, object)
return m.make_perspective_projection( left,
right,
top,
bottom,
near,
far)
@classmethod
def perspective_projection_fov(cls, fov, aspect, near, far):
"""Creates a Matrix44 that projects points in to 2d space
fov -- The field of view (in radians)
aspect -- The aspect ratio of the screen (width / height)
near -- Coordinate of the near clipping plane
far -- Coordinate of the far clipping plane
"""
m = cls.__new__(cls, object)
return m.make_perspective_projection_fov(fov, aspect, near, far)
def __str__(self):
"""'Pretty' formatting of the Matrix44."""
max_len = max( len(format_number(v)) for v in self.components() )
def format_row(row):
return "%s" % " ".join \
( format_number(value).ljust(max_len) for value in row )
rows = [ format_row(row).rstrip() for row in self.rows() ]
max_row_len = max(len(row) for row in rows)
return "\n".join( "[ %s ]" % row.ljust(max_row_len) for row in rows )
def __repr__(self):
def format_row(row):
return "(%s)" % ", ".join( format_number(value) for value in row )
return "Matrix44(%s)" % \
", ".join(format_row(row) for row in self.rows())
def __setitem__(self, coord, value):
"""Sets an individual element in the Matrix44.
coord is a tuple of (row, column)
eg. Matrix44[2,3] = 3.
"""
try:
self._m[coord[0] * 4 + coord[1]] = float(value)
except IndexError:
raise IndexError( "Row and Column should be 0, 1, 2 or 3" )
def __getitem__(self, coord):
"""Gets an individual element in the Matrix44.
coord is a tuple of (row, column)
eg. print Matrix44[2,3]
"""
try:
return self._m[coord[0] * 4 + coord[1]]
except IndexError:
raise IndexError( "Row and Column should be 0, 1, 2 or 3" )
except TypeError:
raise TypeError( "index should be two values containing"\
" the row and column" )
def __iter__(self):
"""Iterates over all 16 values in the Matrix44."""
return iter(self._m[:])
def __neg__(self):
"""Returns the inverse of the matrix."""
return self.get_inverse()
def __mul__(self, rhs):
"""Returns the result of multiplying this Matrix44 by another, called
by the * (multiply) operator."""
m1_0, m1_1, m1_2, m1_3, \
m1_4, m1_5, m1_6, m1_7, \
m1_8, m1_9, m1_10, m1_11, \
m1_12, m1_13, m1_14, m1_15 = self._m
m2_0, m2_1, m2_2, m2_3, \
m2_4, m2_5, m2_6, m2_7, \
m2_8, m2_9, m2_10, m2_11, \
m2_12, m2_13, m2_14, m2_15 = rhs._m
retm = [ m2_0 * m1_0 + m2_1 * m1_4 + m2_2 * m1_8 + m2_3 * m1_12,
m2_0 * m1_1 + m2_1 * m1_5 + m2_2 * m1_9 + m2_3 * m1_13,
m2_0 * m1_2 + m2_1 * m1_6 + m2_2 * m1_10 + m2_3 * m1_14,
m2_0 * m1_3 + m2_1 * m1_7 + m2_2 * m1_11 + m2_3 * m1_15,
m2_4 * m1_0 + m2_5 * m1_4 + m2_6 * m1_8 + m2_7 * m1_12,
m2_4 * m1_1 + m2_5 * m1_5 + m2_6 * m1_9 + m2_7 * m1_13,
m2_4 * m1_2 + m2_5 * m1_6 + m2_6 * m1_10 + m2_7 * m1_14,
m2_4 * m1_3 + m2_5 * m1_7 + m2_6 * m1_11 + m2_7 * m1_15,
m2_8 * m1_0 + m2_9 * m1_4 + m2_10 * m1_8 + m2_11 * m1_12,
m2_8 * m1_1 + m2_9 * m1_5 + m2_10 * m1_9 + m2_11 * m1_13,
m2_8 * m1_2 + m2_9 * m1_6 + m2_10 * m1_10 + m2_11 * m1_14,
m2_8 * m1_3 + m2_9 * m1_7 + m2_10 * m1_11 + m2_11 * m1_15,
m2_12 * m1_0 + m2_13 * m1_4 + m2_14 * m1_8 + m2_15 * m1_12,
m2_12 * m1_1 + m2_13 * m1_5 + m2_14 * m1_9 + m2_15 * m1_13,
m2_12 * m1_2 + m2_13 * m1_6 + m2_14 * m1_10 + m2_15 * m1_14,
m2_12 * m1_3 + m2_13 * m1_7 + m2_14 * m1_11 + m2_15 * m1_15 ]
ret = self.__new__(self.__class__, object)
ret._m = retm
return ret
def __imul__(self, rhs):
"""Multiplies this Matrix44 by another, called by the *= operator."""
m1_0, m1_1, m1_2, m1_3, \
m1_4, m1_5, m1_6, m1_7, \
m1_8, m1_9, m1_10, m1_11, \
m1_12, m1_13, m1_14, m1_15 = self._m
m2_0, m2_1, m2_2, m2_3, \
m2_4, m2_5, m2_6, m2_7, \
m2_8, m2_9, m2_10, m2_11, \
m2_12, m2_13, m2_14, m2_15 = rhs._m
self._m = [ m2_0 * m1_0 + m2_1 * m1_4 + m2_2 * m1_8 + m2_3 * m1_12,
m2_0 * m1_1 + m2_1 * m1_5 + m2_2 * m1_9 + m2_3 * m1_13,
m2_0 * m1_2 + m2_1 * m1_6 + m2_2 * m1_10 + m2_3 * m1_14,
m2_0 * m1_3 + m2_1 * m1_7 + m2_2 * m1_11 + m2_3 * m1_15,
m2_4 * m1_0 + m2_5 * m1_4 + m2_6 * m1_8 + m2_7 * m1_12,
m2_4 * m1_1 + m2_5 * m1_5 + m2_6 * m1_9 + m2_7 * m1_13,
m2_4 * m1_2 + m2_5 * m1_6 + m2_6 * m1_10 + m2_7 * m1_14,
m2_4 * m1_3 + m2_5 * m1_7 + m2_6 * m1_11 + m2_7 * m1_15,
m2_8 * m1_0 + m2_9 * m1_4 + m2_10 * m1_8 + m2_11 * m1_12,
m2_8 * m1_1 + m2_9 * m1_5 + m2_10 * m1_9 + m2_11 * m1_13,
m2_8 * m1_2 + m2_9 * m1_6 + m2_10 * m1_10 + m2_11 * m1_14,
m2_8 * m1_3 + m2_9 * m1_7 + m2_10 * m1_11 + m2_11 * m1_15,
m2_12 * m1_0 + m2_13 * m1_4 + m2_14 * m1_8 + m2_15 * m1_12,
m2_12 * m1_1 + m2_13 * m1_5 + m2_14 * m1_9 + m2_15 * m1_13,
m2_12 * m1_2 + m2_13 * m1_6 + m2_14 * m1_10 + m2_15 * m1_14,
m2_12 * m1_3 + m2_13 * m1_7 + m2_14 * m1_11 + m2_15 * m1_15]
return self
def __copy__(self):
return self.clone(self)
def copy(self):
"""Returns a copy of this matrix."""
return self.clone(self)
def components(self):
"""Returns an iterator for the components in the Matrix44. ie
returns all 16 values."""
return iter(self._m)
def transposed_components(self):
"""Returns an iterator for the components in the Matrix44 in
transposed order."""
m00, m01, m02, m03, \
m10, m11, m12, m13, \
m20, m21, m22, m23, \
m30, m31, m32, m33 = self._m
return iter( ( m00, m10, m20, m30,
m01, m11, m21, m31,
m02, m12, m22, m32,
m03, m13, m23, m33 ) )
def rows(self):
"""Returns an iterator for the rows in the Matrix44 (yields 4 tuples
of 4 values)."""
m = self._m
return iter(( tuple(m[0:4]),
tuple(m[4:8]),
tuple(m[8:12]),
tuple(m[12:16]) ))
def columns(self):
"""Returns an iterator for the columns in the Matrix44 (yields 4
tuples of 4 values)."""
col = self.get_column
return iter((col(0), col(1), col(2), col(3)))
def get_row_vec3(self, row_no):
"""Returns a Vector3 for a given row.
row_no -- The row index
"""
try:
r = row_no*4
x, y, z = self._m[r:r+3]
return Vector3.from_floats(x, y, z)
except IndexError:
raise IndexError( "Row and Column should be 0, 1, 2 or 3" )
def get_column(self, col_no):
"""Returns a column as a tuple of 4 values.
col_no -- The column index
"""
try:
m = self._m
return ( m[col_no],
m[col_no+4],
m[col_no+8],
m[col_no+12] )
except IndexError:
raise IndexError( "Column should be 0, 1, 2 or 3" )
def set_row(self, row_no, row):
"""Sets the values in a row.
row_no -- The index of the row
row -- An container containing the new values
"""
try:
self._setters[row_no](self, row)
except IndexError:
raise IndexError( "Row should be 0, 1, 2 or 3" )
def set_column(self, col_no, col):
"""Sets the values in a column.
col_no -- The index of the column
col -- An sequence of 4 values
"""
try:
col_iter = iter(col)
m = self._m
a, b, c, d = col
m[col_no] = float(a)
m[col_no+4] = float(b)
m[col_no+8] = float(c)
m[col_no+12] = float(d)
except IndexError:
raise IndexError( "Column should be 0, 1, 2 or 3" )
def transform_vec3(self, v):
"""Transforms a vector and returns the result as a Vector3.
v -- Vector to transform
"""
m = self._m
x, y, z = v
return Vector3.from_floats( x * m[0] + y * m[4] + z * m[8] + m[12],
x * m[1] + y * m[5] + z * m[9] + m[13],
x * m[2] + y * m[6] + z * m[10] + m[14] )
def transform(self, v):
"""Transforms a Vector3 and returns the result as a tuple.
v -- Vector to transform
"""
m = self._m
x, y, z = v
return ( x * m[0] + y * m[4] + z * m[8] + m[12],
x * m[1] + y * m[5] + z * m[9] + m[13],
x * m[2] + y * m[6] + z * m[10] + m[14] )
def transform4(self, v):
"""Transforms a 4d vector and returns the result as a tuple.
v -- Vector to transform
"""
m = self._m
x, y, z, w = v
return ( x * m[0] + y * m[4] + z * m[8] + w * m[12],
x * m[1] + y * m[5] + z * m[9] + w * m[13],
x * m[2] + y * m[6] + z * m[10] + w * m[14] )
def iter_transform_vec3(self, points):
"""Transforms a sequence of points, and yields the result as Vector3s
points -- A sequence of vectors
"""
m_0, m_1, m_2, m_3, \
m_4, m_5, m_6, m_7, \
m_8, m_9, m_10, m_11, \
m_12, m_13, m_14, m_15 = self._m
for x, y, z in points:
yield Vector3.from_floats( x * m_0 + y * m_4 + z * m_8 + m_12,
x * m_1 + y * m_5 + z * m_9 + m_13,
x * m_2 + y * m_6 + z * m_10 + m_14 )
def iter_transform(self, points):
"""Transforms a sequence of points and yields the result as tuples.
points -- A sequence of vectors
"""
m_0, m_1, m_2, m_3, \
m_4, m_5, m_6, m_7, \
m_8, m_9, m_10, m_11, \
m_12, m_13, m_14, m_15 = self._m
for x, y, z in points:
yield ( x * m_0 + y * m_4 + z * m_8 + m_12,
x * m_1 + y * m_5 + z * m_9 + m_13,
x * m_2 + y * m_6 + z * m_10 + m_14 )
iter_transform3 = iter_transform
def iter_transform4(self, points):
"""Transforms a sequence of points and yields the result as tuples.
points -- A sequence of vectors
"""
m_0, m_1, m_2, m_3, \
m_4, m_5, m_6, m_7, \
m_8, m_9, m_10, m_11, \
m_12, m_13, m_14, m_15 = self._m
for x, y, z, w in points:
yield ( x * m_0 + y * m_4 + z * m_8 + w * m_12,
x * m_1 + y * m_5 + z * m_9 + w * m_13,
x * m_2 + y * m_6 + z * m_10 + w * m_14 )
def rotate_vec3(self, v):
"""Rotates a Vector3 and returns the result.
The translation part of the Matrix44 is ignored.
v -- Vector to rotate
"""
m = self._m
x, y, z = v
return Vector3.from_floats( x * m[0] + y * m[4] + z * m[8],
x * m[1] + y * m[5] + z * m[9],
x * m[2] + y * m[6] + z * m[10] )
def rotate(self, v):
"""Rotates a Vector3 and returns the result as a tuple
The translation part of the Matrix44 is ignored.
v -- Vector to rotate
"""
m = self._m
x, y, z = v
return ( x * m[0] + y * m[4] + z * m[8],
x * m[1] + y * m[5] + z * m[9],
x * m[2] + y * m[6] + z * m[10] )
def inverse_transform(self, v):
"""Inverse trasforms a Vector3 and returns the result.
Warning: This is expensive, pre-calculate an inverse Matrix44 if you
can.
v -- Vector to transform
"""
return self.get_inverse().transform(v)
def make_identity(self):
"""Makes an identity Matrix44."""
self._m = [1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
0., 0., 0., 1.]
return self
def make_copy(self, other):
"""Makes a copy of another Matrix44."""
self._m = other._m[:]
return self
def make_scale(self, scale_x, scale_y= None, scale_z= None):
"""Makes a scale Matrix44.
If the scale_y and scale_z parameters are not given they default to the same as scale_x.
"""
if scale_y is None:
scale_y = scale_x
if scale_z is None:
scale_z = scale_x
self._m = [float(scale_x), 0., 0., 0.,
0., float(scale_y), 0., 0.,
0., 0., float(scale_z), 0.,
0., 0., 0., 1.]
return self
def make_translation(self, x, y, z):
"""Makes a translation Matrix44."""
self._m = [1., 0., 0., 0.,
0., 1., 0., 0.,
0., 0., 1., 0.,
float(x), float(y), float(z), 1.]
return self
def make_x_rotation(self, angle):
"""Makes a rotation Matrix44 around the x axis."""
cos_a = cos(angle)
sin_a = sin(angle)
self._m = [1., 0., 0., 0.,
0., cos_a, sin_a, 0.,
0., -sin_a, cos_a, 0.,
0., 0., 0., 1.]
return self
def make_y_rotation(self, angle):
"""Makes a rotation Matrix44 around the y axis."""
cos_a = cos(angle)
sin_a = sin(angle)
self._m = [ cos_a, 0., -sin_a, 0.,
0., 1., 0., 0.,
sin_a, 0., cos_a, 0.,
0., 0., 0., 1.]
return self
def make_z_rotation(self, angle):
"""Makes a rotation Matrix44 around the z axis."""
cos_a = cos(angle)
sin_a = sin(angle)
self._m = [ cos_a, sin_a, 0., 0.,
-sin_a, cos_a, 0., 0.,
0., 0., 1., 0.,
0., 0., 0., 1.]
return self
def make_rotation_about_axis(self, axis, angle):
"""Makes a rotation Matrix44 around an axis.
axis -- An iterable containing the axis (three values)
angle -- The angle to rotate (in radians)
"""
c = cos(angle)
s = sin(angle)
omc = 1. - c
x, y, z = axis
self._m = [x*x*omc+c, y*x*omc+z*s, x*z*omc-y*s, 0.,
x*y*omc-z*s, y*y*omc+c, y*z*omc+x*s, 0.,
x*z*omc+y*s, y*z*omc-x*s, z*z*omc+c, 0.,
0., 0., 0., 1.]
return self
def make_xyz_rotation(self, angle_x, angle_y, angle_z):
"""Makes a rotation Matrix44 about 3 axis."""
cx = cos(angle_x)
sx = sin(angle_x)
cy = cos(angle_y)
sy = sin(angle_y)
cz = cos(angle_z)
sz = sin(angle_z)
sxsy = sx*sy
cxsy = cx*sy
# http://web.archive.org/web/20041029003853/http:/www.j3d.org/matrix_faq/matrfaq_latest.html#Q35
#A = cos(angle_x)
#B = sin(angle_x)
#C = cos(angle_y)
#D = sin(angle_y)
#E = cos(angle_z)
#F = sin(angle_z)
# | CE -CF D 0 |
#M = | BDE+AF -BDF+AE -BC 0 |
# | -ADE+BF ADF+BE AC 0 |
# | 0 0 0 1 |
self._m = [ cy*cz, sxsy*cz+cx*sz, -cxsy*cz+sx*sz, 0.,
-cy*sz, -sxsy*sz+cx*cz, cxsy*sz+sx*cz, 0.,
sy, -sx*cy, cx*cy, 0.,
0., 0., 0., 1.]
return self
def make_perspective_projection(self, left, right, top, bottom, near, far):
"""Makes a perspective projection Matrix44.
left -- Coordinate of left of screen
right -- Coordination of right of screen
top -- Coordination of the top of the screen
bottom -- Coordination of the borrom of the screen
near -- Coordination of the near clipping plane
far -- Coordinate of the far clipping plane
"""
self._m = [(2.*near)/(right-left), 0., 0., 0.,
0., (2.*near)/(top-bottom), 0., 0.,
(right+left)/(right-left), (top+bottom)/(top-bottom), -((far+near)/(far-near)), -1.,
0., 0., -((2.*far*near)/(far-near)), 0.]
return self
def make_perspective_projection_fov(self, fov, aspect, near, far):
"""Creates a Matrix44 that projects points in to 2d space
fov -- The field of view (in radians)
aspect -- The aspect ratio of the screen (width / height)
near -- Coordinate of the near clipping plane
far -- Coordinate of the far clipping plane
"""
assert fov < pi, "The field of view should be less than pi radians"\
" (180 degrees)"
range = near*tan(fov/2.);
left = -range*aspect
right = range*aspect
bottom = -range
top = range
#right = tan( fov/2. ) * near
#left = - right
#top = right / aspect
#bottom = -top
self.make_perspective_projection(left, right, bottom, top, near, far)
return self
def transpose(self):
"""Swaps the rows for columns."""
m00, m01, m02, m03, \
m10, m11, m12, m13, \
m20, m21, m22, m23, \
m30, m31, m32, m33 = self._m
self._m = [ m00, m10, m20, m30,
m01, m11, m21, m31,
m02, m12, m22, m32,
m03, m13, m23, m33 ]
def get_transpose(self):
"""Returns a Matrix44 that is a copy of this, but with rows and
columns swapped."""
m00, m01, m02, m03, \
m10, m11, m12, m13, \
m20, m21, m22, m23, \
m30, m31, m32, m33 = self._m
ret = self.__new__(self.__class__, object)
ret._m = [ m00, m10, m20, m30,
m01, m11, m21, m31,
m02, m12, m22, m32,
m03, m13, m23, m33 ]
return ret
def get_inverse_rot_trans(self):
"""Returns the inverse of a Matrix44 with only rotation and
translation. This is faster than the general get_inverse method."""
ret = self.copy()
m = ret._m
i0, i1, i2, i3, \
i4, i5, i6, i7, \
i8, i9, i10, i11, \
i12, i13, i14, i15 = self._m
m[1] = i4
m[4] = i1
m[2] = i8
m[8] = i2
m[6] = i9
m[9] = i6
m[12] = ( m[0] * -i12 +
m[4] * -i13 +
m[8] * -i14 )
m[13] = ( m[1] * -i12 +
m[5] * -i13 +
m[9] * -i14 )
m[14] = ( m[2] * -i12 +
m[6] * -i13 +
m[10] * -i14 )
return ret
def get_inverse(self):
"""Returns the inverse (matrix with the opposite effect) of this
matrix."""
ret = self.__new__(self.__class__, object)
i = self._m
i0, i1, i2, i3, \
i4, i5, i6, i7, \
i8, i9, i10, i11, \
i12, i13, i14, i15 = i
negpos=[0., 0.]
temp = i0 * i5 * i10
negpos[temp > 0.] += temp
temp = i1 * i6 * i8
negpos[temp > 0.] += temp
temp = i2 * i4 * i9
negpos[temp > 0.] += temp
temp = -i2 * i5 * i8
negpos[temp > 0.] += temp
temp = -i1 * i4 * i10
negpos[temp > 0.] += temp
temp = -i0 * i6 * i9