home *** CD-ROM | disk | FTP | other *** search
Wrap
#ifndef __vectormath_h__ #define __vectormath_h__ /*! \file vectormath.h \author Karsten Schwenk \brief This file conatins some often used (fairly simple) vector analysis routines. Most functions in this file come in versions for two, three and four dimensions, following the naming scheme \c functionameXd , where X can be 2, 3 or 4. (E.g. \c vectorInit2d, \c vectorInit3d, ...) All function names start with \c vector... . Functions that return a vector do so by filling in their \b last argument; passing in one of the other arguments here is save (e.g. \c vectorAdd3d(a,b,a) ). Most of them will also return a pointer to that vector to enable nesting of functions. Since the dimension is the only difference between those functions I will only document the version for the \b lowest dimension. So if you are searching for \c vectorDotP4d and find it undocumented, look at \c vectorDotP2d. \see matrixmath.h */ #include <float.h> #define PI 3.141592654f #define DEG(x) ( (x)*(180.0f/PI) ) #define RAD(x) ( (x)*(PI/180.0f) ) #define DOT_PRODUCT_3D(a,b) ( ((a)[0]*(b)[0]) + ((a)[1]*(b)[1]) + ((a)[2]*(b)[2]) ) /* #define vectorInit(x,y,z,v) vectorInit3d(x,y,z,v) #define vectorCopy(a,b) vectorCopy3d(a,b) #define vectorAdd(a,b,c) vectorAdd3d(a,b,c) #define vectorSub(a,b,c) vectorSub3d(a,b,c) #define vectorDotP(a,b,c) vectorDotP3d(a,b,c) #define vectorCrossP(a,b,c) vectorCrossP3d(a,b,c) #define vectorScale(s,a,b) vectorScaleP3d(s,a,b) #define vectorNormalize(a,b) vectorNormalize3d(a,b) #define vectorLength(a) vectorLength3d(a) #define vectorDistance(a,b) vectorDistance3d(a,b) */ typedef float vecbase_t; //!< our vecbase type (could although bee switched to \c double) typedef vecbase_t vec2_t[2]; //!< a two dimensinal vector (not more than an array of two \c floats) typedef vecbase_t vec3_t[3]; //!< a three dimensional vector typedef vecbase_t vec4_t[4]; //!< a four dimensional vector extern vec4_t e1; extern vec4_t e2; extern vec4_t e3; extern vec4_t e4; //! These functions initialize a vector with values void vectorInit2d(vecbase_t x, vecbase_t y, vec2_t v); void vectorInit3d(vecbase_t x, vecbase_t y, vecbase_t z, vec3_t v); void vectorInit4d(vecbase_t x, vecbase_t y, vecbase_t z, vecbase_t w, vec4_t v); //! These functions copy the values of vector source into destination void vectorCopy2d(vec2_t source, vec2_t destination); void vectorCopy3d(vec3_t source, vec3_t destination); void vectorCopy4d(vec4_t source, vec4_t destination); //! These functions add vector b to a (component-wise) and put the result in c. vecbase_t* vectorAdd2d(vec2_t a, vec2_t b, vec2_t c); vecbase_t* vectorAdd3d(vec3_t a, vec3_t b, vec3_t c); vecbase_t* vectorAdd4d(vec4_t a, vec4_t b, vec4_t c); //! These functions substract vector b from a (component-wise) and put the result in c. vecbase_t* vectorSub2d(vec2_t a, vec2_t b, vec2_t c); vecbase_t* vectorSub3d(vec3_t a, vec3_t b, vec3_t c); vecbase_t* vectorSub4d(vec4_t a, vec4_t b, vec4_t c); //! These functions return the dot product (i.e. inner product) of a and b. vecbase_t vectorDotP2d(vec2_t a, vec2_t b); vecbase_t vectorDotP3d(vec3_t a, vec3_t b); vecbase_t vectorDotP4d(vec4_t a, vec4_t b); //! This function calculates the cross product (i.e. outer product) of a and b and puts it in c. vecbase_t* vectorCrossP3d(vec3_t a, vec3_t b, vec3_t c); //! These functions scale (i.e. multiply) vector a by the scalar s and put the resulting vector in b. vecbase_t* vectorScale2d(vecbase_t s, vec2_t a, vec2_t b); vecbase_t* vectorScale3d(vecbase_t s, vec3_t a, vec3_t b); vecbase_t* vectorScale4d(vecbase_t s, vec4_t a, vec4_t b); //! These functions calculate c=a+sb. vecbase_t* vectorMA2d(vec2_t a, vecbase_t s, vec2_t b, vec2_t c); //c=a+sb vecbase_t* vectorMA3d(vec3_t a, vecbase_t s, vec3_t b, vec3_t c); //c=a+sb vecbase_t* vectorMA4d(vec4_t a, vecbase_t s, vec4_t b, vec4_t c); //c=a+sb //! These functions calculate c=ua+vb (i.e. a linear combination of a and b). vecbase_t* vectorLinCombi2d(vecbase_t u, vec2_t a, vecbase_t v, vec2_t b, vec2_t c); // c=u*a+v*b vecbase_t* vectorLinCombi3d(vecbase_t u, vec3_t a, vecbase_t v, vec3_t b, vec3_t c); // c=u*a+v*b vecbase_t* vectorLinCombi4d(vecbase_t u, vec4_t a, vecbase_t v, vec4_t b, vec4_t c); // c=u*a+v*b //! These functions return the magnitude of v. vecbase_t vectorLength2d(vec2_t v); vecbase_t vectorLength3d(vec3_t v); vecbase_t vectorLength4d(vec4_t v); //! These functions return the squared magnitude of v (saves the square root). vecbase_t vectorLengthSquared2d(vec2_t v); vecbase_t vectorLengthSquared3d(vec3_t v); vecbase_t vectorLengthSquared4d(vec4_t v); //! These functions normalize vector a and put the result in b. vecbase_t* vectorNormalize2d(vec2_t a, vec2_t b); vecbase_t* vectorNormalize3d(vec3_t a, vec3_t b); vecbase_t* vectorNormalize4d(vec4_t a, vec4_t b); vecbase_t* vectorNormalizeFast2d(vec2_t a, vec2_t b); vecbase_t* vectorNormalizeFast3d(vec3_t a, vec3_t b); vecbase_t* vectorNormalizeFast4d(vec4_t a, vec4_t b); //! These functions return the distance from point a to point b. vecbase_t vectorPointDistance2d(vec2_t a, vec2_t b); vecbase_t vectorPointDistance3d(vec3_t a, vec3_t b); vecbase_t vectorPointDistance4d(vec4_t a, vec4_t b); //! These functions return the squared distance from point a to point b. vecbase_t vectorPointDistanceSquared2d(vec2_t a, vec2_t b); vecbase_t vectorPointDistanceSquared3d(vec3_t a, vec3_t b); vecbase_t vectorPointDistanceSquared4d(vec4_t a, vec4_t b); //! These functions return the angle between a and b vecbase_t vectorAngle2d(vec2_t a, vec2_t b); vecbase_t vectorAngle3d(vec3_t a, vec3_t b); vecbase_t vectorAngle4d(vec4_t a, vec4_t b); //! These functions check whether or not a and b are equal (WITH AN EPSILON CHECK!). bool vectorEqual2d(vec2_t a, vec2_t b); bool vectorEqual3d(vec3_t a, vec3_t b); bool vectorEqual4d(vec4_t a, vec4_t b); //! These functions check whether or not a and b are equal (WITH AN EXACT CHECK!). bool vectorEqualExact2d(vec2_t a, vec2_t b); bool vectorEqualExact3d(vec3_t a, vec3_t b); bool vectorEqualExact4d(vec4_t a, vec4_t b); //! These functions check whether or not a and b are linearly dependent. bool vectorLinDep2d(vec2_t a, vec2_t b); bool vectorLinDep3d(vec3_t a, vec3_t b); bool vectorLinDep4d(vec4_t a, vec4_t b); //! This function rotates vec angle RADIANS around normal and puts the result in ret vecbase_t* vectorRotateAroundNormal3d(vec3_t vec, vec3_t normal, vecbase_t angle, vec3_t ret); //! This function rotates vec angle RADIANS in direction dir and puts the result in ret vecbase_t* vectorRotateInDir3d(vec3_t vec, vec3_t dir, vecbase_t angle, vec3_t ret); #endif /* __vectormath_h__ */