Mesh Cuts.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. CutsCache::~CutsCache()
  6. {
  7. DeleteN(box_face);
  8. }
  9. CutsCache::CutsCache(C MeshBase &mshb)
  10. {
  11. box=mshb;
  12. boxes.set(box, mshb.faces());
  13. New(box_face, boxes.num ());
  14. // add faces to boxes
  15. C Vec *pos=mshb.vtx.pos();
  16. REPA(mshb.tri) // triangles
  17. {
  18. C Int *p =mshb.tri.ind(i).c;
  19. Box box =pos[p[0]];
  20. box|=pos[p[1]];
  21. box|=pos[p[2]];
  22. BoxI boxi=boxes.coords(box.extend(EPS));
  23. for(Int z=boxi.min.z; z<=boxi.max.z; z++)
  24. for(Int y=boxi.min.y; y<=boxi.max.y; y++)
  25. for(Int x=boxi.min.x; x<=boxi.max.x; x++)box_face[boxes.index(VecI(x, y, z))].add(i);
  26. }
  27. REPA(mshb.quad) // quads
  28. {
  29. C Int *p =mshb.quad.ind(i).c;
  30. Box box =pos[p[0]];
  31. box|=pos[p[1]];
  32. box|=pos[p[2]];
  33. box|=pos[p[3]];
  34. BoxI boxi=boxes.coords(box.extend(EPS));
  35. for(Int z=boxi.min.z; z<=boxi.max.z; z++)
  36. for(Int y=boxi.min.y; y<=boxi.max.y; y++)
  37. for(Int x=boxi.min.x; x<=boxi.max.x; x++)box_face[boxes.index(VecI(x, y, z))].add(i^SIGN_BIT);
  38. }
  39. }
  40. /******************************************************************************/
  41. // DIST
  42. /******************************************************************************/
  43. DIST_TYPE DistPointMesh(C Vec2 &point, C MeshBase &mshb, UInt flag, Flt *distance, Int *index, UInt test_flag)
  44. {
  45. DIST_TYPE type=DIST_NONE;
  46. Int j=-1;
  47. Flt d, dist=0;
  48. C Int *p;
  49. C Vec *pos =mshb.vtx .pos ();
  50. C VecI2 *edge=mshb.edge.ind ();
  51. C VecI *tri =mshb.tri .ind ();
  52. C VecI4 *quad=mshb.quad.ind ();
  53. C Byte *efl =mshb.edge.flag();
  54. //C Int *eid =mshb.edge.id ();
  55. if(flag& VTX_POS)REPA(mshb.vtx ){ d=Dist(point, pos[ i ].xy ); if(!type || d<dist){dist=d; type=DIST_POINT; j=i;}}
  56. if(flag& TRI_IND)REPA(mshb.tri ){p=tri [i].c; d=Dist(point, Tri2(pos[p[0]].xy, pos[p[1]].xy, pos[p[2]].xy )); if(!type || d<dist){dist=d; type=DIST_TRI ; j=i;}}
  57. if(flag&QUAD_IND)REPA(mshb.quad){p=quad[i].c; d=Dist(point, Quad2(pos[p[0]].xy, pos[p[1]].xy, pos[p[2]].xy, pos[p[3]].xy)); if(!type || d<dist){dist=d; type=DIST_QUAD ; j=i;}}
  58. if(flag&EDGE_IND)REPA(mshb.edge)
  59. {
  60. if(test_flag)
  61. {
  62. if(test_flag&TEST_NO_PHYS && efl && efl[i]&ETQ_NO_PHYS )continue;
  63. //if(test_flag&TEST_DOUBLE_SIDE && eid && (eid[i].x==-1)==(eid[i].y==-1))continue;
  64. }
  65. p=edge[i].c; d=DistPointEdge(point, pos[p[0]].xy, pos[p[1]].xy); if(!type || d<dist){dist=d; type=DIST_EDGE; j=i;}
  66. }
  67. if(distance)*distance=dist;
  68. if(index )*index =j ;
  69. return type;
  70. }
  71. DIST_TYPE DistPointMesh(C Vec &point, C MeshBase &mshb, UInt flag, Flt *distance, Int *index)
  72. {
  73. DIST_TYPE type=DIST_NONE;
  74. Int j=-1;
  75. Flt d, dist=0;
  76. C Int *p;
  77. C Vec *pos =mshb.vtx .pos();
  78. C VecI2 *edge=mshb.edge.ind();
  79. C VecI *tri =mshb.tri .ind();
  80. C VecI4 *quad=mshb.quad.ind();
  81. C Vec *tn =mshb.tri .nrm();
  82. C Vec *qn =mshb.quad.nrm();
  83. if(flag& VTX_POS)REPA(mshb.vtx ){ d=Dist (point, pos[ i ] ); if(!type || d<dist){dist=d; type=DIST_POINT; j=i;}}
  84. if(flag&EDGE_IND)REPA(mshb.edge){p=edge[i].c; d=DistPointEdge(point, pos[p[0]], pos[p[1]] ); if(!type || d<dist){dist=d; type=DIST_EDGE ; j=i;}}
  85. if(flag& TRI_IND)REPA(mshb.tri ){p=tri [i].c; d=Dist (point, Tri(pos[p[0]], pos[p[1]], pos[p[2]] , tn ? &tn[i] : null)); if(!type || d<dist){dist=d; type=DIST_TRI ; j=i;}}
  86. if(flag&QUAD_IND)REPA(mshb.quad){p=quad[i].c; d=Dist (point, Quad(pos[p[0]], pos[p[1]], pos[p[2]], pos[p[3]], qn ? &qn[i] : null)); if(!type || d<dist){dist=d; type=DIST_QUAD ; j=i;}}
  87. if(distance)*distance=dist;
  88. if(index )*index =j ;
  89. return type;
  90. }
  91. /******************************************************************************/
  92. // CUTS
  93. /******************************************************************************/
  94. struct DIST
  95. {
  96. DIST_TYPE type;
  97. Int ind, part;
  98. Flt dist;
  99. Vec nrm;
  100. };
  101. static void DISTAdd(Memt<DIST> &memc, DIST_TYPE type, Flt d, Int ind, Int part, C Vec *nrm=null)
  102. {
  103. REPA(memc)
  104. {
  105. DIST &dist=memc[i];
  106. if(dist.dist>d+EPS)memc.remove(i);
  107. }
  108. DIST &dist=memc.New();
  109. dist.type= type;
  110. dist.ind = ind ;
  111. dist.part= part;
  112. dist.dist= d ;
  113. if(nrm)dist.nrm =*nrm ;
  114. }
  115. /******************************************************************************/
  116. // CUTS 2D
  117. /******************************************************************************/
  118. static Int FindClosest(C Vec2 &point, C MeshBase &mshb, Int part, UInt test_flag, Flt &dist, Memt<DIST> &dists)
  119. {
  120. // later codes assume that we'll add 'DIST' only when there's mshb.edge.flag
  121. if(mshb.edge.flag())FREPA(mshb.edge)
  122. {
  123. if(test_flag)
  124. {
  125. if(test_flag&TEST_NO_PHYS && mshb.edge.flag(i)&ETQ_NO_PHYS)continue;
  126. }
  127. DIST_TYPE type;
  128. C Int *p=mshb.edge.ind(i).c;
  129. Flt d=DistPointEdge(point, mshb.vtx.pos(p[0]).xy, mshb.vtx.pos(p[1]).xy, &type);
  130. if( d<=EPS){dist=0; return (mshb.edge.flag(i)&ETQ_LR) ? +1 : -1;}
  131. if( !dists.elms() || d<=dist+EPS){if(!dists.elms() || d<dist)dist=d; DISTAdd(dists, type, d, i, part);}
  132. }
  133. return 0;
  134. }
  135. Bool CutsPointMesh(C Vec2 &point, C MeshBase &mshb, Flt *distance, UInt test_flag)
  136. {
  137. Bool ret=false;
  138. Flt dist;
  139. Memt<DIST> dists;
  140. // we assume that FindClosest will add 'DIST' only when there's mshb.edge.flag
  141. // get distances
  142. if(Int fc=FindClosest(point, mshb, 0, test_flag, dist, dists)){ret=(fc==1); goto end;}
  143. // check for closest edge
  144. FREPA(dists)
  145. {
  146. DIST &dist=dists[i];
  147. if( dist.type==DIST_EDGE)
  148. {
  149. C Int *p =mshb.edge.ind(dist.ind).c;
  150. C Vec2 &p0=mshb.vtx .pos(p[0] ).xy,
  151. &p1=mshb.vtx .pos(p[1] ).xy;
  152. if(mshb.edge.flag(dist.ind)&((DistPointPlane(point, p0, Perp(p1-p0))>0) ? ETQ_R : ETQ_L))ret=true;
  153. goto end;
  154. }
  155. }
  156. // check for closest point
  157. if(dists.elms()>=2)
  158. {
  159. DIST &center =dists.first();
  160. C Vec2 &center_pos=mshb.vtx.pos(mshb.edge.ind(center.ind).c[center.type-DIST_POINT0]).xy;
  161. Flt point_angle=AngleFast(point-center_pos),
  162. max_angle;
  163. Int max_ind=-1;
  164. Bool point_loose;
  165. for(Int i=1; i<dists.elms(); i++) // for each other point that contact's center
  166. {
  167. DIST &dist=dists[i];
  168. Bool pl; C Int *p=mshb.edge.ind(dist.ind).c;
  169. if(Equal(center_pos, mshb.vtx.pos(p[0]).xy))pl=1;else
  170. if(Equal(center_pos, mshb.vtx.pos(p[1]).xy))pl=0;else continue;
  171. Flt angle=AngleFast(mshb.vtx.pos(p[pl]).xy-center_pos); if(angle>point_angle)angle-=PI2;
  172. if(max_ind<0 || angle>max_angle){max_ind=i; max_angle=angle; point_loose=pl;}
  173. }
  174. if(max_ind>=0)
  175. {
  176. DIST &dist=dists[max_ind];
  177. if(mshb.edge.flag(dist.ind)&(point_loose ? ETQ_L : ETQ_R))ret=true;
  178. }
  179. }
  180. end:;
  181. if(distance)*distance=dist;
  182. return ret;
  183. }
  184. Bool CutsPointMesh(C Vec2 &point, C MeshLod &mesh, Flt *distance, UInt test_flag)
  185. {
  186. Bool ret=false;
  187. Flt dist;
  188. Memt<DIST> dists;
  189. // we assume that FindClosest will add 'DIST' only when there's mshb.edge.flag
  190. // get distances
  191. FREPA(mesh)if(Int fc=FindClosest(point, mesh.parts[i].base, i, test_flag, dist, dists)){ret=(fc==1); goto end;}
  192. // check for closest edge
  193. FREPA(dists)
  194. {
  195. DIST &dist=dists[i];
  196. if( dist.type==DIST_EDGE)
  197. {
  198. C MeshBase &mshb=mesh.parts[dist.part].base;
  199. C Int *p =mshb.edge.ind(dist.ind).c;
  200. C Vec2 &p0 =mshb.vtx .pos(p[0] ).xy,
  201. &p1 =mshb.vtx .pos(p[1] ).xy;
  202. if(mshb.edge.flag(dist.ind)&((DistPointPlane(point, p0, Perp(p1-p0))>0) ? ETQ_R : ETQ_L))ret=true;
  203. goto end;
  204. }
  205. }
  206. // check for closest point
  207. if(dists.elms()>=2)
  208. {
  209. DIST &center =dists.first();
  210. C MeshBase &mshb =mesh.parts[center.part].base;
  211. C Vec2 &center_pos=mshb.vtx.pos(mshb.edge.ind(center.ind).c[center.type-DIST_POINT0]).xy;
  212. Flt point_angle=AngleFast(point-center_pos),
  213. max_angle;
  214. Int max_ind=-1;
  215. Bool point_loose;
  216. for(Int i=1; i<dists.elms(); i++) // for each other point that contact's center
  217. {
  218. DIST &dist=dists[i];
  219. C MeshBase &mshb=mesh.parts[dist.part].base;
  220. Bool pl; C Int *p=mshb.edge.ind(dist.ind).c;
  221. if(Equal(center_pos, mshb.vtx.pos(p[0]).xy))pl=1;else
  222. if(Equal(center_pos, mshb.vtx.pos(p[1]).xy))pl=0;else continue;
  223. Flt angle=AngleFast(mshb.vtx.pos(p[pl]).xy-center_pos); if(angle>point_angle)angle-=PI2;
  224. if(max_ind<0 || angle>max_angle){max_ind=i; max_angle=angle; point_loose=pl;}
  225. }
  226. if(max_ind>=0)
  227. {
  228. DIST &dist=dists[max_ind];
  229. C MeshBase &mshb=mesh.parts[dist.part].base;
  230. if(mshb.edge.flag(dist.ind)&(point_loose ? ETQ_L : ETQ_R))ret=true;
  231. }
  232. }
  233. end:;
  234. if(distance)*distance=dist;
  235. return ret;
  236. }
  237. /******************************************************************************/
  238. // CUTS 3D
  239. /******************************************************************************/
  240. struct CutsAdd
  241. {
  242. Vec point; // testing point
  243. C Vec *pos ; // mshb.vtx.pos
  244. C MeshBase *mshb ;
  245. CutsCache *cache;
  246. Flt *dist ;
  247. Memt<DIST> *dists;
  248. };
  249. static Bool Add(C VecI &coords, CutsAdd &ca)
  250. {
  251. Memc<UInt> &faces=ca.cache->box_face[ca.cache->boxes.index(coords)];
  252. REPA(faces)
  253. {
  254. UInt face=faces[i];
  255. if( face&SIGN_BIT) // quad
  256. {
  257. C Int *p=ca.mshb->quad.ind(face^SIGN_BIT).c;
  258. Quad quad(ca.pos[p[0]], ca.pos[p[1]], ca.pos[p[2]], ca.pos[p[3]], ca.mshb->quad.nrm() ? &ca.mshb->quad.nrm(face^SIGN_BIT) : null);
  259. DIST_TYPE type; Flt d=Dist(ca.point, quad, &type);
  260. if(!ca.dists->elms() || d<=*ca.dist+EPS)
  261. {
  262. if(!ca.dists->elms() || d<=*ca.dist)*ca.dist=d;
  263. DISTAdd(*ca.dists, type, d, face, 0, &quad.n);
  264. }
  265. }
  266. else // triangle
  267. {
  268. C Int *p=ca.mshb->tri.ind(face).c;
  269. Tri tri(ca.pos[p[0]], ca.pos[p[1]], ca.pos[p[2]], ca.mshb->tri.nrm() ? &ca.mshb->tri.nrm(face) : null);
  270. DIST_TYPE type; Flt d=Dist(ca.point, tri, &type);
  271. if(!ca.dists->elms() || d<=*ca.dist+EPS)
  272. {
  273. if(!ca.dists->elms() || d<=*ca.dist)*ca.dist=d;
  274. DISTAdd(*ca.dists, type, d, face, 0, &tri.n);
  275. }
  276. }
  277. }
  278. return faces.elms()!=0;
  279. }
  280. static void FindClosest(C Vec &point, C MeshBase &mshb, Int part, Flt &dist, Memt<DIST> &dists)
  281. {
  282. FREPA(mshb.tri)
  283. {
  284. C Int *p=mshb.tri.ind(i).c; Tri tri(mshb.vtx.pos(p[0]), mshb.vtx.pos(p[1]), mshb.vtx.pos(p[2]), mshb.tri.nrm() ? &mshb.tri.nrm(i) : null);
  285. DIST_TYPE type; Flt d=Dist(point, tri, &type);
  286. if(!dists.elms() || d<=dist+EPS)
  287. {
  288. if(!dists.elms() || d<dist)dist=d;
  289. DISTAdd(dists, type, d, i, part, &tri.n);
  290. }
  291. }
  292. FREPA(mshb.quad)
  293. {
  294. C Int *p=mshb.quad.ind(i).c; Quad quad(mshb.vtx.pos(p[0]), mshb.vtx.pos(p[1]), mshb.vtx.pos(p[2]), mshb.vtx.pos(p[3]), mshb.quad.nrm() ? &mshb.quad.nrm(i) : null);
  295. DIST_TYPE type; Flt d=Dist(point, quad, &type);
  296. if(!dists.elms() || d<=dist+EPS)
  297. {
  298. if(!dists.elms() || d<dist)dist=d;
  299. DISTAdd(dists, type, d, i^SIGN_BIT, part, &quad.n);
  300. }
  301. }
  302. }
  303. Bool CutsPointMesh(C Vec &point, C MeshBase &mshb, Flt *distance, CutsCache *cache)
  304. {
  305. Bool ret=false;
  306. Flt dist;
  307. Memt<DIST> dists;
  308. // get distances
  309. if(cache)
  310. {
  311. CutsAdd ca;
  312. ca.point= point;
  313. ca.pos = mshb.vtx.pos();
  314. ca.mshb =&mshb ;
  315. ca.cache= cache;
  316. ca.dist =&dist ;
  317. ca.dists=&dists;
  318. VecI coords=cache->boxes.coords(point);
  319. if(!Add(coords, ca)) // try center first, if not, then try testing box surface starting with radius=1 and increasing each step
  320. {
  321. VecI min =coords,
  322. max =coords,
  323. size=cache->boxes.cells;
  324. for(;;)
  325. {
  326. min--;
  327. max++;
  328. if(min.x>=0 || min.y>=0 || min.z>=0
  329. || max.x<size.x || max.y<size.y || max.z<size.z)
  330. {
  331. VecI minc=min,
  332. maxc=max;
  333. MAX( minc.x, 0); MIN(maxc.x, size.x-1);
  334. MAX( minc.y, 0); MIN(maxc.y, size.y-1);
  335. MAX( minc.z, 0); MIN(maxc.z, size.z-1);
  336. // all sides need to be checked (can't stop on first found because there were errors)
  337. if(min.x==minc.x)for(Int y=minc.y ; y<=maxc.y; y++)for(Int z=minc.z ; z<=maxc.z; z++)Add(VecI(min.x, y, z), ca); // left
  338. if(max.x==maxc.x)for(Int y=minc.y ; y<=maxc.y; y++)for(Int z=minc.z ; z<=maxc.z; z++)Add(VecI(max.x, y, z), ca); // right
  339. if(min.y==minc.y)for(Int x=minc.x+1; x< maxc.x; x++)for(Int z=minc.z ; z<=maxc.z; z++)Add(VecI(x, min.y, z), ca); // bottom , without left and right end
  340. if(max.y==maxc.y)for(Int x=minc.x+1; x< maxc.x; x++)for(Int z=minc.z ; z<=maxc.z; z++)Add(VecI(x, max.y, z), ca); // top , without left and right end
  341. if(min.z==minc.z)for(Int x=minc.x+1; x< maxc.x; x++)for(Int y=minc.y+1; y< maxc.y; y++)Add(VecI(x, y, min.z), ca); // back , without left right bottom and top end
  342. if(max.z==maxc.z)for(Int x=minc.x+1; x< maxc.x; x++)for(Int y=minc.y+1; y< maxc.y; y++)Add(VecI(x, y, max.z), ca); // forward, without left right bottom and top end
  343. if(ca.dists->elms())break;
  344. }else break;
  345. }
  346. }
  347. }else
  348. {
  349. FindClosest(point, mshb, 0, dist, dists);
  350. }
  351. // check for closest plane
  352. FREPA(dists)
  353. {
  354. DIST &dist=dists[i];
  355. if( dist.type==DIST_PLANE)
  356. {
  357. if(dist.ind&SIGN_BIT) // quad
  358. {
  359. C Int *p =mshb.quad.ind(dist.ind^SIGN_BIT).c;
  360. C Vec &p0=mshb.vtx .pos(p[0]),
  361. &p1=mshb.vtx .pos(p[1]),
  362. &p2=mshb.vtx .pos(p[2]),
  363. &p3=mshb.vtx .pos(p[3]);
  364. Tri tri0(p0, p1, p3),
  365. tri1(p1, p2, p3);
  366. if(DistPointPlane(p2, tri0)<=0) // convex
  367. {
  368. ret=DistPointPlane(point, tri0)<=0
  369. && DistPointPlane(point, tri1)<=0;
  370. }
  371. else // concave
  372. {
  373. ret=DistPointPlane(point, tri0)<=0
  374. || DistPointPlane(point, tri1)<=0;
  375. }
  376. }
  377. else // tri
  378. {
  379. ret=(DistPointPlane(point, mshb.vtx.pos(mshb.tri.ind(dist.ind).c[0]), dist.nrm)<=0);
  380. }
  381. goto end;
  382. }
  383. }
  384. // check for closest edge
  385. {
  386. Bool edge_found=false;
  387. Flt edge_angle_nearest,
  388. edge_angle_delta;
  389. FREPA(dists) // find the edge which 3rd point (loose) is within the smallest angle to the tested one
  390. {
  391. DIST &dist=dists[i];
  392. if( dist.type>=DIST_EDGE0 && dist.type<=DIST_EDGE3)
  393. {
  394. C Int *p;
  395. Int vtxs, ei=dist.type-DIST_EDGE0; // edge index
  396. if(dist.ind&SIGN_BIT){p=mshb.quad.ind(dist.ind^SIGN_BIT).c; vtxs=4;}
  397. else {p=mshb.tri .ind(dist.ind ).c; vtxs=3;}
  398. C Vec &edge_p0=mshb.vtx.pos(p[ ei ]),
  399. &edge_p1=mshb.vtx.pos(p[(ei+1)%vtxs]);
  400. Matrix m; m.setPosDir(edge_p0, !(edge_p1-edge_p0));
  401. Flt point_angle=AngleFast (point , m),
  402. edge_angle=AngleFast (mshb.vtx.pos(p[(ei+2)%vtxs]), m),
  403. delta_angle=AngleDelta( edge_angle, point_angle),
  404. abs_delta_angle=Abs (delta_angle);
  405. if(!edge_found || abs_delta_angle<edge_angle_nearest)
  406. {
  407. edge_found =true;
  408. edge_angle_nearest=abs_delta_angle;
  409. edge_angle_delta = delta_angle;
  410. }
  411. }
  412. }
  413. if(edge_found)
  414. {
  415. if(edge_angle_delta<=0)ret=true;
  416. goto end;
  417. }
  418. }
  419. // check for closest vertex
  420. FREPA(dists)
  421. {
  422. DIST &dist=dists[i];
  423. if( dist.type>=DIST_POINT0 && dist.type<=DIST_POINT3) // get first, set as center
  424. {
  425. C Int *p;
  426. Int pi=dist.type-DIST_POINT0; // point index
  427. if(dist.ind&SIGN_BIT)p=mshb.quad.ind(dist.ind^SIGN_BIT).c;
  428. else p=mshb.tri .ind(dist.ind ).c;
  429. C Vec &center_pos=mshb.vtx.pos(p[pi]);
  430. Vec center_nrm=0;
  431. for(Int j=i; j<dists.elms(); j++) // calculate average triangle normal from all vertexes that share same position with 'center'
  432. {
  433. DIST &dist=dists[j];
  434. if( dist.type>=DIST_POINT0 && dist.type<=DIST_POINT3)
  435. {
  436. Int vtxs, pi=dist.type-DIST_POINT0;
  437. if(dist.ind&SIGN_BIT){p=mshb.quad.ind(dist.ind^SIGN_BIT).c; vtxs=4;}
  438. else {p=mshb.tri .ind(dist.ind ).c; vtxs=3;}
  439. if(Equal(mshb.vtx.pos(p[pi]), center_pos)) // only for those which share the same position
  440. {
  441. center_nrm+=AbsAngleBetween(mshb.vtx.pos(p[(pi+vtxs-1)%vtxs]), mshb.vtx.pos(p[pi]), mshb.vtx.pos(p[(pi+1)%vtxs]))*dist.nrm;
  442. }
  443. }
  444. }
  445. if(DistPointPlane(point, center_pos, center_nrm)<=0)ret=true;
  446. goto end;
  447. }
  448. }
  449. end:;
  450. if(distance)*distance=dist;
  451. return ret;
  452. }
  453. Bool CutsPointMesh(C Vec &point, C MeshLod &mesh, Flt *distance)
  454. {
  455. Bool ret=false;
  456. Flt dist;
  457. Memt<DIST> dists;
  458. // get distances
  459. FREPA(mesh)FindClosest(point, mesh.parts[i].base, i, dist, dists);
  460. // check for closest plane
  461. FREPA(dists)
  462. {
  463. DIST &dist=dists[i];
  464. if( dist.type==DIST_PLANE)
  465. {
  466. C MeshBase &mshb=mesh.parts[dist.part].base;
  467. if(dist.ind&SIGN_BIT) // quad
  468. {
  469. C Int *p =mshb.quad.ind(dist.ind^SIGN_BIT).c;
  470. C Vec &p0=mshb.vtx .pos(p[0]),
  471. &p1=mshb.vtx .pos(p[1]),
  472. &p2=mshb.vtx .pos(p[2]),
  473. &p3=mshb.vtx .pos(p[3]);
  474. Tri tri0(p0, p1, p3),
  475. tri1(p1, p2, p3);
  476. if(DistPointPlane(p2, tri0)<=0) // convex
  477. {
  478. ret=DistPointPlane(point, tri0)<=0
  479. && DistPointPlane(point, tri1)<=0;
  480. }
  481. else // concave
  482. {
  483. ret=DistPointPlane(point, tri0)<=0
  484. || DistPointPlane(point, tri1)<=0;
  485. }
  486. }
  487. else // tri
  488. {
  489. ret=(DistPointPlane(point, mshb.vtx.pos(mshb.tri.ind(dist.ind).c[0]), dist.nrm)<=0);
  490. }
  491. goto end;
  492. }
  493. }
  494. // check for closest edge
  495. {
  496. Bool edge_found=false;
  497. Flt edge_angle_nearest,
  498. edge_angle_delta;
  499. FREPA(dists) // find the edge which 3rd point (loose) is within the smallest angle to the tested one
  500. {
  501. DIST &dist=dists[i];
  502. if( dist.type>=DIST_EDGE0 && dist.type<=DIST_EDGE3)
  503. {
  504. C MeshBase &mshb=mesh.parts[dist.part].base;
  505. C Int *p;
  506. Int vtxs, ei=dist.type-DIST_EDGE0; // edge index
  507. if(dist.ind&SIGN_BIT){p=mshb.quad.ind(dist.ind^SIGN_BIT).c; vtxs=4;}
  508. else {p=mshb.tri .ind(dist.ind ).c; vtxs=3;}
  509. C Vec &edge_p0=mshb.vtx.pos(p[ ei ]),
  510. &edge_p1=mshb.vtx.pos(p[(ei+1)%vtxs]);
  511. Matrix m; m.setPosDir(edge_p0, !(edge_p1-edge_p0));
  512. Flt point_angle=AngleFast (point , m),
  513. edge_angle=AngleFast (mshb.vtx.pos(p[(ei+2)%vtxs]), m),
  514. delta_angle=AngleDelta( edge_angle, point_angle),
  515. abs_delta_angle=Abs (delta_angle);
  516. if(!edge_found || abs_delta_angle<edge_angle_nearest)
  517. {
  518. edge_found =true;
  519. edge_angle_nearest=abs_delta_angle;
  520. edge_angle_delta = delta_angle;
  521. }
  522. }
  523. }
  524. if(edge_found)
  525. {
  526. if(edge_angle_delta<=0)ret=true;
  527. goto end;
  528. }
  529. }
  530. // check for closest vertex
  531. FREPA(dists)
  532. {
  533. DIST &dist=dists[i];
  534. if( dist.type>=DIST_POINT0 && dist.type<=DIST_POINT3) // get first, set as center
  535. {
  536. C MeshBase &mshb=mesh.parts[dist.part].base;
  537. C Int *p;
  538. Int pi=dist.type-DIST_POINT0; // point index
  539. if(dist.ind&SIGN_BIT)p=mshb.quad.ind(dist.ind^SIGN_BIT).c;
  540. else p=mshb.tri .ind(dist.ind ).c;
  541. C Vec &center_pos=mshb.vtx.pos(p[pi]);
  542. Vec center_nrm=0;
  543. for(Int j=i; j<dists.elms(); j++) // calculate average triangle normal from all vertexes that share same position with 'center'
  544. {
  545. DIST &dist=dists[j];
  546. if( dist.type>=DIST_POINT0 && dist.type<=DIST_POINT3)
  547. {
  548. Int vtxs, pi=dist.type-DIST_POINT0;
  549. if(dist.ind&SIGN_BIT){p=mshb.quad.ind(dist.ind^SIGN_BIT).c; vtxs=4;}
  550. else {p=mshb.tri .ind(dist.ind ).c; vtxs=3;}
  551. if(Equal(mshb.vtx.pos(p[pi]), center_pos)) // only for those which share the same position
  552. {
  553. center_nrm+=AbsAngleBetween(mshb.vtx.pos(p[(pi+vtxs-1)%vtxs]), mshb.vtx.pos(p[pi]), mshb.vtx.pos(p[(pi+1)%vtxs]))*dist.nrm;
  554. }
  555. }
  556. }
  557. if(DistPointPlane(point, center_pos, center_nrm)<=0)ret=true;
  558. goto end;
  559. }
  560. }
  561. end:;
  562. if(distance)*distance=dist;
  563. return ret;
  564. }
  565. /******************************************************************************/
  566. Bool CutsPointMesh(C Vec2 &point, C Mesh &mesh, Flt *dist, UInt test_flag)
  567. {
  568. if(Cuts(point, Rect(mesh.ext))){C MeshLod &lod=mesh; return CutsPointMesh(point, lod, dist, test_flag);}
  569. return false;
  570. }
  571. Bool CutsPointMesh(C Vec &point, C Mesh &mesh, Flt *dist)
  572. {
  573. if(Cuts(point, mesh.ext)){C MeshLod &lod=mesh; return CutsPointMesh(point, lod, dist);}
  574. return false;
  575. }
  576. /******************************************************************************/
  577. Bool CutsPointMesh(C Vec2 &point, C MeshGroup &mshg, UInt test_flag)
  578. {
  579. if(Cuts(point, Rect(mshg.ext)))REPA(mshg)if(CutsPointMesh(point, mshg.meshes[i], null, test_flag))return true;
  580. return false;
  581. }
  582. Bool CutsPointMesh(C Vec &point, C MeshGroup &mshg)
  583. {
  584. if(Cuts(point, mshg.ext))REPA(mshg)if(CutsPointMesh(point, mshg.meshes[i]))return true;
  585. return false;
  586. }
  587. /******************************************************************************/
  588. // SWEEP
  589. /******************************************************************************/
  590. Bool Sweep(C Vec2 &point, C Vec2 &move, C MeshBase &mshb, Flt *hit_frac, Vec2 *hit_pos, Int *hit_edge)
  591. {
  592. C Vec *pos =mshb.vtx .pos();
  593. C Vec *nrm =mshb.edge.nrm();
  594. C VecI2 *edge=mshb.edge.ind();
  595. if(pos && edge)
  596. {
  597. Bool hit=false;
  598. Int edge_i;
  599. Flt f , frac;
  600. Vec2 hp, hitp;
  601. FREPA(mshb.edge)
  602. {
  603. C Int *p=edge[i].c;
  604. if(SweepPointEdge(point, move, Edge2_I(pos[p[0]].xy, pos[p[1]].xy, nrm ? &nrm[i].xy : null), &f, null, &hp))
  605. if(!hit || f<frac){hit=true; frac=f; hitp=hp; edge_i=i;}
  606. }
  607. if(hit)
  608. {
  609. if(hit_frac)*hit_frac=frac;
  610. if(hit_pos )*hit_pos =hitp;
  611. if(hit_edge)*hit_edge=edge_i;
  612. return true;
  613. }
  614. }
  615. return false;
  616. }
  617. /******************************************************************************/
  618. Bool Sweep(C Vec2 &point, C Vec2 &move, C MeshBase &mshb, C Rects &rects, Index &rect_edge, Flt *hit_frac, Vec2 *hit_pos, Int *hit_edge)
  619. {
  620. C Vec *pos =mshb.vtx .pos();
  621. C Vec *nrm =mshb.edge.nrm();
  622. C VecI2 *edge=mshb.edge.ind();
  623. if(pos && edge)
  624. {
  625. Byte hit=0;
  626. Int hit_dist2, edge_i;
  627. Flt f , frac;
  628. Vec2 hp, hitp;
  629. Vec2 start =rects.coords(point ),
  630. end =rects.coords(point+move);
  631. VecI2 starti=Floor(start);
  632. for(PixelWalkerMask walker(start, end, RectI(0, 0, rects.cells.x-1, rects.cells.y-1)); walker.active(); walker.step())
  633. {
  634. if(hit && hit_dist2<Dist2(walker.pos(), starti))break;
  635. C IndexGroup &ig=rect_edge.group[walker.pos().x + walker.pos().y*rects.cells.x];
  636. REPA(ig)
  637. {
  638. C Int e=ig[i], *p=edge[e].c;
  639. if(SweepPointEdge(point, move, Edge2_I(pos[p[0]].xy, pos[p[1]].xy, nrm ? &nrm[e].xy : null), &f, null, &hp))
  640. if(!hit || f<frac){hit=2; frac=f; hitp=hp; edge_i=e;}
  641. }
  642. if(hit==2){hit=1; hit_dist2=Dist2(rects.coords(hitp), starti);}
  643. }
  644. if(hit)
  645. {
  646. if(hit_frac)*hit_frac=frac;
  647. if(hit_pos )*hit_pos =hitp;
  648. if(hit_edge)*hit_edge=edge_i;
  649. return true;
  650. }
  651. }
  652. return false;
  653. }
  654. /******************************************************************************/
  655. Bool Sweep(C Vec &point, C Vec &move, C MeshBase &mshb, C Matrix *mesh_matrix, Flt *hit_frac, Vec *hit_pos, Int *hit_face, Bool test_quads_as_2_tris, Bool two_sided)
  656. {
  657. Bool hit=false;
  658. Int face_index;
  659. Flt f , frac;
  660. Vec hp, hitp, Point=point, Move=move; if(mesh_matrix){Point/=*mesh_matrix; Move/=mesh_matrix->orn();}
  661. C Int *p;
  662. C VecI * tri_ind=mshb.tri .ind();
  663. C VecI4 *quad_ind=mshb.quad.ind();
  664. C Vec * tri_nrm=mshb.tri .nrm();
  665. C Vec *quad_nrm=mshb.quad.nrm();
  666. C Vec * vtx_pos=mshb.vtx .pos();
  667. if(vtx_pos && tri_ind)REPA(mshb.tri ){p= tri_ind[i].c; if(SweepPointTriEps (Point, Move, Tri (vtx_pos[p[0]], vtx_pos[p[1]], vtx_pos[p[2]], tri_nrm ? & tri_nrm[i] : null), &f, &hp , two_sided))if(!hit || f<frac){hit=true; frac=f; face_index=i ; hitp=hp;}}
  668. if(vtx_pos && quad_ind)REPA(mshb.quad){p=quad_ind[i].c; if(SweepPointQuadEps(Point, Move, Quad(vtx_pos[p[0]], vtx_pos[p[1]], vtx_pos[p[2]], vtx_pos[p[3]], quad_nrm ? &quad_nrm[i] : null), &f, &hp, test_quads_as_2_tris, two_sided))if(!hit || f<frac){hit=true; frac=f; face_index=i^SIGN_BIT; hitp=hp;}}
  669. if(hit)
  670. {
  671. if(hit_frac)*hit_frac=frac;
  672. if(hit_pos )*hit_pos =(mesh_matrix ? point+frac*move : hitp);
  673. if(hit_face)*hit_face=face_index;
  674. return true;
  675. }
  676. return false;
  677. }
  678. /******************************************************************************/
  679. Bool Sweep(C Vec &point, C Vec &move, C MeshRender &mshr, C Matrix *mesh_matrix, Flt *hit_frac, Vec *hit_pos, Int *hit_face, Bool two_sided)
  680. {
  681. Bool hit=false;
  682. Int face_index;
  683. Flt f , frac;
  684. Vec hp, hitp, Point=point, Move=move; if(mesh_matrix){Point/=*mesh_matrix; Move/=mesh_matrix->orn();}
  685. Int pos_ofs=mshr.vtxOfs(VTX_POS);
  686. if( pos_ofs>=0)
  687. if(C Byte *vtx=mshr.vtxLockRead())
  688. {
  689. vtx+=pos_ofs;
  690. if(CPtr index=mshr.indLockRead())
  691. {
  692. Int p0, p1, p2;
  693. REP(mshr.tris())
  694. {
  695. if(mshr._ib.bit16()){U16 *ind=(U16*)index; p0=ind[i*3+0]; p1=ind[i*3+1]; p2=ind[i*3+2];}
  696. else {U32 *ind=(U32*)index; p0=ind[i*3+0]; p1=ind[i*3+1]; p2=ind[i*3+2];}
  697. if(SweepPointTriEps(Point, Move, Tri(*(Vec*)(vtx+p0*mshr.vtxSize()), *(Vec*)(vtx+p1*mshr.vtxSize()), *(Vec*)(vtx+p2*mshr.vtxSize())), &f, &hp, two_sided))if(!hit || f<frac){hit=true; frac=f; face_index=i; hitp=hp;}
  698. }
  699. mshr.indUnlock();
  700. }
  701. mshr.vtxUnlock();
  702. }
  703. if(hit)
  704. {
  705. if(hit_frac)*hit_frac=frac;
  706. if(hit_pos )*hit_pos =(mesh_matrix ? point+frac*move : hitp);
  707. if(hit_face)*hit_face=face_index;
  708. return true;
  709. }
  710. return false;
  711. }
  712. /******************************************************************************/
  713. Bool Sweep(C Vec &point, C Vec &move, C MeshPart &part, C Matrix *mesh_matrix, Flt *hit_frac, Vec *hit_pos, Int *hit_face, Bool test_quads_as_2_tris, Int two_sided, Bool only_visible)
  714. {
  715. if(only_visible && (part.part_flag&MSHP_HIDDEN))return false;
  716. Bool ts=((two_sided<0) ? (part.material() ? !part.material()->cull : false) : (two_sided!=0));
  717. return part.base .is() ? Sweep(point, move, part.base , mesh_matrix, hit_frac, hit_pos, hit_face, test_quads_as_2_tris, ts) :
  718. part.render.is() ? Sweep(point, move, part.render, mesh_matrix, hit_frac, hit_pos, hit_face , ts) : false;
  719. }
  720. /******************************************************************************/
  721. Bool Sweep(C Vec &point, C Vec &move, C MeshLod &mesh, C Matrix *mesh_matrix, Flt *hit_frac, Vec *hit_pos, Int *hit_face, Int *hit_part, Bool test_quads_as_2_tris, Int two_sided, Bool only_visible)
  722. {
  723. Int fi, face_index, part_index;
  724. Flt f , frac;
  725. Vec hp, hitp, Point=point, Move=move; if(mesh_matrix){Point/=*mesh_matrix; Move/=mesh_matrix->orn();}
  726. Bool hit=false;
  727. REPA(mesh)
  728. {
  729. C MeshPart &part=mesh.parts[i];
  730. if(!(only_visible && (part.part_flag&MSHP_HIDDEN)))
  731. {
  732. Bool ts=((two_sided<0) ? (part.material() ? !part.material()->cull : false) : (two_sided!=0));
  733. if(part.base .is() ? Sweep(Point, Move, part.base , null, &f, &hp, &fi, test_quads_as_2_tris, ts) :
  734. part.render.is() ? Sweep(Point, Move, part.render, null, &f, &hp, &fi , ts) : false)
  735. if(!hit || f<frac){hit=true; frac=f; hitp=hp; face_index=fi; part_index=i;}
  736. }
  737. }
  738. if(hit)
  739. {
  740. if(hit_frac)*hit_frac=frac ;
  741. if(hit_pos )*hit_pos =(mesh_matrix ? point+frac*move : hitp);
  742. if(hit_face)*hit_face=face_index;
  743. if(hit_part)*hit_part=part_index;
  744. return true;
  745. }
  746. return false;
  747. }
  748. /******************************************************************************/
  749. Bool Sweep(C Vec &point, C Vec &move, C Mesh &mesh, C Matrix *mesh_matrix, Flt *hit_frac, Vec *hit_pos, Int *hit_face, Int *hit_part, Bool test_quads_as_2_tris, Int two_sided, Bool only_visible)
  750. {
  751. Vec Point=point, Move=move; if(mesh_matrix){Point/=*mesh_matrix; Move/=mesh_matrix->orn();}
  752. if(SweepPointBox(Point, Move, mesh.ext))
  753. {
  754. Flt frac; if(hit_pos && mesh_matrix && !hit_frac)hit_frac=&frac; // we will need it for 'hit_pos' calculation
  755. if(Sweep(Point, Move, mesh.lod(0), null, hit_frac, hit_pos, hit_face, hit_part, test_quads_as_2_tris, two_sided, only_visible))
  756. {
  757. if(hit_pos && mesh_matrix)*hit_pos=point+*hit_frac*move;
  758. return true;
  759. }
  760. }
  761. return false;
  762. }
  763. Bool Sweep(C VecD &point, C VecD &move, C Mesh &mesh, C MatrixM *mesh_matrix, Flt *hit_frac, VecD *hit_pos, Int *hit_face, Int *hit_part, Bool test_quads_as_2_tris, Int two_sided, Bool only_visible)
  764. {
  765. Vec Point, Move; if(mesh_matrix){Point.fromDiv(point, *mesh_matrix); Move.fromDiv(move, mesh_matrix->orn());}else{Point=point; Move=move;}
  766. if(SweepPointBox(Point, Move, mesh.ext))
  767. {
  768. Flt frac; Vec hp;
  769. if(hit_pos && mesh_matrix && !hit_frac)hit_frac=&frac; // we will need it for 'hit_pos' calculation
  770. if(Sweep(Point, Move, mesh.lod(0), null, hit_frac, (hit_pos && !mesh_matrix) ? &hp : null, hit_face, hit_part, test_quads_as_2_tris, two_sided, only_visible))
  771. {
  772. if(hit_pos){if(mesh_matrix)*hit_pos=point+*hit_frac*move;else *hit_pos=hp;} // can't do "*hit_pos=mesh_matrix?:" because of different types and compile fail on Clang
  773. return true;
  774. }
  775. }
  776. return false;
  777. }
  778. /******************************************************************************/
  779. Bool Sweep(C Vec &point, C Vec &move, C MeshGroup &mshg, C Matrix *mesh_matrix, Flt *hit_frac, Vec *hit_pos, Int *hit_face, Int *hit_part, Int *hit_mesh, Bool test_quads_as_2_tris, Int two_sided, Bool only_visible)
  780. {
  781. Int fi, face_index, part_index, mesh_index;
  782. Flt f , frac;
  783. Vec hp, hitp, Point=point, Move=move; if(mesh_matrix){Point/=*mesh_matrix; Move/=mesh_matrix->orn();}
  784. if(SweepPointBox(Point, Move, mshg.ext))
  785. {
  786. Bool hit=false;
  787. REPA(mshg)
  788. {
  789. C Mesh &mesh=mshg.meshes[i];
  790. if(SweepPointBox(Point, Move, mesh.ext))REPAD(j, mesh)
  791. {
  792. C MeshPart &part=mesh.parts[j];
  793. if(!(only_visible && (part.part_flag&MSHP_HIDDEN)))
  794. {
  795. Bool ts=((two_sided<0) ? (part.material() ? !part.material()->cull : false) : (two_sided!=0));
  796. if(part.base .is() ? Sweep(Point, Move, part.base , null, &f, &hp, &fi, test_quads_as_2_tris, ts) :
  797. part.render.is() ? Sweep(Point, Move, part.render, null, &f, &hp, &fi , ts) : false)
  798. if(!hit || f<frac){hit=true; frac=f; hitp=hp; face_index=fi; part_index=j; mesh_index=i;}
  799. }
  800. }
  801. }
  802. if(hit)
  803. {
  804. if(hit_frac)*hit_frac=frac;
  805. if(hit_pos )*hit_pos =(mesh_matrix ? point+frac*move : hitp);
  806. if(hit_face)*hit_face=face_index;
  807. if(hit_part)*hit_part=part_index;
  808. if(hit_mesh)*hit_mesh=mesh_index;
  809. return true;
  810. }
  811. }
  812. return false;
  813. }
  814. /******************************************************************************/
  815. // POS
  816. /******************************************************************************/
  817. Bool PosPointMeshXL(C Vec2 &point, C MeshBase &mshb, Vec *hit_pos, Int *hit_face, C Box *box, Bool test_quads_as_2_tris) {Box b=(box ? *box : mshb); return Sweep(Vec( b .max.x +1, point.y, point.x), Vec(- b .w()-2, 0, 0), mshb, null, null, hit_pos, hit_face , test_quads_as_2_tris);}
  818. Bool PosPointMeshXL(C Vec2 &point, C Mesh &mesh, Vec *hit_pos, Int *hit_face, Int *hit_part , Bool test_quads_as_2_tris) { return Sweep(Vec(mesh.ext.maxX()+1, point.y, point.x), Vec(-mesh.ext.w()-2, 0, 0), mesh, null, null, hit_pos, hit_face, hit_part , test_quads_as_2_tris);}
  819. Bool PosPointMeshXL(C Vec2 &point, C MeshGroup &mshg, Vec *hit_pos, Int *hit_face, Int *hit_part, Int *hit_mesh, Bool test_quads_as_2_tris) { return Sweep(Vec(mshg.ext.maxX()+1, point.y, point.x), Vec(-mshg.ext.w()-2, 0, 0), mshg, null, null, hit_pos, hit_face, hit_part, hit_mesh, test_quads_as_2_tris);}
  820. Bool PosPointMeshXR(C Vec2 &point, C MeshBase &mshb, Vec *hit_pos, Int *hit_face, C Box *box, Bool test_quads_as_2_tris) {Box b=(box ? *box : mshb); return Sweep(Vec( b .min.x -1, point.y, point.x), Vec( b .w()+2, 0, 0), mshb, null, null, hit_pos, hit_face , test_quads_as_2_tris);}
  821. Bool PosPointMeshXR(C Vec2 &point, C Mesh &mesh, Vec *hit_pos, Int *hit_face, Int *hit_part , Bool test_quads_as_2_tris) { return Sweep(Vec(mesh.ext.minX()-1, point.y, point.x), Vec( mesh.ext.w()+2, 0, 0), mesh, null, null, hit_pos, hit_face, hit_part , test_quads_as_2_tris);}
  822. Bool PosPointMeshXR(C Vec2 &point, C MeshGroup &mshg, Vec *hit_pos, Int *hit_face, Int *hit_part, Int *hit_mesh, Bool test_quads_as_2_tris) { return Sweep(Vec(mshg.ext.minX()-1, point.y, point.x), Vec( mshg.ext.w()+2, 0, 0), mshg, null, null, hit_pos, hit_face, hit_part, hit_mesh, test_quads_as_2_tris);}
  823. Bool PosPointMeshY (C Vec2 &point, C MeshBase &mshb, Vec *hit_pos, Int *hit_face, C Box *box, Bool test_quads_as_2_tris) {Box b=(box ? *box : mshb); return Sweep(Vec(point.x, b .max.y +1, point.y), Vec(0, - b .h()-2, 0), mshb, null, null, hit_pos, hit_face , test_quads_as_2_tris);}
  824. Bool PosPointMeshY (C Vec2 &point, C Mesh &mesh, Vec *hit_pos, Int *hit_face, Int *hit_part , Bool test_quads_as_2_tris) { return Sweep(Vec(point.x, mesh.ext.maxY()+1, point.y), Vec(0, -mesh.ext.h()-2, 0), mesh, null, null, hit_pos, hit_face, hit_part , test_quads_as_2_tris);}
  825. Bool PosPointMeshY (C Vec2 &point, C MeshGroup &mshg, Vec *hit_pos, Int *hit_face, Int *hit_part, Int *hit_mesh, Bool test_quads_as_2_tris) { return Sweep(Vec(point.x, mshg.ext.maxY()+1, point.y), Vec(0, -mshg.ext.h()-2, 0), mshg, null, null, hit_pos, hit_face, hit_part, hit_mesh, test_quads_as_2_tris);}
  826. Bool PosPointMeshZF(C Vec2 &point, C MeshBase &mshb, Vec *hit_pos, Int *hit_face, C Box *box, Bool test_quads_as_2_tris) {Box b=(box ? *box : mshb); return Sweep(Vec(point.x, point.y, b .min.z -1), Vec(0, 0, b .d()+2), mshb, null, null, hit_pos, hit_face , test_quads_as_2_tris);}
  827. Bool PosPointMeshZF(C Vec2 &point, C Mesh &mesh, Vec *hit_pos, Int *hit_face, Int *hit_part , Bool test_quads_as_2_tris) { return Sweep(Vec(point.x, point.y, mesh.ext.minZ()-1), Vec(0, 0, mesh.ext.d()+2), mesh, null, null, hit_pos, hit_face, hit_part , test_quads_as_2_tris);}
  828. Bool PosPointMeshZF(C Vec2 &point, C MeshGroup &mshg, Vec *hit_pos, Int *hit_face, Int *hit_part, Int *hit_mesh, Bool test_quads_as_2_tris) { return Sweep(Vec(point.x, point.y, mshg.ext.minZ()-1), Vec(0, 0, mshg.ext.d()+2), mshg, null, null, hit_pos, hit_face, hit_part, hit_mesh, test_quads_as_2_tris);}
  829. Bool PosPointMeshZB(C Vec2 &point, C MeshBase &mshb, Vec *hit_pos, Int *hit_face, C Box *box, Bool test_quads_as_2_tris) {Box b=(box ? *box : mshb); return Sweep(Vec(point.x, point.y, b .max.z +1), Vec(0, 0, - b .d()-2), mshb, null, null, hit_pos, hit_face , test_quads_as_2_tris);}
  830. Bool PosPointMeshZB(C Vec2 &point, C Mesh &mesh, Vec *hit_pos, Int *hit_face, Int *hit_part , Bool test_quads_as_2_tris) { return Sweep(Vec(point.x, point.y, mesh.ext.maxZ()+1), Vec(0, 0, -mesh.ext.d()-2), mesh, null, null, hit_pos, hit_face, hit_part , test_quads_as_2_tris);}
  831. Bool PosPointMeshZB(C Vec2 &point, C MeshGroup &mshg, Vec *hit_pos, Int *hit_face, Int *hit_part, Int *hit_mesh, Bool test_quads_as_2_tris) { return Sweep(Vec(point.x, point.y, mshg.ext.maxZ()+1), Vec(0, 0, -mshg.ext.d()-2), mshg, null, null, hit_pos, hit_face, hit_part, hit_mesh, test_quads_as_2_tris);}
  832. /******************************************************************************/
  833. // REST
  834. /******************************************************************************/
  835. Matrix GetRestMatrix(C Memt<C MeshBase*> &meshes, C Memt<C MeshRender*> &gpu_meshes, C Matrix *initial_matrix, C Vec *mass_center, Flt min_dist, Box *rest_box, Int max_steps)
  836. {
  837. Matrix matrix; if(initial_matrix)matrix=*initial_matrix;else matrix.identity();
  838. MAX(min_dist, EPS);
  839. REPA(gpu_meshes)if(C MeshRender *mesh=gpu_meshes[i])mesh->vtxLockRead();
  840. {
  841. // find first contact point 'a' located at the bottom
  842. Bool found=false; Vec a_org, a;
  843. REPA(meshes)
  844. if(C MeshBase *mesh=meshes[i])
  845. REPA(mesh->vtx)
  846. {
  847. C Vec &p_org=mesh->vtx.pos(i); Vec p=p_org*matrix;
  848. if(!found || p.y<a.y)
  849. {
  850. found=true;
  851. a_org=p_org;
  852. a =p;
  853. }
  854. }
  855. REPA(gpu_meshes)
  856. if(C MeshRender *mesh=gpu_meshes[i])
  857. if(C Byte *vtx_pos=mesh->vtxLockedElm(VTX_POS))
  858. REP(mesh->vtxs())
  859. {
  860. C Vec &p_org=*(Vec*)vtx_pos; vtx_pos+=mesh->vtxSize(); Vec p=p_org*matrix;
  861. if(!found || p.y<a.y)
  862. {
  863. found=true;
  864. a_org=p_org;
  865. a =p;
  866. }
  867. }
  868. if(!found)goto end;
  869. matrix.pos.y-=a.y; a.y=0; // align to bottom
  870. if(!max_steps--)goto end;
  871. // find mass center
  872. Vec com_org; if(mass_center)com_org=*mass_center;else
  873. {
  874. Box box;
  875. Bool found=false;
  876. REPA(meshes)if(C MeshBase *mesh=meshes[i])
  877. {
  878. Box mesh_box; if(mesh->getBox(mesh_box))
  879. {
  880. if(found)box|=mesh_box;else{found=true; box=mesh_box;}
  881. }
  882. }
  883. REPA(gpu_meshes)if(C MeshRender *mesh=gpu_meshes[i])
  884. {
  885. Box mesh_box; if(mesh->getBox(mesh_box))
  886. {
  887. if(found)box|=mesh_box;else{found=true; box=mesh_box;}
  888. }
  889. }
  890. if(!found)goto end;
  891. com_org=box.center();
  892. }
  893. Vec com=com_org*matrix; // get transformed center of mass
  894. find_b_contact:
  895. // get direction from contact point towards center of mass on XZ plane
  896. Vec axis=com-a; axis.y=0; if(!axis.normalize())axis.set(1, 0, 0);
  897. // get smallest slope from 'a' along the 'axis' which will be the second contact point 'b'
  898. found=false; Vec b_org; Flt slope_val;
  899. REPA(meshes)
  900. if(C MeshBase *mesh=meshes[i])
  901. REPA(mesh->vtx)
  902. {
  903. C Vec &p_org=mesh->vtx.pos(i); Vec p=p_org*matrix;
  904. if(!Equal(p, a, min_dist)) // ignore the original point or those close to it. Operate on 'p' and 'a' instead of 'p_org' and 'a_org' so we get correct scale for 'min_dist' in case 'matrix' scales
  905. {
  906. Vec d=p-a;
  907. Flt x=Dot(d, axis); if(x>0)
  908. {
  909. Flt y=d.y, slope=y/x;
  910. if(!found || slope<slope_val)
  911. {
  912. found =true;
  913. slope_val=slope;
  914. b_org =p_org;
  915. }
  916. }
  917. }
  918. }
  919. REPA(gpu_meshes)
  920. if(C MeshRender *mesh=gpu_meshes[i])
  921. if(C Byte *vtx_pos=mesh->vtxLockedElm(VTX_POS))
  922. REP(mesh->vtxs())
  923. {
  924. C Vec &p_org=*(Vec*)vtx_pos; vtx_pos+=mesh->vtxSize(); Vec p=p_org*matrix;
  925. if(!Equal(p, a, min_dist)) // ignore the original point or those close to it. Operate on 'p' and 'a' instead of 'p_org' and 'a_org' so we get correct scale for 'min_dist' in case 'matrix' scales
  926. {
  927. Vec d=p-a;
  928. Flt x=Dot(d, axis); if(x>0)
  929. {
  930. Flt y=d.y, slope=y/x;
  931. if(!found || slope<slope_val)
  932. {
  933. found =true;
  934. slope_val=slope;
  935. b_org =p_org;
  936. }
  937. }
  938. }
  939. }
  940. if(!found)goto end;
  941. Vec right=Perp(axis.xz()).x0y();
  942. matrix-=a; matrix.rotate(right, Atan(slope_val));
  943. matrix+=a;
  944. //a = a_org*matrix; // this matrix change does not affect 'a'
  945. Vec b = b_org*matrix; // calculate contact point
  946. com=com_org*matrix; // adjust com according to new matrix
  947. if(max_steps) // if want to keep looking
  948. if(slope_val>EPS) // if we have rotated (if not then it means that 2 contact points are coplanar and we can proceed with them)
  949. if(Dot(com, axis)>Dot(b, axis)+min_dist) // if mass center is further, then it means the body can still rotate. Operate on 'com' and 'b' instead of 'com_org' and 'b_org' so we get correct scale for 'min_dist' in case 'matrix' scales
  950. {
  951. max_steps--;
  952. a=b; a_org=b_org; // treat second contact point as the first one now
  953. goto find_b_contact;
  954. }
  955. // find third contact point
  956. find_c_contact:
  957. axis=b-a; Vec axis3=axis; axis3.normalize(); axis.y=0; if(!axis.normalize())axis.set(1, 0, 0); // we will rotate along the 'axis' now
  958. right=Perp(axis.xz()).x0y(); if(DistPointPlane(com, a, right)<0){axis.chs(); /*axis3.chs();*/ right.chs(); Swap(a, b); Swap(a_org, b_org);} // if center of mass is on the other side, 'axis3' sign is not important
  959. found=false; Vec c_org;
  960. REPA(meshes)
  961. if(C MeshBase *mesh=meshes[i])
  962. REPA(mesh->vtx)
  963. {
  964. C Vec &p_org=mesh->vtx.pos(i); Vec p=p_org*matrix;
  965. //if(!Equal(p, a, min_dist) && !Equal(p, b, min_dist)) // ignore the original points or those close to them, this check is not needed because of the one below
  966. if(DistPointStr(p, a, axis3)>min_dist) // ignore points on the same line. Operate on 'p' and 'a' instead of 'p_org' and 'a_org' so we get correct scale for 'min_dist' in case 'matrix' scales
  967. {
  968. Vec d=p-a;
  969. Flt x=Dot(d, right); if(x>0) // if point is on the same side as center of mass
  970. {
  971. Flt y=d.y, slope=y/x;
  972. if(!found || slope<slope_val)
  973. {
  974. found =true;
  975. slope_val=slope;
  976. c_org =p_org;
  977. }
  978. }
  979. }
  980. }
  981. REPA(gpu_meshes)
  982. if(C MeshRender *mesh=gpu_meshes[i])
  983. if(C Byte *vtx_pos=mesh->vtxLockedElm(VTX_POS))
  984. REP(mesh->vtxs())
  985. {
  986. C Vec &p_org=*(Vec*)vtx_pos; vtx_pos+=mesh->vtxSize(); Vec p=p_org*matrix;
  987. //if(!Equal(p, a, min_dist) && !Equal(p, b, min_dist)) // ignore the original points or those close to them, this check is not needed because of the one below
  988. if(DistPointStr(p, a, axis3)>min_dist) // ignore points on the same line. Operate on 'p' and 'a' instead of 'p_org' and 'a_org' so we get correct scale for 'min_dist' in case 'matrix' scales
  989. {
  990. Vec d=p-a;
  991. Flt x=Dot(d, right); if(x>0) // if point is on the same side as center of mass
  992. {
  993. Flt y=d.y, slope=y/x;
  994. if(!found || slope<slope_val)
  995. {
  996. found =true;
  997. slope_val=slope;
  998. c_org =p_org;
  999. }
  1000. }
  1001. }
  1002. }
  1003. if(!found)goto end;
  1004. matrix-=a; matrix.rotate(axis, -Atan(slope_val));
  1005. matrix+=a;
  1006. if(max_steps) // want to keep looking further
  1007. {
  1008. max_steps--;
  1009. //a= a_org*matrix; // this matrix change does not affect 'a'
  1010. b= b_org*matrix;
  1011. Vec c= c_org*matrix;
  1012. com=com_org*matrix;
  1013. // we now have 3 contact points forming a triangle, and center of mass
  1014. // check if the center of mass is outside of the triangle, which means the mesh will keep rotating
  1015. DIST_TYPE dt; Flt dist=Dist(com.xz(), Tri2(a.xz(), b.xz(), c.xz()), &dt); if(dist>min_dist)
  1016. {
  1017. if(dt==DIST_EDGE1) // BC edge
  1018. {
  1019. a_org=c_org;
  1020. a =c ;
  1021. goto find_c_contact;
  1022. }else
  1023. if(dt==DIST_POINT0 || dt==DIST_EDGE2 || dt==DIST_POINT2) // A point || CA edge || C point
  1024. {
  1025. b_org=c_org;
  1026. b =c ;
  1027. goto find_c_contact;
  1028. }
  1029. }
  1030. }
  1031. }
  1032. end:
  1033. if(rest_box)
  1034. {
  1035. Bool found=false;
  1036. REPA(meshes)
  1037. if(C MeshBase *mesh=meshes[i])
  1038. REPA(mesh->vtx)
  1039. {
  1040. C Vec &p_org=mesh->vtx.pos(i); Vec p=p_org*matrix;
  1041. if(found)(*rest_box)|=p;else{found=true; (*rest_box)=p;}
  1042. }
  1043. REPA(gpu_meshes)
  1044. if(C MeshRender *mesh=gpu_meshes[i])
  1045. if(C Byte *vtx_pos=mesh->vtxLockedElm(VTX_POS))
  1046. REP(mesh->vtxs())
  1047. {
  1048. C Vec &p_org=*(Vec*)vtx_pos; vtx_pos+=mesh->vtxSize(); Vec p=p_org*matrix;
  1049. if(found)(*rest_box)|=p;else{found=true; (*rest_box)=p;}
  1050. }
  1051. if(!found)rest_box->zero();
  1052. }
  1053. REPA(gpu_meshes)if(C MeshRender *mesh=gpu_meshes[i])mesh->vtxUnlock();
  1054. return matrix;
  1055. }
  1056. Matrix GetRestMatrix(C MeshBase &mesh, C Matrix *initial_matrix, C Vec *mass_center, Flt min_dist, Box *rest_box, Int max_steps)
  1057. {
  1058. Memt<C MeshBase *> meshes; meshes.add(&mesh);
  1059. Memt<C MeshRender*> gpu_meshes;
  1060. return GetRestMatrix(meshes, gpu_meshes, initial_matrix, mass_center, min_dist, rest_box, max_steps);
  1061. }
  1062. Matrix GetRestMatrix(C MemPtr<C MeshPart*> &meshes, C Matrix *initial_matrix, C Vec *mass_center, Flt min_dist, Box *rest_box, Int max_steps, Bool only_visible, Bool only_phys)
  1063. {
  1064. Memt<C MeshBase *> temp;
  1065. Memt<C MeshRender*> gpu_temp;
  1066. REPA(meshes)
  1067. if(C MeshPart *part=meshes[i])
  1068. {
  1069. if(!only_visible || !FlagTest(part->part_flag, MSHP_HIDDEN))
  1070. if(!only_phys || !FlagTest(part->part_flag, MSHP_NO_PHYS_BODY))
  1071. if(part->base .is()) temp.add(&part->base );else
  1072. if(part->render.is())gpu_temp.add(&part->render);
  1073. }
  1074. return GetRestMatrix(temp, gpu_temp, initial_matrix, mass_center, min_dist, rest_box, max_steps);
  1075. }
  1076. Matrix GetRestMatrix(C MemPtr<C MeshLod*> &meshes, C Matrix *initial_matrix, C Vec *mass_center, Flt min_dist, Box *rest_box, Int max_steps, Bool only_visible, Bool only_phys)
  1077. {
  1078. Memt<C MeshPart*> parts;
  1079. REPA(meshes)
  1080. if(C MeshLod *mesh=meshes[i])
  1081. REPA(*mesh)parts.add(&mesh->parts[i]);
  1082. return GetRestMatrix(parts, initial_matrix, mass_center, min_dist, rest_box, max_steps, only_visible, only_phys);
  1083. }
  1084. Matrix GetRestMatrix(C MeshLod &mesh, C Matrix *initial_matrix, C Vec *mass_center, Flt min_dist, Box *rest_box, Int max_steps, Bool only_visible, Bool only_phys)
  1085. {
  1086. C MeshLod *mesh_ptr=&mesh;
  1087. return GetRestMatrix(mesh_ptr, initial_matrix, mass_center, min_dist, rest_box, max_steps, only_visible, only_phys);
  1088. }
  1089. Matrix GetRestMatrix(C Mesh &mesh, C Matrix *initial_matrix, C Vec *mass_center, Flt min_dist, Box *rest_box, Int max_steps, Bool only_visible, Bool only_phys)
  1090. {
  1091. return GetRestMatrix(mesh.lod(0), initial_matrix, mass_center ? mass_center : &mesh.ext.pos, min_dist, rest_box, max_steps, only_visible, only_phys);
  1092. }
  1093. /******************************************************************************/
  1094. }
  1095. /******************************************************************************/