19 #ifndef DETOURCOMMON_H
20 #define DETOURCOMMON_H
45 template<
class T>
inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
51 template<
class T>
inline T
dtMin(T a, T b) {
return a < b ? a : b; }
57 template<
class T>
inline T
dtMax(T a, T b) {
return a > b ? a : b; }
62 template<
class T>
inline T
dtAbs(T a) {
return a < 0 ? -a : a; }
67 template<
class T>
inline T
dtSqr(T a) {
return a*a; }
74 template<
class T>
inline T
dtClamp(T v, T mn, T mx) {
return v < mn ? mn : (v > mx ? mx : v); }
84 inline void dtVcross(
float* dest,
const float* v1,
const float* v2)
86 dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
87 dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
88 dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
95 inline float dtVdot(
const float* v1,
const float* v2)
97 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
105 inline void dtVmad(
float* dest,
const float* v1,
const float* v2,
const float s)
107 dest[0] = v1[0]+v2[0]*s;
108 dest[1] = v1[1]+v2[1]*s;
109 dest[2] = v1[2]+v2[2]*s;
117 inline void dtVlerp(
float* dest,
const float* v1,
const float* v2,
const float t)
119 dest[0] = v1[0]+(v2[0]-v1[0])*t;
120 dest[1] = v1[1]+(v2[1]-v1[1])*t;
121 dest[2] = v1[2]+(v2[2]-v1[2])*t;
128 inline void dtVadd(
float* dest,
const float* v1,
const float* v2)
130 dest[0] = v1[0]+v2[0];
131 dest[1] = v1[1]+v2[1];
132 dest[2] = v1[2]+v2[2];
139 inline void dtVsub(
float* dest,
const float* v1,
const float* v2)
141 dest[0] = v1[0]-v2[0];
142 dest[1] = v1[1]-v2[1];
143 dest[2] = v1[2]-v2[2];
150 inline void dtVscale(
float* dest,
const float* v,
const float t)
160 inline void dtVmin(
float* mn,
const float* v)
162 mn[0] =
dtMin(mn[0], v[0]);
163 mn[1] =
dtMin(mn[1], v[1]);
164 mn[2] =
dtMin(mn[2], v[2]);
170 inline void dtVmax(
float* mx,
const float* v)
172 mx[0] =
dtMax(mx[0], v[0]);
173 mx[1] =
dtMax(mx[1], v[1]);
174 mx[2] =
dtMax(mx[2], v[2]);
182 inline void dtVset(
float* dest,
const float x,
const float y,
const float z)
184 dest[0] =
x; dest[1] =
y; dest[2] =
z;
190 inline void dtVcopy(
float* dest,
const float* a)
202 return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
210 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
217 inline float dtVdist(
const float* v1,
const float* v2)
219 const float dx = v2[0] - v1[0];
220 const float dy = v2[1] - v1[1];
221 const float dz = v2[2] - v1[2];
231 const float dx = v2[0] - v1[0];
232 const float dy = v2[1] - v1[1];
233 const float dz = v2[2] - v1[2];
234 return dx*dx + dy*dy + dz*dz;
243 inline float dtVdist2D(
const float* v1,
const float* v2)
245 const float dx = v2[0] - v1[0];
246 const float dz = v2[2] - v1[2];
256 const float dx = v2[0] - v1[0];
257 const float dz = v2[2] - v1[2];
258 return dx*dx + dz*dz;
278 inline bool dtVequal(
const float* p0,
const float* p1)
280 static const float thr =
dtSqr(1.0f/16384.0f);
291 inline float dtVdot2D(
const float* u,
const float* v)
293 return u[0]*v[0] + u[2]*v[2];
304 return u[2]*v[0] - u[0]*v[2];
316 inline float dtTriArea2D(
const float* a,
const float* b,
const float* c)
318 const float abx = b[0] - a[0];
319 const float abz = b[2] - a[2];
320 const float acx = c[0] - a[0];
321 const float acz = c[2] - a[2];
322 return acx*abz - abx*acz;
333 const unsigned short bmin[3],
const unsigned short bmax[3])
336 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
337 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
338 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
350 const float* bmin,
const float* bmax)
353 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
354 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
355 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
366 const float* a,
const float* b,
const float* c);
377 const float* verts,
int nverts,
378 float& tmin,
float& tmax,
379 int& segMin,
int& segMax);
382 const float* bp,
const float* bq,
390 bool dtPointInPolygon(
const float* pt,
const float* verts,
const int nverts);
393 float* ed,
float* et);
402 void dtCalcPolyCenter(
float* tc,
const unsigned short* idx,
int nidx,
const float* verts);
411 const float* polyb,
const int npolyb);
433 r = (v > 0xffff) << 4; v >>= r;
434 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
435 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
436 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
447 unsigned char tmp = *a;
454 unsigned char*
x = (
unsigned char*)v;
460 unsigned char*
x = (
unsigned char*)v;
466 unsigned char*
x = (
unsigned char*)v;
472 unsigned char*
x = (
unsigned char*)v;
478 unsigned char*
x = (
unsigned char*)v;
483 const float s,
const float t,
float* out);
487 #endif // DETOURCOMMON_H
void dtRandomPointInConvexPoly(const float *pts, const int npts, float *areas, const float s, const float t, float *out)
Definition: DetourCommon.cpp:333
float dtVlenSqr(const float *v)
Definition: DetourCommon.h:208
void dtSwapByte(unsigned char *a, unsigned char *b)
Definition: DetourCommon.h:445
void dtSwapEndian(unsigned short *v)
Definition: DetourCommon.h:452
float dtTriArea2D(const float *a, const float *b, const float *c)
Definition: DetourCommon.h:316
void dtIgnoreUnused(const T &)
Definition: DetourCommon.h:40
void dtCalcPolyCenter(float *tc, const unsigned short *idx, int nidx, const float *verts)
Definition: DetourCommon.cpp:186
float dtVlen(const float *v)
Definition: DetourCommon.h:200
void dtVlerp(float *dest, const float *v1, const float *v2, const float t)
Definition: DetourCommon.h:117
void dtVmad(float *dest, const float *v1, const float *v2, const float s)
Definition: DetourCommon.h:105
float dtVdist(const float *v1, const float *v2)
Definition: DetourCommon.h:217
float dtVperp2D(const float *u, const float *v)
Definition: DetourCommon.h:302
void dtVmin(float *mn, const float *v)
Definition: DetourCommon.h:160
void dtVmax(float *mx, const float *v)
Definition: DetourCommon.h:170
bool dtPointInPolygon(const float *pt, const float *verts, const int nverts)
Definition: DetourCommon.cpp:239
void dtClosestPtPointTriangle(float *closest, const float *p, const float *a, const float *b, const float *c)
Definition: DetourCommon.cpp:24
bool dtIntersectSegmentPoly2D(const float *p0, const float *p1, const float *verts, int nverts, float &tmin, float &tmax, int &segMin, int &segMax)
Definition: DetourCommon.cpp:110
void dtVadd(float *dest, const float *v1, const float *v2)
Definition: DetourCommon.h:128
float dtDistancePtSegSqr2D(const float *pt, const float *p, const float *q, float &t)
Definition: DetourCommon.cpp:170
void dtVnormalize(float *v)
Definition: DetourCommon.h:263
T dtAbs(T a)
Definition: DetourCommon.h:62
float dtVdist2D(const float *v1, const float *v2)
Definition: DetourCommon.h:243
bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3], const unsigned short bmin[3], const unsigned short bmax[3])
Definition: DetourCommon.h:332
float dtMathSqrtf(float x)
Definition: DetourMath.h:13
void dtVset(float *dest, const float x, const float y, const float z)
Definition: DetourCommon.h:182
bool dtClosestHeightPointTriangle(const float *p, const float *a, const float *b, const float *c, float &h)
Definition: DetourCommon.cpp:204
G3D::int16 z
Definition: Vector3int16.h:46
T dtClamp(T v, T mn, T mx)
Definition: DetourCommon.h:74
T dtMax(T a, T b)
Definition: DetourCommon.h:57
G3D::int16 y
Definition: Vector2int16.h:38
bool dtOverlapPolyPoly2D(const float *polya, const int npolya, const float *polyb, const int npolyb)
Definition: DetourCommon.cpp:295
void dtVcross(float *dest, const float *v1, const float *v2)
Definition: DetourCommon.h:84
float dtVdot(const float *v1, const float *v2)
Definition: DetourCommon.h:95
unsigned int dtNextPow2(unsigned int v)
Definition: DetourCommon.h:417
bool dtIntersectSegSeg2D(const float *ap, const float *aq, const float *bp, const float *bq, float &s, float &t)
Definition: DetourCommon.cpp:374
bool dtVequal(const float *p0, const float *p1)
Definition: DetourCommon.h:278
void dtVcopy(float *dest, const float *a)
Definition: DetourCommon.h:190
T dtMin(T a, T b)
Definition: DetourCommon.h:51
float dtVdistSqr(const float *v1, const float *v2)
Definition: DetourCommon.h:229
T dtSqr(T a)
Definition: DetourCommon.h:67
void dtSwap(T &a, T &b)
Definition: DetourCommon.h:45
int dtAlign4(int x)
Definition: DetourCommon.h:441
void dtVsub(float *dest, const float *v1, const float *v2)
Definition: DetourCommon.h:139
void dtVscale(float *dest, const float *v, const float t)
Definition: DetourCommon.h:150
int dtOppositeTile(int side)
Definition: DetourCommon.h:443
bool dtOverlapBounds(const float *amin, const float *amax, const float *bmin, const float *bmax)
Definition: DetourCommon.h:349
float dtVdot2D(const float *u, const float *v)
Definition: DetourCommon.h:291
G3D::int16 x
Definition: Vector2int16.h:37
float dtVdist2DSqr(const float *v1, const float *v2)
Definition: DetourCommon.h:254
unsigned int dtIlog2(unsigned int v)
Definition: DetourCommon.h:429
bool dtDistancePtPolyEdgesSqr(const float *pt, const float *verts, const int nverts, float *ed, float *et)
Definition: DetourCommon.cpp:255