MCPcopy Create free account
hub / github.com/axmolengine/axmol / dtRandomPointInConvexPoly

Function dtRandomPointInConvexPoly

3rdparty/recast/DetourCommon.cpp:332–369  ·  view source on GitHub ↗

Returns a random point in a convex polygon. Adapted from Graphics Gems article.

Source from the content-addressed store, hash-verified

330// Returns a random point in a convex polygon.
331// Adapted from Graphics Gems article.
332void 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
371inline float vperpXZ(const float* a, const float* b) { return a[0]*b[2] - a[2]*b[0]; }
372

Callers 2

findRandomPointMethod · 0.85

Calls 3

dtTriArea2DFunction · 0.85
dtMaxFunction · 0.85
dtMathSqrtfFunction · 0.85

Tested by

no test coverage detected