[10] | 1 | //Copyright and Disclaimer: |
---|
| 2 | //This code is copyright Vienna University of Technology, 2004. |
---|
| 3 | |
---|
| 4 | /* |
---|
| 5 | a few simple mathematical routines; not the most efficient ones, but easy to understand. |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #ifndef MathStuffH |
---|
| 9 | #define MathStuffH |
---|
| 10 | |
---|
| 11 | #include "DataTypes.h" |
---|
| 12 | |
---|
| 13 | //PI |
---|
| 14 | extern const double PI; |
---|
| 15 | //PI/2 |
---|
| 16 | extern const double PI_2; |
---|
| 17 | //PI/180 |
---|
| 18 | extern const double PI_180; |
---|
| 19 | |
---|
| 20 | //vector = (0,0,0) |
---|
| 21 | extern const Vector3 ZERO; |
---|
| 22 | //vector = (1,0,0) |
---|
| 23 | extern const Vector3 UNIT_X; |
---|
| 24 | //vector = (0,1,0) |
---|
| 25 | extern const Vector3 UNIT_Y; |
---|
| 26 | //vector = (0,0,1) |
---|
| 27 | extern const Vector3 UNIT_Z; |
---|
| 28 | |
---|
| 29 | //[1,0,0,0] |
---|
| 30 | //[0,1,0,0] |
---|
| 31 | //[0,0,1,0] |
---|
| 32 | //[0,0,0,1] |
---|
| 33 | extern const Matrix4x4 IDENTITY; |
---|
| 34 | |
---|
| 35 | //absolut value of double |
---|
| 36 | extern double absDouble(const double); |
---|
| 37 | //signum of double |
---|
| 38 | extern double signDouble(const double); |
---|
| 39 | //clamp value in between min and max borders |
---|
| 40 | extern void clamp(double* value, const double min, const double max); |
---|
| 41 | //result = pos+t*dir |
---|
| 42 | extern void linCombVector3(Vector3 result, const Vector3 pos, const Vector3 dir, const double t); |
---|
| 43 | extern double dot(const Vector3, const Vector3); |
---|
| 44 | extern void cross(Vector3, const Vector3, const Vector3); |
---|
| 45 | extern void normalize(Vector3); |
---|
| 46 | extern double squaredLength(const Vector3); |
---|
| 47 | |
---|
| 48 | //[x,0,0,0] |
---|
| 49 | //[0,y,0,0] |
---|
| 50 | //[0,0,z,0] |
---|
| 51 | //[0,0,0,1] |
---|
| 52 | extern void makeScaleMtx(Matrix4x4 output, const double x, const double y, const double z); |
---|
| 53 | //rotation around x axis |
---|
| 54 | extern void makeRotationXMtx(Matrix4x4 output, const double rad); |
---|
| 55 | //rotation around y axis |
---|
| 56 | extern void makeRotationYMtx(Matrix4x4 output, const double rad); |
---|
| 57 | //rotation around z axis |
---|
| 58 | extern void makeRotationZMtx(Matrix4x4 output, const double rad); |
---|
| 59 | //translation |
---|
| 60 | extern void makeTranslationMtx(Matrix4x4 output, Vector3 translation); |
---|
| 61 | |
---|
| 62 | //output = a*b (matrix product) |
---|
| 63 | extern void mult(Matrix4x4 output, const Matrix4x4 a, const Matrix4x4 b); |
---|
| 64 | //output = i^(-1) |
---|
| 65 | extern void invert(Matrix4x4 output, const Matrix4x4 i); |
---|
| 66 | //output = look from position:pos into direction:dir with up-vector:up |
---|
| 67 | extern void look(Matrix4x4 output, const Vector3 pos, const Vector3 dir, const Vector3 up); |
---|
| 68 | //make a scaleTranslate matrix that includes the two values vMin and vMax |
---|
| 69 | extern void scaleTranslateToFit(Matrix4x4 output, const Vector3 vMin, const Vector3 vMax); |
---|
| 70 | //output is initialized with the same result as glPerspective vFovy in degrees |
---|
| 71 | extern void perspectiveDeg(Matrix4x4 output, const double vFovy, const double vAspect, |
---|
| 72 | const double vNearDis, const double vFarDis); |
---|
| 73 | |
---|
| 74 | //calc matrix-vector product; input has assumed homogenous component w = 1 |
---|
| 75 | //before the output is written homogen division is performed (w = 1) |
---|
| 76 | extern void mulHomogenPoint(Vector3 output, const Matrix4x4 m, const Vector3 v); |
---|
| 77 | //min and max are the two extreme points of an AABB containing all the points |
---|
| 78 | extern void calcCubicHull(Vector3 min, Vector3 max, const Vector3* ps, const int size); |
---|
| 79 | //calculates the world coordinates of the view frustum corner points |
---|
| 80 | //input matrix is the (eyeProj*eyeView)^(-1) matrix |
---|
| 81 | extern void calcViewFrustumWorldCoord(Vector3x8, const Matrix4x4); |
---|
| 82 | // mulHomogenPoint each point of VecPoint |
---|
| 83 | extern void transformVecPoint(struct VecPoint* , const Matrix4x4); |
---|
| 84 | // transformVecPoint each VecPoint of Object |
---|
| 85 | extern void transformObject(struct Object*, const Matrix4x4); |
---|
| 86 | //min and max are the two extreme points of an AABB containing all the points of the object |
---|
| 87 | extern void calcObjectCubicHull(Vector3 min, Vector3 max, const struct Object); |
---|
| 88 | |
---|
| 89 | //calculates the six polygons defining an view frustum |
---|
| 90 | extern void calcViewFrustObject(struct Object*, const Vector3x8); |
---|
| 91 | //the given object is clipped by the given AABox; the object is assumed closed |
---|
| 92 | //and is closed after the clipping |
---|
| 93 | extern void clipObjectByAABox(struct Object*, const struct AABox); |
---|
| 94 | //extrudes the object into -lightDir and clippes by the AABox the defining points are returned |
---|
| 95 | extern void includeObjectLightVolume(struct VecPoint* points, const struct Object, |
---|
| 96 | const Vector3 lightDir, const struct AABox sceneAABox); |
---|
| 97 | //calculates the ViewFrustum Object clippes this Object By the sceneAABox and |
---|
| 98 | //extrudes the object into -lightDir and clippes by the AABox the defining points are returned |
---|
| 99 | extern void calcFocusedLightVolumePoints(struct VecPoint* points,const Matrix4x4 invEyeProjView, |
---|
| 100 | const Vector3 lightDir,const struct AABox sceneAABox); |
---|
| 101 | // calculates the index of the nearest vertex in the AAB vertex set |
---|
| 102 | // obtainened by calcAABoxPoints according to the normal vector of a clip plane |
---|
| 103 | extern int calcAABNearestVertexIdx(Vector3 clipPlaneNormal); |
---|
| 104 | // calculates the index of the farthest vertex in the AAB vertex set |
---|
| 105 | // obtainened by calcAABoxPoints according to the normal vector of a clip plane |
---|
| 106 | extern int calcAABFarthestVertexIdx(Vector3 clipPlaneNormal); |
---|
| 107 | |
---|
| 108 | extern double pointPlaneDistance(const struct Plane A, const Vector3 p); |
---|
| 109 | |
---|
| 110 | extern int pointBeforePlane(const struct Plane A, const Vector3 p); |
---|
| 111 | // calculates the view frustum planes of the frustum defined by the eyeProjView matrix |
---|
| 112 | extern void calcViewFrustumPlanes(struct VecPlane* planes, const Matrix4x4 eyeProjView); |
---|
| 113 | // calculates the np-indices of an aab for a set of 6 clipping planes |
---|
| 114 | // the indices are stored in the form (n0, p0, n1, p1, ..., n11, p11) in the array vertexIdx |
---|
| 115 | extern void calcAABNPVertexIndices(int *vertexIndices, const struct VecPlane planes); |
---|
| 116 | // combines two AABs, stores result in a |
---|
| 117 | extern void combineAABoxes(struct AABox *a, const struct AABox b); |
---|
| 118 | // calculates volume of this aab |
---|
| 119 | extern double calcAABoxVolume(const struct AABox aab); |
---|
| 120 | // calculates surface of this aab |
---|
| 121 | double calcAABoxSurface(const struct AABox aab); |
---|
| 122 | // clips box aab by box enclosed |
---|
| 123 | extern void clipAABoxByAABox(struct AABox *aab, const struct AABox enclosed); |
---|
| 124 | // computes world position center of an aab |
---|
| 125 | extern void calcAABoxCenter(Vector3 vec, const struct AABox aab); |
---|
| 126 | |
---|
| 127 | extern void rotateVectorX(Vector3 v, double rad); |
---|
| 128 | extern void rotateVectorY(Vector3 v, double rad); |
---|
| 129 | extern void rotateVectorZ(Vector3 v, double rad); |
---|
| 130 | // rotates vector around axis(must be unit vector) |
---|
| 131 | extern void rotateVector(Vector3 result, const Vector3 input, const float rad, const Vector3 axis); |
---|
| 132 | // generates normal vector of a vector |
---|
| 133 | extern void vectorNormal(Vector3 result, const Vector3 vec); |
---|
| 134 | #endif |
---|