19 #ifndef DETOURCOMMON_H
20 #define DETOURCOMMON_H
22 #include "SharedConfig.h"
41 template<
class T>
inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
47 template<
class T>
inline T
dtMin(T a, T b) {
return a < b ? a : b; }
53 template<
class T>
inline T
dtMax(T a, T b) {
return a > b ? a : b; }
58 template<
class T>
inline T
dtAbs(T a) {
return a < 0 ? -a : a; }
63 template<
class T>
inline T
dtSqr(T a) {
return a*a; }
70 template<
class T>
inline T
dtClamp(T v, T mn, T mx) {
return v < mn ? mn : (v > mx ? mx : v); }
80 inline void dtVcross(
float* dest,
const float* v1,
const float* v2)
82 dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
83 dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
84 dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
91 inline float dtVdot(
const float* v1,
const float* v2)
93 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
101 inline void dtVmad(
float* dest,
const float* v1,
const float* v2,
const float s)
103 dest[0] = v1[0]+v2[0]*s;
104 dest[1] = v1[1]+v2[1]*s;
105 dest[2] = v1[2]+v2[2]*s;
113 inline void dtVlerp(
float* dest,
const float* v1,
const float* v2,
const float t)
115 dest[0] = v1[0]+(v2[0]-v1[0])*t;
116 dest[1] = v1[1]+(v2[1]-v1[1])*t;
117 dest[2] = v1[2]+(v2[2]-v1[2])*t;
124 inline void dtVadd(
float* dest,
const float* v1,
const float* v2)
126 dest[0] = v1[0]+v2[0];
127 dest[1] = v1[1]+v2[1];
128 dest[2] = v1[2]+v2[2];
135 inline void dtVsub(
float* dest,
const float* v1,
const float* v2)
137 dest[0] = v1[0]-v2[0];
138 dest[1] = v1[1]-v2[1];
139 dest[2] = v1[2]-v2[2];
146 inline void dtVscale(
float* dest,
const float* v,
const float t)
156 inline void dtVmin(
float* mn,
const float* v)
158 mn[0] =
dtMin(mn[0], v[0]);
159 mn[1] =
dtMin(mn[1], v[1]);
160 mn[2] =
dtMin(mn[2], v[2]);
166 inline void dtVmax(
float* mx,
const float* v)
168 mx[0] =
dtMax(mx[0], v[0]);
169 mx[1] =
dtMax(mx[1], v[1]);
170 mx[2] =
dtMax(mx[2], v[2]);
178 inline void dtVset(
float* dest,
const float x,
const float y,
const float z)
180 dest[0] = x; dest[1] = y; dest[2] = z;
186 inline void dtVcopy(
float* dest,
const float* a)
198 return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
206 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
213 inline float dtVdist(
const float* v1,
const float* v2)
215 const float dx = v2[0] - v1[0];
216 const float dy = v2[1] - v1[1];
217 const float dz = v2[2] - v1[2];
227 const float dx = v2[0] - v1[0];
228 const float dy = v2[1] - v1[1];
229 const float dz = v2[2] - v1[2];
230 return dx*dx + dy*dy + dz*dz;
239 inline float dtVdist2D(
const float* v1,
const float* v2)
241 const float dx = v2[0] - v1[0];
242 const float dz = v2[2] - v1[2];
252 const float dx = v2[0] - v1[0];
253 const float dz = v2[2] - v1[2];
254 return dx*dx + dz*dz;
274 inline bool dtVequal(
const float* p0,
const float* p1)
276 static const float thr =
dtSqr(1.0f/16384.0f);
287 inline float dtVdot2D(
const float* u,
const float* v)
289 return u[0]*v[0] + u[2]*v[2];
300 return u[2]*v[0] - u[0]*v[2];
312 inline float dtTriArea2D(
const float* a,
const float* b,
const float* c)
314 const float abx = b[0] - a[0];
315 const float abz = b[2] - a[2];
316 const float acx = c[0] - a[0];
317 const float acz = c[2] - a[2];
318 return acx*abz - abx*acz;
329 const unsigned short bmin[3],
const unsigned short bmax[3])
332 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
333 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
334 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
346 const float* bmin,
const float* bmax)
349 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ?
false : overlap;
350 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ?
false : overlap;
351 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ?
false : overlap;
362 const float* a,
const float* b,
const float* c);
373 const float* verts,
int nverts,
374 float& tmin,
float& tmax,
375 int& segMin,
int& segMax);
378 const float* bp,
const float* bq,
386 bool dtPointInPolygon(
const float* pt,
const float* verts,
const int nverts);
389 float* ed,
float* et);
398 void dtCalcPolyCenter(
float* tc,
const unsigned short* idx,
int nidx,
const float* verts);
407 const float* polyb,
const int npolyb);
429 r = (v > 0xffff) << 4; v >>= r;
430 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
431 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
432 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
443 unsigned char tmp = *a;
450 unsigned char* x = (
unsigned char*)v;
456 unsigned char* x = (
unsigned char*)v;
462 unsigned char* x = (
unsigned char*)v;
468 unsigned char* x = (
unsigned char*)v;
474 unsigned char* x = (
unsigned char*)v;
483 unsigned char* x = (
unsigned char*)v;
488 const float s,
const float t,
float* out);
493 int dtConvexhull(
const float* pts,
int npts,
int* out );
497 #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)
Derives the square of the scalar length of the vector.
Definition: DetourCommon.h:204
void dtSwapByte(unsigned char *a, unsigned char *b)
Definition: DetourCommon.h:441
void dtSwapEndian(unsigned short *v)
Definition: DetourCommon.h:448
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:312
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 dtVlen(const float *v)
Derives the scalar length of the vector.
Definition: DetourCommon.h:196
void dtVlerp(float *dest, const float *v1, const float *v2, const float t)
Performs a linear interpolation between two vectors.
Definition: DetourCommon.h:113
void dtVmad(float *dest, const float *v1, const float *v2, const float s)
Performs a scaled vector addition.
Definition: DetourCommon.h:101
float dtVdist(const float *v1, const float *v2)
Returns the distance between two points.
Definition: DetourCommon.h:213
float dtVperp2D(const float *u, const float *v)
Derives the xz-plane 2D perp product of the two vectors.
Definition: DetourCommon.h:298
void dtVmin(float *mn, const float *v)
Selects the minimum value of each element from the specified vectors.
Definition: DetourCommon.h:156
void dtVmax(float *mx, const float *v)
Selects the maximum value of each element from the specified vectors.
Definition: DetourCommon.h:166
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:239
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
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)
Performs a vector addition.
Definition: DetourCommon.h:124
float dtDistancePtSegSqr2D(const float *pt, const float *p, const float *q, float &t)
Definition: DetourCommon.cpp:170
void dtVnormalize(float *v)
Normalizes the vector.
Definition: DetourCommon.h:259
T dtAbs(T a)
Returns the absolute value.
Definition: DetourCommon.h:58
float dtVdist2D(const float *v1, const float *v2)
Derives the distance between the specified points on the xz-plane.
Definition: DetourCommon.h:239
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:328
float dtMathSqrtf(float x)
Definition: DetourMath.h:13
void dtVset(float *dest, const float x, const float y, const float z)
Sets the vector elements to the specified values.
Definition: DetourCommon.h:178
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
T dtClamp(T v, T mn, T mx)
Clamps the value to the specified range.
Definition: DetourCommon.h:70
T dtMax(T a, T b)
Returns the maximum of two values.
Definition: DetourCommon.h:53
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:295
void dtVcross(float *dest, const float *v1, const float *v2)
Derives the cross product of two vectors.
Definition: DetourCommon.h:80
float dtVdot(const float *v1, const float *v2)
Derives the dot product of two vectors.
Definition: DetourCommon.h:91
unsigned int dtNextPow2(unsigned int v)
Definition: DetourCommon.h:413
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)
Performs a 'sloppy' colocation check of the specified points.
Definition: DetourCommon.h:274
void dtVcopy(float *dest, const float *a)
Performs a vector copy.
Definition: DetourCommon.h:186
T dtMin(T a, T b)
Returns the minimum of two values.
Definition: DetourCommon.h:47
int dtConvexhull(const float *pts, int npts, int *out)
Definition: DetourCommon.cpp:407
float dtVdistSqr(const float *v1, const float *v2)
Returns the square of the distance between two points.
Definition: DetourCommon.h:225
T dtSqr(T a)
Returns the square of the value.
Definition: DetourCommon.h:63
void dtSwap(T &a, T &b)
Swaps the values of the two parameters.
Definition: DetourCommon.h:41
int dtAlign4(int x)
Definition: DetourCommon.h:437
void dtVsub(float *dest, const float *v1, const float *v2)
Performs a vector subtraction.
Definition: DetourCommon.h:135
void dtVscale(float *dest, const float *v, const float t)
Scales the vector by the specified value.
Definition: DetourCommon.h:146
int dtOppositeTile(int side)
Definition: DetourCommon.h:439
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:345
float dtVdot2D(const float *u, const float *v)
Derives the dot product of two vectors on the xz-plane.
Definition: DetourCommon.h:287
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:250
unsigned int dtIlog2(unsigned int v)
Definition: DetourCommon.h:425
bool dtDistancePtPolyEdgesSqr(const float *pt, const float *verts, const int nverts, float *ed, float *et)
Definition: DetourCommon.cpp:255