face3.cpp 11 KB

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