Recast Navigation  1.0.35
DetourCommon.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
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 "SharedConfig.h"
23 #include "DetourMath.h"
24 
35 
41 template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
42 
47 template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; }
48 
53 template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; }
54 
58 template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; }
59 
63 template<class T> inline T dtSqr(T a) { return a*a; }
64 
70 template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
71 
75 
80 inline void dtVcross(float* dest, const float* v1, const float* v2)
81 {
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];
85 }
86 
91 inline float dtVdot(const float* v1, const float* v2)
92 {
93  return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
94 }
95 
101 inline void dtVmad(float* dest, const float* v1, const float* v2, const float s)
102 {
103  dest[0] = v1[0]+v2[0]*s;
104  dest[1] = v1[1]+v2[1]*s;
105  dest[2] = v1[2]+v2[2]*s;
106 }
107 
113 inline void dtVlerp(float* dest, const float* v1, const float* v2, const float t)
114 {
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;
118 }
119 
124 inline void dtVadd(float* dest, const float* v1, const float* v2)
125 {
126  dest[0] = v1[0]+v2[0];
127  dest[1] = v1[1]+v2[1];
128  dest[2] = v1[2]+v2[2];
129 }
130 
135 inline void dtVsub(float* dest, const float* v1, const float* v2)
136 {
137  dest[0] = v1[0]-v2[0];
138  dest[1] = v1[1]-v2[1];
139  dest[2] = v1[2]-v2[2];
140 }
141 
146 inline void dtVscale(float* dest, const float* v, const float t)
147 {
148  dest[0] = v[0]*t;
149  dest[1] = v[1]*t;
150  dest[2] = v[2]*t;
151 }
152 
156 inline void dtVmin(float* mn, const float* v)
157 {
158  mn[0] = dtMin(mn[0], v[0]);
159  mn[1] = dtMin(mn[1], v[1]);
160  mn[2] = dtMin(mn[2], v[2]);
161 }
162 
166 inline void dtVmax(float* mx, const float* v)
167 {
168  mx[0] = dtMax(mx[0], v[0]);
169  mx[1] = dtMax(mx[1], v[1]);
170  mx[2] = dtMax(mx[2], v[2]);
171 }
172 
178 inline void dtVset(float* dest, const float x, const float y, const float z)
179 {
180  dest[0] = x; dest[1] = y; dest[2] = z;
181 }
182 
186 inline void dtVcopy(float* dest, const float* a)
187 {
188  dest[0] = a[0];
189  dest[1] = a[1];
190  dest[2] = a[2];
191 }
192 
196 inline float dtVlen(const float* v)
197 {
198  return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
199 }
200 
204 inline float dtVlenSqr(const float* v)
205 {
206  return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
207 }
208 
213 inline float dtVdist(const float* v1, const float* v2)
214 {
215  const float dx = v2[0] - v1[0];
216  const float dy = v2[1] - v1[1];
217  const float dz = v2[2] - v1[2];
218  return dtMathSqrtf(dx*dx + dy*dy + dz*dz);
219 }
220 
225 inline float dtVdistSqr(const float* v1, const float* v2)
226 {
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;
231 }
232 
239 inline float dtVdist2D(const float* v1, const float* v2)
240 {
241  const float dx = v2[0] - v1[0];
242  const float dz = v2[2] - v1[2];
243  return dtMathSqrtf(dx*dx + dz*dz);
244 }
245 
250 inline float dtVdist2DSqr(const float* v1, const float* v2)
251 {
252  const float dx = v2[0] - v1[0];
253  const float dz = v2[2] - v1[2];
254  return dx*dx + dz*dz;
255 }
256 
259 inline void dtVnormalize(float* v)
260 {
261  float d = 1.0f / dtMathSqrtf(dtSqr(v[0]) + dtSqr(v[1]) + dtSqr(v[2]));
262  v[0] *= d;
263  v[1] *= d;
264  v[2] *= d;
265 }
266 
274 inline bool dtVequal(const float* p0, const float* p1)
275 {
276  static const float thr = dtSqr(1.0f/16384.0f);
277  const float d = dtVdistSqr(p0, p1);
278  return d < thr;
279 }
280 
287 inline float dtVdot2D(const float* u, const float* v)
288 {
289  return u[0]*v[0] + u[2]*v[2];
290 }
291 
298 inline float dtVperp2D(const float* u, const float* v)
299 {
300  return u[2]*v[0] - u[0]*v[2];
301 }
302 
306 
312 inline float dtTriArea2D(const float* a, const float* b, const float* c)
313 {
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;
319 }
320 
328 inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3],
329  const unsigned short bmin[3], const unsigned short bmax[3])
330 {
331  bool overlap = true;
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;
335  return overlap;
336 }
337 
345 inline bool dtOverlapBounds(const float* amin, const float* amax,
346  const float* bmin, const float* bmax)
347 {
348  bool overlap = true;
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;
352  return overlap;
353 }
354 
361 void dtClosestPtPointTriangle(float* closest, const float* p,
362  const float* a, const float* b, const float* c);
363 
370 bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
371 
372 bool dtIntersectSegmentPoly2D(const float* p0, const float* p1,
373  const float* verts, int nverts,
374  float& tmin, float& tmax,
375  int& segMin, int& segMax);
376 
377 bool dtIntersectSegSeg2D(const float* ap, const float* aq,
378  const float* bp, const float* bq,
379  float& s, float& t);
380 
386 bool dtPointInPolygon(const float* pt, const float* verts, const int nverts);
387 
388 bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
389  float* ed, float* et);
390 
391 float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
392 
398 void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
399 
406 bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
407  const float* polyb, const int npolyb);
408 
412 
413 inline unsigned int dtNextPow2(unsigned int v)
414 {
415  v--;
416  v |= v >> 1;
417  v |= v >> 2;
418  v |= v >> 4;
419  v |= v >> 8;
420  v |= v >> 16;
421  v++;
422  return v;
423 }
424 
425 inline unsigned int dtIlog2(unsigned int v)
426 {
427  unsigned int r;
428  unsigned int shift;
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;
433  r |= (v >> 1);
434  return r;
435 }
436 
437 inline int dtAlign4(int x) { return (x+3) & ~3; }
438 
439 inline int dtOppositeTile(int side) { return (side+4) & 0x7; }
440 
441 inline void dtSwapByte(unsigned char* a, unsigned char* b)
442 {
443  unsigned char tmp = *a;
444  *a = *b;
445  *b = tmp;
446 }
447 
448 inline void dtSwapEndian(unsigned short* v)
449 {
450  unsigned char* x = (unsigned char*)v;
451  dtSwapByte(x+0, x+1);
452 }
453 
454 inline void dtSwapEndian(short* v)
455 {
456  unsigned char* x = (unsigned char*)v;
457  dtSwapByte(x+0, x+1);
458 }
459 
460 inline void dtSwapEndian(unsigned int* v)
461 {
462  unsigned char* x = (unsigned char*)v;
463  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
464 }
465 
466 inline void dtSwapEndian(int* v)
467 {
468  unsigned char* x = (unsigned char*)v;
469  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
470 }
471 
472 inline void dtSwapEndian(uint64_t* v)
473 {
474  unsigned char* x = (unsigned char*)v;
475  dtSwapByte( x + 0, x + 7 );
476  dtSwapByte( x + 1, x + 6 );
477  dtSwapByte( x + 2, x + 5 );
478  dtSwapByte( x + 3, x + 4 );
479 }
480 
481 inline void dtSwapEndian(float* v)
482 {
483  unsigned char* x = (unsigned char*)v;
484  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
485 }
486 
487 void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
488  const float s, const float t, float* out);
489 
490 // Calculates convex hull on xz-plane of points on 'pts',
491 // stores the indices of the resulting hull in 'out' and
492 // returns number of points on hull.
493 int dtConvexhull( const float* pts, int npts, int* out );
494 
496 
497 #endif // DETOURCOMMON_H
498 
500 
501 // This section contains detailed documentation for members that don't have
502 // a source file. It reduces clutter in the main section of the header.
503 
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