face3.h 9.0 KB

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