absence_thelab

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/common/n3dmath.h	Thu Oct 23 01:46:07 2014 +0300
     1.3 @@ -0,0 +1,425 @@
     1.4 +#ifndef _N3DMATH_H_
     1.5 +#define _N3DMATH_H_
     1.6 +
     1.7 +//	- n3dmath.h - 
     1.8 +//	Nuc3D 2 Header File
     1.9 +//	written by: John Tsiombikas (10 Sep 2002)
    1.10 +//	Last Modification: 14 April 2002
    1.11 +//	---------------------------
    1.12 +//	Mathematical stuff
    1.13 +
    1.14 +#include <iostream>
    1.15 +#include <cmath>
    1.16 +
    1.17 +#ifdef NUC3D_VER_DIRECT3D
    1.18 +#include "d3d8.h"	// Direct3D type definitions (D3DMATRIX)
    1.19 +#endif	//NUC3D_VER_DIRECT3D
    1.20 +
    1.21 +// forward declarations
    1.22 +class Matrix4x4;
    1.23 +class Matrix3x3;
    1.24 +class Vector3;
    1.25 +class Vector2;
    1.26 +class Vector4;
    1.27 +class Quaternion;
    1.28 +
    1.29 +// mathematical constants
    1.30 +const float Pi = 3.1415926535897932f;
    1.31 +const float TwoPi = 6.2831853071795865f;// Pi * 2.0f;
    1.32 +const float HalfPi = 1.5707963267948965f; //Pi * 0.5f;
    1.33 +const float QuarterPi = 0.7853981633974483f; //Pi * 0.25f;
    1.34 +const float ThreeQuartersPi = 2.3561944901923450f; //QuarterPi * 3.0f;
    1.35 +
    1.36 +const float XSmallNumber = 1.e-8f;
    1.37 +const float SmallNumber = 1.e-4f;
    1.38 +
    1.39 +// basis vectors
    1.40 +#define VECTOR3_I	(Vector3(1.0f, 0.0f, 0.0f))
    1.41 +#define VECTOR3_J	(Vector3(0.0f, 1.0f, 0.0f))
    1.42 +#define VECTOR3_K	(Vector3(0.0f, 0.0f, 1.0f))
    1.43 +
    1.44 +#define VECTOR2_I	(Vector2(1.0f, 0.0f))
    1.45 +#define VECTOR2_J	(Vector2(0.0f, 1.0f))
    1.46 +
    1.47 +// angle conversion
    1.48 +#define DEGTORAD(a)	(Pi * (a) / 180)
    1.49 +#define RADTODEG(a) ((a) * 180 / Pi)
    1.50 +
    1.51 +
    1.52 +
    1.53 +// ------------- Vector3 class -------------
    1.54 +
    1.55 +#ifdef NUC3D_VER_DIRECT3D	// if we are using Direct3D version
    1.56 +
    1.57 +class Vector3 : public D3DVECTOR {
    1.58 +public:
    1.59 +
    1.60 +#else	// not D3D
    1.61 +
    1.62 +class Vector3 {
    1.63 +public:
    1.64 +	float x, y, z;
    1.65 +
    1.66 +#endif	//NUC3D_VER_DIRECT3D
    1.67 +
    1.68 +	Vector3();
    1.69 +	Vector3(float x, float y, float z);
    1.70 +
    1.71 +	inline float DotProduct(const Vector3 &vec) const;
    1.72 +	inline Vector3 CrossProduct(const Vector3 &vec) const;
    1.73 +
    1.74 +	inline Vector3 operator +(const Vector3 &vec) const;
    1.75 +	inline Vector3 operator -(const Vector3 &vec) const;
    1.76 +	inline Vector3 operator *(float scalar) const;
    1.77 +	inline Vector3 operator /(float scalar) const;
    1.78 +	inline void operator +=(const Vector3 &vec);
    1.79 +	inline void operator -=(const Vector3 &vec);
    1.80 +	inline void operator *=(float scalar);
    1.81 +	inline void operator /=(float scalar);
    1.82 +	inline Vector3 operator -() const;	// unary minus for inversion
    1.83 +
    1.84 +	inline bool operator >(const Vector3 &vec) const;
    1.85 +	inline bool operator <(const Vector3 &vec) const;
    1.86 +	inline bool operator >(float len) const;
    1.87 +	inline bool operator <(float len) const;
    1.88 +	inline bool operator ==(const Vector3 &vec) const;
    1.89 +	inline bool operator ==(float len) const;
    1.90 +
    1.91 +	inline operator Vector2() const;
    1.92 +	inline operator Vector4() const;
    1.93 +
    1.94 +	inline float Length() const;
    1.95 +	inline float LengthSq() const;
    1.96 +	
    1.97 +	inline void Normalize();
    1.98 +	inline Vector3 Normalized() const;
    1.99 +
   1.100 +	inline Vector3 Reflection(const Vector3 &normal) const;
   1.101 +	Vector3 Refraction(const Vector3 &normal, float FromIOR, float ToIOR) const;
   1.102 +	
   1.103 +	void Transform(const Matrix4x4 &mat);	// transform a vector using a transformation matrix
   1.104 +	void Transform(const Quaternion &quat);	// transform by a quaternion
   1.105 +
   1.106 +	// Direct transformations on the vector
   1.107 +	void Translate(float x, float y, float z);
   1.108 +	void Rotate(float x, float y, float z);
   1.109 +	void Rotate(const Vector3 &axis, float angle);
   1.110 +	void Scale(float x, float y, float z);
   1.111 +
   1.112 +	float &operator [](int index);
   1.113 +
   1.114 +	friend std::ostream &operator <<(std::ostream &out, const Vector3 &vec);
   1.115 +};
   1.116 +
   1.117 +inline float DotProduct(const Vector3 &vec1, const Vector3 &vec2);
   1.118 +inline Vector3 CrossProduct(const Vector3 &vec1, const Vector3 &vec2);
   1.119 +
   1.120 +////////////////////// 4-dimensional vectors ////////////////////////////
   1.121 +class Vector4 {
   1.122 +public:
   1.123 +	float x, y, z, w;
   1.124 +
   1.125 +	Vector4();
   1.126 +	Vector4(const Vector4 &vec);
   1.127 +	Vector4(const Vector3 &vec);	// create from a 3 dimensional vector setting w=1
   1.128 +	Vector4(float x, float y, float z, float w);
   1.129 +
   1.130 +	float DotProduct(const Vector4 &vec) const;
   1.131 +	Vector4 CrossProduct(const Vector4 &vec1, const Vector4 &vec2) const;
   1.132 +
   1.133 +	Vector4 operator +(const Vector4 &vec) const;
   1.134 +	Vector4 operator -(const Vector4 &vec) const;
   1.135 +	Vector4 operator *(float scalar) const;
   1.136 +	Vector4 operator /(float scalar) const;
   1.137 +	void operator +=(const Vector4 &vec);
   1.138 +	void operator -=(const Vector4 &vec);
   1.139 +	void operator *=(float scalar);
   1.140 +	void operator /=(float scalar);
   1.141 +	Vector4 operator -() const;	// unary minus for inversion
   1.142 +
   1.143 +	bool operator >(const Vector4 &vec) const;
   1.144 +	bool operator <(const Vector4 &vec) const;
   1.145 +	bool operator >(float len) const;
   1.146 +	bool operator <(float len) const;
   1.147 +	bool operator ==(const Vector4 &vec) const;
   1.148 +	bool operator ==(float len) const;
   1.149 +
   1.150 +	operator Vector3() const;
   1.151 +
   1.152 +	float Length() const;
   1.153 +	float LengthSq() const;
   1.154 +	
   1.155 +	void Normalize();
   1.156 +	Vector4 Normalized() const;
   1.157 +
   1.158 +	void Transform(const Matrix4x4 &mat);	// transform a vector using a transformation matrix
   1.159 +
   1.160 +	// Direct transformations on the vector
   1.161 +	void Translate(float x, float y, float z, float w);
   1.162 +	void Rotate(float x, float y, float z);
   1.163 +	void Rotate(const Vector3 &axis, float angle);
   1.164 +	void Scale(float x, float y, float z, float w);
   1.165 +
   1.166 +	float &operator [](int index);
   1.167 +
   1.168 +	friend std::ostream &operator <<(std::ostream &out, const Vector3 &vec);
   1.169 +};
   1.170 +
   1.171 +float DotProduct(const Vector4 &vec1, const Vector4 &vec2);
   1.172 +Vector4 CrossProduct(const Vector4 &vec1, const Vector4 &vec2, const Vector4 &vec3);
   1.173 +
   1.174 +///////////////////////////
   1.175 +
   1.176 +class Vector2 {
   1.177 +public:
   1.178 +	float x, y;
   1.179 +
   1.180 +	Vector2();
   1.181 +	Vector2(const Vector2 &vec);
   1.182 +	Vector2(float x, float y);
   1.183 +
   1.184 +	float DotProduct(const Vector2 &vec) const;
   1.185 +	//Vector2 CrossProduct(const Vector2 &vec) const;
   1.186 +
   1.187 +	Vector2 operator +(const Vector2 &vec) const;
   1.188 +	Vector2 operator -(const Vector2 &vec) const;
   1.189 +	Vector2 operator *(float scalar) const;
   1.190 +	Vector2 operator /(float scalar) const;
   1.191 +	void operator +=(const Vector2 &vec);
   1.192 +	void operator -=(const Vector2 &vec);
   1.193 +	void operator *=(float scalar);
   1.194 +	void operator /=(float scalar);
   1.195 +	Vector2 operator -() const;	// unary minus for inversion
   1.196 +
   1.197 +	bool operator >(const Vector2 &vec) const;
   1.198 +	bool operator <(const Vector2 &vec) const;
   1.199 +	bool operator >(float len) const;
   1.200 +	bool operator <(float len) const;
   1.201 +	bool operator ==(const Vector2 &vec) const;
   1.202 +	bool operator ==(float len) const;
   1.203 +
   1.204 +	operator Vector3() const;
   1.205 +	    
   1.206 +	float Length() const;
   1.207 +	float LengthSq() const;
   1.208 +	
   1.209 +	void Normalize();
   1.210 +	Vector2 Normalized() const;
   1.211 +
   1.212 +	Vector2 Reflection(const Vector2 &normal) const;
   1.213 +	Vector2 Refraction(const Vector2 &normal, float FromIOR, float ToIOR) const;
   1.214 +	
   1.215 +	void Transform(const Matrix3x3 &mat);	// transform a vector using a transformation matrix
   1.216 +
   1.217 +	// Direct transformations on the vector
   1.218 +	void Translate(float x, float y);
   1.219 +	void Rotate(float angle);
   1.220 +	void Scale(float x, float y);
   1.221 +
   1.222 +	float &operator [](int index);
   1.223 +
   1.224 +	friend std::ostream &operator <<(std::ostream &out, const Vector2 &vec);
   1.225 +};
   1.226 +
   1.227 +float DotProduct(const Vector3 &vec1, const Vector3 &vec2);
   1.228 +
   1.229 +
   1.230 +struct Vector2i {
   1.231 +	int x, y;
   1.232 +
   1.233 +	Vector2i(int x=0, int y=0) {this->x = x; this->y = y;}
   1.234 +};
   1.235 +
   1.236 +
   1.237 +////////////////// Quaternion ///////////////////////
   1.238 +
   1.239 +class Quaternion {
   1.240 +public:
   1.241 +	float s;
   1.242 +	Vector3 v;
   1.243 +
   1.244 +	Quaternion();
   1.245 +	Quaternion(float s, const Vector3 &v);
   1.246 +	Quaternion(float s, float x, float y, float z);	
   1.247 +
   1.248 +	Quaternion operator +(const Quaternion &quat) const;
   1.249 +	Quaternion operator -(const Quaternion &quat) const;
   1.250 +	Quaternion operator -() const;
   1.251 +	Quaternion operator *(const Quaternion &quat) const;
   1.252 +	
   1.253 +	void operator +=(const Quaternion &quat);
   1.254 +	void operator -=(const Quaternion &quat);
   1.255 +	void operator *=(const Quaternion &quat);
   1.256 +
   1.257 +	void ResetIdentity();
   1.258 +
   1.259 +	Quaternion Conjugate() const;
   1.260 +
   1.261 +	float Length() const;
   1.262 +	float LengthSq() const;
   1.263 +	
   1.264 +	void Normalize();
   1.265 +	Quaternion Normalized() const;
   1.266 +
   1.267 +	Quaternion Inverse() const;
   1.268 +
   1.269 +	void SetRotation(const Vector3 &axis, float angle);
   1.270 +	void Rotate(const Vector3 &axis, float angle);
   1.271 +
   1.272 +	Matrix3x3 GetRotationMatrix() const;
   1.273 +};
   1.274 +
   1.275 +	
   1.276 +
   1.277 +////////////////////////// Matrices //////////////////////////////
   1.278 +
   1.279 +#ifdef NUC3D_VER_DIRECT3D	// if we are using Direct3D version
   1.280 +
   1.281 +class Matrix4x4 : public D3DMATRIX {
   1.282 +public:
   1.283 +
   1.284 +#else	// Software or OpenGL version
   1.285 +
   1.286 +class Matrix4x4 {
   1.287 +public:
   1.288 +	float m[4][4];
   1.289 +
   1.290 +#endif	//NUC3D_VER_DIRECT3D
   1.291 +
   1.292 +	Matrix4x4();
   1.293 +	Matrix4x4(const Matrix4x4 &mat);
   1.294 +	Matrix4x4(const Matrix3x3 &mat);
   1.295 +	Matrix4x4(	float m00, float m01, float m02, float m03,
   1.296 +				float m10, float m11, float m12, float m13,
   1.297 +				float m20, float m21, float m22, float m23,
   1.298 +				float m30, float m31, float m32, float m33 );
   1.299 +
   1.300 +	// basic operations on matrices
   1.301 +	Matrix4x4 operator +(const Matrix4x4 &mat) const;
   1.302 +	Matrix4x4 operator -(const Matrix4x4 &mat) const;
   1.303 +	Matrix4x4 operator *(const Matrix4x4 &mat) const;
   1.304 +	Matrix4x4 operator *(float scalar) const;
   1.305 +	void operator +=(const Matrix4x4 &mat);
   1.306 +	void operator -=(const Matrix4x4 &mat);
   1.307 +	void operator *=(const Matrix4x4 &mat);
   1.308 +	void operator *=(float scalar);
   1.309 +
   1.310 +	void ResetIdentity();
   1.311 +
   1.312 +	// concatenate various transformation matrices with our current matrix
   1.313 +	void Translate(float x, float y, float z);
   1.314 +	void Rotate(float x, float y, float z);
   1.315 +	void Rotate(const Vector3 &axis, float rads);
   1.316 +	void Scale(float x, float y, float z);
   1.317 +
   1.318 +	// set the matrix to a specific transformation matrix
   1.319 +	void SetTranslation(float x, float y, float z);
   1.320 +	void SetRotation(float x, float y, float z);
   1.321 +	void SetRotation(const Vector3 &axis, float angle);
   1.322 +	void SetScaling(float x, float y, float z);
   1.323 +
   1.324 +	// row and column accessors
   1.325 +	void SetColumnVector(const Vector4 &vec, int columnindex);
   1.326 +	void SetRowVector(const Vector4 &vec, int rowindex);
   1.327 +	Vector4 GetColumnVector(int columnindex) const;
   1.328 +	Vector4 GetRowVector(int rowindex) const;
   1.329 +
   1.330 +	// other operations on matrices
   1.331 +	void Transpose();
   1.332 +	Matrix4x4 Transposed() const;
   1.333 +
   1.334 +	float Determinant() const;
   1.335 +	Matrix4x4 Adjoint() const;
   1.336 +	Matrix4x4 Inverse() const;
   1.337 +};
   1.338 +
   1.339 +
   1.340 +////////////////// Matrix3x3 //////////////////
   1.341 +class Matrix3x3 {
   1.342 +public:
   1.343 +	float m[3][3];
   1.344 +
   1.345 +	Matrix3x3();
   1.346 +	Matrix3x3(const Matrix3x3 &mat);
   1.347 +	Matrix3x3(	float m00, float m01, float m02,
   1.348 +				float m10, float m11, float m12,
   1.349 +				float m20, float m21, float m22 );
   1.350 +
   1.351 +	// basic operations on matrices
   1.352 +	Matrix3x3 operator +(const Matrix3x3 &mat) const;
   1.353 +	Matrix3x3 operator -(const Matrix3x3 &mat) const;
   1.354 +	Matrix3x3 operator *(const Matrix3x3 &mat) const;
   1.355 +	Matrix3x3 operator *(float scalar) const;
   1.356 +	void operator +=(const Matrix3x3 &mat);
   1.357 +	void operator -=(const Matrix3x3 &mat);
   1.358 +	void operator *=(const Matrix3x3 &mat);
   1.359 +	void operator *=(float scalar);
   1.360 +
   1.361 +	void ResetIdentity();
   1.362 +
   1.363 +	// concatenate various transformation matrices with our current matrix
   1.364 +	void Translate(float x, float y);
   1.365 +	void Rotate(float angle);
   1.366 +	void Scale(float x, float y);
   1.367 +	
   1.368 +	// set the matrix to a specific transformation matrix
   1.369 +	void SetTranslation(float x, float y);
   1.370 +	void SetRotation(float angle);
   1.371 +	void SetScaling(float x, float y);
   1.372 +
   1.373 +	// row and column accessors
   1.374 +	void SetColumnVector(const Vector3 &vec, int columnindex);
   1.375 +	void SetRowVector(const Vector3 &vec, int rowindex);
   1.376 +	Vector3 GetColumnVector(int columnindex) const;
   1.377 +	Vector3 GetRowVector(int rowindex) const;
   1.378 +
   1.379 +	// other operations on matrices
   1.380 +	void Transpose();
   1.381 +	Matrix3x3 Transposed() const;
   1.382 +
   1.383 +	void OrthoNormalize();
   1.384 +	Matrix3x3 OrthoNormalized();
   1.385 +
   1.386 +	//float Determinant() const; // NYI
   1.387 +	//Matrix3x3 Adjoint() const;
   1.388 +	//Matrix3x3 Inverse() const;
   1.389 +};
   1.390 +
   1.391 +///////////////// ray /////////////////
   1.392 +
   1.393 +class Ray {
   1.394 +public:
   1.395 +	Vector3 Origin;
   1.396 +	Vector3 Direction;
   1.397 +	float Energy;
   1.398 +	float CurrentIOR;
   1.399 +
   1.400 +	Ray();
   1.401 +	Ray(const Vector3 &origin, const Vector3 &direction);
   1.402 +};
   1.403 +
   1.404 +// a coordinate system basis
   1.405 +class Base {
   1.406 +public:
   1.407 +	Vector3 i, j, k;
   1.408 +
   1.409 +	Base();
   1.410 +	Base(const Vector3 &i, const Vector3 &j, const Vector3 &k);
   1.411 +	Base(const Vector3 &dir, bool LeftHanded=true);
   1.412 +
   1.413 +	void Rotate(float x, float y, float z);
   1.414 +	void Rotate(const Vector3 &axis, float angle);
   1.415 +	void Rotate(const Matrix4x4 &mat);
   1.416 +	void Rotate(const Quaternion &quat);
   1.417 +
   1.418 +	Matrix3x3 CreateRotationMatrix() const;
   1.419 +};
   1.420 +
   1.421 +
   1.422 +
   1.423 +float frand(float range);
   1.424 +
   1.425 +
   1.426 +#include "n3dmath.inl"
   1.427 +
   1.428 +#endif	// _N3DMATH_H_
   1.429 \ No newline at end of file