splitplane.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <float.h>
6 #include <math.h>
7 
64 #include "splitplane.h"
65 #include "ConvexDecomposition.h"
66 #include "cd_vector.h"
67 #include "cd_hull.h"
68 #include "cd_wavefront.h"
69 #include "bestfit.h"
70 #include "planetri.h"
71 #include "vlookup.h"
72 #include "meshvolume.h"
73 #include "bestfitobb.h"
74 #include "float_math.h"
75 
76 namespace ConvexDecomposition
77 {
78 
79 static void computePlane(const double *A,const double *B,const double *C,double *plane)
80 {
81 
82  double vx = (B[0] - C[0]);
83  double vy = (B[1] - C[1]);
84  double vz = (B[2] - C[2]);
85 
86  double wx = (A[0] - B[0]);
87  double wy = (A[1] - B[1]);
88  double wz = (A[2] - B[2]);
89 
90  double vw_x = vy * wz - vz * wy;
91  double vw_y = vz * wx - vx * wz;
92  double vw_z = vx * wy - vy * wx;
93 
94  double mag = sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
95 
96  if ( mag < 0.000001f )
97  {
98  mag = 0;
99  }
100  else
101  {
102  mag = 1.0f/mag;
103  }
104 
105  double x = vw_x * mag;
106  double y = vw_y * mag;
107  double z = vw_z * mag;
108 
109 
110  double D = 0.0f - ((x*A[0])+(y*A[1])+(z*A[2]));
111 
112  plane[0] = x;
113  plane[1] = y;
114  plane[2] = z;
115  plane[3] = D;
116 
117 }
118 
119 class Rect3d
120 {
121 public:
122  Rect3d(void) { };
123 
124  Rect3d(const double *bmin,const double *bmax)
125  {
126 
127  mMin[0] = bmin[0];
128  mMin[1] = bmin[1];
129  mMin[2] = bmin[2];
130 
131  mMax[0] = bmax[0];
132  mMax[1] = bmax[1];
133  mMax[2] = bmax[2];
134 
135  }
136 
137  void SetMin(const double *bmin)
138  {
139  mMin[0] = bmin[0];
140  mMin[1] = bmin[1];
141  mMin[2] = bmin[2];
142  }
143 
144  void SetMax(const double *bmax)
145  {
146  mMax[0] = bmax[0];
147  mMax[1] = bmax[1];
148  mMax[2] = bmax[2];
149  }
150 
151  void SetMin(double x,double y,double z)
152  {
153  mMin[0] = x;
154  mMin[1] = y;
155  mMin[2] = z;
156  }
157 
158  void SetMax(double x,double y,double z)
159  {
160  mMax[0] = x;
161  mMax[1] = y;
162  mMax[2] = z;
163  }
164 
165  double mMin[3];
166  double mMax[3];
167 };
168 
169 void splitRect(unsigned int axis,
170  const Rect3d &source,
171  Rect3d &b1,
172  Rect3d &b2,
173  const double *midpoint)
174 {
175  switch ( axis )
176  {
177  case 0:
178  b1.SetMin(source.mMin);
179  b1.SetMax( midpoint[0], source.mMax[1], source.mMax[2] );
180 
181  b2.SetMin( midpoint[0], source.mMin[1], source.mMin[2] );
182  b2.SetMax(source.mMax);
183 
184  break;
185  case 1:
186  b1.SetMin(source.mMin);
187  b1.SetMax( source.mMax[0], midpoint[1], source.mMax[2] );
188 
189  b2.SetMin( source.mMin[0], midpoint[1], source.mMin[2] );
190  b2.SetMax(source.mMax);
191 
192  break;
193  case 2:
194  b1.SetMin(source.mMin);
195  b1.SetMax( source.mMax[0], source.mMax[1], midpoint[2] );
196 
197  b2.SetMin( source.mMin[0], source.mMin[1], midpoint[2] );
198  b2.SetMax(source.mMax);
199 
200  break;
201  }
202 }
203 
204 bool computeSplitPlane(unsigned int vcount,
205  const double *vertices,
206  unsigned int tcount,
207  const unsigned int *indices,
208  ConvexDecompInterface *callback,
209  double *plane)
210 {
211  bool cret = false;
212 
213 
214  double sides[3];
215  double matrix[16];
216 
217  computeBestFitOBB( vcount, vertices, sizeof(double)*3, sides, matrix );
218 
219  double bmax[3];
220  double bmin[3];
221 
222  bmax[0] = sides[0]*0.5f;
223  bmax[1] = sides[1]*0.5f;
224  bmax[2] = sides[2]*0.5f;
225 
226  bmin[0] = -bmax[0];
227  bmin[1] = -bmax[1];
228  bmin[2] = -bmax[2];
229 
230 
231  double dx = sides[0];
232  double dy = sides[1];
233  double dz = sides[2];
234 
235 
236  double laxis = dx;
237 
238  unsigned int axis = 0;
239 
240  if ( dy > dx )
241  {
242  axis = 1;
243  laxis = dy;
244  }
245 
246  if ( dz > dx && dz > dy )
247  {
248  axis = 2;
249  laxis = dz;
250  }
251 
252  double p1[3];
253  double p2[3];
254  double p3[3];
255 
256  p3[0] = p2[0] = p1[0] = bmin[0] + dx*0.5f;
257  p3[1] = p2[1] = p1[1] = bmin[1] + dy*0.5f;
258  p3[2] = p2[2] = p1[2] = bmin[2] + dz*0.5f;
259 
260  Rect3d b(bmin,bmax);
261 
262  Rect3d b1,b2;
263 
264  splitRect(axis,b,b1,b2,p1);
265 
266 
267 // callback->ConvexDebugBound(b1.mMin,b1.mMax,0x00FF00);
268 // callback->ConvexDebugBound(b2.mMin,b2.mMax,0xFFFF00);
269 
270  switch ( axis )
271  {
272  case 0:
273  p2[1] = bmin[1];
274  p2[2] = bmin[2];
275 
276  if ( dz > dy )
277  {
278  p3[1] = bmax[1];
279  p3[2] = bmin[2];
280  }
281  else
282  {
283  p3[1] = bmin[1];
284  p3[2] = bmax[2];
285  }
286 
287  break;
288  case 1:
289  p2[0] = bmin[0];
290  p2[2] = bmin[2];
291 
292  if ( dx > dz )
293  {
294  p3[0] = bmax[0];
295  p3[2] = bmin[2];
296  }
297  else
298  {
299  p3[0] = bmin[0];
300  p3[2] = bmax[2];
301  }
302 
303  break;
304  case 2:
305  p2[0] = bmin[0];
306  p2[1] = bmin[1];
307 
308  if ( dx > dy )
309  {
310  p3[0] = bmax[0];
311  p3[1] = bmin[1];
312  }
313  else
314  {
315  p3[0] = bmin[0];
316  p3[1] = bmax[1];
317  }
318 
319  break;
320  }
321 
322  double tp1[3];
323  double tp2[3];
324  double tp3[3];
325 
326  fm_transform(matrix,p1,tp1);
327  fm_transform(matrix,p2,tp2);
328  fm_transform(matrix,p3,tp3);
329 
330 // callback->ConvexDebugTri(p1,p2,p3,0xFF0000);
331 
332  computePlane(tp1,tp2,tp3,plane);
333 
334  return true;
335 
336 }
337 
338 
339 };
cd_vector.h
ConvexDecomposition::computeBestFitOBB
void computeBestFitOBB(unsigned int vcount, const double *points, unsigned int pstride, double *sides, double *matrix)
Definition: bestfitobb.cpp:142
ConvexDecomposition::computeSplitPlane
bool computeSplitPlane(unsigned int vcount, const double *vertices, unsigned int tcount, const unsigned int *indices, ConvexDecompInterface *callback, double *plane)
Definition: splitplane.cpp:204
float_math.h
planetri.h
ConvexDecomposition::computePlane
static void computePlane(const double *A, const double *B, const double *C, double *plane)
Definition: splitplane.cpp:79
ConvexDecomposition::Rect3d::mMax
double mMax[3]
Definition: splitplane.cpp:166
ConvexDecomposition::Rect3d::SetMin
void SetMin(double x, double y, double z)
Definition: splitplane.cpp:151
ConvexDecomposition::Rect3d::Rect3d
Rect3d(void)
Definition: splitplane.cpp:122
cd_wavefront.h
ConvexDecomposition
Definition: bestfit.cpp:75
ConvexDecomposition::fm_transform
void fm_transform(const double *matrix, const double *v, double *t)
Definition: float_math.cpp:324
meshvolume.h
ConvexDecomposition::Rect3d
Definition: splitplane.cpp:119
ConvexDecomposition::Rect3d::Rect3d
Rect3d(const double *bmin, const double *bmax)
Definition: splitplane.cpp:124
ConvexDecomposition::ConvexDecompInterface
Definition: ConvexDecomposition.h:191
bestfit.h
bestfitobb.h
cd_hull.h
ConvexDecomposition::Rect3d::SetMin
void SetMin(const double *bmin)
Definition: splitplane.cpp:137
ConvexDecomposition::Rect3d::SetMax
void SetMax(const double *bmax)
Definition: splitplane.cpp:144
splitplane.h
ConvexDecomposition::plane
Definition: planetri.cpp:182
ConvexDecomposition.h
ConvexDecomposition::splitRect
void splitRect(unsigned int axis, const Rect3d &source, Rect3d &b1, Rect3d &b2, const double *midpoint)
Definition: splitplane.cpp:169
ConvexDecomposition::Rect3d::SetMax
void SetMax(double x, double y, double z)
Definition: splitplane.cpp:158
vlookup.h
ConvexDecomposition::Rect3d::mMin
double mMin[3]
Definition: splitplane.cpp:165


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