]> gitweb.factorcode.org Git - factor.git/blob - extra/math/matrices/simd/simd.factor
Merge branch 'bags' of git://github.com/littledan/Factor
[factor.git] / extra / math / matrices / simd / simd.factor
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 math.quaternions sequences sequences.private specialized-arrays
5 typed ;
6 FROM: sequences.private => nth-unsafe ;
7 FROM: math.quaternions.private => (q*sign) ;
8 QUALIFIED-WITH: alien.c-types c
9 SPECIALIZED-ARRAY: float-4
10 IN: math.matrices.simd
11
12 STRUCT: matrix4
13     { columns float-4[4] } ;
14
15 INSTANCE: matrix4 immutable-sequence
16
17 M: matrix4 length drop 4 ; inline
18 M: matrix4 nth-unsafe columns>> nth-unsafe ; inline
19 M: matrix4 new-sequence 2drop matrix4 (struct) ; inline
20
21 <PRIVATE
22
23 : columns ( a -- a1 a2 a3 a4 )
24     columns>> first4 ; inline
25
26 :: set-columns ( c1 c2 c3 c4 c -- c )
27     c columns>> :> columns
28     c1 c2 c3 c4 columns 4 set-firstn-unsafe
29     c ; inline
30
31 : make-matrix4 ( ..a quot: ( ..a -- ..b c1 c2 c3 c4 ) -- ..b c )
32     matrix4 (struct) swap dip set-columns ; inline
33
34 :: 2map-columns ( a b quot -- c )
35     [
36         a columns :> ( a1 a2 a3 a4 )
37         b columns :> ( b1 b2 b3 b4 )
38
39         a1 b1 quot call
40         a2 b2 quot call
41         a3 b3 quot call
42         a4 b4 quot call
43     ] make-matrix4 ; inline
44
45 : map-columns ( ... a quot: ( ... col -- ... newcol ) -- ... c )
46     '[ columns _ 4 napply ] make-matrix4 ; inline
47     
48 PRIVATE>
49
50 TYPED: m4+ ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v+ ] 2map-columns ;
51 TYPED: m4- ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v- ] 2map-columns ;
52 TYPED: m4* ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v* ] 2map-columns ;
53 TYPED: m4/ ( a: matrix4 b: matrix4 -- c: matrix4 ) [ v/ ] 2map-columns ;
54
55 TYPED: m4*n ( a: matrix4 b: float -- c: matrix4 ) [ v*n ] curry map-columns ;
56 TYPED: m4/n ( a: matrix4 b: float -- c: matrix4 ) [ v/n ] curry map-columns ;
57 TYPED: n*m4 ( a: float b: matrix4 -- c: matrix4 ) [ n*v ] with map-columns ;
58 TYPED: n/m4 ( a: float b: matrix4 -- c: matrix4 ) [ n/v ] with map-columns ;
59
60 TYPED:: m4. ( a: matrix4 b: matrix4 -- c: matrix4 )
61     [
62         a columns :> ( a1 a2 a3 a4 )
63         b columns :> ( b1 b2 b3 b4 )
64
65         b1 first  a1 n*v :> c1a
66         b2 first  a1 n*v :> c2a
67         b3 first  a1 n*v :> c3a
68         b4 first  a1 n*v :> c4a
69
70         b1 second a2 n*v c1a v+ :> c1b 
71         b2 second a2 n*v c2a v+ :> c2b
72         b3 second a2 n*v c3a v+ :> c3b
73         b4 second a2 n*v c4a v+ :> c4b
74
75         b1 third  a3 n*v c1b v+ :> c1c 
76         b2 third  a3 n*v c2b v+ :> c2c
77         b3 third  a3 n*v c3b v+ :> c3c
78         b4 third  a3 n*v c4b v+ :> c4c
79
80         b1 fourth a4 n*v c1c v+
81         b2 fourth a4 n*v c2c v+
82         b3 fourth a4 n*v c3c v+
83         b4 fourth a4 n*v c4c v+
84     ] make-matrix4 ;
85
86 TYPED:: m4.v ( m: matrix4 v: float-4 -- v': float-4 )
87     m columns :> ( m1 m2 m3 m4 )
88     
89     v first  m1 n*v
90     v second m2 n*v v+
91     v third  m3 n*v v+
92     v fourth m4 n*v v+ ;
93
94 TYPED:: v.m4 ( v: float-4 m: matrix4 -- c: float-4 )
95     m columns [ v v. ] 4 napply float-4-boa ;
96
97 CONSTANT: identity-matrix4
98     S{ matrix4 f
99         float-4-array{
100             float-4{ 1.0 0.0 0.0 0.0 }
101             float-4{ 0.0 1.0 0.0 0.0 }
102             float-4{ 0.0 0.0 1.0 0.0 }
103             float-4{ 0.0 0.0 0.0 1.0 }
104         }
105     }
106
107 CONSTANT: zero-matrix4
108     S{ matrix4 f
109         float-4-array{
110             float-4{ 0.0 0.0 0.0 0.0 }
111             float-4{ 0.0 0.0 0.0 0.0 }
112             float-4{ 0.0 0.0 0.0 0.0 }
113             float-4{ 0.0 0.0 0.0 0.0 }
114         }
115     }
116
117 TYPED:: m4^n ( m: matrix4 n: fixnum -- m^n: matrix4 )
118     identity-matrix4 n [ m m4. ] times ;
119
120 : vmerge-diagonal* ( x y -- h t )
121     [ (vmerge-head) ] [ swap (vmerge-tail) ] 2bi ; inline
122 : vmerge-diagonal ( x -- h t )
123     0.0 float-4-with vmerge-diagonal* ; inline
124
125 TYPED: diagonal-matrix4 ( diagonal: float-4 -- matrix: matrix4 )
126     [ vmerge-diagonal [ vmerge-diagonal ] bi@ ] make-matrix4 ;
127
128 : vmerge-transpose ( a b c d -- a' b' c' d' )
129     [ (vmerge) ] bi-curry@ bi* ; inline
130
131 TYPED: transpose-matrix4 ( matrix: matrix4 -- matrix: matrix4 )
132     [ columns vmerge-transpose vmerge-transpose ] make-matrix4 ;
133
134 : linear>homogeneous ( v -- v' )
135     [ float-4{ t t t f } ] dip float-4{ 0.0 0.0 0.0 1.0 } v? ; inline
136
137 : scale-matrix4 ( factors -- matrix )
138     linear>homogeneous diagonal-matrix4 ; inline
139
140 : ortho-matrix4 ( factors -- matrix )
141     float-4{ 1.0 1.0 1.0 1.0 } swap v/ scale-matrix4 ; inline
142
143 TYPED: translation-matrix4 ( offset: float-4 -- matrix: matrix4 )
144     [
145         linear>homogeneous
146         [ 
147             float-4{ 1.0 0.0 0.0 0.0 }
148             float-4{ 0.0 1.0 0.0 0.0 }
149             float-4{ 0.0 0.0 1.0 0.0 }
150         ] dip
151     ] make-matrix4 ;
152
153 :: (rotation-matrix4) ( diagonal triangle-hi triangle-lo -- matrix )
154     matrix4 (struct) :> triangle-m
155     diagonal scale-matrix4 :> diagonal-m
156
157     triangle-hi { 3 2 1 3 } vshuffle
158     triangle-hi { 3 3 0 3 } vshuffle triangle-lo { 2 3 3 3 } vshuffle vbitor
159                                      triangle-lo { 1 0 3 3 } vshuffle
160     float-4 new
161
162     triangle-m set-columns drop
163
164     diagonal-m triangle-m m4+ ; inline
165
166 TYPED:: rotation-matrix4 ( axis: float-4 theta: float -- matrix: matrix4 )
167     !   x*x + c*(1.0 - x*x)   x*y*(1.0 - c) + s*z   x*z*(1.0 - c) - s*y   0
168     !   x*y*(1.0 - c) - s*z   y*y + c*(1.0 - y*y)   y*z*(1.0 - c) + s*x   0
169     !   x*z*(1.0 - c) + s*y   y*z*(1.0 - c) - s*x   z*z + c*(1.0 - z*z)   0
170     !   0                     0                     0                     1
171     theta cos :> c
172     theta sin :> s
173
174     float-4{ -1.0  1.0 -1.0 0.0 } :> triangle-sign
175
176     c float-4-with :> cc
177     s float-4-with :> ss
178     1.0 float-4-with :> ones
179     ones cc v- :> 1-c
180     axis axis v* :> axis2
181
182     axis2 cc ones axis2 v- v* v+ :> diagonal
183
184     axis { 1 0 0 3 } vshuffle axis { 2 2 1 3 } vshuffle v* 1-c v*
185     float-4{ t t t f } vbitand :> triangle-a
186     ss axis v* triangle-sign v* :> triangle-b
187     triangle-a triangle-b v+ :> triangle-lo
188     triangle-a triangle-b v- :> triangle-hi
189
190     diagonal triangle-hi triangle-lo (rotation-matrix4) ;
191     
192 TYPED:: frustum-matrix4 ( xy: float-4 near: float far: float -- matrix: matrix4 )
193     [
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
196         v/ :> fov
197         
198         float-4{ 0.0 -1.0 0.0 0.0 } :> negone
199
200         fov vmerge-diagonal
201         [ vmerge-diagonal ]
202         [ negone (vmerge) ] bi*
203     ] make-matrix4 ;
204
205 ! interface with quaternions
206 M: float-4 (q*sign)
207     float-4{ -0.0  0.0  0.0  0.0 } vbitxor ; inline
208 M: float-4 qconjugate
209     float-4{  0.0 -0.0 -0.0 -0.0 } vbitxor ; inline
210
211 : euler4 ( phi theta psi -- q )
212     float-4{ 0 0 0 0 } euler-like ; inline
213
214 TYPED:: q>matrix4 ( q: float-4 -- matrix: matrix4 )
215     !   a*a + b*b - c*c - d*d  2*b*c - 2*a*d          2*b*d + 2*a*c          0
216     !   2*b*c + 2*a*d          a*a - b*b + c*c - d*d  2*c*d - 2*a*b          0
217     !   2*b*d - 2*a*c          2*c*d + 2*a*b          a*a - b*b - c*c + d*d  0
218     !   0                      0                      0                      1
219     q { 2 1 1 3 } vshuffle  q { 3 3 2 3 } vshuffle  v*  :> triangle-a
220     q { 0 0 0 3 } vshuffle  q { 1 2 3 3 } vshuffle  v*  :> triangle-b
221
222     triangle-a float-4{ 2.0 2.0 2.0 0.0 } v*  triangle-b float-4{ -2.0 2.0 -2.0 0.0 } v*
223     [ v- ] [ v+ ] 2bi :> ( triangle-hi triangle-lo )
224
225     q q v* first4 {
226         [ [ + ] [ - ] [ - ] tri* ]
227         [ [ - ] [ + ] [ - ] tri* ]
228         [ [ - ] [ - ] [ + ] tri* ]
229     } 4 ncleave 1.0 float-4-boa :> diagonal
230
231     diagonal triangle-hi triangle-lo (rotation-matrix4) ;