vhacdICHull.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_ICHULL_H
  11. #define VHACD_ICHULL_H
  12. #include "vhacdManifoldMesh.h"
  13. #include "vhacdVector.h"
  14. // -- GODOT start --
  15. #include <cstdint>
  16. // -- GODOT end --
  17. namespace VHACD {
  18. //! Incremental Convex Hull algorithm (cf. http://cs.smith.edu/~orourke/books/ftp.html ).
  19. enum ICHullError {
  20. ICHullErrorOK = 0,
  21. ICHullErrorCoplanarPoints,
  22. ICHullErrorNoVolume,
  23. ICHullErrorInconsistent,
  24. ICHullErrorNotEnoughPoints
  25. };
  26. class ICHull {
  27. public:
  28. static const double sc_eps;
  29. //!
  30. bool IsFlat() { return m_isFlat; }
  31. //! Returns the computed mesh
  32. TMMesh& GetMesh() { return m_mesh; }
  33. //! Add one point to the convex-hull
  34. bool AddPoint(const Vec3<double>& point) { return AddPoints(&point, 1); }
  35. //! Add one point to the convex-hull
  36. bool AddPoint(const Vec3<double>& point, int32_t id);
  37. //! Add points to the convex-hull
  38. bool AddPoints(const Vec3<double>* points, size_t nPoints);
  39. //!
  40. ICHullError Process();
  41. //!
  42. ICHullError Process(const uint32_t nPointsCH, const double minVolume = 0.0);
  43. //!
  44. bool IsInside(const Vec3<double>& pt0, const double eps = 0.0);
  45. //!
  46. const ICHull& operator=(ICHull& rhs);
  47. //! Constructor
  48. ICHull();
  49. //! Destructor
  50. ~ICHull(void){};
  51. private:
  52. //! DoubleTriangle builds the initial double triangle. It first finds 3 noncollinear points and makes two faces out of them, in opposite order. It then finds a fourth point that is not coplanar with that face. The vertices are stored in the face structure in counterclockwise order so that the volume between the face and the point is negative. Lastly, the 3 newfaces to the fourth point are constructed and the data structures are cleaned up.
  53. ICHullError DoubleTriangle();
  54. //! MakeFace creates a new face structure from three vertices (in ccw order). It returns a pointer to the face.
  55. CircularListElement<TMMTriangle>* MakeFace(CircularListElement<TMMVertex>* v0,
  56. CircularListElement<TMMVertex>* v1,
  57. CircularListElement<TMMVertex>* v2,
  58. CircularListElement<TMMTriangle>* fold);
  59. //!
  60. CircularListElement<TMMTriangle>* MakeConeFace(CircularListElement<TMMEdge>* e, CircularListElement<TMMVertex>* v);
  61. //!
  62. bool ProcessPoint();
  63. //!
  64. bool ComputePointVolume(double& totalVolume, bool markVisibleFaces);
  65. //!
  66. bool FindMaxVolumePoint(const double minVolume = 0.0);
  67. //!
  68. bool CleanEdges();
  69. //!
  70. bool CleanVertices(uint32_t& addedPoints);
  71. //!
  72. bool CleanTriangles();
  73. //!
  74. bool CleanUp(uint32_t& addedPoints);
  75. //!
  76. bool MakeCCW(CircularListElement<TMMTriangle>* f,
  77. CircularListElement<TMMEdge>* e,
  78. CircularListElement<TMMVertex>* v);
  79. void Clear();
  80. private:
  81. static const int32_t sc_dummyIndex;
  82. TMMesh m_mesh;
  83. SArray<CircularListElement<TMMEdge>*> m_edgesToDelete;
  84. SArray<CircularListElement<TMMEdge>*> m_edgesToUpdate;
  85. SArray<CircularListElement<TMMTriangle>*> m_trianglesToDelete;
  86. Vec3<double> m_normal;
  87. bool m_isFlat;
  88. ICHull(const ICHull& rhs);
  89. };
  90. }
  91. #endif // VHACD_ICHULL_H