absence_thelab

view src/common/n3dmath.h @ 0:1cffe3409164

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Oct 2014 01:46:07 +0300
parents
children
line source
1 #ifndef _N3DMATH_H_
2 #define _N3DMATH_H_
4 // - n3dmath.h -
5 // Nuc3D 2 Header File
6 // written by: John Tsiombikas (10 Sep 2002)
7 // Last Modification: 14 April 2002
8 // ---------------------------
9 // Mathematical stuff
11 #include <iostream>
12 #include <cmath>
14 #ifdef NUC3D_VER_DIRECT3D
15 #include "d3d8.h" // Direct3D type definitions (D3DMATRIX)
16 #endif //NUC3D_VER_DIRECT3D
18 // forward declarations
19 class Matrix4x4;
20 class Matrix3x3;
21 class Vector3;
22 class Vector2;
23 class Vector4;
24 class Quaternion;
26 // mathematical constants
27 const float Pi = 3.1415926535897932f;
28 const float TwoPi = 6.2831853071795865f;// Pi * 2.0f;
29 const float HalfPi = 1.5707963267948965f; //Pi * 0.5f;
30 const float QuarterPi = 0.7853981633974483f; //Pi * 0.25f;
31 const float ThreeQuartersPi = 2.3561944901923450f; //QuarterPi * 3.0f;
33 const float XSmallNumber = 1.e-8f;
34 const float SmallNumber = 1.e-4f;
36 // basis vectors
37 #define VECTOR3_I (Vector3(1.0f, 0.0f, 0.0f))
38 #define VECTOR3_J (Vector3(0.0f, 1.0f, 0.0f))
39 #define VECTOR3_K (Vector3(0.0f, 0.0f, 1.0f))
41 #define VECTOR2_I (Vector2(1.0f, 0.0f))
42 #define VECTOR2_J (Vector2(0.0f, 1.0f))
44 // angle conversion
45 #define DEGTORAD(a) (Pi * (a) / 180)
46 #define RADTODEG(a) ((a) * 180 / Pi)
50 // ------------- Vector3 class -------------
52 #ifdef NUC3D_VER_DIRECT3D // if we are using Direct3D version
54 class Vector3 : public D3DVECTOR {
55 public:
57 #else // not D3D
59 class Vector3 {
60 public:
61 float x, y, z;
63 #endif //NUC3D_VER_DIRECT3D
65 Vector3();
66 Vector3(float x, float y, float z);
68 inline float DotProduct(const Vector3 &vec) const;
69 inline Vector3 CrossProduct(const Vector3 &vec) const;
71 inline Vector3 operator +(const Vector3 &vec) const;
72 inline Vector3 operator -(const Vector3 &vec) const;
73 inline Vector3 operator *(float scalar) const;
74 inline Vector3 operator /(float scalar) const;
75 inline void operator +=(const Vector3 &vec);
76 inline void operator -=(const Vector3 &vec);
77 inline void operator *=(float scalar);
78 inline void operator /=(float scalar);
79 inline Vector3 operator -() const; // unary minus for inversion
81 inline bool operator >(const Vector3 &vec) const;
82 inline bool operator <(const Vector3 &vec) const;
83 inline bool operator >(float len) const;
84 inline bool operator <(float len) const;
85 inline bool operator ==(const Vector3 &vec) const;
86 inline bool operator ==(float len) const;
88 inline operator Vector2() const;
89 inline operator Vector4() const;
91 inline float Length() const;
92 inline float LengthSq() const;
94 inline void Normalize();
95 inline Vector3 Normalized() const;
97 inline Vector3 Reflection(const Vector3 &normal) const;
98 Vector3 Refraction(const Vector3 &normal, float FromIOR, float ToIOR) const;
100 void Transform(const Matrix4x4 &mat); // transform a vector using a transformation matrix
101 void Transform(const Quaternion &quat); // transform by a quaternion
103 // Direct transformations on the vector
104 void Translate(float x, float y, float z);
105 void Rotate(float x, float y, float z);
106 void Rotate(const Vector3 &axis, float angle);
107 void Scale(float x, float y, float z);
109 float &operator [](int index);
111 friend std::ostream &operator <<(std::ostream &out, const Vector3 &vec);
112 };
114 inline float DotProduct(const Vector3 &vec1, const Vector3 &vec2);
115 inline Vector3 CrossProduct(const Vector3 &vec1, const Vector3 &vec2);
117 ////////////////////// 4-dimensional vectors ////////////////////////////
118 class Vector4 {
119 public:
120 float x, y, z, w;
122 Vector4();
123 Vector4(const Vector4 &vec);
124 Vector4(const Vector3 &vec); // create from a 3 dimensional vector setting w=1
125 Vector4(float x, float y, float z, float w);
127 float DotProduct(const Vector4 &vec) const;
128 Vector4 CrossProduct(const Vector4 &vec1, const Vector4 &vec2) const;
130 Vector4 operator +(const Vector4 &vec) const;
131 Vector4 operator -(const Vector4 &vec) const;
132 Vector4 operator *(float scalar) const;
133 Vector4 operator /(float scalar) const;
134 void operator +=(const Vector4 &vec);
135 void operator -=(const Vector4 &vec);
136 void operator *=(float scalar);
137 void operator /=(float scalar);
138 Vector4 operator -() const; // unary minus for inversion
140 bool operator >(const Vector4 &vec) const;
141 bool operator <(const Vector4 &vec) const;
142 bool operator >(float len) const;
143 bool operator <(float len) const;
144 bool operator ==(const Vector4 &vec) const;
145 bool operator ==(float len) const;
147 operator Vector3() const;
149 float Length() const;
150 float LengthSq() const;
152 void Normalize();
153 Vector4 Normalized() const;
155 void Transform(const Matrix4x4 &mat); // transform a vector using a transformation matrix
157 // Direct transformations on the vector
158 void Translate(float x, float y, float z, float w);
159 void Rotate(float x, float y, float z);
160 void Rotate(const Vector3 &axis, float angle);
161 void Scale(float x, float y, float z, float w);
163 float &operator [](int index);
165 friend std::ostream &operator <<(std::ostream &out, const Vector3 &vec);
166 };
168 float DotProduct(const Vector4 &vec1, const Vector4 &vec2);
169 Vector4 CrossProduct(const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3);
171 ///////////////////////////
173 class Vector2 {
174 public:
175 float x, y;
177 Vector2();
178 Vector2(const Vector2 &vec);
179 Vector2(float x, float y);
181 float DotProduct(const Vector2 &vec) const;
182 //Vector2 CrossProduct(const Vector2 &vec) const;
184 Vector2 operator +(const Vector2 &vec) const;
185 Vector2 operator -(const Vector2 &vec) const;
186 Vector2 operator *(float scalar) const;
187 Vector2 operator /(float scalar) const;
188 void operator +=(const Vector2 &vec);
189 void operator -=(const Vector2 &vec);
190 void operator *=(float scalar);
191 void operator /=(float scalar);
192 Vector2 operator -() const; // unary minus for inversion
194 bool operator >(const Vector2 &vec) const;
195 bool operator <(const Vector2 &vec) const;
196 bool operator >(float len) const;
197 bool operator <(float len) const;
198 bool operator ==(const Vector2 &vec) const;
199 bool operator ==(float len) const;
201 operator Vector3() const;
203 float Length() const;
204 float LengthSq() const;
206 void Normalize();
207 Vector2 Normalized() const;
209 Vector2 Reflection(const Vector2 &normal) const;
210 Vector2 Refraction(const Vector2 &normal, float FromIOR, float ToIOR) const;
212 void Transform(const Matrix3x3 &mat); // transform a vector using a transformation matrix
214 // Direct transformations on the vector
215 void Translate(float x, float y);
216 void Rotate(float angle);
217 void Scale(float x, float y);
219 float &operator [](int index);
221 friend std::ostream &operator <<(std::ostream &out, const Vector2 &vec);
222 };
224 float DotProduct(const Vector3 &vec1, const Vector3 &vec2);
227 struct Vector2i {
228 int x, y;
230 Vector2i(int x=0, int y=0) {this->x = x; this->y = y;}
231 };
234 ////////////////// Quaternion ///////////////////////
236 class Quaternion {
237 public:
238 float s;
239 Vector3 v;
241 Quaternion();
242 Quaternion(float s, const Vector3 &v);
243 Quaternion(float s, float x, float y, float z);
245 Quaternion operator +(const Quaternion &quat) const;
246 Quaternion operator -(const Quaternion &quat) const;
247 Quaternion operator -() const;
248 Quaternion operator *(const Quaternion &quat) const;
250 void operator +=(const Quaternion &quat);
251 void operator -=(const Quaternion &quat);
252 void operator *=(const Quaternion &quat);
254 void ResetIdentity();
256 Quaternion Conjugate() const;
258 float Length() const;
259 float LengthSq() const;
261 void Normalize();
262 Quaternion Normalized() const;
264 Quaternion Inverse() const;
266 void SetRotation(const Vector3 &axis, float angle);
267 void Rotate(const Vector3 &axis, float angle);
269 Matrix3x3 GetRotationMatrix() const;
270 };
274 ////////////////////////// Matrices //////////////////////////////
276 #ifdef NUC3D_VER_DIRECT3D // if we are using Direct3D version
278 class Matrix4x4 : public D3DMATRIX {
279 public:
281 #else // Software or OpenGL version
283 class Matrix4x4 {
284 public:
285 float m[4][4];
287 #endif //NUC3D_VER_DIRECT3D
289 Matrix4x4();
290 Matrix4x4(const Matrix4x4 &mat);
291 Matrix4x4(const Matrix3x3 &mat);
292 Matrix4x4( float m00, float m01, float m02, float m03,
293 float m10, float m11, float m12, float m13,
294 float m20, float m21, float m22, float m23,
295 float m30, float m31, float m32, float m33 );
297 // basic operations on matrices
298 Matrix4x4 operator +(const Matrix4x4 &mat) const;
299 Matrix4x4 operator -(const Matrix4x4 &mat) const;
300 Matrix4x4 operator *(const Matrix4x4 &mat) const;
301 Matrix4x4 operator *(float scalar) const;
302 void operator +=(const Matrix4x4 &mat);
303 void operator -=(const Matrix4x4 &mat);
304 void operator *=(const Matrix4x4 &mat);
305 void operator *=(float scalar);
307 void ResetIdentity();
309 // concatenate various transformation matrices with our current matrix
310 void Translate(float x, float y, float z);
311 void Rotate(float x, float y, float z);
312 void Rotate(const Vector3 &axis, float rads);
313 void Scale(float x, float y, float z);
315 // set the matrix to a specific transformation matrix
316 void SetTranslation(float x, float y, float z);
317 void SetRotation(float x, float y, float z);
318 void SetRotation(const Vector3 &axis, float angle);
319 void SetScaling(float x, float y, float z);
321 // row and column accessors
322 void SetColumnVector(const Vector4 &vec, int columnindex);
323 void SetRowVector(const Vector4 &vec, int rowindex);
324 Vector4 GetColumnVector(int columnindex) const;
325 Vector4 GetRowVector(int rowindex) const;
327 // other operations on matrices
328 void Transpose();
329 Matrix4x4 Transposed() const;
331 float Determinant() const;
332 Matrix4x4 Adjoint() const;
333 Matrix4x4 Inverse() const;
334 };
337 ////////////////// Matrix3x3 //////////////////
338 class Matrix3x3 {
339 public:
340 float m[3][3];
342 Matrix3x3();
343 Matrix3x3(const Matrix3x3 &mat);
344 Matrix3x3( float m00, float m01, float m02,
345 float m10, float m11, float m12,
346 float m20, float m21, float m22 );
348 // basic operations on matrices
349 Matrix3x3 operator +(const Matrix3x3 &mat) const;
350 Matrix3x3 operator -(const Matrix3x3 &mat) const;
351 Matrix3x3 operator *(const Matrix3x3 &mat) const;
352 Matrix3x3 operator *(float scalar) const;
353 void operator +=(const Matrix3x3 &mat);
354 void operator -=(const Matrix3x3 &mat);
355 void operator *=(const Matrix3x3 &mat);
356 void operator *=(float scalar);
358 void ResetIdentity();
360 // concatenate various transformation matrices with our current matrix
361 void Translate(float x, float y);
362 void Rotate(float angle);
363 void Scale(float x, float y);
365 // set the matrix to a specific transformation matrix
366 void SetTranslation(float x, float y);
367 void SetRotation(float angle);
368 void SetScaling(float x, float y);
370 // row and column accessors
371 void SetColumnVector(const Vector3 &vec, int columnindex);
372 void SetRowVector(const Vector3 &vec, int rowindex);
373 Vector3 GetColumnVector(int columnindex) const;
374 Vector3 GetRowVector(int rowindex) const;
376 // other operations on matrices
377 void Transpose();
378 Matrix3x3 Transposed() const;
380 void OrthoNormalize();
381 Matrix3x3 OrthoNormalized();
383 //float Determinant() const; // NYI
384 //Matrix3x3 Adjoint() const;
385 //Matrix3x3 Inverse() const;
386 };
388 ///////////////// ray /////////////////
390 class Ray {
391 public:
392 Vector3 Origin;
393 Vector3 Direction;
394 float Energy;
395 float CurrentIOR;
397 Ray();
398 Ray(const Vector3 &origin, const Vector3 &direction);
399 };
401 // a coordinate system basis
402 class Base {
403 public:
404 Vector3 i, j, k;
406 Base();
407 Base(const Vector3 &i, const Vector3 &j, const Vector3 &k);
408 Base(const Vector3 &dir, bool LeftHanded=true);
410 void Rotate(float x, float y, float z);
411 void Rotate(const Vector3 &axis, float angle);
412 void Rotate(const Matrix4x4 &mat);
413 void Rotate(const Quaternion &quat);
415 Matrix3x3 CreateRotationMatrix() const;
416 };
420 float frand(float range);
423 #include "n3dmath.inl"
425 #endif // _N3DMATH_H_