1 | /****************************************************************
|
---|
2 | * *
|
---|
3 | * C++ Vector and Matrix Algebra routines *
|
---|
4 | * Author: Jean-Francois DOUE *
|
---|
5 | * Version 3.1 --- October 1993 *
|
---|
6 | * *
|
---|
7 | ****************************************************************/
|
---|
8 |
|
---|
9 | #ifndef _util_misc_algebra_h
|
---|
10 | #define _util_misc_algebra_h
|
---|
11 |
|
---|
12 | #include <iostream>
|
---|
13 | #include <stdlib.h>
|
---|
14 |
|
---|
15 | namespace sc {
|
---|
16 |
|
---|
17 | // this line defines a new type: pointer to a function which returns a
|
---|
18 | // double and takes as argument a double
|
---|
19 | typedef double (*V_FCT_PTR)(double);
|
---|
20 |
|
---|
21 | class vec2;
|
---|
22 | class vec3;
|
---|
23 | class vec4;
|
---|
24 | class mat3;
|
---|
25 | class mat4;
|
---|
26 |
|
---|
27 | enum {VX, VY, VZ, VW}; // axes
|
---|
28 | enum {PA, PB, PC, PD}; // planes
|
---|
29 | enum {RED, GREEN, BLUE}; // colors
|
---|
30 |
|
---|
31 | /****************************************************************
|
---|
32 | * *
|
---|
33 | * 2D Vector *
|
---|
34 | * *
|
---|
35 | ****************************************************************/
|
---|
36 |
|
---|
37 | class vec2
|
---|
38 | {
|
---|
39 | protected:
|
---|
40 |
|
---|
41 | double n[2];
|
---|
42 |
|
---|
43 | public:
|
---|
44 |
|
---|
45 | // Constructors
|
---|
46 |
|
---|
47 | vec2();
|
---|
48 | vec2(const double x, const double y);
|
---|
49 | vec2(const double d);
|
---|
50 | vec2(const vec2& v); // copy constructor
|
---|
51 | vec2(const vec3& v); // cast v3 to v2
|
---|
52 | vec2(const vec3& v, int dropAxis); // cast v3 to v2
|
---|
53 |
|
---|
54 | // Assignment operators
|
---|
55 |
|
---|
56 | vec2& operator = ( const vec2& v ); // assignment of a vec2
|
---|
57 | vec2& operator += ( const vec2& v ); // incrementation by a vec2
|
---|
58 | vec2& operator -= ( const vec2& v ); // decrementation by a vec2
|
---|
59 | vec2& operator *= ( const double d ); // multiplication by a constant
|
---|
60 | vec2& operator /= ( const double d ); // division by a constant
|
---|
61 | double& operator [] ( int i); // indexing
|
---|
62 | const double& operator[](int i) const; // indexing
|
---|
63 |
|
---|
64 | // special functions
|
---|
65 |
|
---|
66 | double length(); // length of a vec2
|
---|
67 | double length2(); // squared length of a vec2
|
---|
68 | vec2& normalize(); // normalize a vec2
|
---|
69 | vec2& apply(V_FCT_PTR fct); // apply a func. to each component
|
---|
70 |
|
---|
71 | // friends
|
---|
72 |
|
---|
73 | friend vec2 operator - (const vec2& v); // -v1
|
---|
74 | friend vec2 operator + (const vec2& a, const vec2& b); // v1 + v2
|
---|
75 | friend vec2 operator - (const vec2& a, const vec2& b); // v1 - v2
|
---|
76 | friend vec2 operator * (const vec2& a, const double d); // v1 * 3.0
|
---|
77 | friend vec2 operator * (const double d, const vec2& a); // 3.0 * v1
|
---|
78 | friend vec2 operator * (const mat3& a, const vec2& v); // M . v
|
---|
79 | friend vec2 operator * (const vec2& v, mat3& a); // v . M
|
---|
80 | friend double operator * (const vec2& a, const vec2& b); // dot product
|
---|
81 | friend vec2 operator / (const vec2& a, const double d); // v1 / 3.0
|
---|
82 | friend vec3 operator ^ (const vec2& a, const vec2& b); // cross product
|
---|
83 | friend int operator == (const vec2& a, const vec2& b); // v1 == v2 ?
|
---|
84 | friend int operator != (const vec2& a, const vec2& b); // v1 != v2 ?
|
---|
85 | friend std::ostream& operator << (std::ostream& s, vec2& v);// output to stream
|
---|
86 | friend std::istream& operator >> (std::istream& s, vec2& v);// input from strm.
|
---|
87 | friend void swap(vec2& a, vec2& b); // swap v1 & v2
|
---|
88 | friend vec2 min(const vec2& a, const vec2& b); // min(v1, v2)
|
---|
89 | friend vec2 max(const vec2& a, const vec2& b); // max(v1, v2)
|
---|
90 | friend vec2 prod(const vec2& a, const vec2& b); // term by term *
|
---|
91 |
|
---|
92 | // necessary friend declarations
|
---|
93 |
|
---|
94 | friend class vec3;
|
---|
95 | };
|
---|
96 |
|
---|
97 | /****************************************************************
|
---|
98 | * *
|
---|
99 | * 3D Vector *
|
---|
100 | * *
|
---|
101 | ****************************************************************/
|
---|
102 |
|
---|
103 | class vec3
|
---|
104 | {
|
---|
105 | protected:
|
---|
106 |
|
---|
107 | double n[3];
|
---|
108 |
|
---|
109 | public:
|
---|
110 |
|
---|
111 | // Constructors
|
---|
112 |
|
---|
113 | vec3();
|
---|
114 | vec3(const double x, const double y, const double z);
|
---|
115 | vec3(const double d);
|
---|
116 | vec3(const vec3& v); // copy constructor
|
---|
117 | vec3(const vec2& v); // cast v2 to v3
|
---|
118 | vec3(const vec2& v, double d); // cast v2 to v3
|
---|
119 | vec3(const vec4& v); // cast v4 to v3
|
---|
120 | vec3(const vec4& v, int dropAxis); // cast v4 to v3
|
---|
121 |
|
---|
122 | // Assignment operators
|
---|
123 |
|
---|
124 | vec3& operator = ( const vec3& v ); // assignment of a vec3
|
---|
125 | vec3& operator += ( const vec3& v ); // incrementation by a vec3
|
---|
126 | vec3& operator -= ( const vec3& v ); // decrementation by a vec3
|
---|
127 | vec3& operator *= ( const double d ); // multiplication by a constant
|
---|
128 | vec3& operator /= ( const double d ); // division by a constant
|
---|
129 | double& operator [] ( int i); // indexing
|
---|
130 | const double& operator[](int i) const; // indexing
|
---|
131 |
|
---|
132 | // special functions
|
---|
133 |
|
---|
134 | double length(); // length of a vec3
|
---|
135 | double length2(); // squared length of a vec3
|
---|
136 | vec3& normalize(); // normalize a vec3
|
---|
137 | vec3& apply(V_FCT_PTR fct); // apply a func. to each component
|
---|
138 |
|
---|
139 | // friends
|
---|
140 |
|
---|
141 | friend vec3 operator - (const vec3& v); // -v1
|
---|
142 | friend vec3 operator + (const vec3& a, const vec3& b); // v1 + v2
|
---|
143 | friend vec3 operator - (const vec3& a, const vec3& b); // v1 - v2
|
---|
144 | friend vec3 operator * (const vec3& a, const double d); // v1 * 3.0
|
---|
145 | friend vec3 operator * (const double d, const vec3& a); // 3.0 * v1
|
---|
146 | friend vec3 operator * (const mat4& a, const vec3& v); // M . v
|
---|
147 | friend vec3 operator * (const vec3& v, const mat4& a); // v . M
|
---|
148 | friend double operator * (const vec3& a, const vec3& b); // dot product
|
---|
149 | friend vec3 operator / (const vec3& a, const double d); // v1 / 3.0
|
---|
150 | friend vec3 operator ^ (const vec3& a, const vec3& b); // cross product
|
---|
151 | friend int operator == (const vec3& a, const vec3& b); // v1 == v2 ?
|
---|
152 | friend int operator != (const vec3& a, const vec3& b); // v1 != v2 ?
|
---|
153 | friend std::ostream& operator << (std::ostream& s, vec3& v);// output to stream
|
---|
154 | friend std::istream& operator >> (std::istream& s, vec3& v);// input from strm.
|
---|
155 | friend void swap(vec3& a, vec3& b); // swap v1 & v2
|
---|
156 | friend vec3 min(const vec3& a, const vec3& b); // min(v1, v2)
|
---|
157 | friend vec3 max(const vec3& a, const vec3& b); // max(v1, v2)
|
---|
158 | friend vec3 prod(const vec3& a, const vec3& b); // term by term *
|
---|
159 |
|
---|
160 | // necessary friend declarations
|
---|
161 |
|
---|
162 | friend class vec2;
|
---|
163 | friend class vec4;
|
---|
164 | friend class mat3;
|
---|
165 | friend vec2 operator * (const mat3& a, const vec2& v); // linear transform
|
---|
166 | friend mat3 operator * (const mat3& a, const mat3& b); // matrix 3 product
|
---|
167 | };
|
---|
168 |
|
---|
169 | /****************************************************************
|
---|
170 | * *
|
---|
171 | * 4D Vector *
|
---|
172 | * *
|
---|
173 | ****************************************************************/
|
---|
174 |
|
---|
175 | class vec4
|
---|
176 | {
|
---|
177 | protected:
|
---|
178 |
|
---|
179 | double n[4];
|
---|
180 |
|
---|
181 | public:
|
---|
182 |
|
---|
183 | // Constructors
|
---|
184 |
|
---|
185 | vec4();
|
---|
186 | vec4(const double x, const double y, const double z, const double w);
|
---|
187 | vec4(const double d);
|
---|
188 | vec4(const vec4& v); // copy constructor
|
---|
189 | vec4(const vec3& v); // cast vec3 to vec4
|
---|
190 | vec4(const vec3& v, const double d); // cast vec3 to vec4
|
---|
191 |
|
---|
192 | // Assignment operators
|
---|
193 |
|
---|
194 | vec4& operator = ( const vec4& v ); // assignment of a vec4
|
---|
195 | vec4& operator += ( const vec4& v ); // incrementation by a vec4
|
---|
196 | vec4& operator -= ( const vec4& v ); // decrementation by a vec4
|
---|
197 | vec4& operator *= ( const double d ); // multiplication by a constant
|
---|
198 | vec4& operator /= ( const double d ); // division by a constant
|
---|
199 | double& operator [] ( int i); // indexing
|
---|
200 | const double& operator [] ( int i) const; // indexing
|
---|
201 |
|
---|
202 | // special functions
|
---|
203 |
|
---|
204 | double length(); // length of a vec4
|
---|
205 | double length2(); // squared length of a vec4
|
---|
206 | vec4& normalize(); // normalize a vec4
|
---|
207 | vec4& apply(V_FCT_PTR fct); // apply a func. to each component
|
---|
208 |
|
---|
209 | // friends
|
---|
210 |
|
---|
211 | friend vec4 operator - (const vec4& v); // -v1
|
---|
212 | friend vec4 operator + (const vec4& a, const vec4& b); // v1 + v2
|
---|
213 | friend vec4 operator - (const vec4& a, const vec4& b); // v1 - v2
|
---|
214 | friend vec4 operator * (const vec4& a, const double d); // v1 * 3.0
|
---|
215 | friend vec4 operator * (const double d, const vec4& a); // 3.0 * v1
|
---|
216 | friend vec4 operator * (const mat4& a, const vec4& v); // M . v
|
---|
217 | friend vec4 operator * (const vec4& v, const mat4& a); // v . M
|
---|
218 | friend double operator * (const vec4& a, const vec4& b); // dot product
|
---|
219 | friend vec4 operator / (const vec4& a, const double d); // v1 / 3.0
|
---|
220 | friend int operator == (const vec4& a, const vec4& b); // v1 == v2 ?
|
---|
221 | friend int operator != (const vec4& a, const vec4& b); // v1 != v2 ?
|
---|
222 | friend std::ostream& operator << (std::ostream& s, vec4& v);// output to stream
|
---|
223 | friend std::istream& operator >> (std::istream& s, vec4& v);// input from strm.
|
---|
224 | friend void swap(vec4& a, vec4& b); // swap v1 & v2
|
---|
225 | friend vec4 min(const vec4& a, const vec4& b); // min(v1, v2)
|
---|
226 | friend vec4 max(const vec4& a, const vec4& b); // max(v1, v2)
|
---|
227 | friend vec4 prod(const vec4& a, const vec4& b); // term by term *
|
---|
228 |
|
---|
229 | // necessary friend declarations
|
---|
230 |
|
---|
231 | friend class vec3;
|
---|
232 | friend class mat4;
|
---|
233 | friend vec3 operator * (const mat4& a, const vec3& v); // linear transform
|
---|
234 | friend mat4 operator * (const mat4& a, const mat4& b); // matrix 4 product
|
---|
235 | };
|
---|
236 |
|
---|
237 | /****************************************************************
|
---|
238 | * *
|
---|
239 | * 3x3 Matrix *
|
---|
240 | * *
|
---|
241 | ****************************************************************/
|
---|
242 |
|
---|
243 | class mat3
|
---|
244 | {
|
---|
245 | protected:
|
---|
246 |
|
---|
247 | vec3 v[3];
|
---|
248 |
|
---|
249 | public:
|
---|
250 |
|
---|
251 | // Constructors
|
---|
252 |
|
---|
253 | mat3();
|
---|
254 | mat3(const vec3& v0, const vec3& v1, const vec3& v2);
|
---|
255 | mat3(const double d);
|
---|
256 | mat3(const mat3& m);
|
---|
257 |
|
---|
258 | // Assignment operators
|
---|
259 |
|
---|
260 | mat3& operator = ( const mat3& m ); // assignment of a mat3
|
---|
261 | mat3& operator += ( const mat3& m ); // incrementation by a mat3
|
---|
262 | mat3& operator -= ( const mat3& m ); // decrementation by a mat3
|
---|
263 | mat3& operator *= ( const double d ); // multiplication by a constant
|
---|
264 | mat3& operator /= ( const double d ); // division by a constant
|
---|
265 | vec3& operator [] ( int i); // indexing
|
---|
266 | const vec3& operator [] ( int i) const; // indexing
|
---|
267 |
|
---|
268 | // special functions
|
---|
269 |
|
---|
270 | mat3 transpose() const; // transpose
|
---|
271 | mat3 inverse(); // inverse
|
---|
272 | mat3& apply(V_FCT_PTR fct); // apply a func. to each element
|
---|
273 |
|
---|
274 | // friends
|
---|
275 |
|
---|
276 | friend mat3 operator - (const mat3& a); // -m1
|
---|
277 | friend mat3 operator + (const mat3& a, const mat3& b); // m1 + m2
|
---|
278 | friend mat3 operator - (const mat3& a, const mat3& b); // m1 - m2
|
---|
279 | friend mat3 operator * (const mat3& a, const mat3& b); // m1 * m2
|
---|
280 | friend mat3 operator * (const mat3& a, const double d); // m1 * 3.0
|
---|
281 | friend mat3 operator * (const double d, const mat3& a); // 3.0 * m1
|
---|
282 | friend mat3 operator / (const mat3& a, const double d); // m1 / 3.0
|
---|
283 | friend int operator == (const mat3& a, const mat3& b); // m1 == m2 ?
|
---|
284 | friend int operator != (const mat3& a, const mat3& b); // m1 != m2 ?
|
---|
285 | friend std::ostream& operator << (std::ostream& s, mat3& m);// output to stream
|
---|
286 | friend std::istream& operator >> (std::istream& s, mat3& m);// input from strm.
|
---|
287 | friend void swap(mat3& a, mat3& b); // swap m1 & m2
|
---|
288 |
|
---|
289 | // necessary friend declarations
|
---|
290 |
|
---|
291 | friend vec3 operator * (const mat3& a, const vec3& v); // linear transform
|
---|
292 | friend vec2 operator * (const mat3& a, const vec2& v); // linear transform
|
---|
293 | };
|
---|
294 |
|
---|
295 | /****************************************************************
|
---|
296 | * *
|
---|
297 | * 4x4 Matrix *
|
---|
298 | * *
|
---|
299 | ****************************************************************/
|
---|
300 |
|
---|
301 | class mat4
|
---|
302 | {
|
---|
303 | protected:
|
---|
304 |
|
---|
305 | vec4 v[4];
|
---|
306 |
|
---|
307 | public:
|
---|
308 |
|
---|
309 | // Constructors
|
---|
310 |
|
---|
311 | mat4();
|
---|
312 | mat4(const vec4& v0, const vec4& v1, const vec4& v2, const vec4& v3);
|
---|
313 | mat4(const double d);
|
---|
314 | mat4(const mat4& m);
|
---|
315 |
|
---|
316 | // Assignment operators
|
---|
317 |
|
---|
318 | mat4& operator = ( const mat4& m ); // assignment of a mat4
|
---|
319 | mat4& operator += ( const mat4& m ); // incrementation by a mat4
|
---|
320 | mat4& operator -= ( const mat4& m ); // decrementation by a mat4
|
---|
321 | mat4& operator *= ( const double d ); // multiplication by a constant
|
---|
322 | mat4& operator /= ( const double d ); // division by a constant
|
---|
323 | vec4& operator [] ( int i); // indexing
|
---|
324 | const vec4& operator [] ( int i) const; // indexing
|
---|
325 |
|
---|
326 | // special functions
|
---|
327 |
|
---|
328 | mat4 transpose() const; // transpose
|
---|
329 | mat4 inverse(); // inverse
|
---|
330 | mat4& apply(V_FCT_PTR fct); // apply a func. to each element
|
---|
331 |
|
---|
332 | // friends
|
---|
333 |
|
---|
334 | friend mat4 operator - (const mat4& a); // -m1
|
---|
335 | friend mat4 operator + (const mat4& a, const mat4& b); // m1 + m2
|
---|
336 | friend mat4 operator - (const mat4& a, const mat4& b); // m1 - m2
|
---|
337 | friend mat4 operator * (const mat4& a, const mat4& b); // m1 * m2
|
---|
338 | friend mat4 operator * (const mat4& a, const double d); // m1 * 4.0
|
---|
339 | friend mat4 operator * (const double d, const mat4& a); // 4.0 * m1
|
---|
340 | friend mat4 operator / (const mat4& a, const double d); // m1 / 3.0
|
---|
341 | friend int operator == (const mat4& a, const mat4& b); // m1 == m2 ?
|
---|
342 | friend int operator != (const mat4& a, const mat4& b); // m1 != m2 ?
|
---|
343 | friend std::ostream& operator << (std::ostream& s, mat4& m);// output to stream
|
---|
344 | friend std::istream& operator >> (std::istream& s, mat4& m);// input from strm.
|
---|
345 | friend void swap(mat4& a, mat4& b); // swap m1 & m2
|
---|
346 |
|
---|
347 | // necessary friend declarations
|
---|
348 |
|
---|
349 | friend vec4 operator * (const mat4& a, const vec4& v); // linear transform
|
---|
350 | friend vec3 operator * (const mat4& a, const vec3& v); // linear transform
|
---|
351 | };
|
---|
352 |
|
---|
353 | /****************************************************************
|
---|
354 | * *
|
---|
355 | * 2D functions and 3D functions *
|
---|
356 | * *
|
---|
357 | ****************************************************************/
|
---|
358 |
|
---|
359 | mat3 identity2D(); // identity 2D
|
---|
360 | mat3 translation2D(const vec2& v); // translation 2D
|
---|
361 | mat3 rotation2D(const vec2& Center, const double angleDeg); // rotation 2D
|
---|
362 | mat3 scaling2D(const vec2& scaleVector); // scaling 2D
|
---|
363 | mat4 identity3D(); // identity 3D
|
---|
364 | mat4 translation3D(const vec3& v); // translation 3D
|
---|
365 | mat4 rotation3D(const vec3& Axis, const double angleDeg); // rotation 3D
|
---|
366 | mat4 scaling3D(const vec3& scaleVector); // scaling 3D
|
---|
367 | mat4 perspective3D(const double d); // perspective 3D
|
---|
368 |
|
---|
369 | }
|
---|
370 |
|
---|
371 | #endif
|
---|