Recast Navigation
Navigation-mesh Toolset for Games
DetourCommon.h File Reference
#include "DetourMath.h"
#include <stddef.h>

Go to the source code of this file.

Functions

General helper functions
template<class T >
void dtIgnoreUnused (const T &)
 Used to ignore a function parameter. More...
 
template<class T >
void dtSwap (T &a, T &b)
 Swaps the values of the two parameters. More...
 
template<class T >
dtMin (T a, T b)
 Returns the minimum of two values. More...
 
template<class T >
dtMax (T a, T b)
 Returns the maximum of two values. More...
 
template<class T >
dtAbs (T a)
 Returns the absolute value. More...
 
template<class T >
dtSqr (T a)
 Returns the square of the value. More...
 
template<class T >
dtClamp (T v, T mn, T mx)
 Clamps the value to the specified range. More...
 
Vector helper functions.
void dtVcross (float *dest, const float *v1, const float *v2)
 Derives the cross product of two vectors. More...
 
float dtVdot (const float *v1, const float *v2)
 Derives the dot product of two vectors. More...
 
void dtVmad (float *dest, const float *v1, const float *v2, const float s)
 Performs a scaled vector addition. More...
 
void dtVlerp (float *dest, const float *v1, const float *v2, const float t)
 Performs a linear interpolation between two vectors. More...
 
void dtVadd (float *dest, const float *v1, const float *v2)
 Performs a vector addition. More...
 
void dtVsub (float *dest, const float *v1, const float *v2)
 Performs a vector subtraction. More...
 
void dtVscale (float *dest, const float *v, const float t)
 Scales the vector by the specified value. More...
 
void dtVmin (float *mn, const float *v)
 Selects the minimum value of each element from the specified vectors. More...
 
void dtVmax (float *mx, const float *v)
 Selects the maximum value of each element from the specified vectors. More...
 
void dtVset (float *dest, const float x, const float y, const float z)
 Sets the vector elements to the specified values. More...
 
void dtVcopy (float *dest, const float *a)
 Performs a vector copy. More...
 
float dtVlen (const float *v)
 Derives the scalar length of the vector. More...
 
float dtVlenSqr (const float *v)
 Derives the square of the scalar length of the vector. More...
 
float dtVdist (const float *v1, const float *v2)
 Returns the distance between two points. More...
 
float dtVdistSqr (const float *v1, const float *v2)
 Returns the square of the distance between two points. More...
 
float dtVdist2D (const float *v1, const float *v2)
 Derives the distance between the specified points on the xz-plane. More...
 
float dtVdist2DSqr (const float *v1, const float *v2)
 Derives the square of the distance between the specified points on the xz-plane. More...
 
void dtVnormalize (float *v)
 Normalizes the vector. More...
 
bool dtVequal (const float *p0, const float *p1)
 Performs a 'sloppy' colocation check of the specified points. More...
 
bool dtVisfinite (const float *v)
 Checks that the specified vector's components are all finite. More...
 
bool dtVisfinite2D (const float *v)
 Checks that the specified vector's 2D components are finite. More...
 
float dtVdot2D (const float *u, const float *v)
 Derives the dot product of two vectors on the xz-plane. More...
 
float dtVperp2D (const float *u, const float *v)
 Derives the xz-plane 2D perp product of the two vectors. More...
 
Computational geometry helper functions.
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. More...
 
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. More...
 
bool dtOverlapBounds (const float *amin, const float *amax, const float *bmin, const float *bmax)
 Determines if two axis-aligned bounding boxes overlap. More...
 
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. More...
 
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. More...
 
bool dtIntersectSegmentPoly2D (const float *p0, const float *p1, const float *verts, int nverts, float &tmin, float &tmax, int &segMin, int &segMax)
 
bool dtIntersectSegSeg2D (const float *ap, const float *aq, const float *bp, const float *bq, float &s, float &t)
 
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. More...
 
bool dtDistancePtPolyEdgesSqr (const float *pt, const float *verts, const int nverts, float *ed, float *et)
 
float dtDistancePtSegSqr2D (const float *pt, const float *p, const float *q, float &t)
 
void dtCalcPolyCenter (float *tc, const unsigned short *idx, int nidx, const float *verts)
 Derives the centroid of a convex polygon. More...
 
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. More...
 
