Main Page   Class List   Class Members  

  • Main Page
  • User's Guide
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

sdk/extensions/authoring/source/VHACD/inc/FloatMath.h

Go to the documentation of this file.
00001 #ifndef FLOAT_MATH_LIB_H
00002 
00003 #define FLOAT_MATH_LIB_H
00004 
00005 
00006 #include <float.h>
00007 #include <stdint.h>
00008 
00009 namespace FLOAT_MATH
00010 {
00011 
00012 enum FM_ClipState
00013 {
00014   FMCS_XMIN       = (1<<0),
00015   FMCS_XMAX       = (1<<1),
00016   FMCS_YMIN       = (1<<2),
00017   FMCS_YMAX       = (1<<3),
00018   FMCS_ZMIN       = (1<<4),
00019   FMCS_ZMAX       = (1<<5),
00020 };
00021 
00022 enum FM_Axis
00023 {
00024   FM_XAXIS   = (1<<0),
00025   FM_YAXIS   = (1<<1),
00026   FM_ZAXIS   = (1<<2)
00027 };
00028 
00029 enum LineSegmentType
00030 {
00031   LS_START,
00032   LS_MIDDLE,
00033   LS_END
00034 };
00035 
00036 
00037 const float FM_PI = 3.1415926535897932384626433832795028841971693993751f;
00038 const float FM_DEG_TO_RAD = ((2.0f * FM_PI) / 360.0f);
00039 const float FM_RAD_TO_DEG = (360.0f / (2.0f * FM_PI));
00040 
00041 //***************** Float versions
00042 //***
00043 //*** vectors are assumed to be 3 floats or 3 doubles representing X, Y, Z
00044 //*** quaternions are assumed to be 4 floats or 4 doubles representing X,Y,Z,W
00045 //*** matrices are assumed to be 16 floats or 16 doubles representing a standard D3D or OpenGL style 4x4 matrix
00046 //*** bounding volumes are expressed as two sets of 3 floats/double representing bmin(x,y,z) and bmax(x,y,z)
00047 //*** Plane equations are assumed to be 4 floats or 4 doubles representing Ax,By,Cz,D
00048 
00049 FM_Axis fm_getDominantAxis(const float normal[3]);
00050 FM_Axis fm_getDominantAxis(const double normal[3]);
00051 
00052 void fm_decomposeTransform(const float local_transform[16],float trans[3],float rot[4],float scale[3]);
00053 void fm_decomposeTransform(const double local_transform[16],double trans[3],double rot[4],double scale[3]);
00054 
00055 void  fm_multiplyTransform(const float *pA,const float *pB,float *pM);
00056 void  fm_multiplyTransform(const double *pA,const double *pB,double *pM);
00057 
00058 void  fm_inverseTransform(const float matrix[16],float inverse_matrix[16]);
00059 void  fm_inverseTransform(const double matrix[16],double inverse_matrix[16]);
00060 
00061 void  fm_identity(float matrix[16]); // set 4x4 matrix to identity.
00062 void  fm_identity(double matrix[16]); // set 4x4 matrix to identity.
00063 
00064 void  fm_inverseRT(const float matrix[16], const float pos[3], float t[3]); // inverse rotate translate the point.
00065 void  fm_inverseRT(const double matrix[16],const double pos[3],double t[3]); // inverse rotate translate the point.
00066 
00067 void  fm_transform(const float matrix[16], const float pos[3], float t[3]); // rotate and translate this point.
00068 void  fm_transform(const double matrix[16],const double pos[3],double t[3]); // rotate and translate this point.
00069 
00070 float  fm_getDeterminant(const float matrix[16]);
00071 double fm_getDeterminant(const double matrix[16]);
00072 
00073 void fm_getSubMatrix(int32_t ki,int32_t kj,float pDst[16],const float matrix[16]);
00074 void fm_getSubMatrix(int32_t ki,int32_t kj,double pDst[16],const float matrix[16]);
00075 
00076 void  fm_rotate(const float matrix[16],const float pos[3],float t[3]); // only rotate the point by a 4x4 matrix, don't translate.
00077 void  fm_rotate(const double matri[16],const double pos[3],double t[3]); // only rotate the point by a 4x4 matrix, don't translate.
00078 
00079 void  fm_eulerToMatrix(float ax,float ay,float az,float matrix[16]); // convert euler (in radians) to a dest 4x4 matrix (translation set to zero)
00080 void  fm_eulerToMatrix(double ax,double ay,double az,double matrix[16]); // convert euler (in radians) to a dest 4x4 matrix (translation set to zero)
00081 
00082 void  fm_getAABB(uint32_t vcount,const float *points,uint32_t pstride,float bmin[3],float bmax[3]);
00083 void  fm_getAABB(uint32_t vcount,const double *points,uint32_t pstride,double bmin[3],double bmax[3]);
00084 
00085 void  fm_getAABBCenter(const float bmin[3],const float bmax[3],float center[3]);
00086 void  fm_getAABBCenter(const double bmin[3],const double bmax[3],double center[3]);
00087 
00088 void fm_transformAABB(const float bmin[3],const float bmax[3],const float matrix[16],float tbmin[3],float tbmax[3]);
00089 void fm_transformAABB(const double bmin[3],const double bmax[3],const double matrix[16],double tbmin[3],double tbmax[3]);
00090 
00091 void  fm_eulerToQuat(float x,float y,float z,float quat[4]); // convert euler angles to quaternion.
00092 void  fm_eulerToQuat(double x,double y,double z,double quat[4]); // convert euler angles to quaternion.
00093 
00094 void  fm_quatToEuler(const float quat[4],float &ax,float &ay,float &az);
00095 void  fm_quatToEuler(const double quat[4],double &ax,double &ay,double &az);
00096 
00097 void  fm_eulerToQuat(const float euler[3],float quat[4]); // convert euler angles to quaternion. Angles must be radians not degrees!
00098 void  fm_eulerToQuat(const double euler[3],double quat[4]); // convert euler angles to quaternion.
00099 
00100 void  fm_scale(float x,float y,float z,float matrix[16]); // apply scale to the matrix.
00101 void  fm_scale(double x,double y,double z,double matrix[16]); // apply scale to the matrix.
00102 
00103 void  fm_eulerToQuatDX(float x,float y,float z,float quat[4]); // convert euler angles to quaternion using the fucked up DirectX method
00104 void  fm_eulerToQuatDX(double x,double y,double z,double quat[4]); // convert euler angles to quaternion using the fucked up DirectX method
00105 
00106 void  fm_eulerToMatrixDX(float x,float y,float z,float matrix[16]); // convert euler angles to quaternion using the fucked up DirectX method.
00107 void  fm_eulerToMatrixDX(double x,double y,double z,double matrix[16]); // convert euler angles to quaternion using the fucked up DirectX method.
00108 
00109 void  fm_quatToMatrix(const float quat[4],float matrix[16]); // convert quaterinion rotation to matrix, translation set to zero.
00110 void  fm_quatToMatrix(const double quat[4],double matrix[16]); // convert quaterinion rotation to matrix, translation set to zero.
00111 
00112 void  fm_quatRotate(const float quat[4],const float v[3],float r[3]); // rotate a vector directly by a quaternion.
00113 void  fm_quatRotate(const double quat[4],const double v[3],double r[3]); // rotate a vector directly by a quaternion.
00114 
00115 void  fm_getTranslation(const float matrix[16],float t[3]);
00116 void  fm_getTranslation(const double matrix[16],double t[3]);
00117 
00118 void  fm_setTranslation(const float *translation,float matrix[16]);
00119 void  fm_setTranslation(const double *translation,double matrix[16]);
00120 
00121 void  fm_multiplyQuat(const float *qa,const float *qb,float *quat);
00122 void  fm_multiplyQuat(const double *qa,const double *qb,double *quat);
00123 
00124 void  fm_matrixToQuat(const float matrix[16],float quat[4]); // convert the 3x3 portion of a 4x4 matrix into a quaterion as x,y,z,w
00125 void  fm_matrixToQuat(const double matrix[16],double quat[4]); // convert the 3x3 portion of a 4x4 matrix into a quaterion as x,y,z,w
00126 
00127 float fm_sphereVolume(float radius); // return's the volume of a sphere of this radius (4/3 PI * R cubed )
00128 double fm_sphereVolume(double radius); // return's the volume of a sphere of this radius (4/3 PI * R cubed )
00129 
00130 float fm_cylinderVolume(float radius,float h);
00131 double fm_cylinderVolume(double radius,double h);
00132 
00133 float fm_capsuleVolume(float radius,float h);
00134 double fm_capsuleVolume(double radius,double h);
00135 
00136 float fm_distance(const float p1[3],const float p2[3]);
00137 double fm_distance(const double p1[3],const double p2[3]);
00138 
00139 float fm_distanceSquared(const float p1[3],const float p2[3]);
00140 double fm_distanceSquared(const double p1[3],const double p2[3]);
00141 
00142 float fm_distanceSquaredXZ(const float p1[3],const float p2[3]);
00143 double fm_distanceSquaredXZ(const double p1[3],const double p2[3]);
00144 
00145 float fm_computePlane(const float p1[3],const float p2[3],const float p3[3],float *n); // return D
00146 double fm_computePlane(const double p1[3],const double p2[3],const double p3[3],double *n); // return D
00147 
00148 float fm_distToPlane(const float plane[4],const float pos[3]); // computes the distance of this point from the plane.
00149 double fm_distToPlane(const double plane[4],const double pos[3]); // computes the distance of this point from the plane.
00150 
00151 float fm_dot(const float p1[3],const float p2[3]);
00152 double fm_dot(const double p1[3],const double p2[3]);
00153 
00154 void  fm_cross(float cross[3],const float a[3],const float b[3]);
00155 void  fm_cross(double cross[3],const double a[3],const double b[3]);
00156 
00157 void  fm_computeNormalVector(float n[3],const float p1[3],const float p2[3]); // as P2-P1 normalized.
00158 void  fm_computeNormalVector(double n[3],const double p1[3],const double p2[3]); // as P2-P1 normalized.
00159 
00160 bool  fm_computeWindingOrder(const float p1[3],const float p2[3],const float p3[3]); // returns true if the triangle is clockwise.
00161 bool  fm_computeWindingOrder(const double p1[3],const double p2[3],const double p3[3]); // returns true if the triangle is clockwise.
00162 
00163 float  fm_normalize(float n[3]); // normalize this vector and return the distance
00164 double  fm_normalize(double n[3]); // normalize this vector and return the distance
00165 
00166 float  fm_normalizeQuat(float n[4]); // normalize this quat
00167 double  fm_normalizeQuat(double n[4]); // normalize this quat
00168 
00169 void  fm_matrixMultiply(const float A[16],const float B[16],float dest[16]);
00170 void  fm_matrixMultiply(const double A[16],const double B[16],double dest[16]);
00171 
00172 void  fm_composeTransform(const float position[3],const float quat[4],const float scale[3],float matrix[16]);
00173 void  fm_composeTransform(const double position[3],const double quat[4],const double scale[3],double matrix[16]);
00174 
00175 float fm_computeArea(const float p1[3],const float p2[3],const float p3[3]);
00176 double fm_computeArea(const double p1[3],const double p2[3],const double p3[3]);
00177 
00178 void  fm_lerp(const float p1[3],const float p2[3],float dest[3],float lerpValue);
00179 void  fm_lerp(const double p1[3],const double p2[3],double dest[3],double lerpValue);
00180 
00181 bool  fm_insideTriangleXZ(const float test[3],const float p1[3],const float p2[3],const float p3[3]);
00182 bool  fm_insideTriangleXZ(const double test[3],const double p1[3],const double p2[3],const double p3[3]);
00183 
00184 bool  fm_insideAABB(const float pos[3],const float bmin[3],const float bmax[3]);
00185 bool  fm_insideAABB(const double pos[3],const double bmin[3],const double bmax[3]);
00186 
00187 bool  fm_insideAABB(const float obmin[3],const float obmax[3],const float tbmin[3],const float tbmax[3]); // test if bounding box tbmin/tmbax is fully inside obmin/obmax
00188 bool  fm_insideAABB(const double obmin[3],const double obmax[3],const double tbmin[3],const double tbmax[3]); // test if bounding box tbmin/tmbax is fully inside obmin/obmax
00189 
00190 uint32_t fm_clipTestPoint(const float bmin[3],const float bmax[3],const float pos[3]);
00191 uint32_t fm_clipTestPoint(const double bmin[3],const double bmax[3],const double pos[3]);
00192 
00193 uint32_t fm_clipTestPointXZ(const float bmin[3],const float bmax[3],const float pos[3]); // only tests X and Z, not Y
00194 uint32_t fm_clipTestPointXZ(const double bmin[3],const double bmax[3],const double pos[3]); // only tests X and Z, not Y
00195 
00196 
00197 uint32_t fm_clipTestAABB(const float bmin[3],const float bmax[3],const float p1[3],const float p2[3],const float p3[3],uint32_t &andCode);
00198 uint32_t fm_clipTestAABB(const double bmin[3],const double bmax[3],const double p1[3],const double p2[3],const double p3[3],uint32_t &andCode);
00199 
00200 
00201 bool     fm_lineTestAABBXZ(const float p1[3],const float p2[3],const float bmin[3],const float bmax[3],float &time);
00202 bool     fm_lineTestAABBXZ(const double p1[3],const double p2[3],const double bmin[3],const double bmax[3],double &time);
00203 
00204 bool     fm_lineTestAABB(const float p1[3],const float p2[3],const float bmin[3],const float bmax[3],float &time);
00205 bool     fm_lineTestAABB(const double p1[3],const double p2[3],const double bmin[3],const double bmax[3],double &time);
00206 
00207 
00208 void  fm_initMinMax(const float p[3],float bmin[3],float bmax[3]);
00209 void  fm_initMinMax(const double p[3],double bmin[3],double bmax[3]);
00210 
00211 void  fm_initMinMax(float bmin[3],float bmax[3]);
00212 void  fm_initMinMax(double bmin[3],double bmax[3]);
00213 
00214 void  fm_minmax(const float p[3],float bmin[3],float bmax[3]); // accumulate to a min-max value
00215 void  fm_minmax(const double p[3],double bmin[3],double bmax[3]); // accumulate to a min-max value
00216 
00217 // Computes the diagonal length of the bounding box and then inflates the bounding box on all sides
00218 // by the ratio provided.
00219 void fm_inflateMinMax(float bmin[3], float bmax[3], float ratio);
00220 void fm_inflateMinMax(double bmin[3], double bmax[3], double ratio);
00221 
00222 float fm_solveX(const float plane[4],float y,float z); // solve for X given this plane equation and the other two components.
00223 double fm_solveX(const double plane[4],double y,double z); // solve for X given this plane equation and the other two components.
00224 
00225 float fm_solveY(const float plane[4],float x,float z); // solve for Y given this plane equation and the other two components.
00226 double fm_solveY(const double plane[4],double x,double z); // solve for Y given this plane equation and the other two components.
00227 
00228 float fm_solveZ(const float plane[4],float x,float y); // solve for Z given this plane equation and the other two components.
00229 double fm_solveZ(const double plane[4],double x,double y); // solve for Z given this plane equation and the other two components.
00230 
00231 bool  fm_computeBestFitPlane(uint32_t vcount,     // number of input data points
00232                      const float *points,     // starting address of points array.
00233                      uint32_t vstride,    // stride between input points.
00234                      const float *weights,    // *optional point weighting values.
00235                      uint32_t wstride,    // weight stride for each vertex.
00236                      float plane[4]);
00237 
00238 bool  fm_computeBestFitPlane(uint32_t vcount,     // number of input data points
00239                      const double *points,     // starting address of points array.
00240                      uint32_t vstride,    // stride between input points.
00241                      const double *weights,    // *optional point weighting values.
00242                      uint32_t wstride,    // weight stride for each vertex.
00243                      double plane[4]);
00244 
00245 bool  fm_computeCentroid(uint32_t vcount,     // number of input data points
00246                          const float *points,     // starting address of points array.
00247                          uint32_t vstride,    // stride between input points.
00248                          float *center);
00249 
00250 bool  fm_computeCentroid(uint32_t vcount,     // number of input data points
00251                          const double *points,     // starting address of points array.
00252                          uint32_t vstride,    // stride between input points.
00253                          double *center);
00254 
00255 
00256 float  fm_computeBestFitAABB(uint32_t vcount,const float *points,uint32_t pstride,float bmin[3],float bmax[3]); // returns the diagonal distance
00257 double fm_computeBestFitAABB(uint32_t vcount,const double *points,uint32_t pstride,double bmin[3],double bmax[3]); // returns the diagonal distance
00258 
00259 float  fm_computeBestFitSphere(uint32_t vcount,const float *points,uint32_t pstride,float center[3]);
00260 double  fm_computeBestFitSphere(uint32_t vcount,const double *points,uint32_t pstride,double center[3]);
00261 
00262 bool fm_lineSphereIntersect(const float center[3],float radius,const float p1[3],const float p2[3],float intersect[3]);
00263 bool fm_lineSphereIntersect(const double center[3],double radius,const double p1[3],const double p2[3],double intersect[3]);
00264 
00265 bool fm_intersectRayAABB(const float bmin[3],const float bmax[3],const float pos[3],const float dir[3],float intersect[3]);
00266 bool fm_intersectLineSegmentAABB(const float bmin[3],const float bmax[3],const float p1[3],const float p2[3],float intersect[3]);
00267 
00268 bool fm_lineIntersectsTriangle(const float rayStart[3],const float rayEnd[3],const float p1[3],const float p2[3],const float p3[3],float sect[3]);
00269 bool fm_lineIntersectsTriangle(const double rayStart[3],const double rayEnd[3],const double p1[3],const double p2[3],const double p3[3],double sect[3]);
00270 
00271 bool fm_rayIntersectsTriangle(const float origin[3],const float dir[3],const float v0[3],const float v1[3],const float v2[3],float &t);
00272 bool fm_rayIntersectsTriangle(const double origin[3],const double dir[3],const double v0[3],const double v1[3],const double v2[3],double &t);
00273 
00274 bool fm_raySphereIntersect(const float center[3],float radius,const float pos[3],const float dir[3],float distance,float intersect[3]);
00275 bool fm_raySphereIntersect(const double center[3],double radius,const double pos[3],const double dir[3],double distance,double intersect[3]);
00276 
00277 void fm_catmullRom(float out_vector[3],const float p1[3],const float p2[3],const float p3[3],const float *p4, const float s);
00278 void fm_catmullRom(double out_vector[3],const double p1[3],const double p2[3],const double p3[3],const double *p4, const double s);
00279 
00280 bool fm_intersectAABB(const float bmin1[3],const float bmax1[3],const float bmin2[3],const float bmax2[3]);
00281 bool fm_intersectAABB(const double bmin1[3],const double bmax1[3],const double bmin2[3],const double bmax2[3]);
00282 
00283 
00284 // computes the rotation quaternion to go from unit-vector v0 to unit-vector v1
00285 void fm_rotationArc(const float v0[3],const float v1[3],float quat[4]);
00286 void fm_rotationArc(const double v0[3],const double v1[3],double quat[4]);
00287 
00288 float  fm_distancePointLineSegment(const float Point[3],const float LineStart[3],const float LineEnd[3],float intersection[3],LineSegmentType &type,float epsilon);
00289 double fm_distancePointLineSegment(const double Point[3],const double LineStart[3],const double LineEnd[3],double intersection[3],LineSegmentType &type,double epsilon);
00290 
00291 
00292 bool fm_colinear(const double p1[3],const double p2[3],const double p3[3],double epsilon=0.999);               // true if these three points in a row are co-linear
00293 bool fm_colinear(const float  p1[3],const float  p2[3],const float p3[3],float epsilon=0.999f);
00294 
00295 bool fm_colinear(const float a1[3],const float a2[3],const float b1[3],const float b2[3],float epsilon=0.999f);  // true if these two line segments are co-linear.
00296 bool fm_colinear(const double a1[3],const double a2[3],const double b1[3],const double b2[3],double epsilon=0.999);  // true if these two line segments are co-linear.
00297 
00298 enum IntersectResult
00299 {
00300   IR_DONT_INTERSECT,
00301   IR_DO_INTERSECT,
00302   IR_COINCIDENT,
00303   IR_PARALLEL,
00304 };
00305 
00306 IntersectResult fm_intersectLineSegments2d(const float a1[3], const float a2[3], const float b1[3], const float b2[3], float intersectionPoint[3]);
00307 IntersectResult fm_intersectLineSegments2d(const double a1[3],const double a2[3],const double b1[3],const double b2[3],double intersectionPoint[3]);
00308 
00309 IntersectResult fm_intersectLineSegments2dTime(const float a1[3], const float a2[3], const float b1[3], const float b2[3],float &t1,float &t2);
00310 IntersectResult fm_intersectLineSegments2dTime(const double a1[3],const double a2[3],const double b1[3],const double b2[3],double &t1,double &t2);
00311 
00312 // Plane-Triangle splitting
00313 
00314 enum PlaneTriResult
00315 {
00316   PTR_ON_PLANE,
00317   PTR_FRONT,
00318   PTR_BACK,
00319   PTR_SPLIT,
00320 };
00321 
00322 PlaneTriResult fm_planeTriIntersection(const float plane[4],    // the plane equation in Ax+By+Cz+D format
00323                                     const float *triangle, // the source triangle.
00324                                     uint32_t tstride,  // stride in bytes of the input and output *vertices*
00325                                     float        epsilon,  // the co-planer epsilon value.
00326                                     float       *front,    // the triangle in front of the
00327                                     uint32_t &fcount,  // number of vertices in the 'front' triangle
00328                                     float       *back,     // the triangle in back of the plane
00329                                     uint32_t &bcount); // the number of vertices in the 'back' triangle.
00330 
00331 
00332 PlaneTriResult fm_planeTriIntersection(const double plane[4],    // the plane equation in Ax+By+Cz+D format
00333                                     const double *triangle, // the source triangle.
00334                                     uint32_t tstride,  // stride in bytes of the input and output *vertices*
00335                                     double        epsilon,  // the co-planer epsilon value.
00336                                     double       *front,    // the triangle in front of the
00337                                     uint32_t &fcount,  // number of vertices in the 'front' triangle
00338                                     double       *back,     // the triangle in back of the plane
00339                                     uint32_t &bcount); // the number of vertices in the 'back' triangle.
00340 
00341 
00342 void fm_intersectPointPlane(const float p1[3],const float p2[3],float *split,const float plane[4]);
00343 void fm_intersectPointPlane(const double p1[3],const double p2[3],double *split,const double plane[4]);
00344 
00345 PlaneTriResult fm_getSidePlane(const float p[3],const float plane[4],float epsilon);
00346 PlaneTriResult fm_getSidePlane(const double p[3],const double plane[4],double epsilon);
00347 
00348 
00349 void fm_computeBestFitOBB(uint32_t vcount,const float *points,uint32_t pstride,float *sides,float matrix[16],bool bruteForce=true);
00350 void fm_computeBestFitOBB(uint32_t vcount,const double *points,uint32_t pstride,double *sides,double matrix[16],bool bruteForce=true);
00351 
00352 void fm_computeBestFitOBB(uint32_t vcount,const float *points,uint32_t pstride,float *sides,float pos[3],float quat[4],bool bruteForce=true);
00353 void fm_computeBestFitOBB(uint32_t vcount,const double *points,uint32_t pstride,double *sides,double pos[3],double quat[4],bool bruteForce=true);
00354 
00355 void fm_computeBestFitABB(uint32_t vcount,const float *points,uint32_t pstride,float *sides,float pos[3]);
00356 void fm_computeBestFitABB(uint32_t vcount,const double *points,uint32_t pstride,double *sides,double pos[3]);
00357 
00358 
00359 //** Note, if the returned capsule height is less than zero, then you must represent it is a sphere of size radius.
00360 void fm_computeBestFitCapsule(uint32_t vcount,const float *points,uint32_t pstride,float &radius,float &height,float matrix[16],bool bruteForce=true);
00361 void fm_computeBestFitCapsule(uint32_t vcount,const double *points,uint32_t pstride,float &radius,float &height,double matrix[16],bool bruteForce=true);
00362 
00363 
00364 void fm_planeToMatrix(const float plane[4],float matrix[16]); // convert a plane equation to a 4x4 rotation matrix.  Reference vector is 0,1,0
00365 void fm_planeToQuat(const float plane[4],float quat[4],float pos[3]); // convert a plane equation to a quaternion and translation
00366 
00367 void fm_planeToMatrix(const double plane[4],double matrix[16]); // convert a plane equation to a 4x4 rotation matrix
00368 void fm_planeToQuat(const double plane[4],double quat[4],double pos[3]); // convert a plane equation to a quaternion and translation
00369 
00370 inline void fm_doubleToFloat3(const double p[3],float t[3]) { t[0] = (float) p[0]; t[1] = (float)p[1]; t[2] = (float)p[2]; };
00371 inline void fm_floatToDouble3(const float p[3],double t[3]) { t[0] = (double)p[0]; t[1] = (double)p[1]; t[2] = (double)p[2]; };
00372 
00373 
00374 void  fm_eulerMatrix(float ax,float ay,float az,float matrix[16]); // convert euler (in radians) to a dest 4x4 matrix (translation set to zero)
00375 void  fm_eulerMatrix(double ax,double ay,double az,double matrix[16]); // convert euler (in radians) to a dest 4x4 matrix (translation set to zero)
00376 
00377 
00378 float  fm_computeMeshVolume(const float *vertices,uint32_t tcount,const uint32_t *indices);
00379 double fm_computeMeshVolume(const double *vertices,uint32_t tcount,const uint32_t *indices);
00380 
00381 
00382 #define FM_DEFAULT_GRANULARITY 0.001f  // 1 millimeter is the default granularity
00383 
00384 class fm_VertexIndex
00385 {
00386 public:
00387   virtual uint32_t          getIndex(const float pos[3],bool &newPos) = 0;  // get welded index for this float vector[3]
00388   virtual uint32_t          getIndex(const double pos[3],bool &newPos) = 0;  // get welded index for this double vector[3]
00389   virtual const float *   getVerticesFloat(void) const = 0;
00390   virtual const double *  getVerticesDouble(void) const = 0;
00391   virtual const float *   getVertexFloat(uint32_t index) const = 0;
00392   virtual const double *  getVertexDouble(uint32_t index) const = 0;
00393   virtual uint32_t          getVcount(void) const = 0;
00394   virtual bool            isDouble(void) const = 0;
00395   virtual bool            saveAsObj(const char *fname,uint32_t tcount,uint32_t *indices) = 0;
00396 };
00397 
00398 fm_VertexIndex * fm_createVertexIndex(double granularity,bool snapToGrid); // create an indexed vertex system for doubles
00399 fm_VertexIndex * fm_createVertexIndex(float granularity,bool snapToGrid);  // create an indexed vertext system for floats
00400 void             fm_releaseVertexIndex(fm_VertexIndex *vindex);
00401 
00402 
00403 class fm_Triangulate
00404 {
00405 public:
00406   virtual const double *       triangulate3d(uint32_t pcount,
00407                                              const double *points,
00408                                              uint32_t vstride,
00409                                              uint32_t &tcount,
00410                                              bool consolidate,
00411                                              double epsilon) = 0;
00412 
00413   virtual const float  *       triangulate3d(uint32_t pcount,
00414                                              const float  *points,
00415                                              uint32_t vstride,
00416                                              uint32_t &tcount,
00417                                              bool consolidate,
00418                                              float epsilon) = 0;
00419 };
00420 
00421 fm_Triangulate * fm_createTriangulate(void);
00422 void             fm_releaseTriangulate(fm_Triangulate *t);
00423 
00424 
00425 const float * fm_getPoint(const float *points,uint32_t pstride,uint32_t index);
00426 const double * fm_getPoint(const double *points,uint32_t pstride,uint32_t index);
00427 
00428 bool   fm_insideTriangle(float Ax, float Ay,float Bx, float By,float Cx, float Cy,float Px, float Py);
00429 bool   fm_insideTriangle(double Ax, double Ay,double Bx, double By,double Cx, double Cy,double Px, double Py);
00430 float  fm_areaPolygon2d(uint32_t pcount,const float *points,uint32_t pstride);
00431 double fm_areaPolygon2d(uint32_t pcount,const double *points,uint32_t pstride);
00432 
00433 bool  fm_pointInsidePolygon2d(uint32_t pcount,const float *points,uint32_t pstride,const float *point,uint32_t xindex=0,uint32_t yindex=1);
00434 bool  fm_pointInsidePolygon2d(uint32_t pcount,const double *points,uint32_t pstride,const double *point,uint32_t xindex=0,uint32_t yindex=1);
00435 
00436 uint32_t fm_consolidatePolygon(uint32_t pcount,const float *points,uint32_t pstride,float *dest,float epsilon=0.999999f); // collapses co-linear edges.
00437 uint32_t fm_consolidatePolygon(uint32_t pcount,const double *points,uint32_t pstride,double *dest,double epsilon=0.999999); // collapses co-linear edges.
00438 
00439 
00440 bool fm_computeSplitPlane(uint32_t vcount,const double *vertices,uint32_t tcount,const uint32_t *indices,double *plane);
00441 bool fm_computeSplitPlane(uint32_t vcount,const float *vertices,uint32_t tcount,const uint32_t *indices,float *plane);
00442 
00443 void fm_nearestPointInTriangle(const float *pos,const float *p1,const float *p2,const float *p3,float *nearest);
00444 void fm_nearestPointInTriangle(const double *pos,const double *p1,const double *p2,const double *p3,double *nearest);
00445 
00446 float  fm_areaTriangle(const float *p1,const float *p2,const float *p3);
00447 double fm_areaTriangle(const double *p1,const double *p2,const double *p3);
00448 
00449 void fm_subtract(const float *A,const float *B,float *diff); // compute A-B and store the result in 'diff'
00450 void fm_subtract(const double *A,const double *B,double *diff); // compute A-B and store the result in 'diff'
00451 
00452 void fm_multiply(float *A,float scaler);
00453 void fm_multiply(double *A,double scaler);
00454 
00455 void fm_add(const float *A,const float *B,float *sum);
00456 void fm_add(const double *A,const double *B,double *sum);
00457 
00458 void fm_copy3(const float *source,float *dest);
00459 void fm_copy3(const double *source,double *dest);
00460 
00461 // re-indexes an indexed triangle mesh but drops unused vertices.  The output_indices can be the same pointer as the input indices.
00462 // the output_vertices can point to the input vertices if you desire.  The output_vertices buffer should be at least the same size
00463 // is the input buffer.  The routine returns the new vertex count after re-indexing.
00464 uint32_t  fm_copyUniqueVertices(uint32_t vcount,const float *input_vertices,float *output_vertices,uint32_t tcount,const uint32_t *input_indices,uint32_t *output_indices);
00465 uint32_t  fm_copyUniqueVertices(uint32_t vcount,const double *input_vertices,double *output_vertices,uint32_t tcount,const uint32_t *input_indices,uint32_t *output_indices);
00466 
00467 bool    fm_isMeshCoplanar(uint32_t tcount,const uint32_t *indices,const float *vertices,bool doubleSided); // returns true if this collection of indexed triangles are co-planar!
00468 bool    fm_isMeshCoplanar(uint32_t tcount,const uint32_t *indices,const double *vertices,bool doubleSided); // returns true if this collection of indexed triangles are co-planar!
00469 
00470 bool    fm_samePlane(const float p1[4],const float p2[4],float normalEpsilon=0.01f,float dEpsilon=0.001f,bool doubleSided=false); // returns true if these two plane equations are identical within an epsilon
00471 bool    fm_samePlane(const double p1[4],const double p2[4],double normalEpsilon=0.01,double dEpsilon=0.001,bool doubleSided=false);
00472 
00473 void    fm_OBBtoAABB(const float obmin[3],const float obmax[3],const float matrix[16],float abmin[3],float abmax[3]);
00474 
00475 // a utility class that will tessellate a mesh.
00476 class fm_Tesselate
00477 {
00478 public:
00479   virtual const uint32_t * tesselate(fm_VertexIndex *vindex,uint32_t tcount,const uint32_t *indices,float longEdge,uint32_t maxDepth,uint32_t &outcount) = 0;
00480 };
00481 
00482 fm_Tesselate * fm_createTesselate(void);
00483 void           fm_releaseTesselate(fm_Tesselate *t);
00484 
00485 void fm_computeMeanNormals(uint32_t vcount,       // the number of vertices
00486                            const float *vertices,     // the base address of the vertex position data.
00487                            uint32_t vstride,      // the stride between position data.
00488                            float *normals,            // the base address  of the destination for mean vector normals
00489                            uint32_t nstride,      // the stride between normals
00490                            uint32_t tcount,       // the number of triangles
00491                            const uint32_t *indices);     // the triangle indices
00492 
00493 void fm_computeMeanNormals(uint32_t vcount,       // the number of vertices
00494                            const double *vertices,     // the base address of the vertex position data.
00495                            uint32_t vstride,      // the stride between position data.
00496                            double *normals,            // the base address  of the destination for mean vector normals
00497                            uint32_t nstride,      // the stride between normals
00498                            uint32_t tcount,       // the number of triangles
00499                            const uint32_t *indices);     // the triangle indices
00500 
00501 
00502 bool fm_isValidTriangle(const float *p1,const float *p2,const float *p3,float epsilon=0.00001f);
00503 bool fm_isValidTriangle(const double *p1,const double *p2,const double *p3,double epsilon=0.00001f);
00504 
00505 }; // end of namespace
00506 
00507 #endif
Copyright © 2015-2017 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com