1 #include "float_math.h"
10 /*----------------------------------------------------------------------
11 Copyright (c) 2004 Open Dynamics Framework Group
15 Redistribution and use in source and binary forms, with or without modification, are permitted provided
16 that the following conditions are met:
18 Redistributions of source code must retain the above copyright notice, this list of conditions
19 and the following disclaimer.
21 Redistributions in binary form must reproduce the above copyright notice,
22 this list of conditions and the following disclaimer in the documentation
23 and/or other materials provided with the distribution.
25 Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
26 be used to endorse or promote products derived from this software without specific prior written permission.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
29 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 -----------------------------------------------------------------------*/
37 // http://codesuppository.blogspot.com
39 // mailto: jratcliff@infiniplex.net
41 // http://www.amillionpixels.us
44 #include "splitplane.h"
45 #include "ConvexDecomposition.h"
46 #include "cd_vector.h"
48 #include "cd_wavefront.h"
52 #include "meshvolume.h"
54 namespace ConvexDecomposition
57 static void computePlane(const float *A,const float *B,const float *C,float *plane)
60 float vx = (B[0] - C[0]);
61 float vy = (B[1] - C[1]);
62 float vz = (B[2] - C[2]);
64 float wx = (A[0] - B[0]);
65 float wy = (A[1] - B[1]);
66 float wz = (A[2] - B[2]);
68 float vw_x = vy * wz - vz * wy;
69 float vw_y = vz * wx - vx * wz;
70 float vw_z = vx * wy - vy * wx;
72 float mag = sqrtf((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
74 if ( mag < 0.000001f )
88 float D = 0.0f - ((x*A[0])+(y*A[1])+(z*A[2]));
102 Rect3d(const float *bmin,const float *bmax)
115 void SetMin(const float *bmin)
122 void SetMax(const float *bmax)
129 void SetMin(float x,float y,float z)
136 void SetMax(float x,float y,float z)
147 void splitRect(unsigned int axis,
148 const Rect3d &source,
151 const float *midpoint)
156 b1.SetMin(source.mMin);
157 b1.SetMax( midpoint[0], source.mMax[1], source.mMax[2] );
159 b2.SetMin( midpoint[0], source.mMin[1], source.mMin[2] );
160 b2.SetMax(source.mMax);
164 b1.SetMin(source.mMin);
165 b1.SetMax( source.mMax[0], midpoint[1], source.mMax[2] );
167 b2.SetMin( source.mMin[0], midpoint[1], source.mMin[2] );
168 b2.SetMax(source.mMax);
172 b1.SetMin(source.mMin);
173 b1.SetMax( source.mMax[0], source.mMax[1], midpoint[2] );
175 b2.SetMin( source.mMin[0], source.mMin[1], midpoint[2] );
176 b2.SetMax(source.mMax);
182 bool computeSplitPlane(unsigned int vcount,
183 const float *vertices,
185 const unsigned int *indices,
186 ConvexDecompInterface *callback,
189 float bmin[3] = { 1e9, 1e9, 1e9 };
190 float bmax[3] = { -1e9, -1e9, -1e9 };
192 for (unsigned int i=0; i<vcount; i++)
194 const float *p = &vertices[i*3];
196 if ( p[0] < bmin[0] ) bmin[0] = p[0];
197 if ( p[1] < bmin[1] ) bmin[1] = p[1];
198 if ( p[2] < bmin[2] ) bmin[2] = p[2];
200 if ( p[0] > bmax[0] ) bmax[0] = p[0];
201 if ( p[1] > bmax[1] ) bmax[1] = p[1];
202 if ( p[2] > bmax[2] ) bmax[2] = p[2];
206 float dx = bmax[0] - bmin[0];
207 float dy = bmax[1] - bmin[1];
208 float dz = bmax[2] - bmin[2];
213 unsigned int axis = 0;
221 if ( dz > dx && dz > dy )
231 p3[0] = p2[0] = p1[0] = bmin[0] + dx*0.5f;
232 p3[1] = p2[1] = p1[1] = bmin[1] + dy*0.5f;
233 p3[2] = p2[2] = p1[2] = bmin[2] + dz*0.5f;
239 splitRect(axis,b,b1,b2,p1);
242 // callback->ConvexDebugBound(b1.mMin,b1.mMax,0x00FF00);
243 // callback->ConvexDebugBound(b2.mMin,b2.mMax,0xFFFF00);
297 // callback->ConvexDebugTri(p1,p2,p3,0xFF0000);
299 computePlane(p1,p2,p3,plane);