Miscellanious functions.
unsigned int dtNextPow2 (unsigned int v)
 
unsigned int dtIlog2 (unsigned int v)
 
int dtAlign4 (int x)
 
int dtOppositeTile (int side)
 
void dtSwapByte (unsigned char *a, unsigned char *b)
 
void dtSwapEndian (unsigned short *v)
 
void dtSwapEndian (short *v)
 
void dtSwapEndian (unsigned int *v)
 
void dtSwapEndian (int *v)
 
void dtSwapEndian (float *v)
 
void dtRandomPointInConvexPoly (const float *pts, const int npts, float *areas, const float s, const float t, float *out)
 
template<typename TypeToRetrieveAs >
TypeToRetrieveAs * dtGetThenAdvanceBufferPointer (const unsigned char *&buffer, const size_t distanceToAdvance)
 
template<typename TypeToRetrieveAs >
TypeToRetrieveAs * dtGetThenAdvanceBufferPointer (unsigned char *&buffer, const size_t distanceToAdvance)
 

Function Documentation

◆ dtAbs()

template<class T >
T dtAbs ( a)
inline

Returns the absolute value.

Parameters
[in]aThe value.
Returns
The absolute value of the specified value.

◆ dtAlign4()

int dtAlign4 ( int  x)
inline

◆ dtCalcPolyCenter()

void dtCalcPolyCenter ( float *  tc,
const unsigned short *  idx,
int  nidx,
const float *  verts 
)

Derives the centroid of a convex polygon.

Parameters
[out]tcThe centroid of the polgyon. [(x, y, z)]
[in]idxThe polygon indices. [(vertIndex) * nidx]
[in]nidxThe number of indices in the polygon. [Limit: >= 3]
[in]vertsThe polygon vertices. [(x, y, z) * vertCount]

◆ dtClamp()

template<class T >
T dtClamp ( v,
mn,
mx 
)
inline

Clamps the value to the specified range.

Parameters
[in]vThe value to clamp.
[in]mnThe minimum permitted return value.
[in]mxThe maximum permitted return value.
Returns
The value, clamped to the specified range.

◆ dtClosestHeightPointTriangle()

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.

Parameters
[in]pThe reference point from which to test. [(x, y, z)]
[in]aVertex A of triangle ABC. [(x, y, z)]
[in]bVertex B of triangle ABC. [(x, y, z)]
[in]cVertex C of triangle ABC. [(x, y, z)]
[out]hThe resulting height.

◆ dtClosestPtPointTriangle()

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.

Parameters
[out]closestThe closest point on the triangle.
[in]pThe reference point from which to test. [(x, y, z)]
[in]aVertex A of triangle ABC. [(x, y, z)]
[in]bVertex B of triangle ABC. [(x, y, z)]
[in]cVertex C of triangle ABC. [(x, y, z)]

◆ dtDistancePtPolyEdgesSqr()

bool dtDistancePtPolyEdgesSqr ( const float *  pt,
const float *  verts,
const int  nverts,
float *  ed,
float *  et 
)

◆ dtDistancePtSegSqr2D()

float dtDistancePtSegSqr2D ( const float *  pt,
const float *  p,
const float *  q,
float &  t 
)

◆ dtGetThenAdvanceBufferPointer() [1/2]

template<typename TypeToRetrieveAs >
TypeToRetrieveAs* dtGetThenAdvanceBufferPointer ( const unsigned char *&  buffer,
const size_t  distanceToAdvance 
)

◆ dtGetThenAdvanceBufferPointer() [2/2]

template<typename TypeToRetrieveAs >
TypeToRetrieveAs* dtGetThenAdvanceBufferPointer ( unsigned char *&  buffer,
const size_t  distanceToAdvance 
)

◆ dtIgnoreUnused()

template<class T >
void dtIgnoreUnused ( const T &  )

Used to ignore a function parameter.

VS complains about unused parameters and this silences the warning.

◆ dtIlog2()

unsigned int dtIlog2 ( unsigned int  v)
inline

◆ dtIntersectSegmentPoly2D()

