19 #ifndef DETOURCOMMON_H 20 #define DETOURCOMMON_H 38 template<
class T>
inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
44 template<
class T>
inline T
dtMin(T a, T b) {
return a < b ? a : b; }
50 template<
class T>
inline T
dtMax(T a, T b) {
return a > b ? a : b; }
55 template<
class T>
inline T
dtAbs(T a) {
return a < 0 ? -a : a; }
60 template<
class T>
inline T
dtSqr(T a) {
return a*a; }
67 template<
class T>
inline T
dtClamp(T v, T mn, T mx) {
return v < mn ? mn : (v > mx ? mx : v); }
82 inline void dtVcross(
float* dest,
const float* v1,
const float* v2)
84 dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
85 dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
86 dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
93 inline float dtVdot(
const float* v1,
const float* v2)
95 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
103 inline void dtVmad(
float* dest,
const float* v1,
const float* v2,
const float s)
105 dest[0] = v1[0]+v2[0]*s;
106 dest[1] = v1[1]+v2[1]*s;
107 dest[2] = v1[2]+v2[2]*s;
115 inline void dtVlerp(
float* dest,
const float* v1,
const float* v2,
const float t)
117 dest[0] = v1[0]+(v2[0]-v1[0])*t;
118 dest[1] = v1[1]+(v2[1]-v1[1])*t;
119 dest[2] = v1[2]+(v2[2]-v1[2])*t;
126 inline void dtVadd(
float* dest,
const float* v1,
const float* v2)
128 dest[0] = v1[0]+v2[0];
129 dest[1] = v1[1]+v2[1];
130 dest[2] = v1[2]+v2[2];
137 inline void dtVsub(
float* dest,
const float* v1,
const float* v2)
139 dest[0] = v1[0]-v2[0];
140 dest[1] = v1[1]-v2[1];
141 dest[2] = v1[2]-v2[2];
148 inline void dtVscale(
float* dest,
const float* v,
const float t)
158 inline void dtVmin(
float* mn,
const float* v)
160 mn[0] =
dtMin(mn[0], v[0]);
161 mn[1] =
dtMin(mn[1], v[1]);
162 mn[2] =
dtMin(mn[2], v[2]);
168 inline void dtVmax(
float* mx,
const float* v)
170 mx[0] =
dtMax(mx[0], v[0]);
171 mx[1] =
dtMax(mx[1], v[1]);
172 mx[2] =
dtMax(mx[2], v[2]);
180 inline void dtVset(
float* dest,
const float x,
const float y,
const float z)
182 dest[0] = x; dest[1] = y; dest[2] = z;
188 inline void dtVcopy(
float* dest,
const float* a)
200 return dtSqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
208 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
215 inline float dtVdist(
const float* v1,
const float* v2)
217 const float dx = v2[0] - v1[0];
218 const float dy = v2[1] - v1[1];
219 const float dz = v2[2] - v1[2];
220 return dtSqrt(dx*dx + dy*dy + dz*dz);
229 const float dx = v2[0] - v1[0];
230 const float dy = v2[1] - v1[1];
231 const float dz = v2[2] - v1[2];
232 return dx*dx + dy*dy + dz*dz;
241 inline float dtVdist2D(
const float* v1,
const float* v2)
243 const float dx = v2[0] - v1[0];
244 const float dz = v2[2] - v1[2];
245 return dtSqrt(dx*dx + dz*dz);
254 const float dx = v2[0] - v1[0];
255 const float dz = v2[2] - v1[2];
256 return dx*dx + dz*dz;
276 inline bool dtVequal(
const float* p0,
const float* p1)
278 static const float thr =
dtSqr(1.0f/16384.0f);
289 inline float dtVdot2D(
const float* u,
const float* v)
291 return u[0]*v[0] + u[2]*v[2];
302 return u[2]*v[0] - u[0]*v[2];
314 inline float dtTriArea2D(
const float* a,
const float* b,
const float* c)
316 const float abx = b[0] - a[0];
317 const float abz = b[2] - a[2];
318 const float acx = c[0] - a[0];
319 const float acz = c[2] - a[2];
320 return acx*abz - abx*acz;
331 const unsigned short bmin[3],
const unsigned short bmax[3])
334 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
335 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
336 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
348 const float* bmin,
const float* bmax)
351 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
352 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
353 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
364 const float* a,
const float* b,
const float* c);
375 const float* verts,
int nverts,
376 float& tmin,
float& tmax,
377 int& segMin,
int& segMax);
384 bool dtPointInPolygon(
const float* pt,
const float* verts,
const int nverts);
387 float* ed,
float* et);
396 void dtCalcPolyCenter(
float* tc,
const unsigned short* idx,
int nidx,
const float* verts);
405 const float* polyb,
const int npolyb);
427 r = (v > 0xffff) << 4; v >>= r;
428 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
429 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
430 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
441 unsigned char tmp = *a;
448 unsigned char* x = (
unsigned char*)v;
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;
477 const float s,
const float t,
float* out);
481 #endif // DETOURCOMMON_H void dtRandomPointInConvexPoly(const float *pts, const int npts, float *areas, const float s, const float t, float *out)
float dtVlenSqr(const float *v)
Derives the square of the scalar length of the vector. (len * len)
void dtSwapByte(unsigned char *a, unsigned char *b)
void dtSwapEndian(unsigned short *v)
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...
void dtCalcPolyCenter(float *tc, const unsigned short *idx, int nidx, const float *verts)
Derives the centroid of a convex polygon.
float dtVlen(const float *v)
Derives the scalar length of the vector.
void dtVlerp(float *dest, const float *v1, const float *v2, const float t)
Performs a linear interpolation between two vectors. (v1 toward v2)
void dtVmad(float *dest, const float *v1, const float *v2, const float s)
Performs a scaled vector addition. (v1 + (v2 * s))
float dtVdist(const float *v1, const float *v2)
Returns the distance between two points.
float dtVperp2D(const float *u, const float *v)
Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz)
float dtSqrt(float x)
Returns the square root of the value.
void dtVmin(float *mn, const float *v)
Selects the minimum value of each element from the specified vectors.
void dtVmax(float *mx, const float *v)
Selects the maximum value of each element from the specified vectors.
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.
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.
bool dtIntersectSegmentPoly2D(const float *p0, const float *p1, const float *verts, int nverts, float &tmin, float &tmax, int &segMin, int &segMax)
void dtVadd(float *dest, const float *v1, const float *v2)
Performs a vector addition. (v1 + v2)
float dtDistancePtSegSqr2D(const float *pt, const float *p, const float *q, float &t)
void dtVnormalize(float *v)
Normalizes the vector.
T dtAbs(T a)
Returns the absolute value.
float dtVdist2D(const float *v1, const float *v2)
Derives the distance between the specified points on the xz-plane.
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.
void dtVset(float *dest, const float x, const float y, const float z)
Sets the vector elements to the specified values.
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...
T dtClamp(T v, T mn, T mx)
Clamps the value to the specified range.
T dtMax(T a, T b)
Returns the maximum of two values.
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.
void dtVcross(float *dest, const float *v1, const float *v2)
Derives the cross product of two vectors. (v1 x v2)
float dtVdot(const float *v1, const float *v2)
Derives the dot product of two vectors. (v1 . v2)
unsigned int dtNextPow2(unsigned int v)
bool dtVequal(const float *p0, const float *p1)
Performs a 'sloppy' colocation check of the specified points.
void dtVcopy(float *dest, const float *a)
Performs a vector copy.
T dtMin(T a, T b)
Returns the minimum of two values.
float dtVdistSqr(const float *v1, const float *v2)
Returns the square of the distance between two points.
T dtSqr(T a)
Returns the square of the value.
void dtSwap(T &a, T &b)
Swaps the values of the two parameters.
void dtVsub(float *dest, const float *v1, const float *v2)
Performs a vector subtraction. (v1 - v2)
void dtVscale(float *dest, const float *v, const float t)
Scales the vector by the specified value. (v * t)
int dtOppositeTile(int side)
bool dtOverlapBounds(const float *amin, const float *amax, const float *bmin, const float *bmax)
Determines if two axis-aligned bounding boxes overlap.
float dtVdot2D(const float *u, const float *v)
Derives the dot product of two vectors on the xz-plane. (u . v)
float dtVdist2DSqr(const float *v1, const float *v2)
Derives the square of the distance between the specified points on the xz-plane.
unsigned int dtIlog2(unsigned int v)
bool dtDistancePtPolyEdgesSqr(const float *pt, const float *verts, const int nverts, float *ed, float *et)