Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / HACD / hacdManifoldMesh.h
1 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)\r
2 All rights reserved.\r
3 \r
4 \r
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\r
6 \r
7  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\r
8 \r
9  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\r
10 \r
11  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.\r
12 \r
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
14 */\r
15 \r
16 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)\r
17  All rights reserved.\r
18  \r
19  \r
20  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\r
21  \r
22  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\r
23  \r
24  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\r
25  \r
26  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.\r
27  \r
28  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
29  */\r
30 #pragma once\r
31 #ifndef HACD_MANIFOLD_MESH_H\r
32 #define HACD_MANIFOLD_MESH_H\r
33 #include <iostream>\r
34 #include <fstream>\r
35 #include "hacdVersion.h"\r
36 #include "hacdCircularList.h"\r
37 #include "hacdVector.h"\r
38 #include <set>\r
39 namespace HACD\r
40 {\r
41         class TMMTriangle;\r
42         class TMMEdge;\r
43     class TMMesh;\r
44     class ICHull;\r
45         class HACD;\r
46 \r
47     class DPoint  \r
48     {\r
49     public:       \r
50                                                                                                                                 DPoint(Real dist=0, bool computed=false, bool distOnly=false)\r
51                                                                                                                                         :m_dist(dist),\r
52                                                                                                                                          m_computed(computed),\r
53                                                                      m_distOnly(distOnly){};\r
54                                                                                 ~DPoint(){};      \r
55     private:\r
56                 Real                                                    m_dist;\r
57                 bool                                                                                                    m_computed;\r
58         bool                                                    m_distOnly;\r
59                 friend class TMMTriangle;\r
60             friend class TMMesh;\r
61                 friend class GraphVertex;\r
62         friend class GraphEdge;\r
63         friend class Graph;\r
64                 friend class ICHull;\r
65                 friend class HACD;\r
66     };\r
67 \r
68         //!     Vertex data structure used in a triangular manifold mesh (TMM).\r
69         class TMMVertex\r
70         {\r
71                 public:\r
72                                                                                                                                 TMMVertex(void);\r
73                                                                 ~TMMVertex(void);\r
74         \r
75         private:\r
76                         Vec3<Real>                                                                              m_pos;\r
77                         long                                                                                            m_name;\r
78             size_t                                                                                              m_id;\r
79                         CircularListElement<TMMEdge> *                                          m_duplicate;            // pointer to incident cone edge (or NULL)\r
80                         bool                                                                                            m_onHull;\r
81                         bool                                                                                            m_tag;\r
82                                                                                                                                 TMMVertex(const TMMVertex & rhs);\r
83             \r
84                         friend class HACD;            \r
85                         friend class ICHull;\r
86             friend class TMMesh;\r
87             friend class TMMTriangle;\r
88             friend class TMMEdge;\r
89         };\r
90 \r
91         //!     Edge data structure used in a triangular manifold mesh (TMM).\r
92         class TMMEdge\r
93         {               \r
94                 public:\r
95                                                                                                                                 TMMEdge(void);\r
96                                                                                                                                 ~TMMEdge(void);\r
97         private:\r
98             size_t                                                                                              m_id;\r
99                         CircularListElement<TMMTriangle> *                                      m_triangles[2];\r
100                         CircularListElement<TMMVertex> *                                        m_vertices[2];\r
101                         CircularListElement<TMMTriangle> *                                      m_newFace;\r
102 \r
103 \r
104                                                                                                                                 TMMEdge(const TMMEdge & rhs);\r
105                 \r
106                 friend class HACD;\r
107                 friend class ICHull;\r
108         friend class TMMTriangle;\r
109         friend class TMMVertex;\r
110         friend class TMMesh;\r
111         };\r
112 \r
113         //!     Triangle data structure used in a triangular manifold mesh (TMM).\r
114         class TMMTriangle\r
115         {\r
116                 public:\r
117                                                                                                                                 TMMTriangle(void);\r
118                                                                                                                                 ~TMMTriangle(void);\r
119         private:\r
120             size_t                                                                                              m_id;\r
121                         CircularListElement<TMMEdge> *                                          m_edges[3];\r
122                         CircularListElement<TMMVertex> *                                        m_vertices[3];\r
123                         std::set<long>                                                                          m_incidentPoints;\r
124                         bool                                                                                            m_visible;\r
125                                                                                                                                 \r
126                                                                                                                                 TMMTriangle(const TMMTriangle & rhs);\r
127         \r
128                 friend class HACD;\r
129         friend class ICHull;\r
130         friend class TMMesh;\r
131         friend class TMMVertex;\r
132         friend class TMMEdge;\r
133         };\r
134     \r
135     class Material\r
136     {\r
137     public:    \r
138                                                                 Material(void);\r
139                                                                 ~Material(void){}        \r
140 //    private:\r
141         Vec3<double>                                            m_diffuseColor;\r
142         double                                                  m_ambientIntensity;\r
143         Vec3<double>                                            m_specularColor;\r
144         Vec3<double>                                            m_emissiveColor;\r
145         double                                                  m_shininess;\r
146         double                                                  m_transparency;\r
147         \r
148         friend class TMMesh;\r
149                 friend class HACD;\r
150     };\r
151 \r
152         //!     triangular manifold mesh data structure.\r
153         class TMMesh\r
154         {\r
155                 public:\r
156 \r
157                         //! Returns the number of vertices>\r
158                         inline size_t                                                                           GetNVertices() const { return m_vertices.GetSize();}\r
159                         //! Returns the number of edges\r
160                         inline size_t                                                                           GetNEdges() const { return m_edges.GetSize();}\r
161                         //! Returns the number of triangles\r
162                         inline size_t                                                                           GetNTriangles() const { return m_triangles.GetSize();}\r
163             //! Returns the vertices circular list\r
164             inline const CircularList<TMMVertex> &              GetVertices() const { return m_vertices;}\r
165             //! Returns the edges circular list\r
166             inline const CircularList<TMMEdge> &                                GetEdges() const { return m_edges;}\r
167             //! Returns the triangles circular list\r
168             inline const CircularList<TMMTriangle> &                    GetTriangles() const { return m_triangles;}\r
169             //! Returns the vertices circular list\r
170             inline CircularList<TMMVertex> &                    GetVertices() { return m_vertices;}\r
171             //! Returns the edges circular list\r
172             inline CircularList<TMMEdge> &                      GetEdges() { return m_edges;}\r
173             //! Returns the triangles circular list\r
174             inline CircularList<TMMTriangle> &                  GetTriangles() { return m_triangles;}               \r
175                         //! Add vertex to the mesh\r
176                         CircularListElement<TMMVertex> *                                        AddVertex() {return m_vertices.Add();}\r
177                         //! Add vertex to the mesh\r
178                         CircularListElement<TMMEdge> *                                          AddEdge() {return m_edges.Add();}\r
179                         //! Add vertex to the mesh\r
180                         CircularListElement<TMMTriangle> *                                      AddTriangle() {return m_triangles.Add();}\r
181                         //! Print mesh information \r
182                         void                                                                                            Print();\r
183             //!\r
184             void                                                GetIFS(Vec3<Real> * const points, Vec3<long> * const triangles);\r
185             //! Save mesh \r
186             bool                                                                                                Save(const char *fileName);        \r
187             //! Save mesh to VRML 2.0 format \r
188             bool                                                                                                SaveVRML2(std::ofstream &fout);             \r
189             //! Save mesh to VRML 2.0 format \r
190             bool                                                                                                SaveVRML2(std::ofstream &fout, const Material & material);                      \r
191             //!  \r
192             void                                                                                                Clear();\r
193             //!\r
194             void                                                Copy(TMMesh & mesh);\r
195                         //!\r
196                         bool                                                                                            CheckConsistancy();\r
197                         //!\r
198                         bool                                                                                            Normalize();\r
199                         //!\r
200                         bool                                                                                            Denormalize();\r
201             //! Constructor\r
202                                                                                                                                 TMMesh(void);\r
203                         //! Destructor\r
204                         virtual                                                                                         ~TMMesh(void);\r
205 \r
206                 private:\r
207                         CircularList<TMMVertex>                                                         m_vertices;\r
208                         CircularList<TMMEdge>                                                           m_edges;\r
209                         CircularList<TMMTriangle>                                                       m_triangles;\r
210                         Real                                                m_diag;                                             //>! length of the BB diagonal\r
211                         Vec3<Real>                                          m_barycenter;                               //>! barycenter of the mesh\r
212 \r
213                         // not defined\r
214                                                                                                                                 TMMesh(const TMMesh & rhs);\r
215                         friend class ICHull;\r
216                         friend class HACD;\r
217         };\r
218         //! IntersectRayTriangle(): intersect a ray with a 3D triangle\r
219         //!    Input:  a ray R, and a triangle T\r
220         //!    Output: *I = intersection point (when it exists)\r
221         //!             0 = disjoint (no intersect)\r
222         //!             1 = intersect in unique point I1\r
223         long                                                                                                            IntersectRayTriangle( const Vec3<double> & P0, const Vec3<double> & dir, \r
224                                                                                                                                                                           const Vec3<double> & V0, const Vec3<double> & V1, \r
225                                                                                                                                                                           const Vec3<double> & V2, double &t);\r
226 \r
227         // intersect_RayTriangle(): intersect a ray with a 3D triangle\r
228         //    Input:  a ray R, and a triangle T\r
229         //    Output: *I = intersection point (when it exists)\r
230         //    Return: -1 = triangle is degenerate (a segment or point)\r
231         //             0 = disjoint (no intersect)\r
232         //             1 = intersect in unique point I1\r
233         //             2 = are in the same plane\r
234         long                                                                                                            IntersectRayTriangle2(const Vec3<double> & P0, const Vec3<double> & dir, \r
235                                                                                                                                                                            const Vec3<double> & V0, const Vec3<double> & V1, \r
236                                                                                                                                                                            const Vec3<double> & V2, double &r);\r
237 \r
238     /*\r
239      Calculate the line segment PaPb that is the shortest route between\r
240      two lines P1P2 and P3P4. Calculate also the values of mua and mub where\r
241      Pa = P1 + mua (P2 - P1)\r
242      Pb = P3 + mub (P4 - P3)\r
243      Return FALSE if no solution exists.\r
244      */\r
245     bool                                                        IntersectLineLine(const Vec3<double> & p1, const Vec3<double> & p2, \r
246                                                                                   const Vec3<double> & p3, const Vec3<double> & p4,\r
247                                                                                   Vec3<double> & pa, Vec3<double> & pb, \r
248                                                                                   double & mua, double &mub);\r
249 }\r
250 #endif\r