face3.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**************************************************************************/
  2. /* face3.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. #pragma once
  31. #include "core/math/aabb.h"
  32. #include "core/math/plane.h"
  33. #include "core/math/transform_3d.h"
  34. #include "core/math/vector3.h"
  35. struct [[nodiscard]] Face3 {
  36. enum Side {
  37. SIDE_OVER,
  38. SIDE_UNDER,
  39. SIDE_SPANNING,
  40. SIDE_COPLANAR
  41. };
  42. Vector3 vertex[3];
  43. /**
  44. * @param p_plane plane used to split the face
  45. * @param p_res array of at least 3 faces, amount used in function return
  46. * @param p_is_point_over array of at least 3 booleans, determining which face is over the plane, amount used in function return
  47. * @return amount of faces generated by the split, either 0 (means no split possible), 2 or 3
  48. */
  49. int split_by_plane(const Plane &p_plane, Face3 *p_res, bool *p_is_point_over) const;
  50. Plane get_plane(ClockDirection p_dir = CLOCKWISE) const;
  51. Vector3 get_random_point_inside() const;
  52. bool is_degenerate() const;
  53. real_t get_area() const;
  54. Vector3 get_closest_point_to(const Vector3 &p_point) const;
  55. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const;
  56. bool intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const;
  57. void get_support(const Vector3 &p_normal, const Transform3D &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const;
  58. void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const;
  59. AABB get_aabb() const {
  60. AABB aabb(vertex[0], Vector3());
  61. aabb.expand_to(vertex[1]);
  62. aabb.expand_to(vertex[2]);
  63. return aabb;
  64. }
  65. bool intersects_aabb(const AABB &p_aabb) const;
  66. _FORCE_INLINE_ bool intersects_aabb2(const AABB &p_aabb) const;
  67. operator String() const;
  68. Face3() = default;
  69. constexpr Face3(const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) :
  70. vertex{ p_v1, p_v2, p_v3 } {}
  71. };
  72. bool Face3::intersects_aabb2(const AABB &p_aabb) const {
  73. Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]);
  74. Vector3 half_extents = p_aabb.size * 0.5f;
  75. Vector3 ofs = p_aabb.position + half_extents;
  76. Vector3 sup = Vector3(
  77. (perp.x > 0) ? -half_extents.x : half_extents.x,
  78. (perp.y > 0) ? -half_extents.y : half_extents.y,
  79. (perp.z > 0) ? -half_extents.z : half_extents.z);
  80. real_t d = perp.dot(vertex[0]);
  81. real_t dist_a = perp.dot(ofs + sup) - d;
  82. real_t dist_b = perp.dot(ofs - sup) - d;
  83. if (dist_a * dist_b > 0) {
  84. return false; //does not intersect the plane
  85. }
  86. #define TEST_AXIS(m_ax) \
  87. { \
  88. real_t aabb_min = p_aabb.position.m_ax; \
  89. real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \
  90. real_t tri_min, tri_max; \
  91. for (int i = 0; i < 3; i++) { \
  92. if (i == 0 || vertex[i].m_ax > tri_max) \
  93. tri_max = vertex[i].m_ax; \
  94. if (i == 0 || vertex[i].m_ax < tri_min) \
  95. tri_min = vertex[i].m_ax; \
  96. } \
  97. \
  98. if (tri_max < aabb_min || aabb_max < tri_min) \
  99. return false; \
  100. }
  101. TEST_AXIS(x);
  102. TEST_AXIS(y);
  103. TEST_AXIS(z);
  104. #undef TEST_AXIS
  105. const Vector3 edge_norms[3] = {
  106. vertex[0] - vertex[1],
  107. vertex[1] - vertex[2],
  108. vertex[2] - vertex[0],
  109. };
  110. for (int i = 0; i < 12; i++) {
  111. Vector3 from, to;
  112. switch (i) {
  113. case 0: {
  114. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
  115. to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
  116. } break;
  117. case 1: {
  118. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  119. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
  120. } break;
  121. case 2: {
  122. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  123. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  124. } break;
  125. case 3: {
  126. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
  127. to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  128. } break;
  129. case 4: {
  130. from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  131. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  132. } break;
  133. case 5: {
  134. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  135. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  136. } break;
  137. case 6: {
  138. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  139. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  140. } break;
  141. case 7: {
  142. from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  143. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  144. } break;
  145. case 8: {
  146. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  147. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  148. } break;
  149. case 9: {
  150. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
  151. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  152. } break;
  153. case 10: {
  154. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
  155. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  156. } break;
  157. case 11: {
  158. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  159. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  160. } break;
  161. }
  162. Vector3 e1 = from - to;
  163. for (int j = 0; j < 3; j++) {
  164. Vector3 e2 = edge_norms[j];
  165. Vector3 axis = vec3_cross(e1, e2);
  166. if (axis.length_squared() < 0.0001f) {
  167. continue; // coplanar
  168. }
  169. //axis.normalize();
  170. Vector3 sup2 = Vector3(
  171. (axis.x > 0) ? -half_extents.x : half_extents.x,
  172. (axis.y > 0) ? -half_extents.y : half_extents.y,
  173. (axis.z > 0) ? -half_extents.z : half_extents.z);
  174. real_t maxB = axis.dot(ofs + sup2);
  175. real_t minB = axis.dot(ofs - sup2);
  176. if (minB > maxB) {
  177. SWAP(maxB, minB);
  178. }
  179. real_t minT = 1e20, maxT = -1e20;
  180. for (int k = 0; k < 3; k++) {
  181. real_t vert_d = axis.dot(vertex[k]);
  182. if (vert_d > maxT) {
  183. maxT = vert_d;
  184. }
  185. if (vert_d < minT) {
  186. minT = vert_d;
  187. }
  188. }
  189. if (maxB < minT || maxT < minB) {
  190. return false;
  191. }
  192. }
  193. }
  194. return true;
  195. }
  196. template <>
  197. struct is_zero_constructible<Face3> : std::true_type {};