1 #include "float_math.h"
9 /*----------------------------------------------------------------------
10 Copyright (c) 2004 Open Dynamics Framework Group
14 Redistribution and use in source and binary forms, with or without modification, are permitted provided
15 that the following conditions are met:
17 Redistributions of source code must retain the above copyright notice, this list of conditions
18 and the following disclaimer.
20 Redistributions in binary form must reproduce the above copyright notice,
21 this list of conditions and the following disclaimer in the documentation
22 and/or other materials provided with the distribution.
24 Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
25 be used to endorse or promote products derived from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 -----------------------------------------------------------------------*/
36 // http://codesuppository.blogspot.com
38 // mailto: jratcliff@infiniplex.net
40 // http://www.amillionpixels.us
43 #include "bestfitobb.h"
44 #include "float_math.h"
46 // computes the OBB for this set of points relative to this transform matrix.
47 void computeOBB(unsigned int vcount,const float *points,unsigned int pstride,float *sides,const float *matrix)
49 const char *src = (const char *) points;
51 float bmin[3] = { 1e9, 1e9, 1e9 };
52 float bmax[3] = { -1e9, -1e9, -1e9 };
54 for (unsigned int i=0; i<vcount; i++)
56 const float *p = (const float *) src;
59 fm_inverseRT(matrix, p, t ); // inverse rotate translate
61 if ( t[0] < bmin[0] ) bmin[0] = t[0];
62 if ( t[1] < bmin[1] ) bmin[1] = t[1];
63 if ( t[2] < bmin[2] ) bmin[2] = t[2];
65 if ( t[0] > bmax[0] ) bmax[0] = t[0];
66 if ( t[1] > bmax[1] ) bmax[1] = t[1];
67 if ( t[2] > bmax[2] ) bmax[2] = t[2];
77 if ( fabsf(bmin[0]) > sides[0] ) sides[0] = fabsf(bmin[0]);
78 if ( fabsf(bmin[1]) > sides[1] ) sides[1] = fabsf(bmin[1]);
79 if ( fabsf(bmin[2]) > sides[2] ) sides[2] = fabsf(bmin[2]);
87 void computeBestFitOBB(unsigned int vcount,const float *points,unsigned int pstride,float *sides,float *matrix)
93 fm_getAABB(vcount,points,pstride,bmin,bmax);
97 center[0] = (bmax[0]-bmin[0])*0.5f + bmin[0];
98 center[1] = (bmax[1]-bmin[1])*0.5f + bmin[1];
99 center[2] = (bmax[2]-bmin[2])*0.5f + bmin[2];
105 float sweep = 45.0f; // 180 degree sweep on all three axes.
106 float steps = 8.0f; // 16 steps on each axis.
108 float bestVolume = 1e9;
109 float angle[3]={0.f,0.f,0.f};
116 float stepsize = sweep / steps;
118 for (float x=ax-sweep; x<=ax+sweep; x+=stepsize)
120 for (float y=ay-sweep; y<=ay+sweep; y+=stepsize)
122 for (float z=az-sweep; z<=az+sweep; z+=stepsize)
126 fm_eulerMatrix( x*FM_DEG_TO_RAD, y*FM_DEG_TO_RAD, z*FM_DEG_TO_RAD, pmatrix );
128 pmatrix[3*4+0] = center[0];
129 pmatrix[3*4+1] = center[1];
130 pmatrix[3*4+2] = center[2];
134 computeOBB( vcount, points, pstride, psides, pmatrix );
136 float volume = psides[0]*psides[1]*psides[2]; // the volume of the cube
138 if ( volume <= bestVolume )
142 sides[0] = psides[0];
143 sides[1] = psides[1];
144 sides[2] = psides[2];
150 memcpy(matrix,pmatrix,sizeof(float)*16);
151 found = true; // yes, we found an improvement.
164 sweep*=0.5f; // sweep 1/2 the distance as the last time.
168 break; // no improvement, so just