DetourCommon.cpp 9.5 KB

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