vhacdMesh.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #pragma once
  10. #ifndef VHACD_MESH_H
  11. #define VHACD_MESH_H
  12. #include "vhacdSArray.h"
  13. #include "vhacdVector.h"
  14. #define VHACD_DEBUG_MESH
  15. namespace VHACD {
  16. enum AXIS {
  17. AXIS_X = 0,
  18. AXIS_Y = 1,
  19. AXIS_Z = 2
  20. };
  21. struct Plane {
  22. double m_a;
  23. double m_b;
  24. double m_c;
  25. double m_d;
  26. AXIS m_axis;
  27. short m_index;
  28. };
  29. #ifdef VHACD_DEBUG_MESH
  30. struct Material {
  31. Vec3<double> m_diffuseColor;
  32. double m_ambientIntensity;
  33. Vec3<double> m_specularColor;
  34. Vec3<double> m_emissiveColor;
  35. double m_shininess;
  36. double m_transparency;
  37. Material(void)
  38. {
  39. m_diffuseColor.X() = 0.5;
  40. m_diffuseColor.Y() = 0.5;
  41. m_diffuseColor.Z() = 0.5;
  42. m_specularColor.X() = 0.5;
  43. m_specularColor.Y() = 0.5;
  44. m_specularColor.Z() = 0.5;
  45. m_ambientIntensity = 0.4;
  46. m_emissiveColor.X() = 0.0;
  47. m_emissiveColor.Y() = 0.0;
  48. m_emissiveColor.Z() = 0.0;
  49. m_shininess = 0.4;
  50. m_transparency = 0.0;
  51. };
  52. };
  53. #endif // VHACD_DEBUG_MESH
  54. //! Triangular mesh data structure
  55. class Mesh {
  56. public:
  57. void AddPoint(const Vec3<double>& pt) { m_points.PushBack(pt); };
  58. void SetPoint(size_t index, const Vec3<double>& pt) { m_points[index] = pt; };
  59. const Vec3<double>& GetPoint(size_t index) const { return m_points[index]; };
  60. Vec3<double>& GetPoint(size_t index) { return m_points[index]; };
  61. size_t GetNPoints() const { return m_points.Size(); };
  62. double* GetPoints() { return (double*)m_points.Data(); } // ugly
  63. const double* const GetPoints() const { return (double*)m_points.Data(); } // ugly
  64. const Vec3<double>* const GetPointsBuffer() const { return m_points.Data(); } //
  65. Vec3<double>* const GetPointsBuffer() { return m_points.Data(); } //
  66. void AddTriangle(const Vec3<int32_t>& tri) { m_triangles.PushBack(tri); };
  67. void SetTriangle(size_t index, const Vec3<int32_t>& tri) { m_triangles[index] = tri; };
  68. const Vec3<int32_t>& GetTriangle(size_t index) const { return m_triangles[index]; };
  69. Vec3<int32_t>& GetTriangle(size_t index) { return m_triangles[index]; };
  70. size_t GetNTriangles() const { return m_triangles.Size(); };
  71. int32_t* GetTriangles() { return (int32_t*)m_triangles.Data(); } // ugly
  72. const int32_t* const GetTriangles() const { return (int32_t*)m_triangles.Data(); } // ugly
  73. const Vec3<int32_t>* const GetTrianglesBuffer() const { return m_triangles.Data(); }
  74. Vec3<int32_t>* const GetTrianglesBuffer() { return m_triangles.Data(); }
  75. const Vec3<double>& GetCenter() const { return m_center; }
  76. const Vec3<double>& GetMinBB() const { return m_minBB; }
  77. const Vec3<double>& GetMaxBB() const { return m_maxBB; }
  78. void ClearPoints() { m_points.Clear(); }
  79. void ClearTriangles() { m_triangles.Clear(); }
  80. void Clear()
  81. {
  82. ClearPoints();
  83. ClearTriangles();
  84. }
  85. void ResizePoints(size_t nPts) { m_points.Resize(nPts); }
  86. void ResizeTriangles(size_t nTri) { m_triangles.Resize(nTri); }
  87. void CopyPoints(SArray<Vec3<double> >& points) const { points = m_points; }
  88. double GetDiagBB() const { return m_diag; }
  89. double ComputeVolume() const;
  90. void ComputeConvexHull(const double* const pts,
  91. const size_t nPts);
  92. void Clip(const Plane& plane,
  93. SArray<Vec3<double> >& positivePart,
  94. SArray<Vec3<double> >& negativePart) const;
  95. bool IsInside(const Vec3<double>& pt) const;
  96. double ComputeDiagBB();
  97. Vec3<double> &ComputeCenter(void);
  98. #ifdef VHACD_DEBUG_MESH
  99. bool LoadOFF(const std::string& fileName, bool invert);
  100. bool SaveVRML2(const std::string& fileName) const;
  101. bool SaveVRML2(std::ofstream& fout, const Material& material) const;
  102. bool SaveOFF(const std::string& fileName) const;
  103. #endif // VHACD_DEBUG_MESH
  104. //! Constructor.
  105. Mesh();
  106. //! Destructor.
  107. ~Mesh(void);
  108. private:
  109. SArray<Vec3<double> > m_points;
  110. SArray<Vec3<int32_t> > m_triangles;
  111. Vec3<double> m_minBB;
  112. Vec3<double> m_maxBB;
  113. Vec3<double> m_center;
  114. double m_diag;
  115. };
  116. }
  117. #endif