meshvolume.cpp
Go to the documentation of this file.
1 #include "meshvolume.h"
2 
58 namespace ConvexDecomposition
59 {
60 
61 
62 inline double det(const double *p1,const double *p2,const double *p3)
63 {
64  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];
65 }
66 
67 double computeMeshVolume(const double *vertices,unsigned int tcount,const unsigned int *indices)
68 {
69  double volume = 0;
70 
71  const double *p0 = vertices;
72  for (unsigned int i=0; i<tcount; i++,indices+=3)
73  {
74 
75  const double *p1 = &vertices[ indices[0]*3 ];
76  const double *p2 = &vertices[ indices[1]*3 ];
77  const double *p3 = &vertices[ indices[2]*3 ];
78 
79  volume+=det(p1,p2,p3); // compute the volume of the tetrahedran relative to the origin.
80  }
81 
82  volume*=(1.0f/6.0f);
83  if ( volume < 0 )
84  volume*=-1;
85  return volume;
86 }
87 
88 
89 inline void CrossProduct(const double *a,const double *b,double *cross)
90 {
91  cross[0] = a[1]*b[2] - a[2]*b[1];
92  cross[1] = a[2]*b[0] - a[0]*b[2];
93  cross[2] = a[0]*b[1] - a[1]*b[0];
94 }
95 
96 inline double DotProduct(const double *a,const double *b)
97 {
98  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
99 }
100 
101 inline double tetVolume(const double *p0,const double *p1,const double *p2,const double *p3)
102 {
103  double a[3];
104  double b[3];
105  double c[3];
106 
107  a[0] = p1[0] - p0[0];
108  a[1] = p1[1] - p0[1];
109  a[2] = p1[2] - p0[2];
110 
111  b[0] = p2[0] - p0[0];
112  b[1] = p2[1] - p0[1];
113  b[2] = p2[2] - p0[2];
114 
115  c[0] = p3[0] - p0[0];
116  c[1] = p3[1] - p0[1];
117  c[2] = p3[2] - p0[2];
118 
119  double cross[3];
120 
121  CrossProduct( b, c, cross );
122 
123  double volume = DotProduct( a, cross );
124 
125  if ( volume < 0 )
126  return -volume;
127 
128  return volume;
129 }
130 
131 inline double det(const double *p0,const double *p1,const double *p2,const double *p3)
132 {
133  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];
134 }
135 
136 double computeMeshVolume2(const double *vertices,unsigned int tcount,const unsigned int *indices)
137 {
138  double volume = 0;
139 
140  const double *p0 = vertices;
141  for (unsigned int i=0; i<tcount; i++,indices+=3)
142  {
143 
144  const double *p1 = &vertices[ indices[0]*3 ];
145  const double *p2 = &vertices[ indices[1]*3 ];
146  const double *p3 = &vertices[ indices[2]*3 ];
147 
148  volume+=tetVolume(p0,p1,p2,p3); // compute the volume of the tetrahdren relative to the root vertice
149  }
150 
151  return volume * (1.0f / 6.0f );
152 }
153 
154 
155 //** Float versions
156 
157 inline float det(const float *p1,const float *p2,const float *p3)
158 {
159  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];
160 }
161 
162 float computeMeshVolume(const float *vertices,unsigned int tcount,const unsigned int *indices)
163 {
164  float volume = 0;
165 
166  const float *p0 = vertices;
167  for (unsigned int i=0; i<tcount; i++,indices+=3)
168  {
169 
170  const float *p1 = &vertices[ indices[0]*3 ];
171  const float *p2 = &vertices[ indices[1]*3 ];
172  const float *p3 = &vertices[ indices[2]*3 ];
173 
174  volume+=det(p1,p2,p3); // compute the volume of the tetrahedran relative to the origin.
175  }
176 
177  volume*=(1.0f/6.0f);
178  if ( volume < 0 )
179  volume*=-1;
180  return volume;
181 }
182 
183 
184 inline void CrossProduct(const float *a,const float *b,float *cross)
185 {
186  cross[0] = a[1]*b[2] - a[2]*b[1];
187  cross[1] = a[2]*b[0] - a[0]*b[2];
188  cross[2] = a[0]*b[1] - a[1]*b[0];
189 }
190 
191 inline float DotProduct(const float *a,const float *b)
192 {
193  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
194 }
195 
196 inline float tetVolume(const float *p0,const float *p1,const float *p2,const float *p3)
197 {
198  float a[3];
199  float b[3];
200  float c[3];
201 
202  a[0] = p1[0] - p0[0];
203  a[1] = p1[1] - p0[1];
204  a[2] = p1[2] - p0[2];
205 
206  b[0] = p2[0] - p0[0];
207  b[1] = p2[1] - p0[1];
208  b[2] = p2[2] - p0[2];
209 
210  c[0] = p3[0] - p0[0];
211  c[1] = p3[1] - p0[1];
212  c[2] = p3[2] - p0[2];
213 
214  float cross[3];
215 
216  CrossProduct( b, c, cross );
217 
218  float volume = DotProduct( a, cross );
219 
220  if ( volume < 0 )
221  return -volume;
222 
223  return volume;
224 }
225 
226 inline float det(const float *p0,const float *p1,const float *p2,const float *p3)
227 {
228  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];
229 }
230 
231 float computeMeshVolume2(const float *vertices,unsigned int tcount,const unsigned int *indices)
232 {
233  float volume = 0;
234 
235  const float *p0 = vertices;
236  for (unsigned int i=0; i<tcount; i++,indices+=3)
237  {
238 
239  const float *p1 = &vertices[ indices[0]*3 ];
240  const float *p2 = &vertices[ indices[1]*3 ];
241  const float *p3 = &vertices[ indices[2]*3 ];
242 
243  volume+=tetVolume(p0,p1,p2,p3); // compute the volume of the tetrahdren relative to the root vertice
244  }
245 
246  return volume * (1.0f / 6.0f );
247 }
248 
249 
250 };
251 
ConvexDecomposition::CrossProduct
void CrossProduct(const double *a, const double *b, double *cross)
Definition: meshvolume.cpp:89
ConvexDecomposition::computeMeshVolume
double computeMeshVolume(const double *vertices, unsigned int tcount, const unsigned int *indices)
Definition: meshvolume.cpp:67
ConvexDecomposition::det
double det(const double *p1, const double *p2, const double *p3)
Definition: meshvolume.cpp:62
ConvexDecomposition
Definition: bestfit.cpp:75
ConvexDecomposition::computeMeshVolume2
double computeMeshVolume2(const double *vertices, unsigned int tcount, const unsigned int *indices)
Definition: meshvolume.cpp:136
meshvolume.h
ConvexDecomposition::cross
double3 cross(const double3 &a, const double3 &b)
Definition: cd_hull.cpp:672
ConvexDecomposition::tetVolume
double tetVolume(const double *p0, const double *p1, const double *p2, const double *p3)
Definition: meshvolume.cpp:101
ConvexDecomposition::DotProduct
double DotProduct(const double *a, const double *b)
Definition: meshvolume.cpp:96


convex_decomposition
Author(s): John W. Ratcliff
autogenerated on Wed Mar 2 2022 00:04:59