face3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*************************************************************************/
  2. /* face3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #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], 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() < CMP_EPSILON2);
  99. }
  100. Face3::Side Face3::get_side_of(const Face3 &p_face, ClockDirection p_clock_dir) const {
  101. int over = 0, under = 0;
  102. Plane plane = get_plane(p_clock_dir);
  103. for (int i = 0; i < 3; i++) {
  104. const Vector3 &v = p_face.vertex[i];
  105. if (plane.has_point(v)) { //coplanar, don't bother
  106. continue;
  107. }
  108. if (plane.is_point_over(v)) {
  109. over++;
  110. } else {
  111. under++;
  112. }
  113. }
  114. if (over > 0 && under == 0) {
  115. return SIDE_OVER;
  116. } else if (under > 0 && over == 0) {
  117. return SIDE_UNDER;
  118. } else if (under == 0 && over == 0) {
  119. return SIDE_COPLANAR;
  120. } else {
  121. return SIDE_SPANNING;
  122. }
  123. }
  124. Vector3 Face3::get_random_point_inside() const {
  125. real_t a = Math::random(0, 1);
  126. real_t b = Math::random(0, 1);
  127. if (a > b) {
  128. SWAP(a, b);
  129. }
  130. return vertex[0] * a + vertex[1] * (b - a) + vertex[2] * (1.0 - b);
  131. }
  132. Plane Face3::get_plane(ClockDirection p_dir) const {
  133. return Plane(vertex[0], vertex[1], vertex[2], p_dir);
  134. }
  135. Vector3 Face3::get_median_point() const {
  136. return (vertex[0] + vertex[1] + vertex[2]) / 3.0;
  137. }
  138. real_t Face3::get_area() const {
  139. return vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]).length();
  140. }
  141. ClockDirection Face3::get_clock_dir() const {
  142. Vector3 normal = vec3_cross(vertex[0] - vertex[1], vertex[0] - vertex[2]);
  143. //printf("normal is %g,%g,%g x %g,%g,%g- wtfu is %g\n",tofloat(normal.x),tofloat(normal.y),tofloat(normal.z),tofloat(vertex[0].x),tofloat(vertex[0].y),tofloat(vertex[0].z),tofloat( normal.dot( vertex[0] ) ) );
  144. return (normal.dot(vertex[0]) >= 0) ? CLOCKWISE : COUNTERCLOCKWISE;
  145. }
  146. bool Face3::intersects_aabb(const AABB &p_aabb) const {
  147. /** TEST PLANE **/
  148. if (!p_aabb.intersects_plane(get_plane())) {
  149. return false;
  150. }
  151. #define TEST_AXIS(m_ax) \
  152. /** TEST FACE AXIS */ \
  153. { \
  154. real_t aabb_min = p_aabb.position.m_ax; \
  155. real_t aabb_max = p_aabb.position.m_ax + p_aabb.size.m_ax; \
  156. real_t tri_min = vertex[0].m_ax; \
  157. real_t tri_max = vertex[0].m_ax; \
  158. for (int i = 1; i < 3; i++) { \
  159. if (vertex[i].m_ax > tri_max) \
  160. tri_max = vertex[i].m_ax; \
  161. if (vertex[i].m_ax < tri_min) \
  162. tri_min = vertex[i].m_ax; \
  163. } \
  164. \
  165. if (tri_max < aabb_min || aabb_max < tri_min) \
  166. return false; \
  167. }
  168. TEST_AXIS(x);
  169. TEST_AXIS(y);
  170. TEST_AXIS(z);
  171. /** TEST ALL EDGES **/
  172. Vector3 edge_norms[3] = {
  173. vertex[0] - vertex[1],
  174. vertex[1] - vertex[2],
  175. vertex[2] - vertex[0],
  176. };
  177. for (int i = 0; i < 12; i++) {
  178. Vector3 from, to;
  179. p_aabb.get_edge(i, from, to);
  180. Vector3 e1 = from - to;
  181. for (int j = 0; j < 3; j++) {
  182. Vector3 e2 = edge_norms[j];
  183. Vector3 axis = vec3_cross(e1, e2);
  184. if (axis.length_squared() < 0.0001) {
  185. continue; // coplanar
  186. }
  187. axis.normalize();
  188. real_t minA, maxA, minB, maxB;
  189. p_aabb.project_range_in_plane(Plane(axis, 0), minA, maxA);
  190. project_range(axis, Transform(), minB, maxB);
  191. if (maxA < minB || maxB < minA) {
  192. return false;
  193. }
  194. }
  195. }
  196. return true;
  197. }
  198. Face3::operator String() const {
  199. return String() + vertex[0] + ", " + vertex[1] + ", " + vertex[2];
  200. }
  201. void Face3::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  202. for (int i = 0; i < 3; i++) {
  203. Vector3 v = p_transform.xform(vertex[i]);
  204. real_t d = p_normal.dot(v);
  205. if (i == 0 || d > r_max) {
  206. r_max = d;
  207. }
  208. if (i == 0 || d < r_min) {
  209. r_min = d;
  210. }
  211. }
  212. }
  213. void Face3::get_support(const Vector3 &p_normal, const Transform &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const {
  214. #define _FACE_IS_VALID_SUPPORT_THRESHOLD 0.98
  215. #define _EDGE_IS_VALID_SUPPORT_THRESHOLD 0.05
  216. if (p_max <= 0) {
  217. return;
  218. }
  219. Vector3 n = p_transform.basis.xform_inv(p_normal);
  220. /** TEST FACE AS SUPPORT **/
  221. if (get_plane().normal.dot(n) > _FACE_IS_VALID_SUPPORT_THRESHOLD) {
  222. *p_count = MIN(3, p_max);
  223. for (int i = 0; i < *p_count; i++) {
  224. p_vertices[i] = p_transform.xform(vertex[i]);
  225. }
  226. return;
  227. }
  228. /** FIND SUPPORT VERTEX **/
  229. int vert_support_idx = -1;
  230. real_t support_max = 0;
  231. for (int i = 0; i < 3; i++) {
  232. real_t d = n.dot(vertex[i]);
  233. if (i == 0 || d > support_max) {
  234. support_max = d;
  235. vert_support_idx = i;
  236. }
  237. }
  238. /** TEST EDGES AS SUPPORT **/
  239. for (int i = 0; i < 3; i++) {
  240. if (i != vert_support_idx && i + 1 != vert_support_idx) {
  241. continue;
  242. }
  243. // check if edge is valid as a support
  244. real_t dot = (vertex[i] - vertex[(i + 1) % 3]).normalized().dot(n);
  245. dot = ABS(dot);
  246. if (dot < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  247. *p_count = MIN(2, p_max);
  248. for (int j = 0; j < *p_count; j++) {
  249. p_vertices[j] = p_transform.xform(vertex[(j + i) % 3]);
  250. }
  251. return;
  252. }
  253. }
  254. *p_count = 1;
  255. p_vertices[0] = p_transform.xform(vertex[vert_support_idx]);
  256. }
  257. Vector3 Face3::get_closest_point_to(const Vector3 &p_point) const {
  258. Vector3 edge0 = vertex[1] - vertex[0];
  259. Vector3 edge1 = vertex[2] - vertex[0];
  260. Vector3 v0 = vertex[0] - p_point;
  261. real_t a = edge0.dot(edge0);
  262. real_t b = edge0.dot(edge1);
  263. real_t c = edge1.dot(edge1);
  264. real_t d = edge0.dot(v0);
  265. real_t e = edge1.dot(v0);
  266. real_t det = a * c - b * b;
  267. real_t s = b * e - c * d;
  268. real_t t = b * d - a * e;
  269. if (s + t < det) {
  270. if (s < 0.f) {
  271. if (t < 0.f) {
  272. if (d < 0.f) {
  273. s = CLAMP(-d / a, 0.f, 1.f);
  274. t = 0.f;
  275. } else {
  276. s = 0.f;
  277. t = CLAMP(-e / c, 0.f, 1.f);
  278. }
  279. } else {
  280. s = 0.f;
  281. t = CLAMP(-e / c, 0.f, 1.f);
  282. }
  283. } else if (t < 0.f) {
  284. s = CLAMP(-d / a, 0.f, 1.f);
  285. t = 0.f;
  286. } else {
  287. real_t invDet = 1.f / det;
  288. s *= invDet;
  289. t *= invDet;
  290. }
  291. } else {
  292. if (s < 0.f) {
  293. real_t tmp0 = b + d;
  294. real_t tmp1 = c + e;
  295. if (tmp1 > tmp0) {
  296. real_t numer = tmp1 - tmp0;
  297. real_t denom = a - 2 * b + c;
  298. s = CLAMP(numer / denom, 0.f, 1.f);
  299. t = 1 - s;
  300. } else {
  301. t = CLAMP(-e / c, 0.f, 1.f);
  302. s = 0.f;
  303. }
  304. } else if (t < 0.f) {
  305. if (a + d > b + e) {
  306. real_t numer = c + e - b - d;
  307. real_t denom = a - 2 * b + c;
  308. s = CLAMP(numer / denom, 0.f, 1.f);
  309. t = 1 - s;
  310. } else {
  311. s = CLAMP(-d / a, 0.f, 1.f);
  312. t = 0.f;
  313. }
  314. } else {
  315. real_t numer = c + e - b - d;
  316. real_t denom = a - 2 * b + c;
  317. s = CLAMP(numer / denom, 0.f, 1.f);
  318. t = 1.f - s;
  319. }
  320. }
  321. return vertex[0] + s * edge0 + t * edge1;
  322. }