bool dtIntersectSegmentPoly2D ( const float *  p0,
const float *  p1,
const float *  verts,
int  nverts,
float &  tmin,
float &  tmax,
int &  segMin,
int &  segMax 
)

◆ dtIntersectSegSeg2D()

bool dtIntersectSegSeg2D ( const float *  ap,
const float *  aq,
const float *  bp,
const float *  bq,
float &  s,
float &  t 
)

◆ dtMax()

template<class T >
T dtMax ( a,
b 
)
inline

Returns the maximum of two values.

Parameters
[in]aValue A
[in]bValue B
Returns
The maximum of the two values.

◆ dtMin()

template<class T >
T dtMin ( a,
b 
)
inline

Returns the minimum of two values.

Parameters
[in]aValue A
[in]bValue B
Returns
The minimum of the two values.

◆ dtNextPow2()

unsigned int dtNextPow2 ( unsigned int  v)
inline

◆ dtOppositeTile()

int dtOppositeTile ( int  side)
inline

◆ dtOverlapBounds()

bool dtOverlapBounds ( const float *  amin,
const float *  amax,
const float *  bmin,
const float *  bmax 
)
inline

Determines if two axis-aligned bounding boxes overlap.

Parameters
[in]aminMinimum bounds of box A. [(x, y, z)]
[in]amaxMaximum bounds of box A. [(x, y, z)]
[in]bminMinimum bounds of box B. [(x, y, z)]
[in]bmaxMaximum bounds of box B. [(x, y, z)]
Returns
True if the two AABB's overlap.
See also
dtOverlapQuantBounds

◆ dtOverlapPolyPoly2D()

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.

Parameters
[in]polyaPolygon A vertices. [(x, y, z) * npolya]
[in]npolyaThe number of vertices in polygon A.
[in]polybPolygon B vertices. [(x, y, z) * npolyb]
[in]npolybThe number of vertices in polygon B.
Returns
True if the two polygons overlap.

All vertices are projected onto the xz-plane, so the y-values are ignored.

◆ dtOverlapQuantBounds()

bool dtOverlapQuantBounds ( const unsigned short  amin[3],
const unsigned short  amax[3],
const unsigned short  bmin[3],
const unsigned short  bmax[3] 
)
inline

Determines if two axis-aligned bounding boxes overlap.

Parameters
[in]aminMinimum bounds of box A. [(x, y, z)]
[in]amaxMaximum bounds of box A. [(x, y, z)]
[in]bminMinimum bounds of box B. [(x, y, z)]
[in]bmaxMaximum bounds of box B. [(x, y, z)]
Returns
True if the two AABB's overlap.
See also
dtOverlapBounds

◆ dtPointInPolygon()

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.

Parameters
[in]ptThe point to check. [(x, y, z)]
[in]vertsThe polygon vertices. [(x, y, z) * nverts]
[in]nvertsThe number of vertices. [Limit: >= 3]
Returns
True if the point is inside the polygon.

All points are projected onto the xz-plane, so the y-values are ignored.

◆ dtRandomPointInConvexPoly()

void dtRandomPointInConvexPoly ( const float *  pts,
const int  npts,
float *  areas,
const float  s,
const float  t,
float *  out 
)

◆ dtSqr()

template<class T >
T dtSqr ( a)
inline

Returns the square of the value.

Parameters
[in]aThe value.
Returns
The square of the value.

◆ dtSwap()

template<class T >
void dtSwap ( T &  a,
T &  b 
)
inline

Swaps the values of the two parameters.

Parameters
[in,out]aValue A
[in,out]bValue B

◆ dtSwapByte()

void dtSwapByte ( unsigned char *  a,
unsigned char *  b 
)
inline

◆ dtSwapEndian() [1/5]

void dtSwapEndian ( float *  v)
inline

◆ dtSwapEndian() [2/5]

void dtSwapEndian ( int *  v)
inline

◆ dtSwapEndian() [3/5]

void dtSwapEndian ( short *  v)
inline

◆ dtSwapEndian() [4/5]

void dtSwapEndian ( unsigned int *  v)
inline

◆ dtSwapEndian() [5/5]

void dtSwapEndian ( unsigned short *  v)
inline

◆ dtTriArea2D()

float dtTriArea2D ( const float *  a,
const float *  b,
const float *  c 
)
inline

