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);
313 inline float dtVdot2D(
const float* u,
const float* v)
315 return u[0]*v[0] + u[2]*v[2];
326 return u[2]*v[0] - u[0]*v[2];
338 inline float dtTriArea2D(
const float* a,
const float* b,
const float* c)
340 const float abx = b[0] - a[0];
341 const float abz = b[2] - a[2];
342 const float acx = c[0] - a[0];
343 const float acz = c[2] - a[2];
344 return acx*abz - abx*acz;
355 const unsigned short bmin[3],
const unsigned short bmax[3])
358 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
359 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
360 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
372 const float* bmin,
const float* bmax)
375 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
376 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
377 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
388 const float* a,
const float* b,
const float* c);
399 const float* verts,
int nverts,
400 float& tmin,
float& tmax,
401 int& segMin,
int& segMax);
404 const float* bp,
const float* bq,
412 bool dtPointInPolygon(
const float* pt,
const float* verts,
const int nverts);
415 float* ed,
float* et);
424 void dtCalcPolyCenter(
float* tc,
const unsigned short* idx,
int nidx,
const float* verts);
433 const float* polyb,
const int npolyb);
455 r = (v > 0xffff) << 4; v >>= r;
456 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
457 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
458 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
469 unsigned char tmp = *a;
476 unsigned char* x = (
unsigned char*)v;
482 unsigned char* x = (
unsigned char*)v;
488 unsigned char* x = (
unsigned char*)v;
494 unsigned char* x = (
unsigned char*)v;
500 unsigned char* x = (
unsigned char*)v;
505 const float s,
const float t,
float* out);
507 template<
typename TypeToRetrieveAs>
510 TypeToRetrieveAs* returnPointer =
reinterpret_cast<TypeToRetrieveAs*
>(buffer);
511 buffer += distanceToAdvance;
512 return returnPointer;
515 template<
typename TypeToRetrieveAs>
518 TypeToRetrieveAs* returnPointer =
reinterpret_cast<TypeToRetrieveAs*
>(buffer);
519 buffer += distanceToAdvance;
520 return returnPointer;
float dtVdist(const float *v1, const float *v2)
Returns the distance between two points.
Definition: DetourCommon.h:217
void dtVmin(float *mn, const float *v)
Selects the minimum value of each element from the specified vectors.
Definition: DetourCommon.h:160
void dtVmax(float *mx, const float *v)
Selects the maximum value of each element from the specified vectors.
Definition: DetourCommon.h:170
void dtSwapEndian(unsigned short *v)
Definition: DetourCommon.h:474
T dtMin(T a, T b)
Returns the minimum of two values.
Definition: DetourCommon.h:51
void dtVmad(float *dest, const float *v1, const float *v2, const float s)
Performs a scaled vector addition.
Definition: DetourCommon.h:105
bool dtVisfinite(const float *v)
Checks that the specified vector's components are all finite.
Definition: DetourCommon.h:289
void dtVcopy(float *dest, const float *a)
Performs a vector copy.
Definition: DetourCommon.h:190
void dtIgnoreUnused(const T &)
Used to ignore a function parameter.
Definition: DetourCommon.h:40
TypeToRetrieveAs * dtGetThenAdvanceBufferPointer(const unsigned char *&buffer, const size_t distanceToAdvance)
Definition: DetourCommon.h:508
float dtVdist2D(const float *v1, const float *v2)
Derives the distance between the specified points on the xz-plane.
Definition: DetourCommon.h:243
float dtVlen(const float *v)
Derives the scalar length of the vector.
Definition: DetourCommon.h:200
T dtClamp(T v, T mn, T mx)
Clamps the value to the specified range.
Definition: DetourCommon.h:74
void dtClosestPtPointTriangle(float *closest, const float *p, const float *a, const float *b, const float *c)
Derives the closest point on a triangle from the specified reference point.
Definition: DetourCommon.cpp:24
float dtVlenSqr(const float *v)
Derives the square of the scalar length of the vector.
Definition: DetourCommon.h:208
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
bool dtOverlapPolyPoly2D(const float *polya, const int npolya, const float *polyb, const int npolyb)
Determines if the two convex polygons overlap on the xz-plane.
Definition: DetourCommon.cpp:294
float dtVdot(const float *v1, const float *v2)
Derives the dot product of two vectors.
Definition: DetourCommon.h:95
void dtVnormalize(float *v)
Normalizes the vector.
Definition: DetourCommon.h:263
T dtAbs(T a)
Returns the absolute value.
Definition: DetourCommon.h:62
void dtVset(float *dest, const float x, const float y, const float z)
Sets the vector elements to the specified values.
Definition: DetourCommon.h:182
void dtVlerp(float *dest, const float *v1, const float *v2, const float t)
Performs a linear interpolation between two vectors.
Definition: DetourCommon.h:117
bool dtPointInPolygon(const float *pt, const float *verts, const int nverts)
Determines if the specified point is inside the convex polygon on the xz-plane.
Definition: DetourCommon.cpp:238
void dtRandomPointInConvexPoly(const float *pts, const int npts, float *areas, const float s, const float t, float *out)
Definition: DetourCommon.cpp:332
void dtVscale(float *dest, const float *v, const float t)
Scales the vector by the specified value.
Definition: DetourCommon.h:150
T dtMax(T a, T b)
Returns the maximum of two values.
Definition: DetourCommon.h:57
void dtVsub(float *dest, const float *v1, const float *v2)
Performs a vector subtraction.
Definition: DetourCommon.h:139
unsigned int dtIlog2(unsigned int v)
Definition: DetourCommon.h:451
bool dtDistancePtPolyEdgesSqr(const float *pt, const float *verts, const int nverts, float *ed, float *et)
Definition: DetourCommon.cpp:254
void dtVcross(float *dest, const float *v1, const float *v2)
Derives the cross product of two vectors.
Definition: DetourCommon.h:84
float dtDistancePtSegSqr2D(const float *pt, const float *p, const float *q, float &t)
Definition: DetourCommon.cpp:170
float dtVdot2D(const float *u, const float *v)
Derives the dot product of two vectors on the xz-plane.
Definition: DetourCommon.h:313
int dtOppositeTile(int side)
Definition: DetourCommon.h:465
void dtSwapByte(unsigned char *a, unsigned char *b)
Definition: DetourCommon.h:467
unsigned int dtNextPow2(unsigned int v)
Definition: DetourCommon.h:439
void dtVadd(float *dest, const float *v1, const float *v2)
Performs a vector addition.
Definition: DetourCommon.h:128
bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3], const unsigned short bmin[3], const unsigned short bmax[3])
Determines if two axis-aligned bounding boxes overlap.
Definition: DetourCommon.h:354
float dtTriArea2D(const float *a, const float *b, const float *c)
Derives the signed xz-plane area of the triangle ABC, or the relationship of line AB to point C.
Definition: DetourCommon.h:338
float dtVdistSqr(const float *v1, const float *v2)
Returns the square of the distance between two points.
Definition: DetourCommon.h:229
bool dtVequal(const float *p0, const float *p1)
Performs a 'sloppy' colocation check of the specified points.
Definition: DetourCommon.h:278
bool dtOverlapBounds(const float *amin, const float *amax, const float *bmin, const float *bmax)
Determines if two axis-aligned bounding boxes overlap.
Definition: DetourCommon.h:371
float dtVdist2DSqr(const float *v1, const float *v2)
Derives the square of the distance between the specified points on the xz-plane.
Definition: DetourCommon.h:254
bool dtClosestHeightPointTriangle(const float *p, const float *a, const float *b, const float *c, float &h)
Derives the y-axis height of the closest point on the triangle from the specified reference point.
Definition: DetourCommon.cpp:204
bool dtVisfinite2D(const float *v)
Checks that the specified vector's 2D components are finite.
Definition: DetourCommon.h:301
void dtCalcPolyCenter(float *tc, const unsigned short *idx, int nidx, const float *verts)
Derives the centroid of a convex polygon.
Definition: DetourCommon.cpp:186
float dtVperp2D(const float *u, const float *v)
Derives the xz-plane 2D perp product of the two vectors.
Definition: DetourCommon.h:324
T dtSqr(T a)
Returns the square of the value.
Definition: DetourCommon.h:67
int dtAlign4(int x)
Definition: DetourCommon.h:463
void dtSwap(T &a, T &b)
Swaps the values of the two parameters.
Definition: DetourCommon.h:45
bool dtIntersectSegSeg2D(const float *ap, const float *aq, const float *bp, const float *bq, float &s, float &t)
Definition: DetourCommon.cpp:373
float dtMathSqrtf(float x)
Definition: DetourMath.h:13
bool dtMathIsfinite(float x)
Definition: DetourMath.h:19