MPMA Framework 0.4
|
00001 00002 //Luke Lenhart, 2007 00003 //See /docs/License.txt for details on how this code may be used. 00004 00005 //all angles are in radians 00006 00007 #ifndef GEO_H_INCLUDED 00008 #define GEO_H_INCLUDED 00009 00010 #include "../base/Setup.h" 00011 00012 #ifdef MPMA_COMPILE_GEO 00013 00014 #include <cmath> 00015 #include <string> 00016 00017 #include "../base/Types.h" 00018 #include "GeoBases.h" 00019 00021 namespace GEO 00022 { 00023 // -- global constants 00024 00025 extern const float PI2; 00026 extern const float PI; 00027 extern const float FLOAT_TOLERANCE; 00028 00029 00030 // -- vector definitions 00031 00033 struct Vector4: public VectorN<4> 00034 { 00035 //ctors 00036 inline Vector4() {} 00037 inline Vector4(float x, float y, float z, float w) {elements[0]=x; elements[1]=y; elements[2]=z; elements[3]=w;} 00038 inline Vector4(const float *array): VectorN<4>(array) {} 00039 inline Vector4(float c): VectorN<4>(c) {} 00040 inline Vector4(int c): VectorN<4>((float)c) {} 00041 inline Vector4(const Vector4 &o): VectorN<4>((const float*)&o) {} 00042 inline Vector4(const VectorN<4> &o): VectorN<4>((const float*)&o) {} 00043 inline Vector4(const struct Vector3 &o, float w=1); 00044 00045 //other accessors 00046 inline float x() const { return elements[0]; } 00047 inline float& x() { return elements[0]; } 00048 inline float y() const { return elements[1]; } 00049 inline float& y() { return elements[1]; } 00050 inline float z() const { return elements[2]; } 00051 inline float& z() { return elements[2]; } 00052 inline float w() const { return elements[3]; } 00053 inline float& w() { return elements[3]; } 00054 }; 00055 00056 00058 struct Vector3: public VectorN<3> 00059 { 00060 //ctors 00061 inline Vector3() {} 00062 inline Vector3(float x, float y, float z) {elements[0]=x; elements[1]=y; elements[2]=z;} 00063 inline Vector3(const float *array): VectorN<3>(array) {} 00064 inline Vector3(float c): VectorN<3>(c) {} 00065 inline Vector3(int c): VectorN<3>((float)c) {} 00066 inline Vector3(const Vector3 &o): VectorN<3>((const float*)&o) {} 00067 inline Vector3(const VectorN<3> &o): VectorN<3>((const float*)&o) {} 00068 explicit inline Vector3(const Vector4 &o); 00069 inline Vector3(const struct Vector2 &o, float z=1); 00070 00071 //other accessors 00072 inline float x() const { return elements[0]; } 00073 inline float& x() { return elements[0]; } 00074 inline float y() const { return elements[1]; } 00075 inline float& y() { return elements[1]; } 00076 inline float z() const { return elements[2]; } 00077 inline float& z() { return elements[2]; } 00078 }; 00079 00081 struct Vector2: public VectorN<2> 00082 { 00083 //ctors 00084 inline Vector2() {} 00085 inline Vector2(float x, float y) {elements[0]=x; elements[1]=y;} 00086 inline Vector2(const float *array): VectorN<2>(array) {} 00087 inline Vector2(float c): VectorN<2>(c) {} 00088 inline Vector2(int c): VectorN<2>((float)c) {} 00089 inline Vector2(const Vector2 &o): VectorN<2>((const float*)&o) {} 00090 inline Vector2(const VectorN<2> &o): VectorN<2>((const float*)&o) {} 00091 explicit inline Vector2(const Vector3 &o); 00092 00093 //other accessors 00094 inline float x() const { return elements[0]; } 00095 inline float& x() { return elements[0]; } 00096 inline float y() const { return elements[1]; } 00097 inline float& y() { return elements[1]; } 00098 }; 00099 00101 struct Vector1: public VectorN<1> 00102 { 00103 //ctors 00104 inline Vector1() {} 00105 inline Vector1(float x) {elements[0]=x;} 00106 inline Vector1(const float *array): VectorN<1>(array) {} 00107 inline Vector1(int c): VectorN<1>((float)c) {} 00108 inline Vector1(const Vector1 &o): VectorN<1>(o.x()) {} 00109 inline Vector1(const VectorN<1> &o): VectorN<1>(o.elements[0]) {} 00110 explicit inline Vector1(const Vector3 &o); 00111 00112 //other accessors 00113 inline float x() const { return elements[0]; } 00114 inline float& x() { return elements[0]; } 00115 }; 00116 00117 // -- square matrix definitions 00118 00120 struct Matrix4: public MatrixN<4> 00121 { 00122 //useful ctors 00123 inline Matrix4() {} 00124 inline Matrix4(const float *array): MatrixN<4>(array) {} 00125 inline Matrix4(float e0,float e1,float e2,float e3,float e4,float e5,float e6,float e7,float e8,float e9,float e10,float e11,float e12,float e13,float e14,float e15); 00126 inline Matrix4(float c): MatrixN<4>(c) {} 00127 inline Matrix4(int c): MatrixN<4>((float)c) {} 00128 inline Matrix4(const Matrix4 &o): MatrixN<4>((const float*)&o) {} 00129 inline Matrix4(const MatrixN<4> &o): MatrixN<4>((const float*)&o) {} 00130 inline Matrix4(const struct Matrix3 &o); 00131 00133 inline const float* ToGL() const; 00134 private: 00135 static Matrix4 globalGLConversionMatrix; 00136 }; 00137 00139 struct Matrix3: public MatrixN<3> 00140 { 00141 //useful ctors 00142 inline Matrix3() {} 00143 inline Matrix3(const float *array): MatrixN<3>(array) {} 00144 inline Matrix3(float e0,float e1,float e2,float e3,float e4,float e5,float e6,float e7,float e8); 00145 inline Matrix3(float c): MatrixN<3>(c) {} 00146 inline Matrix3(int c): MatrixN<3>((float)c) {} 00147 inline Matrix3(const Matrix3 &o): MatrixN<3>((const float*)&o) {} 00148 inline Matrix3(const MatrixN<3> &o): MatrixN<3>((const float*)&o) {} 00149 explicit inline Matrix3(const struct Matrix4 &o); 00150 inline Matrix3(const struct Matrix2 &o); 00151 }; 00152 00154 struct Matrix2: public MatrixN<2> 00155 { 00156 //useful ctors 00157 inline Matrix2() {} 00158 inline Matrix2(const float *array): MatrixN<2>(array) {} 00159 inline Matrix2(float e0,float e1,float e2,float e3); 00160 inline Matrix2(float c): MatrixN<2>(c) {} 00161 inline Matrix2(int c): MatrixN<2>((float)c) {} 00162 inline Matrix2(const Matrix2 &o): MatrixN<2>((const float*)&o) {} 00163 inline Matrix2(const MatrixN<2> &o): MatrixN<2>((const float*)&o) {} 00164 explicit inline Matrix2(const Matrix3 &o); 00165 }; 00166 00167 00168 // -- quaternion definition 00169 00170 //TODO 00171 00172 00173 // -- generic utilities 00174 00175 inline bool NearEqual(float c0, float c1, float tolerance=FLOAT_TOLERANCE); 00176 template <typename VecType> 00177 inline bool NearEqual(const VecType& v1, const VecType& v2, float tolerance=FLOAT_TOLERANCE); 00178 template <typename VecType> 00179 inline bool NearEqual(const VecType& v, float c, float tolerance=FLOAT_TOLERANCE); 00180 00181 inline bool IsFinite(float c); 00182 template <typename VecType> 00183 inline bool IsFinite(VecType &v); 00184 00185 template <typename VecType> 00186 inline VecType Clamp(const VecType &val, const VecType &min, const VecType &max); 00187 template <> 00188 inline float Clamp(const float &val, const float &min, const float &max); 00189 00190 // -- Vector functions 00191 00192 template <typename VecType1, typename VecType2> 00193 inline float VecDot(const VecType1 &v1, const VecType2 &v2); 00194 00195 inline struct Vector3 VecCross(const struct Vector3 &v1, const struct Vector3 &v2); 00196 00197 template <typename VecType> 00198 inline VecType VecNormal(const VecType &v); 00199 template <typename VecType> 00200 inline void VecNormalize(VecType &v); 00201 00202 template <typename VecType1, typename VecType2> 00203 inline float VecAngleBetween(const VecType1 &l1, const VecType2 &l2); 00204 00205 template <typename VecType1, typename VecType2> 00206 inline float VecDistance(const VecType1 &l1, const VecType2 &l2); 00207 00208 template <typename VecType> 00209 inline float VecLength(const VecType &v); //<!length of a vector 00210 template <typename VecType> 00211 inline float VecLengthSquared(const VecType &v); //<!squared length of a vector 00212 00213 template <typename VecType1, typename VecType2> 00214 inline VecType1 VecReflect(const VecType1 &vec, const VecType2 &norm); 00215 00216 00217 // -- Matrix functions 00218 00219 template <typename MatType> 00220 MatType MatMul(const MatType &m1, const MatType &m2); 00221 00222 float MatDeterminant(const Matrix4 &mat); 00223 float MatDeterminant(const Matrix3 &mat); 00224 inline float MatDeterminant(const Matrix2 &mat); 00225 00226 template <typename MatType> 00227 MatType MatTranspose(const MatType &mat); 00228 00229 template <typename MatType> 00230 MatType MatInverse(const MatType &mat); 00231 00232 inline Matrix3 MatRotateX(float angle); 00233 inline Matrix3 MatRotateY(float angle); 00234 inline Matrix3 MatRotateZ(float angle); 00235 inline Matrix3 MatRotateAxis(const Vector3 &axis, float angle); 00236 00237 template <typename VecType> 00238 inline typename VecType::GreaterMatrixType MatTranslate(const VecType &offset); 00239 00240 template <typename VecType> 00241 inline typename VecType::GreaterMatrixType MatScale(const VecType &scale); 00242 00243 Matrix4 MatProjectionFoV(float fov=PI/2, float aspectYdivX=1.0f, float nearZ=1.0f, float farZ=1000.0f); 00244 00245 Matrix4 MatProjectionOrtho(float left, float right, float bottom, float top, float nearZ=1.0f, float farZ=1000.0f); 00246 inline Matrix4 MatProjectionOrtho(float nearZ=1.0f, float farZ=1000.0f); 00247 00248 Matrix4 MatViewLookAt(const Vector3 &pos, const Vector3 &tar, const Vector3 &vagueUp=Vector3(0,0,1), bool flipVert=false, bool flipHorz=false); 00249 00250 00251 // -- Mixed functions 00252 00253 template <typename VecType> 00254 inline VecType TransformVector(const typename VecType::MatrixType &m, const VecType &v); 00255 00256 }; //namespace GEO 00257 00258 //include template and inline implementations 00259 #ifndef GEO_INCLUDE_INLINE 00260 #define GEO_INCLUDE_INLINE 00261 #include "Geo.cpp" 00262 #endif 00263 00264 #endif //#ifdef MPMA_COMPILE_GEO 00265 00266 #endif // GEO_H_INCLUDED