Derives the signed xz-plane area of the triangle ABC, or the relationship of line AB to point C.

Parameters
[in]aVertex A. [(x, y, z)]
[in]bVertex B. [(x, y, z)]
[in]cVertex C. [(x, y, z)]
Returns
The signed xz-plane area of the triangle.

The vertices are projected onto the xz-plane, so the y-values are ignored.

This is a low cost function than can be used for various purposes. Its main purpose is for point/line relationship testing.

In all cases: A value of zero indicates that all vertices are collinear or represent the same point. (On the xz-plane.)

When used for point/line relationship tests, AB usually represents a line against which the C point is to be tested. In this case:

A positive value indicates that point C is to the left of line AB, looking from A toward B.
A negative value indicates that point C is to the right of lineAB, looking from A toward B.

When used for evaluating a triangle:

The absolute value of the return value is two times the area of the triangle when it is projected onto the xz-plane.

A positive return value indicates:

  • The vertices are wrapped in the normal Detour wrap direction.
  • The triangle's 3D face normal is in the general up direction.

A negative return value indicates:

  • The vertices are reverse wrapped. (Wrapped opposite the normal Detour wrap direction.)
  • The triangle's 3D face normal is in the general down direction.

◆ dtVadd()

void dtVadd ( float *  dest,
const float *  v1,
const float *  v2 
)
inline

Performs a vector addition.

(v1 + v2)

Parameters
[out]destThe result vector. [(x, y, z)]
[in]v1The base vector. [(x, y, z)]
[in]v2The vector to add to v1. [(x, y, z)]

◆ dtVcopy()

void dtVcopy ( float *  dest,
const float *  a 
)
inline

Performs a vector copy.

Parameters
[out]destThe result. [(x, y, z)]
[in]aThe vector to copy. [(x, y, z)]

◆ dtVcross()

void dtVcross ( float *  dest,
const float *  v1,
const float *  v2 
)
inline

Derives the cross product of two vectors.

(v1 x v2)

Parameters
[out]destThe cross product. [(x, y, z)]
[in]v1A Vector [(x, y, z)]
[in]v2A vector [(x, y, z)]

◆ dtVdist()

float dtVdist ( const float *  v1,
const float *  v2 
)
inline

Returns the distance between two points.

Parameters
[in]v1A point. [(x, y, z)]
[in]v2A point. [(x, y, z)]
Returns
The distance between the two points.

◆ dtVdist2D()

float dtVdist2D ( const float *  v1,
const float *  v2 
)
inline

Derives the distance between the specified points on the xz-plane.

Parameters
[in]v1A point. [(x, y, z)]
[in]v2A point. [(x, y, z)]
Returns
The distance between the point on the xz-plane.

The vectors are projected onto the xz-plane, so the y-values are ignored.

◆ dtVdist2DSqr()

float dtVdist2DSqr ( const float *  v1,
const float *  v2 
)
inline

Derives the square of the distance between the specified points on the xz-plane.

Parameters
[in]v1A point. [(x, y, z)]
[in]v2A point. [(x, y, z)]
Returns
The square of the distance between the point on the xz-plane.

◆ dtVdistSqr()

float dtVdistSqr ( const float *  v1,
const float *  v2 
)
inline

Returns the square of the distance between two points.

Parameters
[in]v1A point. [(x, y, z)]
[in]v2A point. [(x, y, z)]
Returns
The square of the distance between the two points.

◆ dtVdot()

float dtVdot ( const float *  v1,
const float *  v2 
)
inline

Derives the dot product of two vectors.

(v1 . v2)

Parameters
[in]v1A Vector [(x, y, z)]
[in]v2A vector [(x, y, z)]
Returns
The dot product.

◆ dtVdot2D()

float dtVdot2D ( const float *  u,
const float *  v 
)
inline

Derives the dot product of two vectors on the xz-plane.

(u . v)

Parameters
[in]uA vector [(x, y, z)]
[in]vA vector [(x, y, z)]
Returns
The dot product on the xz-plane.

The vectors are projected onto the xz-plane, so the y-values are ignored.

◆ dtVequal()

bool dtVequal ( const float *  p0,
const float *  p1 
)
inline

