face3.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**************************************************************************/
  2. /* face3.cpp */
  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. #include "face3.h"
  31. #include "core/math/geometry_3d.h"
  32. int Face3::split_by_plane(const Plane &p_plane, Face3 p_res[3], bool p_is_point_over[3]) const {
  33. ERR_FAIL_COND_V(is_degenerate(), 0);
  34. Vector3 above[4];
  35. int above_count = 0;
  36. Vector3 below[4];
  37. int below_count = 0;
  38. for (int i = 0; i < 3; i++) {
  39. if (p_plane.has_point(vertex[i], (real_t)CMP_EPSILON)) { // point is in plane
  40. ERR_FAIL_COND_V(above_count >= 4, 0);
  41. above[above_count++] = vertex[i];
  42. ERR_FAIL_COND_V(below_count >= 4, 0);
  43. below[below_count++] = vertex[i];
  44. } else {
  45. if (p_plane.is_point_over(vertex[i])) {
  46. //Point is over
  47. ERR_FAIL_COND_V(above_count >= 4, 0);
  48. above[above_count++] = vertex[i];
  49. } else {
  50. //Point is under
  51. ERR_FAIL_COND_V(below_count >= 4, 0);
  52. below[below_count++] = vertex[i];
  53. }
  54. /* Check for Intersection between this and the next vertex*/
  55. Vector3 inters;
  56. if (!p_plane.intersects_segment(vertex[i], vertex[(i + 1) % 3], &inters)) {
  57. continue;
  58. }
  59. /* Intersection goes to both */
  60. ERR_FAIL_COND_V(above_count >= 4, 0);
  61. above[above_count++] = inters;
  62. ERR_FAIL_COND_V(below_count >= 4, 0);
  63. below[below_count++] = inters;
  64. }
  65. }
  66. int polygons_created = 0;
  67. ERR_FAIL_COND_V(above_count >= 4 && below_count >= 4, 0); //bug in the algo
  68. if (above_count >= 3) {
  69. p_res[polygons_created] = Face3(above[0], above[1], above[2]);
  70. p_is_point_over[polygons_created] = true;
  71. polygons_created++;
  72. if (above_count == 4) {
  73. p_res[polygons_created] = Face3(above[2], above[3], above[0]);
  74. p_is_point_over[polygons_created] = true;
  75. polygons_created++;
  76. }
  77. }
  78. if (below_count >= 3) {
  79. p_res[polygons_created] = Face3(below[0], below[1], below[2]);
  80. p_is_point_over[polygons_created] = false;
  81. polygons_created++;
  82. if (below_count == 4) {
  83. p_res[polygons_created] = Face3(below[2], below[3], below[0]);
  84. p_is_point_over[polygons_created] = false;
  85. polygons_created++;
  86. }
  87. }
  88. return polygons_created;
  89. }
  90. bool Face3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const {
  91. return Geometry3D::ray_intersects_triangle(p_from, p_dir, vertex[0], vertex[1], vertex[2], p_intersection);
  92. }
  93. bool Face3::intersects_segment(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *p_intersection) const {
  94. return Geometry3D::segment_intersects_triangle(p_from, p_dir, vertex[0], vertex[1], vertex[2], p_intersection);
  95. }
  96. bool Face3::is_degenerate() const {
  97. Vector3 normal = vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]);
  98. return (normal.length_squared() < (real_t)CMP_EPSILON2);
  99. }
  100. Vector3 Face3::get_random_point_inside() const {
  101. real_t a = Math::random(0.0, 1.0);
  102. real_t b = Math::random(0.0, 1.0);
  103. if (a > b) {
  104. SWAP(a, b);
  105. }
  106. return vertex[0] * a + vertex[1] * (b - a) + vertex[2] * (1.0f - b);
  107. }
  108. Plane Face3::get_plane(ClockDirection p_dir) const {
  109. return Plane(vertex[0], vertex[1], vertex[2], p_dir);
  110. }
  111. real_t Face3::get_area() const {
  112. return vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]).length() * 0.5f;
  113. }
  114. bool Face3::intersects_aabb(const AABB &p_aabb) const {
  115. /** TEST PLANE **/
  116. if (!p_aabb.intersects_plane(get_plane())) {
  117. return false;
  118. }
  119. #define TEST_AXIS(m_ax) \
  120. /** TEST FACE AXIS */ \
  121. { \
  122. real_t aabb_min = p_aabb.position.m_ax; \
  123. real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \
  124. real_t tri_min = vertex[0].m_ax; \
  125. real_t tri_max = vertex[0].m_ax; \
  126. for (int i = 1; i < 3; i++) { \
  127. if (vertex[i].m_ax > tri_max) \
  128. tri_max = vertex[i].m_ax; \
  129. if (vertex[i].m_ax < tri_min) \
  130. tri_min = vertex[i].m_ax; \
  131. } \
  132. \
  133. if (tri_max < aabb_min || aabb_max < tri_min) \
  134. return false; \
  135. }
  136. TEST_AXIS(x);
  137. TEST_AXIS(y);
  138. TEST_AXIS(z);
  139. /** TEST ALL EDGES **/
  140. const Vector3 edge_norms[3] = {
  141. vertex[0] - vertex[1],
  142. vertex[1] - vertex[2],
  143. vertex[2] - vertex[0],
  144. };
  145. for (int i = 0; i < 12; i++) {
  146. Vector3 from, to;
  147. p_aabb.get_edge(i, from, to);
  148. Vector3 e1 = from - to;
  149. for (int j = 0; j < 3; j++) {
  150. Vector3 e2 = edge_norms[j];
  151. Vector3 axis = vec3_cross(e1, e2);
  152. if (axis.length_squared() < 0.0001f) {
  153. continue; // coplanar
  154. }
  155. axis.normalize();
  156. real_t minA, maxA, minB, maxB;
  157. p_aabb.project_range_in_plane(Plane(axis), minA, maxA);
  158. project_range(axis, Transform3D(), minB, maxB);
  159. if (maxA < minB || maxB < minA) {
  160. return false;
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. Face3::operator String() const {
  167. return String() + vertex[0] + ", " + vertex[1] + ", " + vertex[2];
  168. }
  169. void Face3::project_range(const Vector3 &p_normal, const Transform3D &p_transform, real_t &r_min, real_t &r_max) const {
  170. for (int i = 0; i < 3; i++) {
  171. Vector3 v = p_transform.xform(vertex[i]);
  172. real_t d = p_normal.dot(v);
  173. if (i == 0 || d > r_max) {
  174. r_max = d;
  175. }
  176. if (i == 0 || d < r_min) {
  177. r_min = d;
  178. }
  179. }
  180. }
  181. void Face3::get_support(const Vector3 &p_normal, const Transform3D &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const {
  182. constexpr double face_support_threshold = 0.98;
  183. constexpr double edge_support_threshold = 0.05;
  184. if (p_max <= 0) {
  185. return;
  186. }
  187. Vector3 n = p_transform.basis.xform_inv(p_normal);
  188. /** TEST FACE AS SUPPORT **/
  189. if (get_plane().normal.dot(n) > face_support_threshold) {
  190. *p_count = MIN(3, p_max);
  191. for (int i = 0; i < *p_count; i++) {
  192. p_vertices[i] = p_transform.xform(vertex[i]);
  193. }
  194. return;
  195. }
  196. /** FIND SUPPORT VERTEX **/
  197. int vert_support_idx = -1;
  198. real_t support_max = 0;
  199. for (int i = 0; i < 3; i++) {
  200. real_t d = n.dot(vertex[i]);
  201. if (i == 0 || d > support_max) {
  202. support_max = d;
  203. vert_support_idx = i;
  204. }
  205. }
  206. /** TEST EDGES AS SUPPORT **/
  207. for (int i = 0; i < 3; i++) {
  208. if (i != vert_support_idx && i + 1 != vert_support_idx) {
  209. continue;
  210. }
  211. // check if edge is valid as a support
  212. real_t dot = (vertex[i] - vertex[(i + 1) % 3]).normalized().dot(n);
  213. dot = ABS(dot);
  214. if (dot < edge_support_threshold) {
  215. *p_count = MIN(2, p_max);
  216. for (int j = 0; j < *p_count; j++) {
  217. p_vertices[j] = p_transform.xform(vertex[(j + i) % 3]);
  218. }
  219. return;
  220. }
  221. }
  222. *p_count = 1;
  223. p_vertices[0] = p_transform.xform(vertex[vert_support_idx]);
  224. }
  225. Vector3 Face3::get_closest_point_to(const Vector3 &p_point) const {
  226. Vector3 edge0 = vertex[1] - vertex[0];
  227. Vector3 edge1 = vertex[2] - vertex[0];
  228. Vector3 v0 = vertex[0] - p_point;
  229. real_t a = edge0.dot(edge0);
  230. real_t b = edge0.dot(edge1);
  231. real_t c = edge1.dot(edge1);
  232. real_t d = edge0.dot(v0);
  233. real_t e = edge1.dot(v0);
  234. real_t det = a * c - b * b;
  235. real_t s = b * e - c * d;
  236. real_t t = b * d - a * e;
  237. if (s + t < det) {
  238. if (s < 0.f) {
  239. if (t < 0.f) {
  240. if (d < 0.f) {
  241. s = CLAMP(-d / a, 0.f, 1.f);
  242. t = 0.f;
  243. } else {
  244. s = 0.f;
  245. t = CLAMP(-e / c, 0.f, 1.f);
  246. }
  247. } else {
  248. s = 0.f;
  249. t = CLAMP(-e / c, 0.f, 1.f);
  250. }
  251. } else if (t < 0.f) {
  252. s = CLAMP(-d / a, 0.f, 1.f);
  253. t = 0.f;
  254. } else {
  255. real_t invDet = 1.f / det;
  256. s *= invDet;
  257. t *= invDet;
  258. }
  259. } else {
  260. if (s < 0.f) {
  261. real_t tmp0 = b + d;
  262. real_t tmp1 = c + e;
  263. if (tmp1 > tmp0) {
  264. real_t numer = tmp1 - tmp0;
  265. real_t denom = a - 2 * b + c;
  266. s = CLAMP(numer / denom, 0.f, 1.f);
  267. t = 1 - s;
  268. } else {
  269. t = CLAMP(-e / c, 0.f, 1.f);
  270. s = 0.f;
  271. }
  272. } else if (t < 0.f) {
  273. if (a + d > b + e) {
  274. real_t numer = c + e - b - d;
  275. real_t denom = a - 2 * b + c;
  276. s = CLAMP(numer / denom, 0.f, 1.f);
  277. t = 1 - s;
  278. } else {
  279. s = CLAMP(-d / a, 0.f, 1.f);
  280. t = 0.f;
  281. }
  282. } else {
  283. real_t numer = c + e - b - d;
  284. real_t denom = a - 2 * b + c;
  285. s = CLAMP(numer / denom, 0.f, 1.f);
  286. t = 1.f - s;
  287. }
  288. }
  289. return vertex[0] + s * edge0 + t * edge1;
  290. }