face3.h 9.4 KB

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