UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DetourCommon.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2// Modified version of Recast/Detour's source file
3
4//
5// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
6//
7// This software is provided 'as-is', without any express or implied
8// warranty. In no event will the authors be held liable for any damages
9// arising from the use of this software.
10// Permission is granted to anyone to use this software for any purpose,
11// including commercial applications, and to alter it and redistribute it
12// freely, subject to the following restrictions:
13// 1. The origin of this software must not be misrepresented; you must not
14// claim that you wrote the original software. If you use this software
15// in a product, an acknowledgment in the product documentation would be
16// appreciated but is not required.
17// 2. Altered source versions must be plainly marked as such, and must not be
18// misrepresented as being the original software.
19// 3. This notice may not be removed or altered from any source distribution.
20//
21
22#ifndef DETOURCOMMON_H
23#define DETOURCOMMON_H
24
25#include "CoreMinimal.h"
27#include "HAL/PlatformMath.h"
28
29// DT_STATS is set from the UE Stats settings
30#define DT_STATS STATS
31
44
48template<class T> inline void dtSwap(T& a, T& b) { T t = a; a = b; b = t; }
49
54template<class T> inline T dtMin(T a, T b) { return a < b ? a : b; }
55//@UE BEGIN Adding support for LWCoords. Overloading allows this to be called where one of the parameters is a double and the other a float.
56inline dtReal dtMin(dtReal a, dtReal b) { return dtMin<dtReal>(a, b); }
57//@UE END Adding support for LWCoords.
58
63template<class T> inline T dtMax(T a, T b) { return a > b ? a : b; }
64//@UE BEGIN Adding support for LWCoords. Overloading allows this to be called where one of the parameters is a double and the other a float.
65inline dtReal dtMax(dtReal a, dtReal b) { return dtMax<dtReal>(a, b); }
66//@UE END Adding support for LWCoords.
67
71template<class T> inline T dtAbs(T a) { return a < 0 ? -a : a; }
72
76template<class T> inline T dtSqr(T a) { return a*a; }
77
83template<class T> inline T dtClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
84//@UE BEGIN Adding support for LWCoords. Overloading allows this to be called where one of the parameters is a double and the other a float.
86//@UE END Adding support for LWCoords.
87
88inline float dtFloor(float x)
89{
90 return floorf(x);
91}
92
93inline double dtFloor(double x)
94{
95 return floor(x);
96}
97
98inline float dtCeil(float x)
99{
100 return ceilf(x);
101}
102
103inline double dtCeil(double x)
104{
105 return ceil(x);
106}
107
108inline float dtSin(float x)
109{
110 return sinf(x);
111}
112
113inline double dtSin(double x)
114{
115 return sin(x);
116}
117
118inline float dtCos(float x)
119{
120 return cosf(x);
121}
122
123inline double dtCos(double x)
124{
125 return cos(x);
126}
127
128inline float dtAtan2(float x, float y)
129{
130 return atan2f(x, y);
131}
132
133inline double dtAtan2(double x, double y)
134{
135 return atan2(x, y);
136}
137
138inline float dtSqrt(float x)
139{
140 return sqrtf(x);
141}
142
143inline double dtSqrt(double x)
144{
145 return sqrt(x);
146}
147
148inline float dtfMod(float x, float y)
149{
150 return fmodf(x, y);
151}
152
153inline double dtfMod(double x, double y)
154{
155 return fmod(x, y);
156}
157
158inline float dtLerp(float a, float b, float t)
159{
160 return a + (b-a)*t;
161}
162
166
171inline void dtVcross(dtReal* dest, const dtReal* v1, const dtReal* v2)
172{
173 dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
174 dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
175 dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
176}
177
182inline dtReal dtVdot(const dtReal* v1, const dtReal* v2)
183{
184 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
185}
186
192inline void dtVmad(dtReal* dest, const dtReal* v1, const dtReal* v2, const dtReal s)
193{
194 dest[0] = v1[0]+v2[0]*s;
195 dest[1] = v1[1]+v2[1]*s;
196 dest[2] = v1[2]+v2[2]*s;
197}
198
204inline void dtVlerp(dtReal* dest, const dtReal* v1, const dtReal* v2, const dtReal t)
205{
206 dest[0] = v1[0]+(v2[0]-v1[0])*t;
207 dest[1] = v1[1]+(v2[1]-v1[1])*t;
208 dest[2] = v1[2]+(v2[2]-v1[2])*t;
209}
210
215inline void dtVadd(dtReal* dest, const dtReal* v1, const dtReal* v2)
216{
217 dest[0] = v1[0]+v2[0];
218 dest[1] = v1[1]+v2[1];
219 dest[2] = v1[2]+v2[2];
220}
221
226inline void dtVsub(dtReal* dest, const dtReal* v1, const dtReal* v2)
227{
228 dest[0] = v1[0]-v2[0];
229 dest[1] = v1[1]-v2[1];
230 dest[2] = v1[2]-v2[2];
231}
232
237inline void dtVscale(dtReal* dest, const dtReal* v, const dtReal t)
238{
239 dest[0] = v[0]*t;
240 dest[1] = v[1]*t;
241 dest[2] = v[2]*t;
242}
243
247inline void dtVmin(dtReal* mn, const dtReal* v)
248{
249 mn[0] = dtMin(mn[0], v[0]);
250 mn[1] = dtMin(mn[1], v[1]);
251 mn[2] = dtMin(mn[2], v[2]);
252}
253
257inline void dtVmax(dtReal* mx, const dtReal* v)
258{
259 mx[0] = dtMax(mx[0], v[0]);
260 mx[1] = dtMax(mx[1], v[1]);
261 mx[2] = dtMax(mx[2], v[2]);
262}
263
269inline void dtVset(dtReal* dest, const dtReal x, const dtReal y, const dtReal z)
270{
271 dest[0] = x; dest[1] = y; dest[2] = z;
272}
273
277inline void dtVcopy(dtReal* dest, const dtReal* a)
278{
279 dest[0] = a[0];
280 dest[1] = a[1];
281 dest[2] = a[2];
282}
283
287inline dtReal dtVlen(const dtReal* v)
288{
289 return dtSqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
290}
291
295inline dtReal dtVlenSqr(const dtReal* v)
296{
297 return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
298}
299
304inline dtReal dtVdist(const dtReal* v1, const dtReal* v2)
305{
306 const dtReal dx = v2[0] - v1[0];
307 const dtReal dy = v2[1] - v1[1];
308 const dtReal dz = v2[2] - v1[2];
309 return dtSqrt(dx*dx + dy*dy + dz*dz);
310}
311
316inline dtReal dtVdistSqr(const dtReal* v1, const dtReal* v2)
317{
318 const dtReal dx = v2[0] - v1[0];
319 const dtReal dy = v2[1] - v1[1];
320 const dtReal dz = v2[2] - v1[2];
321 return dx*dx + dy*dy + dz*dz;
322}
323
330inline dtReal dtVdist2D(const dtReal* v1, const dtReal* v2)
331{
332 const dtReal dx = v2[0] - v1[0];
333 const dtReal dz = v2[2] - v1[2];
334 return dtSqrt(dx*dx + dz*dz);
335}
336
341inline dtReal dtVdist2DSqr(const dtReal* v1, const dtReal* v2)
342{
343 const dtReal dx = v2[0] - v1[0];
344 const dtReal dz = v2[2] - v1[2];
345 return dx*dx + dz*dz;
346}
347
350inline void dtVnormalize(dtReal* v)
351{
352 dtReal d = 1.0f / dtSqrt(dtSqr(v[0]) + dtSqr(v[1]) + dtSqr(v[2]));
353 v[0] *= d;
354 v[1] *= d;
355 v[2] *= d;
356}
357
361inline bool dtVisEqual(const unsigned short* a, const unsigned short* b)
362{
363 return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
364}
365
373inline bool dtVequal(const dtReal* p0, const dtReal* p1)
374{
375 static const dtReal thr = dtSqr(dtReal(1.)/16384.0f);
376 const dtReal d = dtVdistSqr(p0, p1);
377 return d < thr;
378}
379
386inline dtReal dtVdot2D(const dtReal* u, const dtReal* v)
387{
388 return u[0]*v[0] + u[2]*v[2];
389}
390
397inline dtReal dtVperp2D(const dtReal* u, const dtReal* v)
398{
399 return u[2]*v[0] - u[0]*v[2];
400}
401
405
411inline dtReal dtTriArea2D(const dtReal* a, const dtReal* b, const dtReal* c)
412{
413 const dtReal abx = b[0] - a[0];
414 const dtReal abz = b[2] - a[2];
415 const dtReal acx = c[0] - a[0];
416 const dtReal acz = c[2] - a[2];
417 return acx*abz - abx*acz;
418}
419
427inline bool dtOverlapQuantBounds(const unsigned short amin[3], const unsigned short amax[3],
428 const unsigned short bmin[3], const unsigned short bmax[3])
429{
430 bool overlap = true;
431 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
432 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
433 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
434 return overlap;
435}
436
444inline bool dtOverlapBounds(const dtReal* amin, const dtReal* amax,
445 const dtReal* bmin, const dtReal* bmax)
446{
447 bool overlap = true;
448 overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
449 overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
450 overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
451 return overlap;
452}
453
461 const dtReal* a, const dtReal* b, const dtReal* c);
462
469bool dtClosestHeightPointTriangle(const dtReal* p, const dtReal* a, const dtReal* b, const dtReal* c, dtReal& h);
470
471bool dtIntersectSegmentPoly2D(const dtReal* p0, const dtReal* p1,
472 const dtReal* verts, int nverts,
473 dtReal& tmin, dtReal& tmax,
474 int& segMin, int& segMax);
475
476bool dtIntersectSegSeg2D(const dtReal* ap, const dtReal* aq,
477 const dtReal* bp, const dtReal* bq,
478 dtReal& s, dtReal& t);
479
485bool dtPointInPolygon(const dtReal* pt, const dtReal* verts, const int nverts);
486
487bool dtDistancePtPolyEdgesSqr(const dtReal* pt, const dtReal* verts, const int nverts,
488 dtReal* ed, dtReal* et);
489
490dtReal dtDistancePtSegSqr2D(const dtReal* pt, const dtReal* p, const dtReal* q, dtReal& t);
491dtReal dtDistancePtSegSqr(const dtReal* pt, const dtReal* p, const dtReal* q);
492
498void dtCalcPolyCenter(dtReal* tc, const unsigned short* idx, int nidx, const dtReal* verts);
499
506bool dtOverlapPolyPoly2D(const dtReal* polya, const int npolya,
507 const dtReal* polyb, const int npolyb);
508
512
513inline unsigned int dtNextPow2(unsigned int v)
514{
515 v--;
516 v |= v >> 1;
517 v |= v >> 2;
518 v |= v >> 4;
519 v |= v >> 8;
520 v |= v >> 16;
521 v++;
522 return v;
523}
524
525inline unsigned int dtIlog2(unsigned int v)
526{
527 unsigned int r;
528 unsigned int shift;
529 r = (v > 0xffff) << 4; v >>= r;
530 shift = (v > 0xff) << 3; v >>= shift; r |= shift;
531 shift = (v > 0xf) << 2; v >>= shift; r |= shift;
532 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
533 r |= (v >> 1);
534 return r;
535}
536
537//@UE BEGIN Align to 8 byte boundaries when using double precision
538inline int dtAlign(int x)
539{
540#if DT_LARGE_WORLD_COORDINATES_DISABLED
541 return (x + 3) & ~3; // Align to 4 byte boundary
542#else
543 return (x + 7) & ~7; // Align to 8 byte boundary
544#endif
545}
546//@UE END
547
548inline int dtOppositeTile(int side) { return (side+4) & 0x7; }
549
550inline void dtSwapByte(unsigned char* a, unsigned char* b)
551{
552 unsigned char tmp = *a;
553 *a = *b;
554 *b = tmp;
555}
556
557inline void dtSwapEndian(unsigned short* v)
558{
559 unsigned char* x = (unsigned char*)v;
560 dtSwapByte(x+0, x+1);
561}
562
563inline void dtSwapEndian(short* v)
564{
565 unsigned char* x = (unsigned char*)v;
566 dtSwapByte(x+0, x+1);
567}
568
569inline void dtSwapEndian(unsigned int* v)
570{
571 unsigned char* x = (unsigned char*)v;
572 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
573}
574
575inline void dtSwapEndian(int* v)
576{
577 unsigned char* x = (unsigned char*)v;
578 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
579}
580
581inline void dtSwapEndian(unsigned long long int* v)
582{
583 unsigned char* x = (unsigned char*)v;
584 dtSwapByte(x + 0, x + 7);
585 dtSwapByte(x + 1, x + 6);
586 dtSwapByte(x + 2, x + 5);
587 dtSwapByte(x + 3, x + 4);
588}
589
590inline void dtSwapEndian(long long int* v)
591{
592 unsigned char* x = (unsigned char*)v;
593 dtSwapByte(x + 0, x + 7);
594 dtSwapByte(x + 1, x + 6);
595 dtSwapByte(x + 2, x + 5);
596 dtSwapByte(x + 3, x + 4);
597}
598
599inline void dtSwapEndian(float* v)
600{
601 unsigned char* x = (unsigned char*)v;
602 dtSwapByte(x+0, x+3); dtSwapByte(x+1, x+2);
603}
604
605// @UE BEGIN Adding support for LWCoords.
606inline void dtSwapEndian(double* v)
607{
608 unsigned char* x = (unsigned char*)v;
609 dtSwapByte(x + 0, x + 7); dtSwapByte(x + 1, x + 6); dtSwapByte(x + 2, x + 5); dtSwapByte(x + 3, x + 4);
610}
611//@UE END Adding support for LWCoords.
612
613void dtRandomPointInConvexPoly(const dtReal* pts, const int npts, dtReal* areas,
614 const dtReal s, const dtReal t, dtReal* out);
615
616// @UE BEGIN
624
629
634void dtVRot90(dtReal* dest, const dtReal* v, const dtRotation rot);
635
640void dtVRot90(unsigned short* dest, const unsigned short* v, const dtRotation rot);
641
647void dtRotate90(dtReal* dest, const dtReal* v, const dtReal* center, const dtRotation rot);
648
654void dtRotate90(unsigned short* dest, const unsigned short* v, const unsigned short* center, const dtRotation rot);
655// @UE END
656
658
659#endif // DETOURCOMMON_H
660
662
663// This section contains detailed documentation for members that don't have
664// a source file. It reduces clutter in the main section of the header.
665
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
bool dtVisEqual(const unsigned short *a, const unsigned short *b)
Definition DetourCommon.h:361
void dtVnormalize(dtReal *v)
Definition DetourCommon.h:350
void dtVmax(dtReal *mx, const dtReal *v)
Definition DetourCommon.h:257
void dtSwapEndian(unsigned short *v)
Definition DetourCommon.h:557
dtReal dtDistancePtSegSqr2D(const dtReal *pt, const dtReal *p, const dtReal *q, dtReal &t)
Definition DetourCommon.cpp:173
T dtMin(T a, T b)
Definition DetourCommon.h:54
void dtVlerp(dtReal *dest, const dtReal *v1, const dtReal *v2, const dtReal t)
Definition DetourCommon.h:204
void dtCalcPolyCenter(dtReal *tc, const unsigned short *idx, int nidx, const dtReal *verts)
Definition DetourCommon.cpp:216
void dtVset(dtReal *dest, const dtReal x, const dtReal y, const dtReal z)
Definition DetourCommon.h:269
void dtVadd(dtReal *dest, const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:215
float dtFloor(float x)
Definition DetourCommon.h:88
bool dtClosestHeightPointTriangle(const dtReal *p, const dtReal *a, const dtReal *b, const dtReal *c, dtReal &h)
Definition DetourCommon.cpp:234
bool dtDistancePtPolyEdgesSqr(const dtReal *pt, const dtReal *verts, const int nverts, dtReal *ed, dtReal *et)
Definition DetourCommon.cpp:283
bool dtPointInPolygon(const dtReal *pt, const dtReal *verts, const int nverts)
Definition DetourCommon.cpp:267
void dtVsub(dtReal *dest, const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:226
T dtClamp(T v, T mn, T mx)
Definition DetourCommon.h:83
dtReal dtVdist2DSqr(const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:341
float dtfMod(float x, float y)
Definition DetourCommon.h:148
dtRotation
Definition DetourCommon.h:618
@ DT_ROTATE_270
Definition DetourCommon.h:622
@ DT_ROTATE_0
Definition DetourCommon.h:619
@ DT_ROTATE_90
Definition DetourCommon.h:620
@ DT_ROTATE_180
Definition DetourCommon.h:621
float dtCeil(float x)
Definition DetourCommon.h:98
T dtAbs(T a)
Definition DetourCommon.h:71
void dtVmin(dtReal *mn, const dtReal *v)
Definition DetourCommon.h:247
void dtClosestPtPointTriangle(dtReal *closest, const dtReal *p, const dtReal *a, const dtReal *b, const dtReal *c)
Definition DetourCommon.cpp:27
dtReal dtVdistSqr(const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:316
T dtMax(T a, T b)
Definition DetourCommon.h:63
float dtSqrt(float x)
Definition DetourCommon.h:138
void dtVcopy(dtReal *dest, const dtReal *a)
Definition DetourCommon.h:277
bool dtIntersectSegSeg2D(const dtReal *ap, const dtReal *aq, const dtReal *bp, const dtReal *bq, dtReal &s, dtReal &t)
Definition DetourCommon.cpp:501
unsigned int dtIlog2(unsigned int v)
Definition DetourCommon.h:525
dtReal dtTriArea2D(const dtReal *a, const dtReal *b, const dtReal *c)
Definition DetourCommon.h:411
float dtCos(float x)
Definition DetourCommon.h:118
float dtSin(float x)
Definition DetourCommon.h:108
void dtVcross(dtReal *dest, const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:171
dtReal dtDistancePtSegSqr(const dtReal *pt, const dtReal *p, const dtReal *q)
Definition DetourCommon.cpp:191
void dtRandomPointInConvexPoly(const dtReal *pts, const int npts, dtReal *areas, const dtReal s, const dtReal t, dtReal *out)
Definition DetourCommon.cpp:361
int dtOppositeTile(int side)
Definition DetourCommon.h:548
int dtAlign(int x)
Definition DetourCommon.h:538
void dtVRot90(dtReal *dest, const dtReal *v, const dtRotation rot)
Definition DetourCommon.cpp:423
float dtLerp(float a, float b, float t)
Definition DetourCommon.h:158
dtRotation dtSelectRotation(dtReal rotationDeg)
Definition DetourCommon.cpp:405
void dtSwapByte(unsigned char *a, unsigned char *b)
Definition DetourCommon.h:550
bool dtOverlapBounds(const dtReal *amin, const dtReal *amax, const dtReal *bmin, const dtReal *bmax)
Definition DetourCommon.h:444
dtReal dtVdot2D(const dtReal *u, const dtReal *v)
Definition DetourCommon.h:386
bool dtIntersectSegmentPoly2D(const dtReal *p0, const dtReal *p1, const dtReal *verts, int nverts, dtReal &tmin, dtReal &tmax, int &segMin, int &segMax)
Definition DetourCommon.cpp:113
dtReal dtVdist2D(const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:330
unsigned int dtNextPow2(unsigned int v)
Definition DetourCommon.h:513
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:427
dtReal dtVperp2D(const dtReal *u, const dtReal *v)
Definition DetourCommon.h:397
float dtAtan2(float x, float y)
Definition DetourCommon.h:128
dtReal dtVlen(const dtReal *v)
Definition DetourCommon.h:287
bool dtVequal(const dtReal *p0, const dtReal *p1)
Definition DetourCommon.h:373
void dtVmad(dtReal *dest, const dtReal *v1, const dtReal *v2, const dtReal s)
Definition DetourCommon.h:192
dtReal dtVdist(const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:304
T dtSqr(T a)
Definition DetourCommon.h:76
void dtVscale(dtReal *dest, const dtReal *v, const dtReal t)
Definition DetourCommon.h:237
void dtSwap(T &a, T &b)
Definition DetourCommon.h:48
dtReal dtVdot(const dtReal *v1, const dtReal *v2)
Definition DetourCommon.h:182
dtReal dtVlenSqr(const dtReal *v)
Definition DetourCommon.h:295
bool dtOverlapPolyPoly2D(const dtReal *polya, const int npolya, const dtReal *polyb, const int npolyb)
Definition DetourCommon.cpp:323
void dtRotate90(dtReal *dest, const dtReal *v, const dtReal *center, const dtRotation rot)
Definition DetourCommon.cpp:475
double dtReal
Definition DetourLargeWorldCoordinates.h:15
float shift(float inValue, float expShift)
Definition RenderCore.cpp:859
char * dest
Definition lz4.h:709
float v
Definition radaudio_mdct.cpp:62