Skip to content

Commit

Permalink
Add wrappers for the standard math library
Browse files Browse the repository at this point in the history
Simplifies transition to software based math libraries
for projects requiring determinism.
  • Loading branch information
mendsley committed Sep 16, 2013
1 parent 7b065f5 commit b0e4e6a
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 40 deletions.
6 changes: 3 additions & 3 deletions DebugUtils/Source/DebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
//

#define _USE_MATH_DEFINES
#include <math.h>
#include <string.h>
#include "DebugDraw.h"
#include "DetourMath.h"


duDebugDraw::~duDebugDraw()
Expand Down Expand Up @@ -180,8 +180,8 @@ void duAppendCylinderWire(struct duDebugDraw* dd, float minx, float miny, float
for (int i = 0; i < NUM_SEG; ++i)
{
const float a = (float)i/(float)NUM_SEG*DU_PI*2;
dir[i*2] = cosf(a);
dir[i*2+1] = sinf(a);
dir[i*2] = dtMathCosf(a);
dir[i*2+1] = dtMathSinf(a);
}
}

Expand Down
1 change: 0 additions & 1 deletion DebugUtils/Source/DetourDebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include "DebugDraw.h"
#include "DetourDebugDraw.h"
#include "DetourNavMesh.h"
Expand Down
21 changes: 21 additions & 0 deletions Detour/Include/DetourMath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef DETOURMATH_H
#define DETOURMATH_H

/**
@defgroup detour Detour
Members in this module are wrappers around the standard math library
*/

#include <math.h>

#define dtMathFabs(x) fabs(x)
#define dtMathSqrtf(x) sqrtf(x)
#define dtMathFloorf(x) floorf(x)
#define dtMathCeilf(x) ceilf(x)
#define dtMathCosf(x) cosf(x)
#define dtMathSinf(x) sinf(x)
#define dtMathAtan2f(y, x) atan2f(y, x)

#endif
4 changes: 2 additions & 2 deletions Detour/Source/DetourCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include "DetourCommon.h"
#include "DetourMath.h"

//////////////////////////////////////////////////////////////////////////////////////////

float dtSqrt(float x)
{
return sqrtf(x);
return dtMathSqrtf(x);
}

void dtClosestPtPointTriangle(float* closest, const float* p,
Expand Down
2 changes: 1 addition & 1 deletion Detour/Source/DetourNavMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include <float.h>
#include <string.h>
#include <stdio.h>
#include "DetourNavMesh.h"
#include "DetourNode.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"
#include <new>
Expand Down
6 changes: 3 additions & 3 deletions Detour/Source/DetourNavMeshBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "DetourNavMesh.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourNavMeshBuilder.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"
Expand Down Expand Up @@ -202,8 +202,8 @@ static int createBVTree(const unsigned short* verts, const int /*nverts*/,
if (z > it.bmax[2]) it.bmax[2] = z;
}
// Remap y
it.bmin[1] = (unsigned short)floorf((float)it.bmin[1]*ch/cs);
it.bmax[1] = (unsigned short)ceilf((float)it.bmax[1]*ch/cs);
it.bmin[1] = (unsigned short)dtMathFloorf((float)it.bmin[1]*ch/cs);
it.bmax[1] = (unsigned short)dtMathCeilf((float)it.bmax[1]*ch/cs);
}

int curNode = 0;
Expand Down
4 changes: 2 additions & 2 deletions Detour/Source/DetourNavMeshQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include <float.h>
#include <string.h>
#include "DetourNavMeshQuery.h"
#include "DetourNavMesh.h"
#include "DetourNode.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"
#include <new>
Expand Down Expand Up @@ -3342,7 +3342,7 @@ dtStatus dtNavMeshQuery::findDistanceToWall(dtPolyRef startRef, const float* cen
dtVsub(hitNormal, centerPos, hitPos);
dtVnormalize(hitNormal);

*hitDist = sqrtf(radiusSqr);
*hitDist = dtMathSqrtf(radiusSqr);

return status;
}
Expand Down
8 changes: 4 additions & 4 deletions DetourCrowd/Source/DetourCrowd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//

