Geo.h

Go to the documentation of this file.
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 
00100 
00101     // -- square matrix definitions
00102 
00104     struct Matrix4: public MatrixN<4>
00105     {
00106         //useful ctors
00107         inline Matrix4() {} 
00108         inline Matrix4(const float *array): MatrixN<4>(array) {} 
00109         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); 
00110         inline Matrix4(float c): MatrixN<4>(c) {} 
00111         inline Matrix4(int c): MatrixN<4>((float)c) {} 
00112         inline Matrix4(const Matrix4 &o): MatrixN<4>((const float*)&o) {} 
00113         inline Matrix4(const MatrixN<4> &o): MatrixN<4>((const float*)&o) {} 
00114         inline Matrix4(const struct Matrix3 &o); 
00115 
00117         inline const float* ToGL() const;
00118     private:
00119         static Matrix4 globalGLConversionMatrix;
00120     };
00121 
00123     struct Matrix3: public MatrixN<3>
00124     {
00125         //useful ctors
00126         inline Matrix3() {} 
00127         inline Matrix3(const float *array): MatrixN<3>(array) {} 
00128         inline Matrix3(float e0,float e1,float e2,float e3,float e4,float e5,float e6,float e7,float e8); 
00129         inline Matrix3(float c): MatrixN<3>(c) {} 
00130         inline Matrix3(int c): MatrixN<3>((float)c) {} 
00131         inline Matrix3(const Matrix3 &o): MatrixN<3>((const float*)&o) {} 
00132         inline Matrix3(const MatrixN<3> &o): MatrixN<3>((const float*)&o) {} 
00133         explicit inline Matrix3(const struct Matrix4 &o); 
00134         inline Matrix3(const struct Matrix2 &o); 
00135     };
00136 
00138     struct Matrix2: public MatrixN<2>
00139     {
00140         //useful ctors
00141         inline Matrix2() {} 
00142         inline Matrix2(const float *array): MatrixN<2>(array) {} 
00143         inline Matrix2(float e0,float e1,float e2,float e3); 
00144         inline Matrix2(float c): MatrixN<2>(c) {} 
00145         inline Matrix2(int c): MatrixN<2>((float)c) {} 
00146         inline Matrix2(const Matrix2 &o): MatrixN<2>((const float*)&o) {} 
00147         inline Matrix2(const MatrixN<2> &o): MatrixN<2>((const float*)&o) {} 
00148         explicit inline Matrix2(const Matrix3 &o); 
00149     };
00150 
00151 
00152     // -- quaternion definition
00153 
00154     //TODO
00155 
00156 
00157     // -- generic utilities
00158 
00159     inline bool NearEqual(float c0, float c1, float tolerance=FLOAT_TOLERANCE); 
00160     template <typename VecType>
00161     inline bool NearEqual(const VecType& v1, const VecType& v2, float tolerance=FLOAT_TOLERANCE); 
00162     template <typename VecType>
00163     inline bool NearEqual(const VecType& v, float c, float tolerance=FLOAT_TOLERANCE); 
00164 
00165     inline bool IsFinite(float c); 
00166     template <typename VecType>
00167     inline bool IsFinite(VecType &v); 
00168 
00169 
00170     // -- Vector functions
00171 
00172     template <typename VecType1, typename VecType2>
00173     inline float VecDot(const VecType1 &v1, const VecType2 &v2); 
00174 
00175     inline struct Vector3 VecCross(const struct Vector3 &v1, const struct Vector3 &v2); 
00176 
00177     template <typename VecType>
00178     inline VecType VecNormal(const VecType &v); 
00179     template <typename VecType>
00180     inline void VecNormalize(VecType &v); 
00181 
00182     template <typename VecType1, typename VecType2>
00183     inline float VecAngleBetween(const VecType1 &l1, const VecType2 &l2); 
00184 
00185     template <typename VecType1, typename VecType2>
00186     inline float VecDistance(const VecType1 &l1, const VecType2 &l2); 
00187 
00188     template <typename VecType>
00189     inline float VecLength(const VecType &v); //<!length of a vector
00190     template <typename VecType>
00191     inline float VecLengthSquared(const VecType &v); //<!squared length of a vector
00192 
00193     template <typename VecType1, typename VecType2>
00194     inline VecType1 VecReflect(const VecType1 &vec, const VecType2 &norm); 
00195 
00196 
00197     // -- Matrix functions
00198 
00199     template <typename MatType>
00200     MatType MatMul(const MatType &m1, const MatType &m2); 
00201 
00202     float MatDeterminant(const Matrix4 &mat); 
00203     float MatDeterminant(const Matrix3 &mat); 
00204     inline float MatDeterminant(const Matrix2 &mat); 
00205 
00206     template <typename MatType>
00207     MatType MatTranspose(const MatType &mat); 
00208 
00209     template <typename MatType>
00210     MatType MatInverse(const MatType &mat); 
00211 
00212     inline Matrix3 MatRotateX(float angle); 
00213     inline Matrix3 MatRotateY(float angle); 
00214     inline Matrix3 MatRotateZ(float angle); 
00215     inline Matrix3 MatRotateAxis(const Vector3 &axis, float angle); 
00216 
00217     template <typename VecType>
00218     inline typename VecType::GreaterMatrixType MatTranslate(const VecType &offset); 
00219 
00220     template <typename VecType>
00221     inline typename VecType::GreaterMatrixType MatScale(const VecType &scale); 
00222 
00223     Matrix4 MatProjectionFoV(float fov=PI/2, float aspectYdivX=1.0f, float nearZ=1.0f, float farZ=1000.0f); 
00224 
00225     Matrix4 MatProjectionOrtho(float left, float right, float bottom, float top, float nearZ=1.0f, float farZ=1000.0f); 
00226     inline Matrix4 MatProjectionOrtho(float nearZ=1.0f, float farZ=1000.0f); 
00227 
00228     Matrix4 MatViewLookAt(const Vector3 &pos, const Vector3 &tar, const Vector3 &vagueUp=Vector3(0,0,1), bool flipVert=false, bool flipHorz=false); 
00229 
00230 
00231     // -- Mixed functions
00232 
00233     template <typename VecType>
00234     inline VecType TransformVector(const typename VecType::MatrixType &m, const VecType &v); 
00235 
00236 }; //namespace GEO
00237 
00238 //include template and inline implementations
00239 #ifndef GEO_INCLUDE_INLINE
00240     #define GEO_INCLUDE_INLINE
00241     #include "Geo.cpp"
00242 #endif
00243 
00244 #endif //#ifdef MPMA_COMPILE_GEO
00245 
00246 #endif // GEO_H_INCLUDED
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends