Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / ConvexDecomposition / ConvexDecomposition.h
1 #ifndef CONVEX_DECOMPOSITION_H
2
3 #define CONVEX_DECOMPOSITION_H
4
5 /*----------------------------------------------------------------------
6 Copyright (c) 2004 Open Dynamics Framework Group
7 www.physicstools.org
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without modification, are permitted provided
11 that the following conditions are met:
12
13 Redistributions of source code must retain the above copyright notice, this list of conditions
14 and the following disclaimer.
15
16 Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19
20 Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
21 be used to endorse or promote products derived from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 -----------------------------------------------------------------------*/
31
32 // http://codesuppository.blogspot.com
33 //
34 // mailto: jratcliff@infiniplex.net
35 //
36 // http://www.amillionpixels.us
37 //
38
39
40 #ifdef _WIN32
41 #include <memory.h> //memcpy
42 #endif
43 #include <string.h>
44 #include <stdio.h>
45 #include "LinearMath/btAlignedObjectArray.h"
46
47
48
49 extern unsigned int MAXDEPTH ;
50 extern float CONCAVE_PERCENT ;
51 extern float MERGE_PERCENT   ;
52
53
54 typedef btAlignedObjectArray< unsigned int > UintVector;
55
56
57
58 namespace ConvexDecomposition
59 {
60
61         class ConvexResult
62         {
63         public:
64                 ConvexResult(void)
65                 {
66                         mHullVcount = 0;
67                         mHullVertices = 0;
68                         mHullTcount = 0;
69                         mHullIndices = 0;
70                 }
71
72                 ConvexResult(unsigned int hvcount,const float *hvertices,unsigned int htcount,const unsigned int *hindices)
73                 {
74                         mHullVcount = hvcount;
75                         if ( mHullVcount )
76                         {
77                                 mHullVertices = new float[mHullVcount*sizeof(float)*3];
78                                 memcpy(mHullVertices, hvertices, sizeof(float)*3*mHullVcount );
79                         }
80                         else
81                         {
82                                 mHullVertices = 0;
83                         }
84
85                         mHullTcount = htcount;
86
87                         if ( mHullTcount )
88                         {
89                                 mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
90                                 memcpy(mHullIndices,hindices, sizeof(unsigned int)*mHullTcount*3 );
91                         }
92                         else
93                         {
94                                 mHullIndices = 0;
95                         }
96
97                 }
98
99                 ConvexResult(const ConvexResult &r)
100                 {
101                         mHullVcount = r.mHullVcount;
102                         if ( mHullVcount )
103                         {
104                                 mHullVertices = new float[mHullVcount*sizeof(float)*3];
105                                 memcpy(mHullVertices, r.mHullVertices, sizeof(float)*3*mHullVcount );
106                         }
107                         else
108                         {
109                                 mHullVertices = 0;
110                         }
111                         mHullTcount = r.mHullTcount;
112                         if ( mHullTcount )
113                         {
114                                 mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
115                                 memcpy(mHullIndices, r.mHullIndices, sizeof(unsigned int)*mHullTcount*3 );
116                         }
117                         else
118                         {
119                                 mHullIndices = 0;
120                         }
121                 }
122
123                 ~ConvexResult(void)
124                 {
125                         delete [] mHullVertices;
126                         delete [] mHullIndices;
127                 }
128
129                 // the convex hull.
130                 unsigned int                mHullVcount;
131                 float *                                           mHullVertices;
132                 unsigned  int       mHullTcount;
133                 unsigned int                     *mHullIndices;
134
135                 float               mHullVolume;                    // the volume of the convex hull.
136
137                 float               mOBBSides[3];                         // the width, height and breadth of the best fit OBB
138                 float               mOBBCenter[3];      // the center of the OBB
139                 float               mOBBOrientation[4]; // the quaternion rotation of the OBB.
140                 float               mOBBTransform[16];  // the 4x4 transform of the OBB.
141                 float               mOBBVolume;         // the volume of the OBB
142
143                 float               mSphereRadius;      // radius and center of best fit sphere
144                 float               mSphereCenter[3];
145                 float               mSphereVolume;      // volume of the best fit sphere
146
147
148
149         };
150
151         class ConvexDecompInterface
152         {
153         public:
154                 virtual ~ConvexDecompInterface() {};
155                 virtual void ConvexDebugTri(const float *p1,const float *p2,const float *p3,unsigned int color) { };
156                 virtual void ConvexDebugPoint(const float *p,float dist,unsigned int color) { };
157                 virtual void ConvexDebugBound(const float *bmin,const float *bmax,unsigned int color) { };
158                 virtual void ConvexDebugOBB(const float *sides, const float *matrix,unsigned int color) { };
159
160                 virtual void ConvexDecompResult(ConvexResult &result) = 0;
161
162
163
164         };
165
166         // just to avoid passing a zillion parameters to the method the
167         // options are packed into this descriptor.
168         class DecompDesc
169         {
170         public:
171                 DecompDesc(void)
172                 {
173                         mVcount = 0;
174                         mVertices = 0;
175                         mTcount   = 0;
176                         mIndices  = 0;
177                         mDepth    = 5;
178                         mCpercent = 5;
179                         mPpercent = 5;
180                         mMaxVertices = 32;
181                         mSkinWidth   = 0;
182                         mCallback    = 0;
183                 }
184
185                 // describes the input triangle.
186                 unsigned        int     mVcount;   // the number of vertices in the source mesh.
187                 const float  *mVertices; // start of the vertex position array.  Assumes a stride of 3 floats.
188                 unsigned int  mTcount;   // the number of triangles in the source mesh.
189                 unsigned int *mIndices;  // the indexed triangle list array (zero index based)
190
191                 // options
192                 unsigned int  mDepth;    // depth to split, a maximum of 10, generally not over 7.
193                 float         mCpercent; // the concavity threshold percentage.  0=20 is reasonable.
194                 float         mPpercent; // the percentage volume conservation threshold to collapse hulls. 0-30 is reasonable.
195
196                 // hull output limits.
197                 unsigned int  mMaxVertices; // maximum number of vertices in the output hull. Recommended 32 or less.
198                 float         mSkinWidth;   // a skin width to apply to the output hulls.
199
200                 ConvexDecompInterface *mCallback; // the interface to receive back the results.
201
202         };
203
204         // perform approximate convex decomposition on a mesh.
205         unsigned int performConvexDecomposition(const DecompDesc &desc); // returns the number of hulls produced.
206
207
208         void calcConvexDecomposition(unsigned int           vcount,
209                 const float           *vertices,
210                 unsigned int           tcount,
211                 const unsigned int    *indices,
212                 ConvexDecompInterface *callback,
213                 float                  masterVolume,
214                 unsigned int           depth);
215
216
217 }
218
219
220 #endif