1 ! (c)Joe Groff bsd license
2 USING: accessors classes.struct fry generalizations kernel locals
3 math math.combinatorics math.functions math.matrices.simd math.vectors
4 math.vectors.simd sequences sequences.private specialized-arrays
6 QUALIFIED-WITH: alien.c-types c
8 SPECIALIZED-ARRAY: float-4
14 INSTANCE: matrix4 immutable-sequence
16 M: matrix4 length drop 4 ; inline
17 M: matrix4 nth-unsafe rows>> nth-unsafe ; inline
18 M: matrix4 new-sequence 2drop matrix4 (struct) ; inline
22 : rows ( a -- a1 a2 a3 a4 )
23 rows>> 4 firstn ; inline
25 :: set-rows ( c1 c2 c3 c4 c -- c )
33 : make-matrix4 ( quot: ( -- c1 c2 c3 c4 ) -- c )
34 matrix4 (struct) swap dip set-rows ; inline
36 :: 2map-rows ( a b quot -- c )
38 a rows :> a4 :> a3 :> a2 :> a1
39 b rows :> b4 :> b3 :> b2 :> b1
45 ] make-matrix4 ; inline
47 : map-rows ( a quot -- c )
48 '[ rows _ 4 napply ] make-matrix4 ; inline
52 TYPED: m4+ ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v+ ] 2map-rows ;
53 TYPED: m4- ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v- ] 2map-rows ;
54 TYPED: m4* ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v* ] 2map-rows ;
55 TYPED: m4/ ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v/ ] 2map-rows ;
57 TYPED: m4*n ( a: matrix4 b: float -- c: matrix4 ) [ v*n ] curry map-rows ;
58 TYPED: m4/n ( a: matrix4 b: float -- c: matrix4 ) [ v/n ] curry map-rows ;
59 TYPED: n*m4 ( a: float b: matrix4 -- c: matrix4 ) [ n*v ] with map-rows ;
60 TYPED: n/m4 ( a: float b: matrix4 -- c: matrix4 ) [ n/v ] with map-rows ;
62 TYPED:: m4. ( a: matrix4 b: matrix4 -- c: matrix4 )
64 a rows :> a4 :> a3 :> a2 :> a1
65 b rows :> b4 :> b3 :> b2 :> b1
67 a1 first b1 n*v :> c1a
68 a2 first b1 n*v :> c2a
69 a3 first b1 n*v :> c3a
70 a4 first b1 n*v :> c4a
72 a1 second b2 n*v c1a v+ :> c1b
73 a2 second b2 n*v c2a v+ :> c2b
74 a3 second b2 n*v c3a v+ :> c3b
75 a4 second b2 n*v c4a v+ :> c4b
77 a1 third b3 n*v c1b v+ :> c1c
78 a2 third b3 n*v c2b v+ :> c2c
79 a3 third b3 n*v c3b v+ :> c3c
80 a4 third b3 n*v c4b v+ :> c4c
82 a1 fourth b4 n*v c1c v+
83 a2 fourth b4 n*v c2c v+
84 a3 fourth b4 n*v c3c v+
85 a4 fourth b4 n*v c4c v+
88 TYPED:: v.m4 ( a: float-4 b: matrix4 -- c: float-4 )
89 b rows :> b4 :> b3 :> b2 :> b1
96 TYPED:: m4.v ( a: matrix4 b: float-4 -- c: float-4 )
97 a rows [ b v. ] 4 napply float-4-boa ;
99 CONSTANT: identity-matrix4
102 float-4{ 1.0 0.0 0.0 0.0 }
103 float-4{ 0.0 1.0 0.0 0.0 }
104 float-4{ 0.0 0.0 1.0 0.0 }
105 float-4{ 0.0 0.0 0.0 1.0 }
109 CONSTANT: zero-matrix4
112 float-4{ 0.0 0.0 0.0 0.0 }
113 float-4{ 0.0 0.0 0.0 0.0 }
114 float-4{ 0.0 0.0 0.0 0.0 }
115 float-4{ 0.0 0.0 0.0 0.0 }
119 TYPED:: m4^n ( m: matrix4 n: fixnum -- m^n: matrix4 )
120 identity-matrix4 n [ m m4. ] times ;
122 : vmerge-diagonal* ( x y -- h t )
123 [ vmerge-head ] [ swap vmerge-tail ] 2bi ; inline
124 : vmerge-diagonal ( x -- h t )
125 0.0 float-4-with vmerge-diagonal* ;
127 TYPED: diagonal-matrix4 ( diagonal: float-4 -- matrix: matrix4 )
128 [ vmerge-diagonal [ vmerge-diagonal ] bi@ ] make-matrix4 ;
130 : vmerge-transpose ( a b c d -- a' b' c' d' )
131 [ vmerge ] bi-curry@ bi* ; inline
133 TYPED: transpose-matrix4 ( matrix: matrix4 -- matrix: matrix4 )
134 [ rows vmerge-transpose vmerge-transpose ] make-matrix4 ;
136 : scale-matrix4 ( factors -- matrix )
137 [ float-4{ t t t f } ] dip float-4{ 0.0 0.0 0.0 1.0 } v?
140 : ortho-matrix4 ( factors -- matrix )
141 float-4{ 1.0 1.0 1.0 1.0 } swap v/ scale-matrix4 ; inline
143 TYPED:: translation-matrix4 ( offset: float-4 -- matrix: matrix4 )
145 float-4{ 1.0 1.0 1.0 1.0 } :> diagonal
147 offset 0 float-4-with vmerge
148 [ 0 float-4-with swap vmerge ] bi@ drop :> z :> y :> x
150 diagonal y vmerge-diagonal*
151 [ x vmerge-diagonal* ]
152 [ z vmerge-diagonal* ] bi*
155 TYPED:: rotation-matrix4 ( axis: float-4 theta: float -- matrix: matrix4 )
156 ! x*x + c*(1.0 - x*x) x*y*(1.0 - c) - s*z x*z*(1.0 - c) + s*y 0
157 ! x*y*(1.0 - c) + s*z y*y + c*(1.0 - y*y) y*z*(1.0 - c) - s*x 0
158 ! x*z*(1.0 - c) - s*y y*z*(1.0 - c) + s*x z*z + c*(1.0 - z*z) 0
160 matrix4 (struct) :> triangle-m
164 float-4{ 1.0 -1.0 1.0 0.0 } :> triangle-sign
168 1.0 float-4-with :> ones
170 axis axis v* :> axis2
172 axis2 cc ones axis2 v- v* v+ :> diagonal
174 axis { 1 0 0 3 } vshuffle axis { 2 2 1 3 } vshuffle v* 1-c v*
175 float-4{ t t t f } vbitand :> triangle-a
176 ss axis v* triangle-sign v* :> triangle-b
177 triangle-a triangle-b v+ :> triangle-lo
178 triangle-a triangle-b v- :> triangle-hi
180 diagonal scale-matrix4 :> diagonal-m
182 triangle-hi { 3 2 1 3 } vshuffle
183 triangle-hi { 3 3 0 3 } vshuffle triangle-lo { 2 3 3 3 } vshuffle v+
184 triangle-lo { 1 0 3 3 } vshuffle
187 triangle-m set-rows drop
189 diagonal-m triangle-m m4+ ;
191 TYPED:: frustum-matrix4 ( xy: float-4 near: float far: float -- matrix: matrix4 )
192 matrix4 (struct) :> c
194 near near near far + 2 near far * * float-4-boa :> num
195 float-4{ t t f f } xy near far - float-4-with v? :> denom
198 fov { 0 0 0 0 } vshuffle float-4{ t f f f } vbitand
199 fov { 1 1 1 1 } vshuffle float-4{ f t f f } vbitand
200 fov { 2 2 2 3 } vshuffle float-4{ f f t t } vbitand
201 float-4{ 0.0 0.0 -1.0 0.0 }