Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / ConvexDecomposition / cd_hull.h
1 #ifndef CD_HULL_H
2
3 #define CD_HULL_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 namespace ConvexDecomposition
33 {
34
35 class HullResult
36 {
37 public:
38         HullResult(void)
39         {
40                 mPolygons = true;
41                 mNumOutputVertices = 0;
42                 mOutputVertices = 0;
43                 mNumFaces = 0;
44                 mNumIndices = 0;
45                 mIndices = 0;
46         }
47         bool                    mPolygons;                  // true if indices represents polygons, false indices are triangles
48         unsigned int            mNumOutputVertices;         // number of vertices in the output hull
49         float                  *mOutputVertices;            // array of vertices, 3 floats each x,y,z
50         unsigned int            mNumFaces;                  // the number of faces produced
51         unsigned int            mNumIndices;                // the total number of indices
52         unsigned int           *mIndices;                   // pointer to indices.
53
54 // If triangles, then indices are array indexes into the vertex list.
55 // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
56 };
57
58 enum HullFlag
59 {
60         QF_TRIANGLES         = (1<<0),             // report results as triangles, not polygons.
61         QF_REVERSE_ORDER     = (1<<1),             // reverse order of the triangle indices.
62         QF_SKIN_WIDTH        = (1<<2),             // extrude hull based on this skin width
63         QF_DEFAULT           = 0
64 };
65
66
67 class HullDesc
68 {
69 public:
70         HullDesc(void)
71         {
72                 mFlags          = QF_DEFAULT;
73                 mVcount         = 0;
74                 mVertices       = 0;
75                 mVertexStride   = sizeof(float)*3;
76                 mNormalEpsilon  = 0.001f;
77                 mMaxVertices            = 4096; // maximum number of points to be considered for a convex hull.
78                 mMaxFaces                               = 4096;
79                 mSkinWidth                      = 0.01f; // default is one centimeter
80         };
81
82         HullDesc(HullFlag flag,
83                                          unsigned int vcount,
84                                          const float *vertices,
85                                          unsigned int stride)
86         {
87                 mFlags          = flag;
88                 mVcount         = vcount;
89                 mVertices       = vertices;
90                 mVertexStride   = stride;
91                 mNormalEpsilon  = 0.001f;
92                 mMaxVertices    = 4096;
93                 mSkinWidth = 0.01f; // default is one centimeter
94         }
95
96         bool HasHullFlag(HullFlag flag) const
97         {
98                 if ( mFlags & flag ) return true;
99                 return false;
100         }
101
102         void SetHullFlag(HullFlag flag)
103         {
104                 mFlags|=flag;
105         }
106
107         void ClearHullFlag(HullFlag flag)
108         {
109                 mFlags&=~flag;
110         }
111
112         unsigned int      mFlags;           // flags to use when generating the convex hull.
113         unsigned int      mVcount;          // number of vertices in the input point cloud
114         const float      *mVertices;        // the array of vertices.
115         unsigned int      mVertexStride;    // the stride of each vertex, in bytes.
116         float             mNormalEpsilon;   // the epsilon for removing duplicates.  This is a normalized value, if normalized bit is on.
117         float             mSkinWidth;
118         unsigned int      mMaxVertices;               // maximum number of vertices to be considered for the hull!
119         unsigned int      mMaxFaces;
120 };
121
122 enum HullError
123 {
124         QE_OK,            // success!
125         QE_FAIL           // failed.
126 };
127
128 class HullLibrary
129 {
130 public:
131
132         HullError CreateConvexHull(const HullDesc       &desc,           // describes the input request
133                                                                                                                         HullResult           &result);        // contains the resulst
134
135         HullError ReleaseResult(HullResult &result); // release memory allocated for this result, we are done with it.
136
137 private:
138
139         void BringOutYourDead(const float *verts,unsigned int vcount, float *overts,unsigned int &ocount,unsigned int *indices,unsigned indexcount);
140
141         bool    CleanupVertices(unsigned int svcount,
142                                                                                                         const float *svertices,
143                                                                                                         unsigned int stride,
144                                                                                                         unsigned int &vcount,       // output number of vertices
145                                                                                                         float *vertices,                 // location to store the results.
146                                                                                                         float  normalepsilon,
147                                                                                                         float *scale);
148 };
149
150 }
151
152 #endif
153