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