Performs a 'sloppy' colocation check of the specified points.

Parameters
[in]p0A point. [(x, y, z)]
[in]p1A point. [(x, y, z)]
Returns
True if the points are considered to be at the same location.

Basically, this function will return true if the specified points are close enough to eachother to be considered colocated.

◆ dtVisfinite()

bool dtVisfinite ( const float *  v)
inline

Checks that the specified vector's components are all finite.

Parameters
[in]vA point. [(x, y, z)]
Returns
True if all of the point's components are finite, i.e. not NaN or any of the infinities.

◆ dtVisfinite2D()

bool dtVisfinite2D ( const float *  v)
inline

Checks that the specified vector's 2D components are finite.

Parameters
[in]vA point. [(x, y, z)]

◆ dtVlen()

float dtVlen ( const float *  v)
inline

Derives the scalar length of the vector.

Parameters
[in]vThe vector. [(x, y, z)]
Returns
The scalar length of the vector.

◆ dtVlenSqr()

float dtVlenSqr ( const float *  v)
inline

Derives the square of the scalar length of the vector.

(len * len)

Parameters
[in]vThe vector. [(x, y, z)]
Returns
The square of the scalar length of the vector.

◆ dtVlerp()

void dtVlerp ( float *  dest,
const float *  v1,
const float *  v2,
const float  t 
)
inline

Performs a linear interpolation between two vectors.

(v1 toward v2)

Parameters
[out]destThe result vector. [(x, y, x)]
[in]v1The starting vector.
[in]v2The destination vector.
[in]tThe interpolation factor. [Limits: 0 <= value <= 1.0]

◆ dtVmad()

void dtVmad ( float *  dest,
const float *  v1,
const float *  v2,
const float  s 
)
inline

Performs a scaled vector addition.

(v1 + (v2 * s))

Parameters
[out]destThe result vector. [(x, y, z)]
[in]v1The base vector. [(x, y, z)]
[in]v2The vector to scale and add to v1. [(x, y, z)]
[in]sThe amount to scale v2 by before adding to v1.

◆ dtVmax()

void dtVmax ( float *  mx,
const float *  v 
)
inline

Selects the maximum value of each element from the specified vectors.

Parameters
[in,out]mxA vector. (Will be updated with the result.) [(x, y, z)]
[in]vA vector. [(x, y, z)]

◆ dtVmin()

void dtVmin ( float *  mn,
const float *  v 
)
inline

Selects the minimum value of each element from the specified vectors.

Parameters
[in,out]mnA vector. (Will be updated with the result.) [(x, y, z)]
[in]vA vector. [(x, y, z)]

◆ dtVnormalize()

void dtVnormalize ( float *  v)
inline

Normalizes the vector.

Parameters
[in,out]vThe vector to normalize. [(x, y, z)]

◆ dtVperp2D()

float dtVperp2D ( const float *  u,
const float *  v 
)
inline

Derives the xz-plane 2D perp product of the two vectors.

(uz*vx - ux*vz)

Parameters
[in]uThe LHV vector [(x, y, z)]
[in]vThe RHV vector [(x, y, z)]
Returns
The perp dot product on the xz-plane.

The vectors are projected onto the xz-plane, so the y-values are ignored.

◆ dtVscale()

void dtVscale ( float *  dest,
const float *  v,
const float  t 
)
inline

Scales the vector by the specified value.

(v * t)

Parameters
[out]destThe result vector. [(x, y, z)]
[in]vThe vector to scale. [(x, y, z)]
[in]tThe scaling factor.

◆ dtVset()

void dtVset ( float *  dest,
const float  x,
const float  y,
const float  z 
)
inline

Sets the vector elements to the specified values.

Parameters
[out]destThe result vector. [(x, y, z)]
[in]xThe x-value of the vector.
[in]yThe y-value of the vector.
[in]zThe z-value of the vector.

◆ dtVsub()

void dtVsub ( float *  dest,
const float *  v1,
const float *  v2 
)
inline

Performs a vector subtraction.

(v1 - v2)

Parameters
[out]destThe result vector. [(x, y, z)]
[in]v1The base vector. [(x, y, z)]
[in]v2The vector to subtract from v1. [(x, y, z)]