TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DetourCommon.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18 
19 #ifndef DETOURCOMMON_H
20 #define DETOURCOMMON_H
21 
22 #include "DetourMath.h"
23 
34 
40 template<class T> void dtIgnoreUnused(const T&) { }
41 
45 template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
46 
51 template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; }
52 
57 template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; }
58 
62 template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; }
63 
67 template<class T> inline T dtSqr(T a) { return a*a; }
68 
74 template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
75 
79 
84 inline void dtVcross(float* dest, const float* v1, const float* v2)
85 {
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];
89 }
90 
95 inline float dtVdot(const float* v1, const float* v2)
96 {
97  return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
98 }
99 
105 inline void dtVmad(float* dest, const float* v1, const float* v2, const float s)
106 {
107  dest[0] = v1[0]+v2[0]*s;
108  dest[1] = v1[1]+v2[1]*s;
109  dest[2] = v1[2]+v2[2]*s;
110 }
111 
117 inline void dtVlerp(float* dest, const float* v1, const float* v2, const float t)
118 {
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;
122 }
123 
128 inline void dtVadd(float* dest, const float* v1, const float* v2)
129 {
130  dest[0] = v1[0]+v2[0];
131  dest[1] = v1[1]+v2[1];
132  dest[2] = v1[2]+v2[2];
133 }
134 
139 inline void dtVsub(float* dest, const float* v1, const float* v2)
140 {
141  dest[0] = v1[0]-v2[0];
142  dest[1] = v1[1]-v2[1];
143  dest[2] = v1[2]-v2[2];
144 }
145 
150 inline void dtVscale(float* dest, const float* v, const float t)
151 {
152  dest[0] = v[0]*t;
153  dest[1] = v[1]*t;
154  dest[2] = v[2]*t;
155 }
156 
160 inline void dtVmin(float* mn, const float* v)
161 {
162  mn[0] = dtMin(mn[0], v[0]);
163  mn[1] = dtMin(mn[1], v[1]);
164  mn[2] = dtMin(mn[2], v[2]);
165 }
166 
170 inline void dtVmax(float* mx, const float* v)
171 {
172  mx[0] = dtMax(mx[0], v[0]);
173  mx[1] = dtMax(mx[1], v[1]);
174  mx[2] = dtMax(mx[2], v[2]);
175 }
176 
182 inline void dtVset(float* dest, const float x, const float y, const float z)
183 {
184  dest[0] = x; dest[1] = y; dest[2] = z;
185 }
186 
190 inline void dtVcopy(float* dest, const float* a)
191 {
192  dest[0] = a[0];
193  dest[1] = a[1];
194  dest[2] = a[2];
195 }
196 
200 inline float dtVlen(const float* v)
201 {
202  return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
203 }
204 
208 inline float dtVlenSqr(const float* v)
209 {
210  return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
211 }
212 
217 inline float dtVdist(const float* v1, const float* v2)
218 {
219  const float dx = v2[0] - v1[0];
220  const float dy = v2[1] - v1[1];
221  const float dz = v2[2] - v1[2];
222  return dtMathSqrtf(dx*dx + dy*dy + dz*dz);
223 }
224 
229 inline float dtVdistSqr(const float* v1, const float* v2)
230 {
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;
235 }
236 
243 inline float dtVdist2D(const float* v1, const float* v2)
244 {
245  const float dx = v2[0] - v1[0];
246  const float dz = v2[2] - v1[2];
247  return dtMathSqrtf(dx*dx + dz*dz);
248 }
249 
254 inline float dtVdist2DSqr(const float* v1, const float* v2)
255 {
256  const float dx = v2[0] - v1[0];
257  const float dz = v2[2] - v1[2];
258  return dx*dx + dz*dz;
259 }
260 
263 inline void dtVnormalize(float* v)
264 {
265  float d = 1.0f / dtMathSqrtf(dtSqr(v[0]) + dtSqr(v[1]) + dtSqr(v[2]));
266  v[0] *= d;
267  v[1] *= d;
268  v[2] *= d;
269 }
270 
278 inline bool dtVequal(const float* p0, const float* p1)
279 {
280  static const float thr = dtSqr(1.0f/16384.0f);
281  const float d = dtVdistSqr(p0, p1);
282  return d < thr;
283 }
284 
291 inline float dtVdot2D(const float* u, const float* v)
292 {
293  return u[0]*v[0] + u[2]*v[2];
294 }
295 
302 inline float dtVperp2D(const float* u, const float* v)
303 {
304  return u[2]*v[0] - u[0]*v[2];
305 }
306 
310 
316 inline float dtTriArea2D(const float* a, const float* b, const float* c)
317 {
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;
323 }
324 
332 inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3],
333  const unsigned short bmin[3], const unsigned short bmax[3])
334 {
335  bool overlap = true;
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;
339  return overlap;
340 }
341 
349 inline bool dtOverlapBounds(const float* amin, const float* amax,
350  const float* bmin, const float* bmax)
351 {
352  bool overlap = true;
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;
356  return overlap;
357 }
358 
365 void dtClosestPtPointTriangle(float* closest, const float* p,
366  const float* a, const float* b, const float* c);
367 
374 bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
375 
376 bool dtIntersectSegmentPoly2D(const float* p0, const float* p1,
377  const float* verts, int nverts,
378  float& tmin, float& tmax,
379  int& segMin, int& segMax);
380 
381 bool dtIntersectSegSeg2D(const float* ap, const float* aq,
382  const float* bp, const float* bq,
383  float& s, float& t);
384 
390 bool dtPointInPolygon(const float* pt, const float* verts, const int nverts);
391 
392 bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
393  float* ed, float* et);
394 
395 float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
396 
402 void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
403 
410 bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
411  const float* polyb, const int npolyb);
412 
416 
417 inline unsigned int dtNextPow2(unsigned int v)
418 {
419  v--;
420  v |= v >> 1;
421  v |= v >> 2;
422  v |= v >> 4;
423  v |= v >> 8;
424  v |= v >> 16;
425  v++;
426  return v;
427 }
428 
429 inline unsigned int dtIlog2(unsigned int v)
430 {
431  unsigned int r;
432  unsigned int shift;
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;
437  r |= (v >> 1);
438  return r;
439 }
440 
441 inline int dtAlign4(int x) { return (x+3) & ~3; }
442 
443 inline int dtOppositeTile(int side) { return (side+4) & 0x7; }
444 
445 inline void dtSwapByte(unsigned char* a, unsigned char* b)
446 {
447  unsigned char tmp = *a;
448  *a = *b;
449  *b = tmp;
450 }
451 
452 inline void dtSwapEndian(unsigned short* v)
453 {
454  unsigned char* x = (unsigned char*)v;
455  dtSwapByte(x+0, x+1);
456 }
457 
458 inline void dtSwapEndian(short* v)
459 {
460  unsigned char* x = (unsigned char*)v;
461  dtSwapByte(x+0, x+1);
462 }
463 
464 inline void dtSwapEndian(unsigned int* v)
465 {
466  unsigned char* x = (unsigned char*)v;
467  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
468 }
469 
470 inline void dtSwapEndian(int* v)
471 {
472  unsigned char* x = (unsigned char*)v;
473  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
474 }
475 
476 inline void dtSwapEndian(float* v)
477 {
478  unsigned char* x = (unsigned char*)v;
479  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
480 }
481 
482 void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
483  const float s, const float t, float* out);
484 
486 
487 #endif // DETOURCOMMON_H
488 
490 
491 // This section contains detailed documentation for members that don't have
492 // a source file. It reduces clutter in the main section of the header.
493 
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