face3.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*************************************************************************/
  2. /* face3.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "face3.h"
  30. #include "geometry.h"
  31. int Face3::split_by_plane(const Plane& p_plane,Face3 p_res[3],bool p_is_point_over[3]) const {
  32. ERR_FAIL_COND_V(is_degenerate(),0);
  33. Vector3 above[4];
  34. int above_count=0;
  35. Vector3 below[4];
  36. int below_count=0;
  37. for (int i=0;i<3;i++) {
  38. if (p_plane.has_point( vertex[i], CMP_EPSILON )) { // point is in plane
  39. ERR_FAIL_COND_V(above_count>=4,0);
  40. above[above_count++]=vertex[i];
  41. ERR_FAIL_COND_V(below_count>=4,0);
  42. below[below_count++]=vertex[i];
  43. } else {
  44. if (p_plane.is_point_over( vertex[i])) {
  45. //Point is over
  46. ERR_FAIL_COND_V(above_count>=4,0);
  47. above[above_count++]=vertex[i];
  48. } else {
  49. //Point is under
  50. ERR_FAIL_COND_V(below_count>=4,0);
  51. below[below_count++]=vertex[i];
  52. }
  53. /* Check for Intersection between this and the next vertex*/
  54. Vector3 inters;
  55. if (!p_plane.intersects_segment( vertex[i],vertex[(i+1)%3],&inters))
  56. continue;
  57. /* Intersection goes to both */
  58. ERR_FAIL_COND_V(above_count>=4,0);
  59. above[above_count++]=inters;
  60. ERR_FAIL_COND_V(below_count>=4,0);
  61. below[below_count++]=inters;
  62. }
  63. }
  64. int polygons_created=0;
  65. ERR_FAIL_COND_V( above_count>=4 && below_count>=4 , 0 ); //bug in the algo
  66. if (above_count>=3) {
  67. p_res[polygons_created]=Face3( above[0], above[1], above[2] );
  68. p_is_point_over[polygons_created]=true;
  69. polygons_created++;
  70. if (above_count==4) {
  71. p_res[polygons_created]=Face3( above[2], above[3], above[0] );
  72. p_is_point_over[polygons_created]=true;
  73. polygons_created++;
  74. }
  75. }
  76. if (below_count>=3) {
  77. p_res[polygons_created]=Face3( below[0], below[1], below[2] );
  78. p_is_point_over[polygons_created]=false;
  79. polygons_created++;
  80. if (below_count==4) {
  81. p_res[polygons_created]=Face3( below[2], below[3], below[0] );
  82. p_is_point_over[polygons_created]=false;
  83. polygons_created++;
  84. }
  85. }
  86. return polygons_created;
  87. }
  88. bool Face3::intersects_ray(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection) const {
  89. return Geometry::ray_intersects_triangle(p_from,p_dir,vertex[0],vertex[1],vertex[2],p_intersection);
  90. }
  91. bool Face3::intersects_segment(const Vector3& p_from,const Vector3& p_dir,Vector3 * p_intersection) const {
  92. return Geometry::segment_intersects_triangle(p_from,p_dir,vertex[0],vertex[1],vertex[2],p_intersection);
  93. }
  94. bool Face3::is_degenerate() const {
  95. Vector3 normal=vec3_cross(vertex[0]-vertex[1], vertex[0]-vertex[2]);
  96. return (normal.length_squared() < CMP_EPSILON2);
  97. }
  98. Face3::Side Face3::get_side_of(const Face3& p_face,ClockDirection p_clock_dir) const {
  99. int over=0,under=0;
  100. Plane plane=get_plane(p_clock_dir);
  101. for (int i=0;i<3;i++) {
  102. const Vector3 &v=p_face.vertex[i];
  103. if (plane.has_point(v)) //coplanar, dont bother
  104. continue;
  105. if (plane.is_point_over(v))
  106. over++;
  107. else
  108. under++;
  109. }
  110. if ( over > 0 && under == 0 )
  111. return SIDE_OVER;
  112. else if (under > 0 && over ==0 )
  113. return SIDE_UNDER;
  114. else if (under ==0 && over == 0)
  115. return SIDE_COPLANAR;
  116. else
  117. return SIDE_SPANNING;
  118. }
  119. Vector3 Face3::get_random_point_inside() const {
  120. float a=Math::random(0,1);
  121. float b=Math::random(0,1);
  122. if (a>b) {
  123. SWAP(a,b);
  124. }
  125. return vertex[0]*a + vertex[1]*(b-a) + vertex[2]*(1.0-b);
  126. }
  127. Plane Face3::get_plane(ClockDirection p_dir) const {
  128. return Plane( vertex[0], vertex[1], vertex[2] , p_dir );
  129. }
  130. Vector3 Face3::get_median_point() const {
  131. return (vertex[0] + vertex[1] + vertex[2])/3.0;
  132. }
  133. real_t Face3::get_area() const {
  134. return vec3_cross(vertex[0]-vertex[1], vertex[0]-vertex[2]).length();
  135. }
  136. ClockDirection Face3::get_clock_dir() const {
  137. Vector3 normal=vec3_cross(vertex[0]-vertex[1], vertex[0]-vertex[2]);
  138. //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] ) ) );
  139. return ( normal.dot( vertex[0] ) >= 0 ) ? CLOCKWISE : COUNTERCLOCKWISE;
  140. }
  141. bool Face3::intersects_aabb(const AABB& p_aabb) const {
  142. /** TEST PLANE **/
  143. if (!p_aabb.intersects_plane( get_plane() ))
  144. return false;
  145. /** TEST FACE AXIS */
  146. #define TEST_AXIS(m_ax)\
  147. {\
  148. float aabb_min=p_aabb.pos.m_ax;\
  149. float aabb_max=p_aabb.pos.m_ax+p_aabb.size.m_ax;\
  150. float tri_min,tri_max;\
  151. for (int i=0;i<3;i++) {\
  152. if (i==0 || vertex[i].m_ax > tri_max)\
  153. tri_max=vertex[i].m_ax;\
  154. if (i==0 || vertex[i].m_ax < tri_min)\
  155. tri_min=vertex[i].m_ax;\
  156. }\
  157. \
  158. if (tri_max<aabb_min || aabb_max<tri_min)\
  159. return false;\
  160. }
  161. TEST_AXIS(x);
  162. TEST_AXIS(y);
  163. TEST_AXIS(z);
  164. /** TEST ALL EDGES **/
  165. Vector3 edge_norms[3]={
  166. vertex[0]-vertex[1],
  167. vertex[1]-vertex[2],
  168. vertex[2]-vertex[0],
  169. };
  170. for (int i=0;i<12;i++) {
  171. Vector3 from,to;
  172. p_aabb.get_edge(i,from,to);
  173. Vector3 e1=from-to;
  174. for (int j=0;j<3;j++) {
  175. Vector3 e2=edge_norms[j];
  176. Vector3 axis=vec3_cross( e1, e2 );
  177. if (axis.length_squared()<0.0001)
  178. continue; // coplanar
  179. axis.normalize();
  180. float minA,maxA,minB,maxB;
  181. p_aabb.project_range_in_plane(Plane(axis,0),minA,maxA);
  182. project_range(axis,Transform(),minB,maxB);
  183. if (maxA<minB || maxB<minA)
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. Face3::operator String() const {
  190. return String()+vertex[0]+", "+vertex[1]+", "+vertex[2];
  191. }
  192. void Face3::project_range(const Vector3& p_normal,const Transform& p_transform,float& r_min, float& r_max) const {
  193. for (int i=0;i<3;i++) {
  194. Vector3 v=p_transform.xform(vertex[i]);
  195. float d=p_normal.dot(v);
  196. if (i==0 || d > r_max)
  197. r_max=d;
  198. if (i==0 || d < r_min)
  199. r_min=d;
  200. }
  201. }
  202. void Face3::get_support(const Vector3& p_normal,const Transform& p_transform,Vector3 *p_vertices,int* p_count,int p_max) const {
  203. #define _FACE_IS_VALID_SUPPORT_TRESHOLD 0.98
  204. #define _EDGE_IS_VALID_SUPPORT_TRESHOLD 0.05
  205. if (p_max<=0)
  206. return;
  207. Vector3 n=p_transform.basis.xform_inv(p_normal);
  208. /** TEST FACE AS SUPPORT **/
  209. if (get_plane().normal.dot(n) > _FACE_IS_VALID_SUPPORT_TRESHOLD) {
  210. *p_count=MIN(3,p_max);
  211. for (int i=0;i<*p_count;i++) {
  212. p_vertices[i]=p_transform.xform(vertex[i]);
  213. }
  214. return;
  215. }
  216. /** FIND SUPPORT VERTEX **/
  217. int vert_support_idx=-1;
  218. float support_max;
  219. for (int i=0;i<3;i++) {
  220. float d=n.dot(vertex[i]);
  221. if (i==0 || d > support_max) {
  222. support_max=d;
  223. vert_support_idx=i;
  224. }
  225. }
  226. /** TEST EDGES AS SUPPORT **/
  227. for (int i=0;i<3;i++) {
  228. if (i!=vert_support_idx && i+1!=vert_support_idx)
  229. continue;
  230. // check if edge is valid as a support
  231. float dot=(vertex[i]-vertex[(i+1)%3]).normalized().dot(n);
  232. dot=ABS(dot);
  233. if (dot < _EDGE_IS_VALID_SUPPORT_TRESHOLD) {
  234. *p_count=MIN(2,p_max);
  235. for (int j=0;j<*p_count;j++)
  236. p_vertices[j]=p_transform.xform(vertex[(j+i)%3]);
  237. return;
  238. }
  239. }
  240. *p_count=1;
  241. p_vertices[0]=p_transform.xform(vertex[vert_support_idx]);
  242. }
  243. Vector3 Face3::get_closest_point_to(const Vector3& p_point) const {
  244. Vector3 edge0 = vertex[1] - vertex[0];
  245. Vector3 edge1 = vertex[2] - vertex[0];
  246. Vector3 v0 = vertex[0] - p_point;
  247. float a = edge0.dot( edge0 );
  248. float b = edge0.dot( edge1 );
  249. float c = edge1.dot( edge1 );
  250. float d = edge0.dot( v0 );
  251. float e = edge1.dot( v0 );
  252. float det = a*c - b*b;
  253. float s = b*e - c*d;
  254. float t = b*d - a*e;
  255. if ( s + t < det )
  256. {
  257. if ( s < 0.f )
  258. {
  259. if ( t < 0.f )
  260. {
  261. if ( d < 0.f )
  262. {
  263. s = CLAMP( -d/a, 0.f, 1.f );
  264. t = 0.f;
  265. }
  266. else
  267. {
  268. s = 0.f;
  269. t = CLAMP( -e/c, 0.f, 1.f );
  270. }
  271. }
  272. else
  273. {
  274. s = 0.f;
  275. t = CLAMP( -e/c, 0.f, 1.f );
  276. }
  277. }
  278. else if ( t < 0.f )
  279. {
  280. s = CLAMP( -d/a, 0.f, 1.f );
  281. t = 0.f;
  282. }
  283. else
  284. {
  285. float invDet = 1.f / det;
  286. s *= invDet;
  287. t *= invDet;
  288. }
  289. }
  290. else
  291. {
  292. if ( s < 0.f )
  293. {
  294. float tmp0 = b+d;
  295. float tmp1 = c+e;
  296. if ( tmp1 > tmp0 )
  297. {
  298. float numer = tmp1 - tmp0;
  299. float denom = a-2*b+c;
  300. s = CLAMP( numer/denom, 0.f, 1.f );
  301. t = 1-s;
  302. }
  303. else
  304. {
  305. t = CLAMP( -e/c, 0.f, 1.f );
  306. s = 0.f;
  307. }
  308. }
  309. else if ( t < 0.f )
  310. {
  311. if ( a+d > b+e )
  312. {
  313. float numer = c+e-b-d;
  314. float denom = a-2*b+c;
  315. s = CLAMP( numer/denom, 0.f, 1.f );
  316. t = 1-s;
  317. }
  318. else
  319. {
  320. s = CLAMP( -e/c, 0.f, 1.f );
  321. t = 0.f;
  322. }
  323. }
  324. else
  325. {
  326. float numer = c+e-b-d;
  327. float denom = a-2*b+c;
  328. s = CLAMP( numer/denom, 0.f, 1.f );
  329. t = 1.f - s;
  330. }
  331. }
  332. return vertex[0] + s * edge0 + t * edge1;
  333. }