hacdManifoldMesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. 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.
  6. 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  7. 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.
  8. */
  9. /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  12. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  13. 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.
  14. 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  15. 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.
  16. */
  17. #pragma once
  18. #ifndef HACD_MANIFOLD_MESH_H
  19. #define HACD_MANIFOLD_MESH_H
  20. #include <iostream>
  21. #include <fstream>
  22. #include "hacdVersion.h"
  23. #include "hacdCircularList.h"
  24. #include "hacdVector.h"
  25. #include <set>
  26. namespace HACD
  27. {
  28. class TMMTriangle;
  29. class TMMEdge;
  30. class TMMesh;
  31. class ICHull;
  32. class HACD;
  33. class DPoint
  34. {
  35. public:
  36. DPoint(Real dist=0, bool computed=false, bool distOnly=false)
  37. :m_dist(dist),
  38. m_computed(computed),
  39. m_distOnly(distOnly){};
  40. ~DPoint(){};
  41. private:
  42. Real m_dist;
  43. bool m_computed;
  44. bool m_distOnly;
  45. friend class TMMTriangle;
  46. friend class TMMesh;
  47. friend class GraphVertex;
  48. friend class GraphEdge;
  49. friend class Graph;
  50. friend class ICHull;
  51. friend class HACD;
  52. };
  53. //! Vertex data structure used in a triangular manifold mesh (TMM).
  54. class TMMVertex
  55. {
  56. public:
  57. TMMVertex(void);
  58. ~TMMVertex(void);
  59. private:
  60. Vec3<Real> m_pos;
  61. long m_name;
  62. size_t m_id;
  63. CircularListElement<TMMEdge> * m_duplicate; // pointer to incident cone edge (or NULL)
  64. bool m_onHull;
  65. bool m_tag;
  66. TMMVertex(const TMMVertex & rhs);
  67. friend class HACD;
  68. friend class ICHull;
  69. friend class TMMesh;
  70. friend class TMMTriangle;
  71. friend class TMMEdge;
  72. };
  73. //! Edge data structure used in a triangular manifold mesh (TMM).
  74. class TMMEdge
  75. {
  76. public:
  77. TMMEdge(void);
  78. ~TMMEdge(void);
  79. private:
  80. size_t m_id;
  81. CircularListElement<TMMTriangle> * m_triangles[2];
  82. CircularListElement<TMMVertex> * m_vertices[2];
  83. CircularListElement<TMMTriangle> * m_newFace;
  84. TMMEdge(const TMMEdge & rhs);
  85. friend class HACD;
  86. friend class ICHull;
  87. friend class TMMTriangle;
  88. friend class TMMVertex;
  89. friend class TMMesh;
  90. };
  91. //! Triangle data structure used in a triangular manifold mesh (TMM).
  92. class TMMTriangle
  93. {
  94. public:
  95. TMMTriangle(void);
  96. ~TMMTriangle(void);
  97. private:
  98. size_t m_id;
  99. CircularListElement<TMMEdge> * m_edges[3];
  100. CircularListElement<TMMVertex> * m_vertices[3];
  101. std::set<long> m_incidentPoints;
  102. bool m_visible;
  103. TMMTriangle(const TMMTriangle & rhs);
  104. friend class HACD;
  105. friend class ICHull;
  106. friend class TMMesh;
  107. friend class TMMVertex;
  108. friend class TMMEdge;
  109. };
  110. class Material
  111. {
  112. public:
  113. Material(void);
  114. ~Material(void){}
  115. // private:
  116. Vec3<double> m_diffuseColor;
  117. double m_ambientIntensity;
  118. Vec3<double> m_specularColor;
  119. Vec3<double> m_emissiveColor;
  120. double m_shininess;
  121. double m_transparency;
  122. friend class TMMesh;
  123. friend class HACD;
  124. };
  125. //! triangular manifold mesh data structure.
  126. class TMMesh
  127. {
  128. public:
  129. //! Returns the number of vertices>
  130. inline size_t GetNVertices() const { return m_vertices.GetSize();}
  131. //! Returns the number of edges
  132. inline size_t GetNEdges() const { return m_edges.GetSize();}
  133. //! Returns the number of triangles
  134. inline size_t GetNTriangles() const { return m_triangles.GetSize();}
  135. //! Returns the vertices circular list
  136. inline const CircularList<TMMVertex> & GetVertices() const { return m_vertices;}
  137. //! Returns the edges circular list
  138. inline const CircularList<TMMEdge> & GetEdges() const { return m_edges;}
  139. //! Returns the triangles circular list
  140. inline const CircularList<TMMTriangle> & GetTriangles() const { return m_triangles;}
  141. //! Returns the vertices circular list
  142. inline CircularList<TMMVertex> & GetVertices() { return m_vertices;}
  143. //! Returns the edges circular list
  144. inline CircularList<TMMEdge> & GetEdges() { return m_edges;}
  145. //! Returns the triangles circular list
  146. inline CircularList<TMMTriangle> & GetTriangles() { return m_triangles;}
  147. //! Add vertex to the mesh
  148. CircularListElement<TMMVertex> * AddVertex() {return m_vertices.Add();}
  149. //! Add vertex to the mesh
  150. CircularListElement<TMMEdge> * AddEdge() {return m_edges.Add();}
  151. //! Add vertex to the mesh
  152. CircularListElement<TMMTriangle> * AddTriangle() {return m_triangles.Add();}
  153. //! Print mesh information
  154. void Print();
  155. //!
  156. void GetIFS(Vec3<Real> * const points, Vec3<long> * const triangles);
  157. //! Save mesh
  158. bool Save(const char *fileName);
  159. //! Save mesh to VRML 2.0 format
  160. bool SaveVRML2(std::ofstream &fout);
  161. //! Save mesh to VRML 2.0 format
  162. bool SaveVRML2(std::ofstream &fout, const Material & material);
  163. //!
  164. void Clear();
  165. //!
  166. void Copy(TMMesh & mesh);
  167. //!
  168. bool CheckConsistancy();
  169. //!
  170. bool Normalize();
  171. //!
  172. bool Denormalize();
  173. //! Constructor
  174. TMMesh(void);
  175. //! Destructor
  176. virtual ~TMMesh(void);
  177. private:
  178. CircularList<TMMVertex> m_vertices;
  179. CircularList<TMMEdge> m_edges;
  180. CircularList<TMMTriangle> m_triangles;
  181. Real m_diag; //>! length of the BB diagonal
  182. Vec3<Real> m_barycenter; //>! barycenter of the mesh
  183. // not defined
  184. TMMesh(const TMMesh & rhs);
  185. friend class ICHull;
  186. friend class HACD;
  187. };
  188. //! IntersectRayTriangle(): intersect a ray with a 3D triangle
  189. //! Input: a ray R, and a triangle T
  190. //! Output: *I = intersection point (when it exists)
  191. //! 0 = disjoint (no intersect)
  192. //! 1 = intersect in unique point I1
  193. long IntersectRayTriangle( const Vec3<double> & P0, const Vec3<double> & dir,
  194. const Vec3<double> & V0, const Vec3<double> & V1,
  195. const Vec3<double> & V2, double &t);
  196. // intersect_RayTriangle(): intersect a ray with a 3D triangle
  197. // Input: a ray R, and a triangle T
  198. // Output: *I = intersection point (when it exists)
  199. // Return: -1 = triangle is degenerate (a segment or point)
  200. // 0 = disjoint (no intersect)
  201. // 1 = intersect in unique point I1
  202. // 2 = are in the same plane
  203. long IntersectRayTriangle2(const Vec3<double> & P0, const Vec3<double> & dir,
  204. const Vec3<double> & V0, const Vec3<double> & V1,
  205. const Vec3<double> & V2, double &r);
  206. /*
  207. Calculate the line segment PaPb that is the shortest route between
  208. two lines P1P2 and P3P4. Calculate also the values of mua and mub where
  209. Pa = P1 + mua (P2 - P1)
  210. Pb = P3 + mub (P4 - P3)
  211. Return FALSE if no solution exists.
  212. */
  213. bool IntersectLineLine(const Vec3<double> & p1, const Vec3<double> & p2,
  214. const Vec3<double> & p3, const Vec3<double> & p4,
  215. Vec3<double> & pa, Vec3<double> & pb,
  216. double & mua, double &mub);
  217. }
  218. #endif