RavEngine
Loading...
Searching...
No Matches
DetourCommon.h
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
41template<class T> void dtIgnoreUnused(const T&) { }
42
46template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
47
52template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; }
53
58template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; }
59
63template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; }
64
68template<class T> inline T dtSqr(T a) { return a*a; }
69
75template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
76
80
85inline void dtVcross(float* dest, const float* v1, const float* v2)
86{
87 dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
88 dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
89 dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
90}
91
96inline float dtVdot(const float* v1, const float* v2)
97{
98 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
99}
100
106inline void dtVmad(float* dest, const float* v1, const float* v2, const float s)
107{
108 dest[0] = v1[0]+v2[0]*s;
109 dest[1] = v1[1]+v2[1]*s;
110 dest[2] = v1[2]+v2[2]*s;
111}
112
118inline void dtVlerp(float* dest, const float* v1, const float* v2, const float t)
119{
120 dest[0] = v1[0]+(v2[0]-v1[0])*t;
121 dest[1] = v1[1]+(v2[1]-v1[1])*t;
122 dest[2] = v1[2]+(v2[2]-v1[2])*t;
123}
124
129inline void dtVadd(float* dest, const float* v1, const float* v2)
130{
131 dest[0] = v1[0]+v2[0];
132 dest[1] = v1[1]+v2[1];
133 dest[2] = v1[2]+v2[2];
134}
135
140inline void dtVsub(float* dest, const float* v1, const float* v2)
141{
142 dest[0] = v1[0]-v2[0];
143 dest[1] = v1[1]-v2[1];
144 dest[2] = v1[2]-v2[2];
145}
146
151inline void dtVscale(float* dest, const float* v, const float t)
152{
153 dest[0] = v[0]*t;
154 dest[1] = v[1]*t;
155 dest[2] = v[2]*t;
156}
157
161inline void dtVmin(float* mn, const float* v)
162{
163 mn[0] = dtMin(mn[0], v[0]);
164 mn[1] = dtMin(mn[1], v[1]);
165 mn[2] = dtMin(mn[2], v[2]);
166}
167
171inline void dtVmax(float* mx, const float* v)
172{
173 mx[0] = dtMax(mx[0], v[0]);
174 mx[1] = dtMax(mx[1], v[1]);
175 mx[2] = dtMax(mx[2], v[2]);
176}
177
183inline void dtVset(float* dest, const float x, const float y, const float z)
184{
185 dest[0] = x; dest[1] = y; dest[2] = z;
186}
187
191inline void dtVcopy(float* dest, const float* a)
192{
193 dest[0] = a[0];
194 dest[1] = a[1];
195 dest[2] = a[2];
196}
197
201inline float dtVlen(const float* v)
202{
203 return dtMathSqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
204}
205
209inline float dtVlenSqr(const float* v)
210{
211 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
212}
213
218inline float dtVdist(const float* v1, const float* v2)
219{
220 const float dx = v2[0] - v1[0];
221 const float dy = v2[1] - v1[1];
222 const float dz = v2[2] - v1[2];
223 return dtMathSqrtf(dx*dx + dy*dy + dz*dz);
224}
225
230inline float dtVdistSqr(const float* v1, const float* v2)
231{
232 const float dx = v2[0] - v1[0];
233 const float dy = v2[1] - v1[1];
234 const float dz = v2[2] - v1[2];
235 return dx*dx + dy*dy + dz*dz;
236}
237
244inline float dtVdist2D(const float* v1, const float* v2)
245{
246 const float dx = v2[0] - v1[0];
247 const float dz = v2[2] - v1[2];
248 return dtMathSqrtf(dx*dx + dz*dz);
249}
250
255inline float dtVdist2DSqr(const float* v1, const float* v2)
256{
257 const float dx = v2[0] - v1[0];
258 const float dz = v2[2] - v1[2];
259 return dx*dx + dz*dz;
260}
261
264inline void dtVnormalize(float* v)
265{
266 float d = 1.0f / dtMathSqrtf(dtSqr(v[0]) + dtSqr(v[1]) + dtSqr(v[2]));
267 v[0] *= d;
268 v[1] *= d;
269 v[2] *= d;
270}
271
279inline bool dtVequal(const float* p0, const float* p1)
280{
281 static const float thr = dtSqr(1.0f/16384.0f);
282 const float d = dtVdistSqr(p0, p1);
283 return d < thr;
284}
285
290inline bool dtVisfinite(const float* v)
291{
292 bool result =
293 dtMathIsfinite(v[0]) &&
294 dtMathIsfinite(v[1]) &&
295 dtMathIsfinite(v[2]);
296
297 return result;
298}
299
302inline bool dtVisfinite2D(const float* v)
303{
304 bool result = dtMathIsfinite(v[0]) && dtMathIsfinite(v[2]);
305 return result;
306}
307
314inline float dtVdot2D(const float* u, const float* v)
315{
316 return u[0]*v[0] + u[2]*v[2];
317}
318
325inline float dtVperp2D(const float* u, const float* v)
326{
327 return u[2]*v[0] - u[0]*v[2];
328}
329
333
339inline float dtTriArea2D(const float* a, const float* b, const float* c)
340{
341 const float abx = b[0] - a[0];
342 const float abz = b[2] - a[2];
343 const float acx = c[0] - a[0];
344 const float acz = c[2] - a[2];
345 return acx*abz - abx*acz;
346}
347
355inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3],
356 const unsigned short bmin[3], const unsigned short bmax[3])
357{
358 bool overlap = true;
359 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
360 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
361 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
362 return overlap;
363}
364
372inline bool dtOverlapBounds(const float* amin, const float* amax,
373 const float* bmin, const float* bmax)
374{
375 bool overlap = true;
376 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
377 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
378 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
379 return overlap;
380}
381
388void dtClosestPtPointTriangle(float* closest, const float* p,
389 const float* a, const float* b, const float* c);
390
397bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
398
399bool dtIntersectSegmentPoly2D(const float* p0, const float* p1,
400 const float* verts, int nverts,
401 float& tmin, float& tmax,
402 int& segMin, int& segMax);
403
404bool dtIntersectSegSeg2D(const float* ap, const float* aq,
405 const float* bp, const float* bq,
406 float& s, float& t);
407
413bool dtPointInPolygon(const float* pt, const float* verts, const int nverts);
414
415bool dtDistancePtPolyEdgesSqr(const float* pt, const float* verts, const int nverts,
416 float* ed, float* et);
417
418float dtDistancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
419
425void dtCalcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
426
433bool dtOverlapPolyPoly2D(const float* polya, const int npolya,
434 const float* polyb, const int npolyb);
435
439
440inline unsigned int dtNextPow2(unsigned int v)
441{
442 v--;
443 v |= v >> 1;
444 v |= v >> 2;
445 v |= v >> 4;
446 v |= v >> 8;
447 v |= v >> 16;
448 v++;
449 return v;
450}
451
452inline unsigned int dtIlog2(unsigned int v)
453{
454 unsigned int r;
455 unsigned int shift;
456 r = (v > 0xffff) << 4; v >>= r;
457 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
458 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
459 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
460 r |= (v >> 1);
461 return r;
462}
463
464inline int dtAlign4(int x) { return (x+3) & ~3; }
465
466inline int dtOppositeTile(int side) { return (side+4) & 0x7; }
467
468inline void dtSwapByte(unsigned char* a, unsigned char* b)
469{
470 unsigned char tmp = *a;
471 *a = *b;
472 *b = tmp;
473}
474
475inline void dtSwapEndian(unsigned short* v)
476{
477 unsigned char* x = (unsigned char*)v;
478 dtSwapByte(x+0, x+1);
479}
480
481inline void dtSwapEndian(short* v)
482{
483 unsigned char* x = (unsigned char*)v;
484 dtSwapByte(x+0, x+1);
485}
486
487inline void dtSwapEndian(unsigned int* v)
488{
489 unsigned char* x = (unsigned char*)v;
490 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
491}
492
493inline void dtSwapEndian(int* v)
494{
495 unsigned char* x = (unsigned char*)v;
496 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
497}
498
499inline void dtSwapEndian(float* v)
500{
501 unsigned char* x = (unsigned char*)v;
502 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
503}
504
505void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
506 const float s, const float t, float* out);
507
508template<typename TypeToRetrieveAs>
509TypeToRetrieveAs* dtGetThenAdvanceBufferPointer(const unsigned char*& buffer, const size_t distanceToAdvance)
510{
511 TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer);
512 buffer += distanceToAdvance;
513 return returnPointer;
514}
515
516template<typename TypeToRetrieveAs>
517TypeToRetrieveAs* dtGetThenAdvanceBufferPointer(unsigned char*& buffer, const size_t distanceToAdvance)
518{
519 TypeToRetrieveAs* returnPointer = reinterpret_cast<TypeToRetrieveAs*>(buffer);
520 buffer += distanceToAdvance;
521 return returnPointer;
522}
523
524
526
527#endif // DETOURCOMMON_H
528
530
531// This section contains detailed documentation for members that don't have
532// a source file. It reduces clutter in the main section of the header.
533