Planeshift
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 
32 
38 template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
39 
44 template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; }
45 
50 template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; }
51 
55 template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; }
56 
60 template<class T> inline T dtSqr(T a) { return a*a; }
61 
67 template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
68 
72 float dtSqrt(float x);
73 
77 
82 inline void dtVcross(float* dest, const float* v1, const float* v2)
83 {
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];
87 }
88 
93 inline float dtVdot(const float* v1, const float* v2)
94 {
95  return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
96 }
97 
103 inline void dtVmad(float* dest, const float* v1, const float* v2, const float s)
104 {
105  dest[0] = v1[0]+v2[0]*s;
106  dest[1] = v1[1]+v2[1]*s;
107  dest[2] = v1[2]+v2[2]*s;
108 }
109 
115 inline void dtVlerp(float* dest, const float* v1, const float* v2, const float t)
116 {
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;
120 }
121 
126 inline void dtVadd(float* dest, const float* v1, const float* v2)
127 {
128  dest[0] = v1[0]+v2[0];
129  dest[1] = v1[1]+v2[1];
130  dest[2] = v1[2]+v2[2];
131 }
132 
137 inline void dtVsub(float* dest, const float* v1, const float* v2)
138 {
139  dest[0] = v1[0]-v2[0];
140  dest[1] = v1[1]-v2[1];
141  dest[2] = v1[2]-v2[2];
142 }
143 
148 inline void dtVscale(float* dest, const float* v, const float t)
149 {
150  dest[0] = v[0]*t;
151  dest[1] = v[1]*t;
152  dest[2] = v[2]*t;
153 }
154 
158 inline void dtVmin(float* mn, const float* v)
159 {
160  mn[0] = dtMin(mn[0], v[0]);
161  mn[1] = dtMin(mn[1], v[1]);
162  mn[2] = dtMin(mn[2], v[2]);
163 }
164 
168 inline void dtVmax(float* mx, const float* v)
169 {
170  mx[0] = dtMax(mx[0], v[0]);
171  mx[1] = dtMax(mx[1], v[1]);
172  mx[2] = dtMax(mx[2], v[2]);
173 }
174 
180 inline void dtVset(float* dest, const float x, const float y, const float z)
181 {
182  dest[0] = x; dest[1] = y; dest[2] = z;
183 }
184 
188 inline void dtVcopy(float* dest, const float* a)
189 {
190  dest[0] = a[0];
191  dest[1] = a[1];
192  dest[2] = a[2];
193 }
194 
198 inline float dtVlen(const float* v)
199 {
200  return dtSqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
201 }
202 
206 inline float dtVlenSqr(const float* v)
207 {
208  return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
209 }
210 
215 inline float dtVdist(const float* v1, const float* v2)
216 {
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);
221 }
222 
227 inline float dtVdistSqr(const float* v1, const float* v2)
228 {
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;
233 }
234 
241 inline float dtVdist2D(const float* v1, const float* v2)
242 {
243  const float dx = v2[0] - v1[0];
244  const float dz = v2[2] - v1[2];
245  return dtSqrt(dx*dx + dz*dz);
246 }
247 
252 inline float dtVdist2DSqr(const float* v1, const float* v2)
253 {
254  const float dx = v2[0] - v1[0];
255  const float dz = v2[2] - v1[2];
256  return dx*dx + dz*dz;
257 }
258 
261 inline void dtVnormalize(float* v)
262 {
263  float d = 1.0f / dtSqrt(dtSqr(v[0]) + dtSqr(v[1]) + dtSqr(v[2]));
264  v[0] *= d;
265  v[1] *= d;
266  v[2] *= d;
267 }
268 
276 inline bool dtVequal(const float* p0, const float* p1)
277 {
278  static const float thr = dtSqr(1.0f/16384.0f);
279  const float d = dtVdistSqr(p0, p1);
280  return d < thr;
281 }
282 
289 inline float dtVdot2D(const float* u, const float* v)
290 {
291  return u[0]*v[0] + u[2]*v[2];
292 }
293 
300 inline float dtVperp2D(const float* u, const float* v)
301 {
302  return u[2]*v[0] - u[0]*v[2];
303 }
304 
308 
314 inline float dtTriArea2D(const float* a, const float* b, const float* c)
315 {
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;
321 }
322 
330 inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3],
331  const unsigned short bmin[3], const unsigned short bmax[3])
332 {
333  bool overlap = true;
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;
337  return overlap;
338 }
339 
347 inline bool dtOverlapBounds(const float* amin, const float* amax,
348  const float* bmin, const float* bmax)
349 {
350  bool overlap = true;
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;
354  return overlap;
355 }
356 
363 void dtClosestPtPointTriangle(float* closest, const float* p,
364  const float* a, const float* b, const float* c);
365 
372 bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
373 
374 bool dtIntersectSegmentPoly2D(const float* p0, const float* p1,
375  const float* verts, int nverts,
376  float& tmin, float& tmax,
377  int& segMin, int& segMax);
378 
384 bool dtPointInPolygon(const float* pt, const float* verts, const int nverts);
385 
386 bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
387  float* ed, float* et);
388 
389 float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
390 
396 void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
397 
404 bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
405  const float* polyb, const int npolyb);
406 
410 
411 inline unsigned int dtNextPow2(unsigned int v)
412 {
413  v--;
414  v |= v >> 1;
415  v |= v >> 2;
416  v |= v >> 4;
417  v |= v >> 8;
418  v |= v >> 16;
419  v++;
420  return v;
421 }
422 
423 inline unsigned int dtIlog2(unsigned int v)
424 {
425  unsigned int r;
426  unsigned int shift;
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;
431  r |= (v >> 1);
432  return r;
433 }
434 
435 inline int dtAlign4(int x) { return (x+3) & ~3; }
436 
437 inline int dtOppositeTile(int side) { return (side+4) & 0x7; }
438 
439 inline void dtSwapByte(unsigned char* a, unsigned char* b)
440 {
441  unsigned char tmp = *a;
442  *a = *b;
443  *b = tmp;
444 }
445 
446 inline void dtSwapEndian(unsigned short* v)
447 {
448  unsigned char* x = (unsigned char*)v;
449  dtSwapByte(x+0, x+1);
450 }
451 
452 inline void dtSwapEndian(short* v)
453 {
454  unsigned char* x = (unsigned char*)v;
455  dtSwapByte(x+0, x+1);
456 }
457 
458 inline void dtSwapEndian(unsigned int* v)
459 {
460  unsigned char* x = (unsigned char*)v;
461  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
462 }
463 
464 inline void dtSwapEndian(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(float* v)
471 {
472  unsigned char* x = (unsigned char*)v;
473  dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
474 }
475 
476 void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
477  const float s, const float t, float* out);
478 
480 
481 #endif // DETOURCOMMON_H
482 
484 
485 // This section contains detailed documentation for members that don't have
486 // a source file. It reduces clutter in the main section of the header.
487 
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)
Definition: DetourCommon.h:206
void dtSwapByte(unsigned char *a, unsigned char *b)
Definition: DetourCommon.h:439
void dtSwapEndian(unsigned short *v)
Definition: DetourCommon.h:446
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:314
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.
Definition: DetourCommon.h:198
void dtVlerp(float *dest, const float *v1, const float *v2, const float t)
Performs a linear interpolation between two vectors. (v1 toward v2)
Definition: DetourCommon.h:115
void dtVmad(float *dest, const float *v1, const float *v2, const float s)
Performs a scaled vector addition. (v1 + (v2 * s))
Definition: DetourCommon.h:103
float dtVdist(const float *v1, const float *v2)
Returns the distance between two points.
Definition: DetourCommon.h:215
float dtVperp2D(const float *u, const float *v)
Derives the xz-plane 2D perp product of the two vectors. (uz*vx - ux*vz)
Definition: DetourCommon.h:300
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.
Definition: DetourCommon.h:158
void dtVmax(float *mx, const float *v)
Selects the maximum value of each element from the specified vectors.
Definition: DetourCommon.h:168
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)
Definition: DetourCommon.h:126
float dtDistancePtSegSqr2D(const float *pt, const float *p, const float *q, float &t)
void dtVnormalize(float *v)
Normalizes the vector.
Definition: DetourCommon.h:261
T dtAbs(T a)
Returns the absolute value.
Definition: DetourCommon.h:55
float dtVdist2D(const float *v1, const float *v2)
Derives the distance between the specified points on the xz-plane.
Definition: DetourCommon.h:241
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:330
void dtVset(float *dest, const float x, const float y, const float z)
Sets the vector elements to the specified values.
Definition: DetourCommon.h:180
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.
Definition: DetourCommon.h:67
T dtMax(T a, T b)
Returns the maximum of two values.
Definition: DetourCommon.h:50
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)
Definition: DetourCommon.h:82
float dtVdot(const float *v1, const float *v2)
Derives the dot product of two vectors. (v1 . v2)
Definition: DetourCommon.h:93
unsigned int dtNextPow2(unsigned int v)
Definition: DetourCommon.h:411
bool dtVequal(const float *p0, const float *p1)
Performs a &#39;sloppy&#39; colocation check of the specified points.
Definition: DetourCommon.h:276
void dtVcopy(float *dest, const float *a)
Performs a vector copy.
Definition: DetourCommon.h:188
T dtMin(T a, T b)
Returns the minimum of two values.
Definition: DetourCommon.h:44
float dtVdistSqr(const float *v1, const float *v2)
Returns the square of the distance between two points.
Definition: DetourCommon.h:227
T dtSqr(T a)
Returns the square of the value.
Definition: DetourCommon.h:60
void dtSwap(T &a, T &b)
Swaps the values of the two parameters.
Definition: DetourCommon.h:38
int dtAlign4(int x)
Definition: DetourCommon.h:435
void dtVsub(float *dest, const float *v1, const float *v2)
Performs a vector subtraction. (v1 - v2)
Definition: DetourCommon.h:137
void dtVscale(float *dest, const float *v, const float t)
Scales the vector by the specified value. (v * t)
Definition: DetourCommon.h:148
int dtOppositeTile(int side)
Definition: DetourCommon.h:437
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:347
float dtVdot2D(const float *u, const float *v)
Derives the dot product of two vectors on the xz-plane. (u . v)
Definition: DetourCommon.h:289
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:252
unsigned int dtIlog2(unsigned int v)
Definition: DetourCommon.h:423
bool dtDistancePtPolyEdgesSqr(const float *pt, const float *verts, const int nverts, float *ed, float *et)