face3.h 9.7 KB

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