00001 #include "meshvolume.h"
00002
00058 namespace ConvexDecomposition
00059 {
00060
00061
00062 inline double det(const double *p1,const double *p2,const double *p3)
00063 {
00064 return p1[0]*p2[1]*p3[2] + p2[0]*p3[1]*p1[2] + p3[0]*p1[1]*p2[2] -p1[0]*p3[1]*p2[2] - p2[0]*p1[1]*p3[2] - p3[0]*p2[1]*p1[2];
00065 }
00066
00067 double computeMeshVolume(const double *vertices,unsigned int tcount,const unsigned int *indices)
00068 {
00069 double volume = 0;
00070
00071 const double *p0 = vertices;
00072 for (unsigned int i=0; i<tcount; i++,indices+=3)
00073 {
00074
00075 const double *p1 = &vertices[ indices[0]*3 ];
00076 const double *p2 = &vertices[ indices[1]*3 ];
00077 const double *p3 = &vertices[ indices[2]*3 ];
00078
00079 volume+=det(p1,p2,p3);
00080 }
00081
00082 volume*=(1.0f/6.0f);
00083 if ( volume < 0 )
00084 volume*=-1;
00085 return volume;
00086 }
00087
00088
00089 inline void CrossProduct(const double *a,const double *b,double *cross)
00090 {
00091 cross[0] = a[1]*b[2] - a[2]*b[1];
00092 cross[1] = a[2]*b[0] - a[0]*b[2];
00093 cross[2] = a[0]*b[1] - a[1]*b[0];
00094 }
00095
00096 inline double DotProduct(const double *a,const double *b)
00097 {
00098 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
00099 }
00100
00101 inline double tetVolume(const double *p0,const double *p1,const double *p2,const double *p3)
00102 {
00103 double a[3];
00104 double b[3];
00105 double c[3];
00106
00107 a[0] = p1[0] - p0[0];
00108 a[1] = p1[1] - p0[1];
00109 a[2] = p1[2] - p0[2];
00110
00111 b[0] = p2[0] - p0[0];
00112 b[1] = p2[1] - p0[1];
00113 b[2] = p2[2] - p0[2];
00114
00115 c[0] = p3[0] - p0[0];
00116 c[1] = p3[1] - p0[1];
00117 c[2] = p3[2] - p0[2];
00118
00119 double cross[3];
00120
00121 CrossProduct( b, c, cross );
00122
00123 double volume = DotProduct( a, cross );
00124
00125 if ( volume < 0 )
00126 return -volume;
00127
00128 return volume;
00129 }
00130
00131 inline double det(const double *p0,const double *p1,const double *p2,const double *p3)
00132 {
00133 return p1[0]*p2[1]*p3[2] + p2[0]*p3[1]*p1[2] + p3[0]*p1[1]*p2[2] -p1[0]*p3[1]*p2[2] - p2[0]*p1[1]*p3[2] - p3[0]*p2[1]*p1[2];
00134 }
00135
00136 double computeMeshVolume2(const double *vertices,unsigned int tcount,const unsigned int *indices)
00137 {
00138 double volume = 0;
00139
00140 const double *p0 = vertices;
00141 for (unsigned int i=0; i<tcount; i++,indices+=3)
00142 {
00143
00144 const double *p1 = &vertices[ indices[0]*3 ];
00145 const double *p2 = &vertices[ indices[1]*3 ];
00146 const double *p3 = &vertices[ indices[2]*3 ];
00147
00148 volume+=tetVolume(p0,p1,p2,p3);
00149 }
00150
00151 return volume * (1.0f / 6.0f );
00152 }
00153
00154
00155
00156
00157 inline float det(const float *p1,const float *p2,const float *p3)
00158 {
00159 return p1[0]*p2[1]*p3[2] + p2[0]*p3[1]*p1[2] + p3[0]*p1[1]*p2[2] -p1[0]*p3[1]*p2[2] - p2[0]*p1[1]*p3[2] - p3[0]*p2[1]*p1[2];
00160 }
00161
00162 float computeMeshVolume(const float *vertices,unsigned int tcount,const unsigned int *indices)
00163 {
00164 float volume = 0;
00165
00166 const float *p0 = vertices;
00167 for (unsigned int i=0; i<tcount; i++,indices+=3)
00168 {
00169
00170 const float *p1 = &vertices[ indices[0]*3 ];
00171 const float *p2 = &vertices[ indices[1]*3 ];
00172 const float *p3 = &vertices[ indices[2]*3 ];
00173
00174 volume+=det(p1,p2,p3);
00175 }
00176
00177 volume*=(1.0f/6.0f);
00178 if ( volume < 0 )
00179 volume*=-1;
00180 return volume;
00181 }
00182
00183
00184 inline void CrossProduct(const float *a,const float *b,float *cross)
00185 {
00186 cross[0] = a[1]*b[2] - a[2]*b[1];
00187 cross[1] = a[2]*b[0] - a[0]*b[2];
00188 cross[2] = a[0]*b[1] - a[1]*b[0];
00189 }
00190
00191 inline float DotProduct(const float *a,const float *b)
00192 {
00193 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
00194 }
00195
00196 inline float tetVolume(const float *p0,const float *p1,const float *p2,const float *p3)
00197 {
00198 float a[3];
00199 float b[3];
00200 float c[3];
00201
00202 a[0] = p1[0] - p0[0];
00203 a[1] = p1[1] - p0[1];
00204 a[2] = p1[2] - p0[2];
00205
00206 b[0] = p2[0] - p0[0];
00207 b[1] = p2[1] - p0[1];
00208 b[2] = p2[2] - p0[2];
00209
00210 c[0] = p3[0] - p0[0];
00211 c[1] = p3[1] - p0[1];
00212 c[2] = p3[2] - p0[2];
00213
00214 float cross[3];
00215
00216 CrossProduct( b, c, cross );
00217
00218 float volume = DotProduct( a, cross );
00219
00220 if ( volume < 0 )
00221 return -volume;
00222
00223 return volume;
00224 }
00225
00226 inline float det(const float *p0,const float *p1,const float *p2,const float *p3)
00227 {
00228 return p1[0]*p2[1]*p3[2] + p2[0]*p3[1]*p1[2] + p3[0]*p1[1]*p2[2] -p1[0]*p3[1]*p2[2] - p2[0]*p1[1]*p3[2] - p3[0]*p2[1]*p1[2];
00229 }
00230
00231 float computeMeshVolume2(const float *vertices,unsigned int tcount,const unsigned int *indices)
00232 {
00233 float volume = 0;
00234
00235 const float *p0 = vertices;
00236 for (unsigned int i=0; i<tcount; i++,indices+=3)
00237 {
00238
00239 const float *p1 = &vertices[ indices[0]*3 ];
00240 const float *p2 = &vertices[ indices[1]*3 ];
00241 const float *p3 = &vertices[ indices[2]*3 ];
00242
00243 volume+=tetVolume(p0,p1,p2,p3);
00244 }
00245
00246 return volume * (1.0f / 6.0f );
00247 }
00248
00249
00250 };
00251