DetourCommon.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include "DetourCommon.h"
  19. #include "DetourMath.h"
  20. //////////////////////////////////////////////////////////////////////////////////////////
  21. void dtClosestPtPointTriangle(float* closest, const float* p,
  22. const float* a, const float* b, const float* c)
  23. {
  24. // Check if P in vertex region outside A
  25. float ab[3], ac[3], ap[3];
  26. dtVsub(ab, b, a);
  27. dtVsub(ac, c, a);
  28. dtVsub(ap, p, a);
  29. float d1 = dtVdot(ab, ap);
  30. float d2 = dtVdot(ac, ap);
  31. if (d1 <= 0.0f && d2 <= 0.0f)
  32. {
  33. // barycentric coordinates (1,0,0)
  34. dtVcopy(closest, a);
  35. return;
  36. }
  37. // Check if P in vertex region outside B
  38. float bp[3];
  39. dtVsub(bp, p, b);
  40. float d3 = dtVdot(ab, bp);
  41. float d4 = dtVdot(ac, bp);
  42. if (d3 >= 0.0f && d4 <= d3)
  43. {
  44. // barycentric coordinates (0,1,0)
  45. dtVcopy(closest, b);
  46. return;
  47. }
  48. // Check if P in edge region of AB, if so return projection of P onto AB
  49. float vc = d1*d4 - d3*d2;
  50. if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f)
  51. {
  52. // barycentric coordinates (1-v,v,0)
  53. float v = d1 / (d1 - d3);
  54. closest[0] = a[0] + v * ab[0];
  55. closest[1] = a[1] + v * ab[1];
  56. closest[2] = a[2] + v * ab[2];
  57. return;
  58. }
  59. // Check if P in vertex region outside C
  60. float cp[3];
  61. dtVsub(cp, p, c);
  62. float d5 = dtVdot(ab, cp);
  63. float d6 = dtVdot(ac, cp);
  64. if (d6 >= 0.0f && d5 <= d6)
  65. {
  66. // barycentric coordinates (0,0,1)
  67. dtVcopy(closest, c);
  68. return;
  69. }
  70. // Check if P in edge region of AC, if so return projection of P onto AC
  71. float vb = d5*d2 - d1*d6;
  72. if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f)
  73. {
  74. // barycentric coordinates (1-w,0,w)
  75. float w = d2 / (d2 - d6);
  76. closest[0] = a[0] + w * ac[0];
  77. closest[1] = a[1] + w * ac[1];
  78. closest[2] = a[2] + w * ac[2];
  79. return;
  80. }
  81. // Check if P in edge region of BC, if so return projection of P onto BC
  82. float va = d3*d6 - d5*d4;
  83. if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f)
  84. {
  85. // barycentric coordinates (0,1-w,w)
  86. float w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
  87. closest[0] = b[0] + w * (c[0] - b[0]);
  88. closest[1] = b[1] + w * (c[1] - b[1]);
  89. closest[2] = b[2] + w * (c[2] - b[2]);
  90. return;
  91. }
  92. // P inside face region. Compute Q through its barycentric coordinates (u,v,w)
  93. float denom = 1.0f / (va + vb + vc);
  94. float v = vb * denom;
  95. float w = vc * denom;
  96. closest[0] = a[0] + ab[0] * v + ac[0] * w;
  97. closest[1] = a[1] + ab[1] * v + ac[1] * w;
  98. closest[2] = a[2] + ab[2] * v + ac[2] * w;
  99. }
  100. bool dtIntersectSegmentPoly2D(const float* p0, const float* p1,
  101. const float* verts, int nverts,
  102. float& tmin, float& tmax,
  103. int& segMin, int& segMax)
  104. {
  105. static const float EPS = 0.00000001f;
  106. tmin = 0;
  107. tmax = 1;
  108. segMin = -1;
  109. segMax = -1;
  110. float dir[3];
  111. dtVsub(dir, p1, p0);
  112. for (int i = 0, j = nverts-1; i < nverts; j=i++)
  113. {
  114. float edge[3], diff[3];
  115. dtVsub(edge, &verts[i*3], &verts[j*3]);
  116. dtVsub(diff, p0, &verts[j*3]);
  117. const float n = dtVperp2D(edge, diff);
  118. const float d = dtVperp2D(dir, edge);
  119. if (fabsf(d) < EPS)
  120. {
  121. // S is nearly parallel to this edge
  122. if (n < 0)
  123. return false;
  124. else
  125. continue;
  126. }
  127. const float t = n / d;
  128. if (d < 0)
  129. {
  130. // segment S is entering across this edge
  131. if (t > tmin)
  132. {
  133. tmin = t;
  134. segMin = j;
  135. // S enters after leaving polygon
  136. if (tmin > tmax)
  137. return false;
  138. }
  139. }
  140. else
  141. {
  142. // segment S is leaving across this edge
  143. if (t < tmax)
  144. {
  145. tmax = t;
  146. segMax = j;
  147. // S leaves before entering polygon
  148. if (tmax < tmin)
  149. return false;
  150. }
  151. }
  152. }
  153. return true;
  154. }
  155. float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t)
  156. {
  157. float pqx = q[0] - p[0];
  158. float pqz = q[2] - p[2];
  159. float dx = pt[0] - p[0];
  160. float dz = pt[2] - p[2];
  161. float d = pqx*pqx + pqz*pqz;
  162. t = pqx*dx + pqz*dz;
  163. if (d > 0) t /= d;
  164. if (t < 0) t = 0;
  165. else if (t > 1) t = 1;
  166. dx = p[0] + t*pqx - pt[0];
  167. dz = p[2] + t*pqz - pt[2];
  168. return dx*dx + dz*dz;
  169. }
  170. void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts)
  171. {
  172. tc[0] = 0.0f;
  173. tc[1] = 0.0f;
  174. tc[2] = 0.0f;
  175. for (int j = 0; j < nidx; ++j)
  176. {
  177. const float* v = &verts[idx[j]*3];
  178. tc[0] += v[0];
  179. tc[1] += v[1];
  180. tc[2] += v[2];
  181. }
  182. const float s = 1.0f / nidx;
  183. tc[0] *= s;
  184. tc[1] *= s;
  185. tc[2] *= s;
  186. }
  187. bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h)
  188. {
  189. float v0[3], v1[3], v2[3];
  190. dtVsub(v0, c,a);
  191. dtVsub(v1, b,a);
  192. dtVsub(v2, p,a);
  193. const float dot00 = dtVdot2D(v0, v0);
  194. const float dot01 = dtVdot2D(v0, v1);
  195. const float dot02 = dtVdot2D(v0, v2);
  196. const float dot11 = dtVdot2D(v1, v1);
  197. const float dot12 = dtVdot2D(v1, v2);
  198. // Compute barycentric coordinates
  199. const float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
  200. const float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  201. const float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  202. // The (sloppy) epsilon is needed to allow to get height of points which
  203. // are interpolated along the edges of the triangles.
  204. static const float EPS = 1e-4f;
  205. // If point lies inside the triangle, return interpolated ycoord.
  206. if (u >= -EPS && v >= -EPS && (u+v) <= 1+EPS)
  207. {
  208. h = a[1] + v0[1]*u + v1[1]*v;
  209. return true;
  210. }
  211. return false;
  212. }
  213. /// @par
  214. ///
  215. /// All points are projected onto the xz-plane, so the y-values are ignored.
  216. bool dtPointInPolygon(const float* pt, const float* verts, const int nverts)
  217. {
  218. // TODO: Replace pnpoly with triArea2D tests?
  219. int i, j;
  220. bool c = false;
  221. for (i = 0, j = nverts-1; i < nverts; j = i++)
  222. {
  223. const float* vi = &verts[i*3];
  224. const float* vj = &verts[j*3];
  225. if (((vi[2] > pt[2]) != (vj[2] > pt[2])) &&
  226. (pt[0] < (vj[0]-vi[0]) * (pt[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
  227. c = !c;
  228. }
  229. return c;
  230. }
  231. bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
  232. float* ed, float* et)
  233. {
  234. // TODO: Replace pnpoly with triArea2D tests?
  235. int i, j;
  236. bool c = false;
  237. for (i = 0, j = nverts-1; i < nverts; j = i++)
  238. {
  239. const float* vi = &verts[i*3];
  240. const float* vj = &verts[j*3];
  241. if (((vi[2] > pt[2]) != (vj[2] > pt[2])) &&
  242. (pt[0] < (vj[0]-vi[0]) * (pt[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
  243. c = !c;
  244. ed[j] = dtDistancePtSegSqr2D(pt, vj, vi, et[j]);
  245. }
  246. return c;
  247. }
  248. static void projectPoly(const float* axis, const float* poly, const int npoly,
  249. float& rmin, float& rmax)
  250. {
  251. rmin = rmax = dtVdot2D(axis, &poly[0]);
  252. for (int i = 1; i < npoly; ++i)
  253. {
  254. const float d = dtVdot2D(axis, &poly[i*3]);
  255. rmin = dtMin(rmin, d);
  256. rmax = dtMax(rmax, d);
  257. }
  258. }
  259. inline bool overlapRange(const float amin, const float amax,
  260. const float bmin, const float bmax,
  261. const float eps)
  262. {
  263. return ((amin+eps) > bmax || (amax-eps) < bmin) ? false : true;
  264. }
  265. /// @par
  266. ///
  267. /// All vertices are projected onto the xz-plane, so the y-values are ignored.
  268. bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
  269. const float* polyb, const int npolyb)
  270. {
  271. const float eps = 1e-4f;
  272. for (int i = 0, j = npolya-1; i < npolya; j=i++)
  273. {
  274. const float* va = &polya[j*3];
  275. const float* vb = &polya[i*3];
  276. const float n[3] = { vb[2]-va[2], 0, -(vb[0]-va[0]) };
  277. float amin,amax,bmin,bmax;
  278. projectPoly(n, polya, npolya, amin,amax);
  279. projectPoly(n, polyb, npolyb, bmin,bmax);
  280. if (!overlapRange(amin,amax, bmin,bmax, eps))
  281. {
  282. // Found separating axis
  283. return false;
  284. }
  285. }
  286. for (int i = 0, j = npolyb-1; i < npolyb; j=i++)
  287. {
  288. const float* va = &polyb[j*3];
  289. const float* vb = &polyb[i*3];
  290. const float n[3] = { vb[2]-va[2], 0, -(vb[0]-va[0]) };
  291. float amin,amax,bmin,bmax;
  292. projectPoly(n, polya, npolya, amin,amax);
  293. projectPoly(n, polyb, npolyb, bmin,bmax);
  294. if (!overlapRange(amin,amax, bmin,bmax, eps))
  295. {
  296. // Found separating axis
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. // Returns a random point in a convex polygon.
  303. // Adapted from Graphics Gems article.
  304. void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
  305. const float s, const float t, float* out)
  306. {
  307. // Calc triangle araes
  308. float areasum = 0.0f;
  309. for (int i = 2; i < npts; i++) {
  310. areas[i] = dtTriArea2D(&pts[0], &pts[(i-1)*3], &pts[i*3]);
  311. areasum += dtMax(0.001f, areas[i]);
  312. }
  313. // Find sub triangle weighted by area.
  314. const float thr = s*areasum;
  315. float acc = 0.0f;
  316. float u = 0.0f;
  317. int tri = 0;
  318. for (int i = 2; i < npts; i++) {
  319. const float dacc = areas[i];
  320. if (thr >= acc && thr < (acc+dacc))
  321. {
  322. u = (thr - acc) / dacc;
  323. tri = i;
  324. break;
  325. }
  326. acc += dacc;
  327. }
  328. float v = dtMathSqrtf(t);
  329. const float a = 1 - v;
  330. const float b = (1 - u) * v;
  331. const float c = u * v;
  332. const float* pa = &pts[0];
  333. const float* pb = &pts[(tri-1)*3];
  334. const float* pc = &pts[tri*3];
  335. out[0] = a*pa[0] + b*pb[0] + c*pc[0];
  336. out[1] = a*pa[1] + b*pb[1] + c*pc[1];
  337. out[2] = a*pa[2] + b*pb[2] + c*pc[2];
  338. }
  339. inline float vperpXZ(const float* a, const float* b) { return a[0]*b[2] - a[2]*b[0]; }
  340. bool dtIntersectSegSeg2D(const float* ap, const float* aq,
  341. const float* bp, const float* bq,
  342. float& s, float& t)
  343. {
  344. float u[3], v[3], w[3];
  345. dtVsub(u,aq,ap);
  346. dtVsub(v,bq,bp);
  347. dtVsub(w,ap,bp);
  348. float d = vperpXZ(u,v);
  349. if (fabsf(d) < 1e-6f) return false;
  350. s = vperpXZ(v,w) / d;
  351. t = vperpXZ(u,w) / d;
  352. return true;
  353. }