convex_hull.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**************************************************************************/
  2. /* convex_hull.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef CONVEX_HULL_H
  31. #define CONVEX_HULL_H
  32. /*
  33. Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
  34. This software is provided 'as-is', without any express or implied warranty.
  35. In no event will the authors be held liable for any damages arising from the use of this software.
  36. Permission is granted to anyone to use this software for any purpose,
  37. including commercial applications, and to alter it and redistribute it freely,
  38. subject to the following restrictions:
  39. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  40. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  41. 3. This notice may not be removed or altered from any source distribution.
  42. */
  43. #include "core/math/geometry_3d.h"
  44. #include "core/math/vector3.h"
  45. #include "core/templates/local_vector.h"
  46. #include "core/templates/vector.h"
  47. /// Convex hull implementation based on Preparata and Hong
  48. /// See https://code.google.com/archive/p/bullet/issues/275
  49. /// Ole Kniemeyer, MAXON Computer GmbH
  50. class ConvexHullComputer {
  51. public:
  52. class Edge {
  53. private:
  54. int32_t next = 0;
  55. int32_t reverse = 0;
  56. int32_t target_vertex = 0;
  57. friend class ConvexHullComputer;
  58. public:
  59. int32_t get_next_relative() const {
  60. return next;
  61. }
  62. int32_t get_source_vertex() const {
  63. return (this + reverse)->target_vertex;
  64. }
  65. int32_t get_target_vertex() const {
  66. return target_vertex;
  67. }
  68. const Edge *get_next_edge_of_vertex() const // clockwise list of all edges of a vertex
  69. {
  70. return this + next;
  71. }
  72. const Edge *get_next_edge_of_face() const // counter-clockwise list of all edges of a face
  73. {
  74. return (this + reverse)->get_next_edge_of_vertex();
  75. }
  76. const Edge *get_reverse_edge() const {
  77. return this + reverse;
  78. }
  79. };
  80. // Vertices of the output hull
  81. LocalVector<Vector3> vertices;
  82. // Edges of the output hull
  83. LocalVector<Edge> edges;
  84. // Faces of the convex hull. Each entry is an index into the "edges" array pointing to an edge of the face. Faces are planar n-gons
  85. LocalVector<int32_t> faces;
  86. /*
  87. Compute convex hull of "count" vertices stored in "coords".
  88. If "shrink" is positive, the convex hull is shrunken by that amount (each face is moved by "shrink" length units
  89. towards the center along its normal).
  90. If "shrinkClamp" is positive, "shrink" is clamped to not exceed "shrinkClamp * innerRadius", where "innerRadius"
  91. is the minimum distance of a face to the center of the convex hull.
  92. The returned value is the amount by which the hull has been shrunken. If it is negative, the amount was so large
  93. that the resulting convex hull is empty.
  94. The output convex hull can be found in the member variables "vertices", "edges", "faces".
  95. */
  96. real_t compute(const Vector3 *p_coords, int32_t p_count, real_t p_shrink, real_t p_shrink_clamp);
  97. static Error convex_hull(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_mesh);
  98. };
  99. #endif // CONVEX_HULL_H