97 void computeOBB(
unsigned int vcount,
const double *points,
unsigned int pstride,
double *sides,
double *matrix)
99 const char *src = (
const char *) points;
101 double bmin[3] = { 1e9, 1e9, 1e9 };
102 double bmax[3] = { -1e9, -1e9, -1e9 };
104 for (
unsigned int i=0; i<vcount; i++)
106 const double *p = (
const double *) src;
111 if ( t[0] < bmin[0] ) bmin[0] = t[0];
112 if ( t[1] < bmin[1] ) bmin[1] = t[1];
113 if ( t[2] < bmin[2] ) bmin[2] = t[2];
115 if ( t[0] > bmax[0] ) bmax[0] = t[0];
116 if ( t[1] > bmax[1] ) bmax[1] = t[1];
117 if ( t[2] > bmax[2] ) bmax[2] = t[2];
124 sides[0] = bmax[0]-bmin[0];
125 sides[1] = bmax[1]-bmin[1];
126 sides[2] = bmax[2]-bmin[2];
128 center[0] = sides[0]*0.5f+bmin[0];
129 center[1] = sides[1]*0.5f+bmin[1];
130 center[2] = sides[2]*0.5f+bmin[2];
136 matrix[12]+=ocenter[0];
137 matrix[13]+=ocenter[1];
138 matrix[14]+=ocenter[2];
142 void computeBestFitOBB(
unsigned int vcount,
const double *points,
unsigned int pstride,
double *sides,
double *matrix)
152 center[0] = (bmax[0]-bmin[0])*0.5f + bmin[0];
153 center[1] = (bmax[1]-bmin[1])*0.5f + bmin[1];
154 center[2] = (bmax[2]-bmin[2])*0.5f + bmin[2];
160 double sweep = 45.0f;
163 double bestVolume = 1e9;
171 double stepsize = sweep / steps;
173 for (
double x=ax-sweep; x<=ax+sweep; x+=stepsize)
175 for (
double y=ay-sweep; y<=ay+sweep; y+=stepsize)
177 for (
double z=az-sweep; z<=az+sweep; z+=stepsize)
183 pmatrix[3*4+0] = center[0];
184 pmatrix[3*4+1] = center[1];
185 pmatrix[3*4+2] = center[2];
189 computeOBB( vcount, points, pstride, psides, pmatrix );
191 double volume = psides[0]*psides[1]*psides[2];
193 if ( volume < bestVolume )
197 sides[0] = psides[0];
198 sides[1] = psides[1];
199 sides[2] = psides[2];
205 memcpy(matrix,pmatrix,
sizeof(
double)*16);
231 void computeBestFitOBB(
unsigned int vcount,
const double *points,
unsigned int pstride,
double *sides,
double *pos,
double *quat)
242 void computeBestFitABB(
unsigned int vcount,
const double *points,
unsigned int pstride,
double *sides,
double *pos)
255 const char *cp = (
const char *) points;
256 for (
unsigned int i=0; i<vcount; i++)
258 const double *p = (
const double *) cp;
260 if ( p[0] < bmin[0] ) bmin[0] = p[0];
261 if ( p[1] < bmin[1] ) bmin[1] = p[1];
262 if ( p[2] < bmin[2] ) bmin[2] = p[2];
264 if ( p[0] > bmax[0] ) bmax[0] = p[0];
265 if ( p[1] > bmax[1] ) bmax[1] = p[1];
266 if ( p[2] > bmax[2] ) bmax[2] = p[2];
272 sides[0] = bmax[0] - bmin[0];
273 sides[1] = bmax[1] - bmin[1];
274 sides[2] = bmax[2] - bmin[2];
276 pos[0] = bmin[0]+sides[0]*0.5f;
277 pos[1] = bmin[1]+sides[1]*0.5f;
278 pos[2] = bmin[2]+sides[2]*0.5f;
283 void computeBestFitOBB(
unsigned int vcount,
const float *points,
unsigned int pstride,
float *sides,
float *pos,
float *quat)
285 double *temp =
new double[vcount*3];
286 const char *src = (
const char *)points;
288 for (
unsigned int i=0; i<vcount; i++)
290 const float *s = (
const float *) src;
306 sides[0] = (float) dsides[0];
307 sides[1] = (float) dsides[1];
308 sides[2] = (float) dsides[2];
312 pos[0] = (float) dpos[0];
313 pos[1] = (float) dpos[1];
314 pos[2] = (float) dpos[2];
318 quat[0] = (float) dquat[0];
319 quat[1] = (float) dquat[1];
320 quat[2] = (float) dquat[2];
321 quat[3] = (float) dquat[3];
328 void computeBestFitABB(
unsigned int vcount,
const float *points,
unsigned int pstride,
float *sides,
float *pos)
341 const char *cp = (
const char *) points;
342 for (
unsigned int i=0; i<vcount; i++)
344 const float *p = (
const float *) cp;
346 if ( p[0] < bmin[0] ) bmin[0] = p[0];
347 if ( p[1] < bmin[1] ) bmin[1] = p[1];
348 if ( p[2] < bmin[2] ) bmin[2] = p[2];
350 if ( p[0] > bmax[0] ) bmax[0] = p[0];
351 if ( p[1] > bmax[1] ) bmax[1] = p[1];
352 if ( p[2] > bmax[2] ) bmax[2] = p[2];
358 sides[0] = bmax[0] - bmin[0];
359 sides[1] = bmax[1] - bmin[1];
360 sides[2] = bmax[2] - bmin[2];
362 pos[0] = bmin[0]+sides[0]*0.5f;
363 pos[1] = bmin[1]+sides[1]*0.5f;
364 pos[2] = bmin[2]+sides[2]*0.5f;