1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 
11 
12 
13 #include <math.h>
14 
15 /*
16  * NB - not meant to be compiled directly
17  * #define Type, c1,..., Neg3,... and then #include this file
18  */
19 
20 #if INLINE
21 #    define STATIC static
22 #    if paxc
23 /*#        pragma inline (Neg,Add,Sub,Mul,Div,Min,Max,Lerp)*/
24 /*#        pragma inline (Dot,Cross,Length,Normalize)*/
25 /*#        pragma inline (Apply,Transpose,Invert,AdjointTranspose)*/
26 #    endif
27 #else
28 #    define STATIC
29 #endif
30 
31 #ifdef Neg3
Neg3(Point3 v)32 STATIC Point3 Neg3(Point3 v)
33 {
34     v.c1 = -v.c1;
35     v.c2 = -v.c2;
36     v.c3 = -v.c3;
37     return v;
38 }
39 #endif
40 
41 #ifdef Add3
Add3(Point3 v,Point3 w)42 STATIC Point3 Add3(Point3 v, Point3 w)
43 {
44     v.c1 += w.c1;
45     v.c2 += w.c2;
46     v.c3 += w.c3;
47     return v;
48 }
49 #endif
50 
51 #ifdef Sub3
Sub3(Point3 v,Point3 w)52 STATIC Point3 Sub3(Point3 v, Point3 w)
53 {
54     v.c1 -= w.c1;
55     v.c2 -= w.c2;
56     v.c3 -= w.c3;
57     return v;
58 }
59 #endif
60 
61 #ifdef Mul3
Mul3(Point3 v,double f)62 STATIC Point3 Mul3(Point3 v, double f)
63 {
64     v.c1 *= f;
65     v.c2 *= f;
66     v.c3 *= f;
67     return v;
68 }
69 #endif
70 
71 #ifdef Div3
Div3(Point3 v,double f)72 STATIC Point3 Div3(Point3 v, double f)
73 {
74     v.c1 /= f;
75     v.c2 /= f;
76     v.c3 /= f;
77     return v;
78 }
79 #endif
80 
81 #ifdef Min3
Min3(Point3 v,Point3 w)82 STATIC Point3 Min3(Point3 v, Point3 w)
83 {
84     v.c1 = v.c1<w.c1? v.c1 : w.c1;
85     v.c2 = v.c2<w.c2? v.c2 : w.c2;
86     v.c3 = v.c3<w.c3? v.c3 : w.c3;
87     return v;
88 }
89 #endif
90 
91 #ifdef Max3
Max3(Point3 v,Point3 w)92 STATIC Point3 Max3(Point3 v, Point3 w)
93 {
94     v.c1 = v.c1>w.c1? v.c1 : w.c1;
95     v.c2 = v.c2>w.c2? v.c2 : w.c2;
96     v.c3 = v.c3>w.c3? v.c3 : w.c3;
97     return v;
98 }
99 #endif
100 
101 #ifdef Lerp3
Lerp3(double t,Point3 a,Point3 b)102 STATIC Point3 Lerp3(double t, Point3 a, Point3 b)
103 {
104     float s = 1-t;
105     a.c1 = a.c1*t + b.c1*s;
106     a.c2 = a.c2*t + b.c2*s;
107     a.c3 = a.c3*t + b.c3*s;
108     return a;
109 }
110 #endif
111 
112 #ifdef Dot3
Dot3(Point3 v,Point3 w)113 STATIC float Dot3(Point3 v, Point3 w)
114 {
115     return v.x*w.x + v.y*w.y + v.z*w.z;
116 }
117 #endif
118 
119 #ifdef Cross3
Cross3(Point3 v,Point3 w)120 STATIC Point3 Cross3(Point3 v, Point3 w)
121 {
122     Point3 r;
123     r.c1 = v.c2*w.c3 - v.c3*w.c2;
124     r.c2 = v.c3*w.c1 - v.c1*w.c3;
125     r.c3 = v.c1*w.c2 - v.c2*w.c1;
126     return r;
127 }
128 #endif
129 
130 #ifdef Length3
Length3(Point3 v)131 STATIC double Length3(Point3 v)
132 {
133     double v1 = v.c1;
134     double v2 = v.c2;
135     double v3 = v.c3;
136     return sqrt(v1*v1 + v2*v2 + v3*v3);
137 }
138 #endif
139 
140 #ifdef Normalize3
Normalize3(Point3 v)141 STATIC Point3 Normalize3(Point3 v)
142 {
143     float d = Length3(v);
144     if (d) {
145 	v.c1 /= d;
146 	v.c2 /= d;
147 	v.c3 /= d;
148     }
149     return v;
150 }
151 #endif
152 
153 #ifdef Apply3
Apply3(Point3 p,Matrix3 t)154 STATIC Point3 Apply3(Point3 p, Matrix3 t)
155 {
156     Point3 q;
157     q.c1 = t.A[0][0]*p.c1 + t.A[1][0]*p.c2 + t.A[2][0]*p.c3 + t.b[0];
158     q.c2 = t.A[0][1]*p.c1 + t.A[1][1]*p.c2 + t.A[2][1]*p.c3 + t.b[1];
159     q.c3 = t.A[0][2]*p.c1 + t.A[1][2]*p.c2 + t.A[2][2]*p.c3 + t.b[2];
160     return q;
161 }
162 #endif
163 
164 #ifdef Transpose3
Transpose3(Matrix3 t)165 STATIC Matrix3 Transpose3(Matrix3 t)
166 {
167     Matrix3 s;
168     s.A[0][0] = t.A[0][0];  s.A[0][1] = t.A[1][0];  s.A[0][2] = t.A[2][0];
169     s.A[1][0] = t.A[0][1];  s.A[1][1] = t.A[1][1];  s.A[1][2] = t.A[2][1];
170     s.A[2][0] = t.A[0][2];  s.A[2][1] = t.A[1][2];  s.A[2][2] = t.A[2][2];
171     s.b[0] = s.b[1] = s.b[2] = 0;  /* XXX - ??? */
172     return s;
173 }
174 #endif
175 
176 #define COF(a,b,c,d) (t.A[a][c] * t.A[b][d] - t.A[a][d] * t.A[b][c])
177 
178 #ifdef Invert3
Invert3(Matrix3 t)179 STATIC Matrix3 Invert3(Matrix3 t)
180 {
181     Point3 p;
182     Matrix3 s;
183     double inv;
184 
185     inv = t.A[0][0]*COF(1,2,1,2)+t.A[0][1]*COF(1,2,2,0)+t.A[0][2]*COF(1,2,0,1);
186     inv = 1.0 / inv;
187 
188     s.A[0][0] = COF(1,2,1,2) * inv;
189     s.A[1][0] = COF(1,2,2,0) * inv;
190     s.A[2][0] = COF(1,2,0,1) * inv;
191     s.A[0][1] = COF(2,0,1,2) * inv;
192     s.A[1][1] = COF(2,0,2,0) * inv;
193     s.A[2][1] = COF(2,0,0,1) * inv;
194     s.A[0][2] = COF(0,1,1,2) * inv;
195     s.A[1][2] = COF(0,1,2,0) * inv;
196     s.A[2][2] = COF(0,1,0,1) * inv;
197 
198     s.b[0] = s.b[1] = s.b[2] = 0;
199     p.c1 = -t.b[0]; p.c2 = -t.b[1]; p.c3 = -t.b[2];
200     p  = Apply3(p, s);
201     s.b[0] = p.c1; s.b[1] = p.c2; s.b[2] = p.c3;
202 
203     return s;
204 }
205 #endif
206 
207 #ifdef Determinant3
Determinant3(Matrix3 t)208 STATIC float Determinant3(Matrix3 t)
209 {
210     return
211 	t.A[0][0]*COF(1,2,1,2)+t.A[0][1]*COF(1,2,2,0)+t.A[0][2]*COF(1,2,0,1);
212 }
213 #endif
214 
215 #ifdef AdjointTranspose3
AdjointTranspose3(Matrix3 t)216 STATIC Matrix3 AdjointTranspose3(Matrix3 t)
217 {
218     Matrix3 s;
219 
220     s.A[0][0] = COF(1,2,1,2);
221     s.A[0][1] = COF(1,2,2,0);
222     s.A[0][2] = COF(1,2,0,1);
223     s.A[1][0] = COF(2,0,1,2);
224     s.A[1][1] = COF(2,0,2,0);
225     s.A[1][2] = COF(2,0,0,1);
226     s.A[2][0] = COF(0,1,1,2);
227     s.A[2][1] = COF(0,1,2,0);
228     s.A[2][2] = COF(0,1,0,1);
229 
230     /* XXX - ??? - NB check users if you change this */
231     s.b[0] = s.b[1] = s.b[2] = 0;
232 
233     return s;
234 }
235 #endif
236 
237 #undef STATIC
238