#define _USE_MATH_DEFINES
#include <math.h>
#include <string.h>
#include <float.h>
#include <stdlib.h>
Expand All @@ -27,6 +26,7 @@
#include "DetourNavMeshQuery.h"
#include "DetourObstacleAvoidance.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAssert.h"
#include "DetourAlloc.h"

Expand Down Expand Up @@ -206,7 +206,7 @@ static int getNeighbours(const float* pos, const float height, const float range
// Check for overlap.
float diff[3];
dtVsub(diff, pos, ag->npos);
if (fabsf(diff[1]) >= (height+ag->params.height)/2.0f)
if (dtMathFabs(diff[1]) >= (height+ag->params.height)/2.0f)
continue;
diff[1] = 0;
const float distSqr = dtVlenSqr(diff);
Expand Down Expand Up @@ -1200,7 +1200,7 @@ void dtCrowd::update(const float dt, dtCrowdAgentDebugInfo* debug)
continue;
if (distSqr > dtSqr(separationDist))
continue;
const float dist = sqrtf(distSqr);
const float dist = dtMathSqrtf(distSqr);
const float weight = separationWeight * (1.0f - dtSqr(dist*invSeparationDist));

dtVmad(disp, disp, diff, weight/dist);
Expand Down Expand Up @@ -1318,7 +1318,7 @@ void dtCrowd::update(const float dt, dtCrowdAgentDebugInfo* debug)
float dist = dtVlenSqr(diff);
if (dist > dtSqr(ag->params.radius + nei->params.radius))
continue;
dist = sqrtf(dist);
dist = dtMathSqrtf(dist);
float pen = (ag->params.radius + nei->params.radius) - dist;
if (dist < 0.0001f)
{
Expand Down
6 changes: 3 additions & 3 deletions DetourCrowd/Source/DetourObstacleAvoidance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include "DetourObstacleAvoidance.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"
#include <string.h>
#include <math.h>
#include <float.h>
#include <new>

Expand Down Expand Up @@ -58,7 +58,7 @@ static int isectRaySeg(const float* ap, const float* u,
dtVsub(v,bq,bp);
dtVsub(w,ap,bp);
float d = dtVperp2D(u,v);
if (fabsf(d) < 1e-6f) return 0;
if (dtMathFabs(d) < 1e-6f) return 0;
d = 1.0f/d;
t = dtVperp2D(v,w) * d;
if (t < 0 || t > 1) return 0;
Expand Down Expand Up @@ -482,7 +482,7 @@ int dtObstacleAvoidanceQuery::sampleVelocityAdaptive(const float* pos, const flo
const int nd = dtClamp(ndivs, 1, DT_MAX_PATTERN_DIVS);
const int nr = dtClamp(nrings, 1, DT_MAX_PATTERN_RINGS);
const float da = (1.0f/nd) * DT_PI*2;
const float dang = atan2f(dvel[2], dvel[0]);
const float dang = dtMathAtan2f(dvel[2], dvel[0]);

// Always add sample at zero
pat[npat*2+0] = 0;
Expand Down
18 changes: 9 additions & 9 deletions DetourCrowd/Source/DetourProximityGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
// 3. This notice may not be removed or altered from any source distribution.
//

#include <math.h>
#include <string.h>
#include <new>
#include "DetourProximityGrid.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"

Expand Down Expand Up @@ -103,10 +103,10 @@ void dtProximityGrid::addItem(const unsigned short id,
const float minx, const float miny,
const float maxx, const float maxy)
{
const int iminx = (int)floorf(minx * m_invCellSize);
const int iminy = (int)floorf(miny * m_invCellSize);
const int imaxx = (int)floorf(maxx * m_invCellSize);
const int imaxy = (int)floorf(maxy * m_invCellSize);
const int iminx = (int)dtMathFloorf(minx * m_invCellSize);
const int iminy = (int)dtMathFloorf(miny * m_invCellSize);
const int imaxx = (int)dtMathFloorf(maxx * m_invCellSize);
const int imaxy = (int)dtMathFloorf(maxy * m_invCellSize);

m_bounds[0] = dtMin(m_bounds[0], iminx);
m_bounds[1] = dtMin(m_bounds[1], iminy);
Expand Down Expand Up @@ -137,10 +137,10 @@ int dtProximityGrid::queryItems(const float minx, const float miny,
const float maxx, const float maxy,
unsigned short* ids, const int maxIds) const
{
const int iminx = (int)floorf(minx * m_invCellSize);
const int iminy = (int)floorf(miny * m_invCellSize);
const int imaxx = (int)floorf(maxx * m_invCellSize);
const int imaxy = (int)floorf(maxy * m_invCellSize);
const int iminx = (int)dtMathFloorf(minx * m_invCellSize);
const int iminy = (int)dtMathFloorf(miny * m_invCellSize);
const int imaxx = (int)dtMathFloorf(maxx * m_invCellSize);
const int imaxy = (int)dtMathFloorf(maxy * m_invCellSize);

int n = 0;

Expand Down
10 changes: 5 additions & 5 deletions DetourTileCache/Source/DetourTileCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "DetourNavMeshBuilder.h"
#include "DetourNavMesh.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"
#include <math.h>
#include <string.h>
#include <new>

Expand Down Expand Up @@ -409,10 +409,10 @@ dtStatus dtTileCache::queryTiles(const float* bmin, const float* bmax,

const float tw = m_params.width * m_params.cs;
const float th = m_params.height * m_params.cs;
const int tx0 = (int)floorf((bmin[0]-m_params.orig[0]) / tw);
const int tx1 = (int)floorf((bmax[0]-m_params.orig[0]) / tw);
const int ty0 = (int)floorf((bmin[2]-m_params.orig[2]) / th);
const int ty1 = (int)floorf((bmax[2]-m_params.orig[2]) / th);
const int tx0 = (int)dtMathFloorf((bmin[0]-m_params.orig[0]) / tw);
const int tx1 = (int)dtMathFloorf((bmax[0]-m_params.orig[0]) / tw);
const int ty0 = (int)dtMathFloorf((bmin[2]-m_params.orig[2]) / th);
const int ty1 = (int)dtMathFloorf((bmax[2]-m_params.orig[2]) / th);

for (int ty = ty0; ty <= ty1; ++ty)
{
Expand Down
14 changes: 7 additions & 7 deletions DetourTileCache/Source/DetourTileCacheBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
//

#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourStatus.h"
#include "DetourAssert.h"
#include "DetourTileCacheBuilder.h"
#include <string.h>
#include <math.h>


template<class T> class dtFixedArray
Expand Down Expand Up @@ -1968,12 +1968,12 @@ dtStatus dtMarkCylinderArea(dtTileCacheLayer& layer, const float* orig, const fl
const float px = (pos[0]-orig[0])*ics;
const float pz = (pos[2]-orig[2])*ics;

int minx = (int)floorf((bmin[0]-orig[0])*ics);
int miny = (int)floorf((bmin[1]-orig[1])*ich);
int minz = (int)floorf((bmin[2]-orig[2])*ics);
int maxx = (int)floorf((bmax[0]-orig[0])*ics);
int maxy = (int)floorf((bmax[1]-orig[1])*ich);
int maxz = (int)floorf((bmax[2]-orig[2])*ics);
int minx = (int)dtMathFloorf((bmin[0]-orig[0])*ics);
int miny = (int)dtMathFloorf((bmin[1]-orig[1])*ich);
int minz = (int)dtMathFloorf((bmin[2]-orig[2])*ics);
int maxx = (int)dtMathFloorf((bmax[0]-orig[0])*ics);
int maxy = (int)dtMathFloorf((bmax[1]-orig[1])*ich);
int maxz = (int)dtMathFloorf((bmax[2]-orig[2])*ics);

if (maxx < 0) return DT_SUCCESS;
if (minx >= w) return DT_SUCCESS;
Expand Down
4 changes: 4 additions & 0 deletions RecastDemo/Build/VC9/Recast.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@
RelativePath="..\..\..\Detour\Include\DetourCommon.h"
>
</File>
<File
RelativePath="..\..\..\Detour\Include\DetourMath.h"
>
</File>
<File
RelativePath="..\..\..\Detour\Include\DetourNavMesh.h"
>
Expand Down

0 comments on commit b0e4e6a

Please sign in to comment.