1 //////////////////////////////////////////////////////////////////////
2 //
3 // Pixie
4 //
5 // Copyright � 1999 - 2003, Okan Arikan
6 //
7 // Contact: okan@cs.utexas.edu
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 ///////////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////////
25 //
26 // File : algebra.h
27 // Classes : -
28 // Description : Various linear algebra routines
29 //
30 ////////////////////////////////////////////////////////////////////////
31 #ifndef ALGEBRA_H
32 #define ALGEBRA_H
33
34 #include <math.h>
35
36 #include "global.h"
37 #include "align.h"
38
39
40 ////////////////////////////////////////////////////////////////////////////
41 //
42 //
43 //
44 // Matrix - vector types
45 //
46 //
47 //
48 ////////////////////////////////////////////////////////////////////////////
49 typedef float vector[3]; // an array of 3 floats
50 typedef float quaternion[4]; // an array of 4 floats
51 typedef float htpoint[4]; // an array of 4 floats
52 typedef float matrix[16]; // an array of 16 floats
53
54 typedef double dvector[3]; // an array of 3 doubles
55 typedef double dquaternion[4]; // an array of 4 doubles
56 typedef double dhtpoint[4]; // an array of 4 floats
57 typedef double dmatrix[16]; // an array of 16 doubles
58
59 // Row major matrix element order (compatible with openGL)
60 #define element(row,column) (row+(column<<2))
61
62 extern const matrix identityMatrix; // Points to the identity matrix
63
64 #define COMP_X 0
65 #define COMP_Y 1
66 #define COMP_Z 2
67
68 #define COMP_R 0
69 #define COMP_G 1
70 #define COMP_B 2
71
72
73 ////////////////////////////////////////////////////////////////////////////
74 //
75 //
76 //
77 // Faster versions of some math functions
78 //
79 //
80 //
81 ////////////////////////////////////////////////////////////////////////////
82
83
84 ////////////////////////////////////////////////////////////////////////////
85 // Fast inverse square root
isqrtf(float number)86 inline float isqrtf(float number) {
87 uint32_t i;
88 float x2, y;
89 const float threehalfs = 1.5F;
90 union { float f; uint32_t i; } u;
91
92
93 x2 = number * 0.5F;
94 y = number;
95 u.f = number;
96 i = u.i;
97 i = 0x5f3759df - ( i >> 1 );
98 u.i = i;
99 y = u.f;
100 y = y * ( threehalfs - ( x2 * y * y ) );
101 // y = y * ( threehalfs - ( x2 * y * y ) ); // Da second iteration
102
103 return y;
104 }
105
106 ////////////////////////////////////////////////////////////////////////////
107 // Fast floating point absolute value
absf(float f)108 inline float absf(float f) {
109 union { float f; uint32_t i; } u;
110 u.f = f;
111 u.i = u.i & 0x7FFFFFFF;
112 return u.f;
113 }
114
115
116
117
118
119
120
121
122 ////////////////////////////////////////////////////////////////////////////
123 //
124 //
125 //
126 // Low precision routines are not available on all platforms
127 //
128 // FIXME: check the avalibility of sqrtf etc thru configure.in
129 //
130 ////////////////////////////////////////////////////////////////////////////
131 #ifndef HAVE_SQRTF
132 #define sqrtf (float) sqrt
133 #endif
134
135 #ifndef HAVE_COSF
136 #define cosf (float) cos
137 #endif
138
139 #ifndef HAVE_SINF
140 #define sinf (float) sin
141 #endif
142
143 #ifndef HAVE_TANF
144 #define tanf (float) tan
145 #endif
146
147 #ifndef HAVE_ATAN2F
148 #define atan2f (float) atan2
149 #endif
150
151 #ifndef HAVE_POWF
152 #define powf (float) pow
153 #endif
154
155 #ifndef HAVE_LOGF
156 #define logf (float) log
157 #endif
158
159 #ifndef HAVE_FMODF
160 #define fmodf (float) fmod
161 #endif
162
163
164
165
166
167
168
169
170
171
172
173
174
175 ////////////////////////////////////////////////////////////////////////////
176 //
177 //
178 //
179 // Define some misc vector/matrix functions
180 //
181 //
182 //
183 ////////////////////////////////////////////////////////////////////////////
184 #define SCALAR_TYPE float
185 #define VECTOR_TYPE vector
186 #define MATRIX_TYPE matrix
187 #define SQRT sqrtf
188 #include "mathSpec.h"
189 #undef SCALAR_TYPE
190 #undef VECTOR_TYPE
191 #undef MATRIX_TYPE
192 #undef SQRT
193
194
195 #define SCALAR_TYPE double
196 #define VECTOR_TYPE dvector
197 #define MATRIX_TYPE dmatrix
198 #define SQRT sqrt
199 #include "mathSpec.h"
200 #undef SCALAR_TYPE
201 #undef VECTOR_TYPE
202 #undef MATRIX_TYPE
203 #undef SQRT
204
205
206
normalizevf(float * r,const float * v)207 inline void normalizevf(float *r,const float *v) {
208 const float l = isqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
209
210 r[0] = (float) (v[0]*l);
211 r[1] = (float) (v[1]*l);
212 r[2] = (float) (v[2]*l);
213 }
214
215
normalizevf(float * v)216 inline void normalizevf(float *v) {
217 const float l = isqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
218
219 v[0] = (float) (v[0]*l);
220 v[1] = (float) (v[1]*l);
221 v[2] = (float) (v[2]*l);
222 }
223
224
225 #endif
226
227