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 <cmath>
00011 #include <string>
00012 
00013 #pragma pack(push,4) //align to 4 bytes
00014 
00015 #include "../base/Types.h"
00016 #include "GeoBases.h"
00017 
00019 namespace GEO
00020 {
00021     // -- global constants
00022 
00023     extern const float PI2; 
00024     extern const float PI; 
00025     extern const float FLOAT_TOLERANCE; 
00026 
00027     
00028     // -- vector definitions    
00029 
00031     struct Vector4: public VectorN<4>
00032     {
00033         //ctors
00034         inline Vector4() {} 
00035         inline Vector4(float x, float y, float z, float w) {elem[0]=x; elem[1]=y; elem[2]=z; elem[3]=w;} 
00036         inline Vector4(const float *array): VectorN<4>(array) {} 
00037         inline Vector4(float c): VectorN<4>(c) {} 
00038         inline Vector4(int c): VectorN<4>((float)c) {} 
00039         inline Vector4(const Vector4 &o): VectorN<4>((const float*)&o) {} 
00040         inline Vector4(const VectorN<4> &o): VectorN<4>((const float*)&o) {} 
00041         inline Vector4(const struct Vector3 &o, float w=1); 
00042 
00043         //other accessors
00044         inline float x() const { return elem[0]; } 
00045         inline float& x() { return elem[0]; } 
00046         inline float y() const { return elem[1]; } 
00047         inline float& y() { return elem[1]; } 
00048         inline float z() const { return elem[2]; } 
00049         inline float& z() { return elem[2]; } 
00050         inline float w() const { return elem[3]; } 
00051         inline float& w() { return elem[3]; } 
00052     };
00053 
00054 
00056     struct Vector3: public VectorN<3>
00057     {
00058         //ctors
00059         inline Vector3() {} 
00060         inline Vector3(float x, float y, float z) {elem[0]=x; elem[1]=y; elem[2]=z;} 
00061         inline Vector3(const float *array): VectorN<3>(array) {} 
00062         inline Vector3(float c): VectorN<3>(c) {} 
00063         inline Vector3(int c): VectorN<3>((float)c) {} 
00064         inline Vector3(const Vector3 &o): VectorN<3>((const float*)&o) {} 
00065         inline Vector3(const VectorN<3> &o): VectorN<3>((const float*)&o) {} 
00066         explicit inline Vector3(const Vector4 &o); 
00067         inline Vector3(const struct Vector2 &o, float z=1);
00068         
00069         //other accessors
00070         inline float x() const { return elem[0]; } 
00071         inline float& x() { return elem[0]; } 
00072         inline float y() const { return elem[1]; } 
00073         inline float& y() { return elem[1]; } 
00074         inline float z() const { return elem[2]; } 
00075         inline float& z() { return elem[2]; } 
00076     };
00077 
00079     struct Vector2: public VectorN<2>
00080     {
00081         //ctors
00082         inline Vector2() {} 
00083         inline Vector2(float x, float y) {elem[0]=x; elem[1]=y;} 
00084         inline Vector2(const float *array): VectorN<2>(array) {} 
00085         inline Vector2(float c): VectorN<2>(c) {} 
00086         inline Vector2(int c): VectorN<2>((float)c) {} 
00087         inline Vector2(const Vector2 &o): VectorN<2>((const float*)&o) {} 
00088         inline Vector2(const VectorN<2> &o): VectorN<2>((const float*)&o) {} 
00089         explicit inline Vector2(const Vector3 &o); 
00090         
00091         //other accessors
00092         inline float x() const { return elem[0]; } 
00093         inline float& x() { return elem[0]; } 
00094         inline float y() const { return elem[1]; } 
00095         inline float& y() { return elem[1]; } 
00096     };
00097 
00098 
00099     // -- square matrix definitions
00100 
00102     struct Matrix4: public MatrixN<4>
00103     {
00104         //useful ctors
00105         inline Matrix4() {} 
00106         inline Matrix4(const float *array): MatrixN<4>(array) {} 
00107         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); 
00108         inline Matrix4(float c): MatrixN<4>(c) {} 
00109         inline Matrix4(int c): MatrixN<4>((float)c) {} 
00110         inline Matrix4(const Matrix4 &o): MatrixN<4>((const float*)&o) {} 
00111         inline Matrix4(const MatrixN<4> &o): MatrixN<4>((const float*)&o) {} 
00112         inline Matrix4(const struct Matrix3 &o); 
00113     };
00114     
00116     struct Matrix3: public MatrixN<3>
00117     {
00118         //useful ctors
00119         inline Matrix3() {} 
00120         inline Matrix3(const float *array): MatrixN<3>(array) {} 
00121         inline Matrix3(float e0,float e1,float e2,float e3,float e4,float e5,float e6,float e7,float e8); 
00122         inline Matrix3(float c): MatrixN<3>(c) {} 
00123         inline Matrix3(int c): MatrixN<3>((float)c) {} 
00124         inline Matrix3(const Matrix3 &o): MatrixN<3>((const float*)&o) {} 
00125         inline Matrix3(const MatrixN<3> &o): MatrixN<3>((const float*)&o) {} 
00126         explicit inline Matrix3(const struct Matrix4 &o); 
00127         inline Matrix3(const struct Matrix2 &o); 
00128     };
00129     
00131     struct Matrix2: public MatrixN<2>
00132     {
00133         //useful ctors
00134         inline Matrix2() {} 
00135         inline Matrix2(const float *array): MatrixN<2>(array) {} 
00136         inline Matrix2(float e0,float e1,float e2,float e3); 
00137         inline Matrix2(float c): MatrixN<2>(c) {} 
00138         inline Matrix2(int c): MatrixN<2>((float)c) {} 
00139         inline Matrix2(const Matrix2 &o): MatrixN<2>((const float*)&o) {} 
00140         inline Matrix2(const MatrixN<2> &o): MatrixN<2>((const float*)&o) {} 
00141         explicit inline Matrix2(const Matrix3 &o); 
00142     };
00143     
00144     
00145     // -- quaternion definition
00146     
00147     //TODO
00148     
00149     
00150     // -- generic utilities
00151     
00152     inline bool NearEqual(float c0, float c1, float tolerance=FLOAT_TOLERANCE); 
00153     template <typename VecType>
00154     inline bool NearEqual(const VecType& v1, const VecType& v2, float tolerance=FLOAT_TOLERANCE); 
00155     template <typename VecType>
00156     inline bool NearEqual(const VecType& v, float c, float tolerance=FLOAT_TOLERANCE); 
00157 
00158     inline bool IsFinite(float c); 
00159     template <typename VecType>
00160     inline bool IsFinite(VecType &v); 
00161 
00162 
00163     // -- Vector functions
00164 
00165     template <typename VecType>
00166     inline float VecDot(const VecType &v1, const VecType &v2); 
00167 
00168     inline struct Vector3 VecCross(const struct Vector3 &v1, const struct Vector3 &v2); 
00169     
00170     template <typename VecType>
00171     inline VecType VecNormal(const VecType &v); 
00172     template <typename VecType>
00173     inline void VecNormalize(VecType &v); 
00174 
00175     template <typename VecType>
00176     inline float VecAngleBetween(const VecType &l1, const VecType &l2); 
00177 
00178     template <typename VecType>
00179     inline VecType VecReflect(const VecType &vec, const VecType &norm); 
00180     
00181     
00182     // -- Matrix functions
00183     
00184     template <typename MatType>
00185     MatType MatMul(const MatType &m1, const MatType &m2); 
00186 
00187     float MatDeterminant(const Matrix4 &mat); 
00188     float MatDeterminant(const Matrix3 &mat); 
00189     inline float MatDeterminant(const Matrix2 &mat); 
00190 
00191     template <typename MatType>
00192     MatType MatTranspose(const MatType &mat); 
00193 
00194     template <typename MatType>
00195     MatType MatInverse(const MatType &mat); 
00196     
00197     inline Matrix3 MatRotateX(float angle); 
00198     inline Matrix3 MatRotateY(float angle); 
00199     inline Matrix3 MatRotateZ(float angle); 
00200     inline Matrix3 MatRotateAxis(const Vector3 &axis, float angle); 
00201     
00202     template <typename VecType>
00203     inline typename VecType::GreaterMatrixType MatTranslate(const VecType &offset); 
00204 
00205     Matrix4 MatProjectionFoV(float fov=PI/2, float aspectYdivX=1.0f, float nearZ=1.0f, float farZ=1000.0f); 
00206 
00207     Matrix4 MatViewLookAt(const Vector3 &pos, const Vector3 &tar, const Vector3 &vagueUp=Vector3(0,0,1), bool flipVert=false, bool flipHorz=false); 
00208     
00209     
00210     // -- Mixed functions
00211     
00212     template <typename VecType>
00213     inline VecType TransformVector(const typename VecType::MatrixType &m, const VecType &v); 
00214     
00215 }; //namespace GEO
00216 
00217 //include template and inline implementations
00218 #ifndef GEO_INCLUDE_INLINE
00219     #define GEO_INCLUDE_INLINE
00220     #include "Geo.cpp"
00221 #endif
00222 
00223 #pragma pack(pop) //undo alignment change
00224 
00225 #endif // GEO_H_INCLUDED

Generated on Wed Feb 13 20:57:04 2008 for MPMA Framework by  doxygen 1.5.4