Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/LinearMath/btConvexHull.h @ 2056

Last change on this file since 2056 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1
2/*
3Stan Melax Convex Hull Computation
4Copyright (c) 2008 Stan Melax http://www.melax.com/
5
6This software is provided 'as-is', without any express or implied warranty.
7In no event will the authors be held liable for any damages arising from the use of this software.
8Permission is granted to anyone to use this software for any purpose,
9including commercial applications, and to alter it and redistribute it freely,
10subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
132. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
143. This notice may not be removed or altered from any source distribution.
15*/
16
17///includes modifications/improvements by John Ratcliff, see BringOutYourDead below.
18
19#ifndef CD_HULL_H
20#define CD_HULL_H
21
22#include "LinearMath/btVector3.h"
23#include "LinearMath/btAlignedObjectArray.h"
24
25typedef btAlignedObjectArray<unsigned int> TUIntArray;
26
27class HullResult
28{
29public:
30        HullResult(void)
31        {
32                mPolygons = true;
33                mNumOutputVertices = 0;
34                mNumFaces = 0;
35                mNumIndices = 0;
36        }
37        bool                    mPolygons;                  // true if indices represents polygons, false indices are triangles
38        unsigned int            mNumOutputVertices;         // number of vertices in the output hull
39        btAlignedObjectArray<btVector3> m_OutputVertices;            // array of vertices
40        unsigned int            mNumFaces;                  // the number of faces produced
41        unsigned int            mNumIndices;                // the total number of indices
42        btAlignedObjectArray<unsigned int>    m_Indices;                   // pointer to indices.
43
44// If triangles, then indices are array indexes into the vertex list.
45// If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
46};
47
48enum HullFlag
49{
50        QF_TRIANGLES         = (1<<0),             // report results as triangles, not polygons.
51        QF_REVERSE_ORDER     = (1<<1),             // reverse order of the triangle indices.
52        QF_DEFAULT           = QF_TRIANGLES
53};
54
55
56class HullDesc
57{
58public:
59        HullDesc(void)
60        {
61                mFlags          = QF_DEFAULT;
62                mVcount         = 0;
63                mVertices       = 0;
64                mVertexStride   = sizeof(btVector3);
65                mNormalEpsilon  = 0.001f;
66                mMaxVertices    = 4096; // maximum number of points to be considered for a convex hull.
67                mMaxFaces       = 4096;
68        };
69
70        HullDesc(HullFlag flag,
71                 unsigned int vcount,
72                 const btVector3 *vertices,
73                 unsigned int stride = sizeof(btVector3))
74        {
75                mFlags          = flag;
76                mVcount         = vcount;
77                mVertices       = vertices;
78                mVertexStride   = stride;
79                mNormalEpsilon  = btScalar(0.001);
80                mMaxVertices    = 4096;
81        }
82
83        bool HasHullFlag(HullFlag flag) const
84        {
85                if ( mFlags & flag ) return true;
86                return false;
87        }
88
89        void SetHullFlag(HullFlag flag)
90        {
91                mFlags|=flag;
92        }
93
94        void ClearHullFlag(HullFlag flag)
95        {
96                mFlags&=~flag;
97        }
98
99        unsigned int      mFlags;           // flags to use when generating the convex hull.
100        unsigned int      mVcount;          // number of vertices in the input point cloud
101        const btVector3  *mVertices;        // the array of vertices.
102        unsigned int      mVertexStride;    // the stride of each vertex, in bytes.
103        btScalar             mNormalEpsilon;   // the epsilon for removing duplicates.  This is a normalized value, if normalized bit is on.
104        unsigned int      mMaxVertices;     // maximum number of vertices to be considered for the hull!
105        unsigned int      mMaxFaces;
106};
107
108enum HullError
109{
110        QE_OK,            // success!
111        QE_FAIL           // failed.
112};
113
114class btPlane
115{
116        public:
117        btVector3       normal;
118        btScalar        dist;   // distance below origin - the D from plane equasion Ax+By+Cz+D=0
119                        btPlane(const btVector3 &n,btScalar d):normal(n),dist(d){}
120                        btPlane():normal(),dist(0){}
121       
122};
123
124
125
126class ConvexH
127{
128  public:
129        class HalfEdge
130        {
131          public:
132                short ea;         // the other half of the edge (index into edges list)
133                unsigned char v;  // the vertex at the start of this edge (index into vertices list)
134                unsigned char p;  // the facet on which this edge lies (index into facets list)
135                HalfEdge(){}
136                HalfEdge(short _ea,unsigned char _v, unsigned char _p):ea(_ea),v(_v),p(_p){}
137        };
138        ConvexH()
139        {
140                int i;
141                i=0;
142        }
143        ~ConvexH()
144        {
145                int i;
146                i=0;
147        }
148        btAlignedObjectArray<btVector3> vertices;
149        btAlignedObjectArray<HalfEdge> edges;
150        btAlignedObjectArray<btPlane>  facets;
151        ConvexH(int vertices_size,int edges_size,int facets_size);
152};
153
154
155class int4
156{
157public:
158        int x,y,z,w;
159        int4(){};
160        int4(int _x,int _y, int _z,int _w){x=_x;y=_y;z=_z;w=_w;}
161        const int& operator[](int i) const {return (&x)[i];}
162        int& operator[](int i) {return (&x)[i];}
163};
164
165class PHullResult
166{
167public:
168
169        PHullResult(void)
170        {
171                mVcount = 0;
172                mIndexCount = 0;
173                mFaceCount = 0;
174                mVertices = 0;
175        }
176
177        unsigned int mVcount;
178        unsigned int mIndexCount;
179        unsigned int mFaceCount;
180        btVector3*   mVertices;
181        TUIntArray m_Indices;
182};
183
184
185
186///The HullLibrary class can create a convex hull from a collection of vertices, using the ComputeHull method.
187///The btShapeHull class uses this HullLibrary to create a approximate convex mesh given a general (non-polyhedral) convex shape.
188class HullLibrary
189{
190
191        btAlignedObjectArray<class Tri*> m_tris;
192
193public:
194
195        btAlignedObjectArray<int> m_vertexIndexMapping;
196
197
198        HullError CreateConvexHull(const HullDesc& desc, // describes the input request
199                                   HullResult&     result);        // contains the resulst
200        HullError ReleaseResult(HullResult &result); // release memory allocated for this result, we are done with it.
201
202private:
203
204        bool ComputeHull(unsigned int vcount,const btVector3 *vertices,PHullResult &result,unsigned int vlimit);
205
206        class Tri*      allocateTriangle(int a,int b,int c);
207        void    deAllocateTriangle(Tri*);
208        void b2bfix(Tri* s,Tri*t);
209
210        void removeb2b(Tri* s,Tri*t);
211
212        void checkit(Tri *t);
213
214        Tri* extrudable(btScalar epsilon);
215
216        int calchull(btVector3 *verts,int verts_count, TUIntArray& tris_out, int &tris_count,int vlimit);
217
218        int calchullgen(btVector3 *verts,int verts_count, int vlimit);
219
220        int4 FindSimplex(btVector3 *verts,int verts_count,btAlignedObjectArray<int> &allow);
221
222        class ConvexH* ConvexHCrop(ConvexH& convex,const btPlane& slice);
223
224        void extrude(class Tri* t0,int v);
225
226        ConvexH* test_cube();
227
228        //BringOutYourDead (John Ratcliff): When you create a convex hull you hand it a large input set of vertices forming a 'point cloud'.
229        //After the hull is generated it give you back a set of polygon faces which index the *original* point cloud.
230        //The thing is, often times, there are many 'dead vertices' in the point cloud that are on longer referenced by the hull.
231        //The routine 'BringOutYourDead' find only the referenced vertices, copies them to an new buffer, and re-indexes the hull so that it is a minimal representation.
232        void BringOutYourDead(const btVector3* verts,unsigned int vcount, btVector3* overts,unsigned int &ocount,unsigned int* indices,unsigned indexcount);
233
234        bool CleanupVertices(unsigned int svcount,
235                             const btVector3* svertices,
236                             unsigned int stride,
237                             unsigned int &vcount, // output number of vertices
238                             btVector3* vertices, // location to store the results.
239                             btScalar  normalepsilon,
240                             btVector3& scale);
241};
242
243
244#endif
245
Note: See TracBrowser for help on using the repository browser.