Recast Navigation
Navigation-mesh Toolset for Games
Loading...
Searching...
No Matches
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 "DetourMath.h"
23#include <stddef.h>
24
37
40template<class T> void dtIgnoreUnused(const T&) { }
41
45template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
46
51template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; }
52
57template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; }
58
62template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; }
63
67template<class T> inline T dtSqr(T a) { return a*a; }
68
74template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
75
79
84inline 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
95inline 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
105inline 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
117inline 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
128inline 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
139inline 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
150inline 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
160inline 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
170inline 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
182inline 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
190inline 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
200inline float dtVlen(const float* v)
201{
202 return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
203}
204
208inline float dtVlenSqr(const float* v)
209{
210 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
211}
212
217inline 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
229inline 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
243inline 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
254inline 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
263inline 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
278inline 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
289inline bool dtVisfinite(const float* v)
290{
291 bool result =
292 dtMathIsfinite(v[0]) &&
293 dtMathIsfinite(v[1]) &&
294 dtMathIsfinite(v[2]);
295
296 return result;
297}
298
301inline bool dtVisfinite2D(const float* v)
302{
303 bool result = dtMathIsfinite(v[0]) && dtMathIsfinite(v[2]);
304 return result;
305}
306
313inline float dtVdot2D(const float* u, const float* v)
314{
315 return u[0]*v[0] + u[2]*v[2];
316}
317
324inline float dtVperp2D(const float* u, const float* v)
325{
326 return u[2]*v[0] - u[0]*v[2];
327}
328
332
338inline float dtTriArea2D(const float* a, const float* b, const float* c)
339{
340 const float abx = b[0] - a[0];
341 const float abz = b[2] - a[2];
342 const float acx = c[0] - a[0];
343 const float acz = c[2] - a[2];
344 return acx*abz - abx*acz;
345}
346
354inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3],
355 const unsigned short bmin[3], const unsigned short bmax[3])
356{
357 bool overlap = true;
358 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
359 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
360 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
361 return overlap;
362}
363
371inline bool dtOverlapBounds(const float* amin, const float* amax,
372 const float* bmin, const float* bmax)
373{
374 bool overlap = true;
375 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
376 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
377 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
378 return overlap;
379}
380
387void dtClosestPtPointTriangle(float* closest, const float* p,
388 const float* a, const float* b, const float* c);
389
396bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
397
398bool dtIntersectSegmentPoly2D(const float* p0, const float* p1,
399 const float* verts, int nverts,
400 float& tmin, float& tmax,
401 int& segMin, int& segMax);
402
403bool dtIntersectSegSeg2D(const float* ap, const float* aq,
404 const float* bp, const float* bq,
405 float& s, float& t);
406
412bool dtPointInPolygon(const float* pt, const float* verts, const int nverts);
413
414bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
415 float* ed, float* et);
416
417float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
418
424void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
425
432bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
433 const float* polyb, const int npolyb);
434
438
439inline unsigned int dtNextPow2(unsigned int v)
440{
441 v--;
442 v |= v >> 1;
443 v |= v >> 2;
444 v |= v >> 4;
445 v |= v >> 8;
446 v |= v >> 16;
447 v++;
448 return v;
449}
450
451inline unsigned int dtIlog2(unsigned int v)
452{
453 unsigned int r;
454 unsigned int shift;
455 r = (v > 0xffff) << 4; v >>= r;
456 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
457 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
458 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
459 r |= (v >> 1);
460 return r;
461}
462
463inline int dtAlign4(int x) { return (x+3) & ~3; }
464
465inline int dtOppositeTile(int side) { return (side+4) & 0x7; }
466
467inline void dtSwapByte(unsigned char* a, unsigned char* b)
468{
469 unsigned char tmp = *a;
470 *a = *b;
471 *b = tmp;
472}
473
474inline void dtSwapEndian(unsigned short* v)
475{
476 unsigned char* x = (unsigned char*)v;
477 dtSwapByte(x+0, x+1);
478}
479
480inline void dtSwapEndian(short* v)
481{
482 unsigned char* x = (unsigned char*)v;
483 dtSwapByte(x+0, x+1);
484}
485
486inline void dtSwapEndian(unsigned int* v)
487{
488 unsigned char* x = (unsigned char*)v;
489 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
490}
491
492inline void dtSwapEndian(int* v)
493{
494 unsigned char* x = (unsigned char*)v;
495 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
496}
497
498inline void dtSwapEndian(float* v)
499{
500 unsigned char* x = (unsigned char*)v;
501 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
502}
503
504void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
505 const float s, const float t, float* out);
506
507template<typename TypeToRetrieveAs>
508TypeToRetrieveAs* dtGetThenAdvanceBufferPointer(const unsigned char*& buffer, const size_t distanceToAdvance)
509{
510 TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer);
511 buffer += distanceToAdvance;
512 return returnPointer;
513}
514
515template<typename TypeToRetrieveAs>
516TypeToRetrieveAs* dtGetThenAdvanceBufferPointer(unsigned char*& buffer, const size_t distanceToAdvance)
517{
518 TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer);
519 buffer += distanceToAdvance;
520 return returnPointer;
521}
522
523
525
526#endif // DETOURCOMMON_H
527
529
530// This section contains detailed documentation for members that don't have
531// a source file. It reduces clutter in the main section of the header.
532
float dtVdist(const float *v1, const float *v2)
Returns the distance between two points.
Definition DetourCommon.h:217
void dtVmin(float *mn, const float *v)
Selects the minimum value of each element from the specified vectors.
Definition DetourCommon.h:160
void dtVmax(float *mx, const float *v)
Selects the maximum value of each element from the specified vectors.
Definition DetourCommon.h:170
void dtSwapEndian(unsigned short *v)
Definition DetourCommon.h:474
T dtMin(T a, T b)
Returns the minimum of two values.
Definition DetourCommon.h:51
void dtVmad(float *dest, const float *v1, const float *v2, const float s)
Performs a scaled vector addition.
Definition DetourCommon.h:105
bool dtVisfinite(const float *v)
Checks that the specified vector's components are all finite.
Definition DetourCommon.h:289
void dtVcopy(float *dest, const float *a)
Performs a vector copy.
Definition DetourCommon.h:190
void dtIgnoreUnused(const T &)
Used to ignore a function parameter.
Definition DetourCommon.h:40
float dtVdist2D(const float *v1, const float *v2)
Derives the distance between the specified points on the xz-plane.
Definition DetourCommon.h:243
float dtVlen(const float *v)
Derives the scalar length of the vector.
Definition DetourCommon.h:200
T dtClamp(T v, T mn, T mx)
Clamps the value to the specified range.
Definition DetourCommon.h:74
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
float dtVlenSqr(const float *v)
Derives the square of the scalar length of the vector.
Definition DetourCommon.h:208
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
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:294
float dtVdot(const float *v1, const float *v2)
Derives the dot product of two vectors.
Definition DetourCommon.h:95
void dtVnormalize(float *v)
Normalizes the vector.
Definition DetourCommon.h:263
T dtAbs(T a)
Returns the absolute value.
Definition DetourCommon.h:62
void dtVset(float *dest, const float x, const float y, const float z)
Sets the vector elements to the specified values.
Definition DetourCommon.h:182
void dtVlerp(float *dest, const float *v1, const float *v2, const float t)
Performs a linear interpolation between two vectors.
Definition DetourCommon.h:117
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:238
void dtRandomPointInConvexPoly(const float *pts, const int npts, float *areas, const float s, const float t, float *out)
Definition DetourCommon.cpp:332
void dtVscale(float *dest, const float *v, const float t)
Scales the vector by the specified value.
Definition DetourCommon.h:150
T dtMax(T a, T b)
Returns the maximum of two values.
Definition DetourCommon.h:57
void dtVsub(float *dest, const float *v1, const float *v2)
Performs a vector subtraction.
Definition DetourCommon.h:139
unsigned int dtIlog2(unsigned int v)
Definition DetourCommon.h:451
bool dtDistancePtPolyEdgesSqr(const float *pt, const float *verts, const int nverts, float *ed, float *et)
Definition DetourCommon.cpp:254
void dtVcross(float *dest, const float *v1, const float *v2)
Derives the cross product of two vectors.
Definition DetourCommon.h:84
float dtDistancePtSegSqr2D(const float *pt, const float *p, const float *q, float &t)
Definition DetourCommon.cpp:170
float dtVdot2D(const float *u, const float *v)
Derives the dot product of two vectors on the xz-plane.
Definition DetourCommon.h:313
int dtOppositeTile(int side)
Definition DetourCommon.h:465
void dtSwapByte(unsigned char *a, unsigned char *b)
Definition DetourCommon.h:467
TypeToRetrieveAs * dtGetThenAdvanceBufferPointer(const unsigned char *&buffer, const size_t distanceToAdvance)
Definition DetourCommon.h:508
unsigned int dtNextPow2(unsigned int v)
Definition DetourCommon.h:439
void dtVadd(float *dest, const float *v1, const float *v2)
Performs a vector addition.
Definition DetourCommon.h:128
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:354
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:338
float dtVdistSqr(const float *v1, const float *v2)
Returns the square of the distance between two points.
Definition DetourCommon.h:229
bool dtVequal(const float *p0, const float *p1)
Performs a 'sloppy' colocation check of the specified points.
Definition DetourCommon.h:278
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:371
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:254
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
bool dtVisfinite2D(const float *v)
Checks that the specified vector's 2D components are finite.
Definition DetourCommon.h:301
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 dtVperp2D(const float *u, const float *v)
Derives the xz-plane 2D perp product of the two vectors.
Definition DetourCommon.h:324
T dtSqr(T a)
Returns the square of the value.
Definition DetourCommon.h:67
int dtAlign4(int x)
Definition DetourCommon.h:463
void dtSwap(T &a, T &b)
Swaps the values of the two parameters.
Definition DetourCommon.h:45
bool dtIntersectSegSeg2D(const float *ap, const float *aq, const float *bp, const float *bq, float &s, float &t)
Definition DetourCommon.cpp:373
float dtMathSqrtf(float x)
Definition DetourMath.h:13
bool dtMathIsfinite(float x)
Definition DetourMath.h:19