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) {elements[0]=x; elements[1]=y; elements[2]=z; elements[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 elements[0]; } 
00045         inline float& x() { return elements[0]; } 
00046         inline float y() const { return elements[1]; } 
00047         inline float& y() { return elements[1]; } 
00048         inline float z() const { return elements[2]; } 
00049         inline float& z() { return elements[2]; } 
00050         inline float w() const { return elements[3]; } 
00051         inline float& w() { return elements[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) {elements[0]=x; elements[1]=y; elements[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 elements[0]; } 
00071         inline float& x() { return elements[0]; } 
00072         inline float y() const { return elements[1]; } 
00073         inline float& y() { return elements[1]; } 
00074         inline float z() const { return elements[2]; } 
00075         inline float& z() { return elements[2]; } 
00076     };
00077 
00079     struct Vector2: public VectorN<2>
00080     {
00081         //ctors
00082         inline Vector2() {} 
00083         inline Vector2(float x, float y) {elements[0]=x; elements[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 elements[0]; } 
00093         inline float& x() { return elements[0]; } 
00094         inline float y() const { return elements[1]; } 
00095         inline float& y() { return elements[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 
00115         inline const float* ToGL() const;
00116     private:
00117         static Matrix4 globalGLConversionMatrix;
00118     };
00119     
00121     struct Matrix3: public MatrixN<3>
00122     {
00123         //useful ctors
00124         inline Matrix3() {} 
00125         inline Matrix3(const float *array): MatrixN<3>(array) {} 
00126         inline Matrix3(float e0,float e1,float e2,float e3,float e4,float e5,float e6,float e7,float e8); 
00127         inline Matrix3(float c): MatrixN<3>(c) {} 
00128         inline Matrix3(int c): MatrixN<3>((float)c) {} 
00129         inline Matrix3(const Matrix3 &o): MatrixN<3>((const float*)&o) {} 
00130         inline Matrix3(const MatrixN<3> &o): MatrixN<3>((const float*)&o) {} 
00131         explicit inline Matrix3(const struct Matrix4 &o); 
00132         inline Matrix3(const struct Matrix2 &o); 
00133     };
00134     
00136     struct Matrix2: public MatrixN<2>
00137     {
00138         //useful ctors
00139         inline Matrix2() {} 
00140         inline Matrix2(const float *array): MatrixN<2>(array) {} 
00141         inline Matrix2(float e0,float e1,float e2,float e3); 
00142         inline Matrix2(float c): MatrixN<2>(c) {} 
00143         inline Matrix2(int c): MatrixN<2>((float)c) {} 
00144         inline Matrix2(const Matrix2 &o): MatrixN<2>((const float*)&o) {} 
00145         inline Matrix2(const MatrixN<2> &o): MatrixN<2>((const float*)&o) {} 
00146         explicit inline Matrix2(const Matrix3 &o); 
00147     };
00148     
00149     
00150     // -- quaternion definition
00151     
00152     //TODO
00153     
00154     
00155     // -- generic utilities
00156     
00157     inline bool NearEqual(float c0, float c1, float tolerance=FLOAT_TOLERANCE); 
00158     template <typename VecType>
00159     inline bool NearEqual(const VecType& v1, const VecType& v2, float tolerance=FLOAT_TOLERANCE); 
00160     template <typename VecType>
00161     inline bool NearEqual(const VecType& v, float c, float tolerance=FLOAT_TOLERANCE); 
00162 
00163     inline bool IsFinite(float c); 
00164     template <typename VecType>
00165     inline bool IsFinite(VecType &v); 
00166 
00167 
00168     // -- Vector functions
00169 
00170     template <typename VecType1, typename VecType2>
00171     inline float VecDot(const VecType1 &v1, const VecType2 &v2); 
00172 
00173     inline struct Vector3 VecCross(const struct Vector3 &v1, const struct Vector3 &v2); 
00174     
00175     template <typename VecType>
00176     inline VecType VecNormal(const VecType &v); 
00177     template <typename VecType>
00178     inline void VecNormalize(VecType &v); 
00179 
00180     template <typename VecType1, typename VecType2>
00181     inline float VecAngleBetween(const VecType1 &l1, const VecType2 &l2); 
00182 
00183     template <typename VecType1, typename VecType2>
00184     inline float VecDistance(const VecType1 &l1, const VecType2 &l2); 
00185 
00186     template <typename VecType>
00187     inline float VecLength(const VecType &v); //<!length of a vector
00188     template <typename VecType>
00189     inline float VecLengthSquared(const VecType &v); //<!squared length of a vector
00190 
00191     template <typename VecType1, typename VecType2>
00192     inline VecType1 VecReflect(const VecType1 &vec, const VecType2 &norm); 
00193     
00194     
00195     // -- Matrix functions
00196     
00197     template <typename MatType>
00198     MatType MatMul(const MatType &m1, const MatType &m2); 
00199 
00200     float MatDeterminant(const Matrix4 &mat); 
00201     float MatDeterminant(const Matrix3 &mat); 
00202     inline float MatDeterminant(const Matrix2 &mat); 
00203 
00204     template <typename MatType>
00205     MatType MatTranspose(const MatType &mat); 
00206 
00207     template <typename MatType>
00208     MatType MatInverse(const MatType &mat); 
00209     
00210     inline Matrix3 MatRotateX(float angle); 
00211     inline Matrix3 MatRotateY(float angle); 
00212     inline Matrix3 MatRotateZ(float angle); 
00213     inline Matrix3 MatRotateAxis(const Vector3 &axis, float angle); 
00214     
00215     template <typename VecType>
00216     inline typename VecType::GreaterMatrixType MatTranslate(const VecType &offset); 
00217 
00218     Matrix4 MatProjectionFoV(float fov=PI/2, float aspectYdivX=1.0f, float nearZ=1.0f, float farZ=1000.0f); 
00219     
00220     Matrix4 MatProjectionOrtho(float left, float right, float bottom, float top, float nearZ=1.0f, float farZ=1000.0f); 
00221     inline Matrix4 MatProjectionOrtho(float nearZ=1.0f, float farZ=1000.0f); 
00222 
00223     Matrix4 MatViewLookAt(const Vector3 &pos, const Vector3 &tar, const Vector3 &vagueUp=Vector3(0,0,1), bool flipVert=false, bool flipHorz=false); 
00224     
00225     
00226     // -- Mixed functions
00227     
00228     template <typename VecType>
00229     inline VecType TransformVector(const typename VecType::MatrixType &m, const VecType &v); 
00230     
00231 }; //namespace GEO
00232 
00233 //include template and inline implementations
00234 #ifndef GEO_INCLUDE_INLINE
00235     #define GEO_INCLUDE_INLINE
00236     #include "Geo.cpp"
00237 #endif
00238 
00239 #pragma pack(pop) //undo alignment change
00240 
00241 #endif // GEO_H_INCLUDED

Generated on Sat Aug 9 15:05:05 2008 for MPMA Framework by  doxygen 1.5.6