DetourCommon.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. const float EPS = 1e-6f;
  190. float v0[3], v1[3], v2[3];
  191. dtVsub(v0, c, a);
  192. dtVsub(v1, b, a);
  193. dtVsub(v2, p, a);
  194. // Compute scaled barycentric coordinates
  195. float denom = v0[0] * v1[2] - v0[2] * v1[0];
  196. if (fabsf(denom) < EPS)
  197. return false;
  198. float u = v1[2] * v2[0] - v1[0] * v2[2];
  199. float v = v0[0] * v2[2] - v0[2] * v2[0];
  200. if (denom < 0) {
  201. denom = -denom;
  202. u = -u;
  203. v = -v;
  204. }
  205. // If point lies inside the triangle, return interpolated ycoord.
  206. if (u >= 0.0f && v >= 0.0f && (u + v) <= denom) {
  207. h = a[1] + (v0[1] * u + v1[1] * v) / denom;
  208. return true;
  209. }
  210. return false;
  211. }
  212. /// @par
  213. ///
  214. /// All points are projected onto the xz-plane, so the y-values are ignored.
  215. bool dtPointInPolygon(const float* pt, const float* verts, const int nverts)
  216. {
  217. // TODO: Replace pnpoly with triArea2D tests?
  218. int i, j;
  219. bool c = false;
  220. for (i = 0, j = nverts-1; i < nverts; j = i++)
  221. {
  222. const float* vi = &verts[i*3];
  223. const float* vj = &verts[j*3];
  224. if (((vi[2] > pt[2]) != (vj[2] > pt[2])) &&
  225. (pt[0] < (vj[0]-vi[0]) * (pt[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
  226. c = !c;
  227. }
  228. return c;
  229. }
  230. bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
  231. float* ed, float* et)
  232. {
  233. // TODO: Replace pnpoly with triArea2D tests?
  234. int i, j;
  235. bool c = false;
  236. for (i = 0, j = nverts-1; i < nverts; j = i++)
  237. {
  238. const float* vi = &verts[i*3];
  239. const float* vj = &verts[j*3];
  240. if (((vi[2] > pt[2]) != (vj[2] > pt[2])) &&
  241. (pt[0] < (vj[0]-vi[0]) * (pt[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
  242. c = !c;
  243. ed[j] = dtDistancePtSegSqr2D(pt, vj, vi, et[j]);
  244. }
  245. return c;
  246. }
  247. static void projectPoly(const float* axis, const float* poly, const int npoly,
  248. float& rmin, float& rmax)
  249. {
  250. rmin = rmax = dtVdot2D(axis, &poly[0]);
  251. for (int i = 1; i < npoly; ++i)
  252. {
  253. const float d = dtVdot2D(axis, &poly[i*3]);
  254. rmin = dtMin(rmin, d);
  255. rmax = dtMax(rmax, d);
  256. }
  257. }
  258. inline bool overlapRange(const float amin, const float amax,
  259. const float bmin, const float bmax,
  260. const float eps)
  261. {
  262. return ((amin+eps) > bmax || (amax-eps) < bmin) ? false : true;
  263. }
  264. /// @par
  265. ///
  266. /// All vertices are projected onto the xz-plane, so the y-values are ignored.
  267. bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
  268. const float* polyb, const int npolyb)
  269. {
  270. const float eps = 1e-4f;
  271. for (int i = 0, j = npolya-1; i < npolya; j=i++)
  272. {
  273. const float* va = &polya[j*3];
  274. const float* vb = &polya[i*3];
  275. const float n[3] = { vb[2]-va[2], 0, -(vb[0]-va[0]) };
  276. float amin,amax,bmin,bmax;
  277. projectPoly(n, polya, npolya, amin,amax);
  278. projectPoly(n, polyb, npolyb, bmin,bmax);
  279. if (!overlapRange(amin,amax, bmin,bmax, eps))
  280. {
  281. // Found separating axis
  282. return false;
  283. }
  284. }
  285. for (int i = 0, j = npolyb-1; i < npolyb; j=i++)
  286. {
  287. const float* va = &polyb[j*3];
  288. const float* vb = &polyb[i*3];
  289. const float n[3] = { vb[2]-va[2], 0, -(vb[0]-va[0]) };
  290. float amin,amax,bmin,bmax;
  291. projectPoly(n, polya, npolya, amin,amax);
  292. projectPoly(n, polyb, npolyb, bmin,bmax);
  293. if (!overlapRange(amin,amax, bmin,bmax, eps))
  294. {
  295. // Found separating axis
  296. return false;
  297. }
  298. }
  299. return true;
  300. }
  301. // Returns a random point in a convex polygon.
  302. // Adapted from Graphics Gems article.
  303. void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
  304. const float s, const float t, float* out)
  305. {
  306. // Calc triangle araes
  307. float areasum = 0.0f;
  308. for (int i = 2; i < npts; i++) {
  309. areas[i] = dtTriArea2D(&pts[0], &pts[(i-1)*3], &pts[i*3]);
  310. areasum += dtMax(0.001f, areas[i]);
  311. }
  312. // Find sub triangle weighted by area.
  313. const float thr = s*areasum;
  314. float acc = 0.0f;
  315. float u = 1.0f;
  316. int tri = npts - 1;
  317. for (int i = 2; i < npts; i++) {
  318. const float dacc = areas[i];
  319. if (thr >= acc && thr < (acc+dacc))
  320. {
  321. u = (thr - acc) / dacc;
  322. tri = i;
  323. break;
  324. }
  325. acc += dacc;
  326. }
  327. float v = dtMathSqrtf(t);
  328. const float a = 1 - v;
  329. const float b = (1 - u) * v;
  330. const float c = u * v;
  331. const float* pa = &pts[0];
  332. const float* pb = &pts[(tri-1)*3];
  333. const float* pc = &pts[tri*3];
  334. out[0] = a*pa[0] + b*pb[0] + c*pc[0];
  335. out[1] = a*pa[1] + b*pb[1] + c*pc[1];
  336. out[2] = a*pa[2] + b*pb[2] + c*pc[2];
  337. }
  338. inline float vperpXZ(const float* a, const float* b) { return a[0]*b[2] - a[2]*b[0]; }
  339. bool dtIntersectSegSeg2D(const float* ap, const float* aq,
  340. const float* bp, const float* bq,
  341. float& s, float& t)
  342. {
  343. float u[3], v[3], w[3];
  344. dtVsub(u,aq,ap);
  345. dtVsub(v,bq,bp);
  346. dtVsub(w,ap,bp);
  347. float d = vperpXZ(u,v);
  348. if (fabsf(d) < 1e-6f) return false;
  349. s = vperpXZ(v,w) / d;
  350. t = vperpXZ(u,w) / d;
  351. return true;
  352. }