face3.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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_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. bool is_degenerate() const;
  54. real_t get_area() const;
  55. Vector3 get_closest_point_to(const Vector3 &p_point) const;
  56. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const;
  57. bool intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection = nullptr) const;
  58. void get_support(const Vector3 &p_normal, const Transform3D &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const;
  59. void project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const;
  60. AABB get_aabb() const {
  61. AABB aabb(vertex[0], Vector3());
  62. aabb.expand_to(vertex[1]);
  63. aabb.expand_to(vertex[2]);
  64. return aabb;
  65. }
  66. bool intersects_aabb(const AABB &p_aabb) const;
  67. _FORCE_INLINE_ bool intersects_aabb2(const AABB &p_aabb) const;
  68. operator String() const;
  69. inline Face3() {}
  70. inline Face3(const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
  71. vertex[0] = p_v1;
  72. vertex[1] = p_v2;
  73. vertex[2] = p_v3;
  74. }
  75. };
  76. bool Face3::intersects_aabb2(const AABB &p_aabb) const {
  77. Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]);
  78. Vector3 half_extents = p_aabb.size * 0.5f;
  79. Vector3 ofs = p_aabb.position + half_extents;
  80. Vector3 sup = Vector3(
  81. (perp.x > 0) ? -half_extents.x : half_extents.x,
  82. (perp.y > 0) ? -half_extents.y : half_extents.y,
  83. (perp.z > 0) ? -half_extents.z : half_extents.z);
  84. real_t d = perp.dot(vertex[0]);
  85. real_t dist_a = perp.dot(ofs + sup) - d;
  86. real_t dist_b = perp.dot(ofs - sup) - d;
  87. if (dist_a * dist_b > 0) {
  88. return false; //does not intersect the plane
  89. }
  90. #define TEST_AXIS(m_ax) \
  91. { \
  92. real_t aabb_min = p_aabb.position.m_ax; \
  93. real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \
  94. real_t tri_min, tri_max; \
  95. for (int i = 0; i < 3; i++) { \
  96. if (i == 0 || vertex[i].m_ax > tri_max) \
  97. tri_max = vertex[i].m_ax; \
  98. if (i == 0 || vertex[i].m_ax < tri_min) \
  99. tri_min = vertex[i].m_ax; \
  100. } \
  101. \
  102. if (tri_max < aabb_min || aabb_max < tri_min) \
  103. return false; \
  104. }
  105. TEST_AXIS(x);
  106. TEST_AXIS(y);
  107. TEST_AXIS(z);
  108. #undef TEST_AXIS
  109. const Vector3 edge_norms[3] = {
  110. vertex[0] - vertex[1],
  111. vertex[1] - vertex[2],
  112. vertex[2] - vertex[0],
  113. };
  114. for (int i = 0; i < 12; i++) {
  115. Vector3 from, to;
  116. switch (i) {
  117. case 0: {
  118. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
  119. to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
  120. } break;
  121. case 1: {
  122. from = Vector3(p_aabb.position.x + p_aabb.size.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);
  124. } break;
  125. case 2: {
  126. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  127. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  128. } break;
  129. case 3: {
  130. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
  131. to = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  132. } break;
  133. case 4: {
  134. from = Vector3(p_aabb.position.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);
  136. } break;
  137. case 5: {
  138. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  139. 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);
  140. } break;
  141. case 6: {
  142. 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);
  143. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  144. } break;
  145. case 7: {
  146. from = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.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);
  148. } break;
  149. case 8: {
  150. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  151. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z + p_aabb.size.z);
  152. } break;
  153. case 9: {
  154. from = Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z);
  155. to = Vector3(p_aabb.position.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  156. } break;
  157. case 10: {
  158. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z);
  159. to = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y + p_aabb.size.y, p_aabb.position.z);
  160. } break;
  161. case 11: {
  162. from = Vector3(p_aabb.position.x + p_aabb.size.x, p_aabb.position.y, p_aabb.position.z + p_aabb.size.z);
  163. 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);
  164. } break;
  165. }
  166. Vector3 e1 = from - to;
  167. for (int j = 0; j < 3; j++) {
  168. Vector3 e2 = edge_norms[j];
  169. Vector3 axis = vec3_cross(e1, e2);
  170. if (axis.length_squared() < 0.0001f) {
  171. continue; // coplanar
  172. }
  173. //axis.normalize();
  174. Vector3 sup2 = Vector3(
  175. (axis.x > 0) ? -half_extents.x : half_extents.x,
  176. (axis.y > 0) ? -half_extents.y : half_extents.y,
  177. (axis.z > 0) ? -half_extents.z : half_extents.z);
  178. real_t maxB = axis.dot(ofs + sup2);
  179. real_t minB = axis.dot(ofs - sup2);
  180. if (minB > maxB) {
  181. SWAP(maxB, minB);
  182. }
  183. real_t minT = 1e20, maxT = -1e20;
  184. for (int k = 0; k < 3; k++) {
  185. real_t vert_d = axis.dot(vertex[k]);
  186. if (vert_d > maxT) {
  187. maxT = vert_d;
  188. }
  189. if (vert_d < minT) {
  190. minT = vert_d;
  191. }
  192. }
  193. if (maxB < minT || maxT < minB) {
  194. return false;
  195. }
  196. }
  197. }
  198. return true;
  199. }
  200. #endif // FACE3_H