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