Recast: boilerplate code and constants deduplication

This commit is contained in:
Kawe Mazidjatari 2024-09-04 13:39:05 +02:00
parent 33acd65e5a
commit 6050ce5555
12 changed files with 81 additions and 93 deletions

View File

@ -74,20 +74,6 @@ static int convexhull(const float* pts, int npts, int* out)
return i;
}
static int pointInPoly(int nvert, const float* verts, const float* p) // todo(amos) deduplicate.
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++)
{
const float* vi = &verts[i*3];
const float* vj = &verts[j*3];
if (((vi[1] > p[1]) != (vj[1] > p[1])) &&
(p[0] < (vj[0]-vi[0]) * (p[1]-vi[1]) / (vj[1]-vi[1]) + vi[0]) )
c = !c;
}
return c;
}
ConvexVolumeTool::ConvexVolumeTool() :
m_editor(0),
@ -173,7 +159,7 @@ void ConvexVolumeTool::handleClick(const float* /*s*/, const float* p, bool shif
const ConvexVolume* vols = geom->getConvexVolumes();
for (int i = 0; i < geom->getConvexVolumeCount(); ++i)
{
if (pointInPoly(vols[i].nverts, vols[i].verts, p) &&
if (rdPointInPolygon(p, vols[i].verts, vols[i].nverts) &&
p[1] >= vols[i].hmin && p[1] <= vols[i].hmax)
{
nearestIndex = i;

View File

@ -1273,7 +1273,7 @@ unsigned char* Editor_TileMesh::buildTileMesh(const int tx, const int ty, const
// If polygon connects to a polygon on a neighbouring tile, flag it.
for (int j = 0; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX)
if (p[j] == RD_MESH_NULL_IDX)
break;
if ((p[nvp+j] & 0x8000) == 0)
continue;

View File

@ -19,6 +19,7 @@ add_sources( SOURCE_GROUP "Include"
"Shared/Include/SharedAlloc.h"
"Shared/Include/SharedAssert.h"
"Shared/Include/SharedCommon.h"
"Shared/Include/SharedConst.h"
"Shared/Include/SharedDefs.h"
"Shared/Include/SharedMath.h"
)

View File

@ -661,7 +661,7 @@ void duDebugDrawPolyMesh(duDebugDraw* dd, const struct rcPolyMesh& mesh, const f
unsigned short vi[3];
for (int j = 2; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
vi[0] = p[0];
vi[1] = p[j-1];
vi[2] = p[j];
@ -685,9 +685,9 @@ void duDebugDrawPolyMesh(duDebugDraw* dd, const struct rcPolyMesh& mesh, const f
const unsigned short* p = &mesh.polys[i*nvp*2];
for (int j = 0; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
if (p[nvp+j] & 0x8000) continue;
const int nj = (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) ? 0 : j+1;
const int nj = (j+1 >= nvp || p[j+1] == RD_MESH_NULL_IDX) ? 0 : j+1;
const int vi[2] = {p[j], p[nj]};
for (int k = 0; k < 2; ++k)
@ -710,9 +710,9 @@ void duDebugDrawPolyMesh(duDebugDraw* dd, const struct rcPolyMesh& mesh, const f
const unsigned short* p = &mesh.polys[i*nvp*2];
for (int j = 0; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
if ((p[nvp+j] & 0x8000) == 0) continue;
const int nj = (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) ? 0 : j+1;
const int nj = (j+1 >= nvp || p[j+1] == RD_MESH_NULL_IDX) ? 0 : j+1;
const int vi[2] = {p[j], p[nj]};
unsigned int col = colb;

View File

@ -76,7 +76,7 @@ bool duDumpPolyMeshToObj(rcPolyMesh& pmesh, duFileIO* io)
const unsigned short* p = &pmesh.polys[i*nvp*2];
for (int j = 2; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
ioprintf(io, "f %d %d %d\n", p[0]+1, p[j-1]+1, p[j]+1);
}
}

View File

@ -23,8 +23,6 @@
#include "Detour/Include/DetourNavMesh.h"
#include "Detour/Include/DetourNavMeshBuilder.h"
static unsigned short MESH_NULL_IDX = 0xffff;
struct BVItem
{
@ -165,15 +163,6 @@ static void subdivide(BVItem* items, int nitems, int imin, int imax, rdTempVecto
}
}
static const unsigned short DT_MESH_NULL_IDX = 0xffff;
static int countPolyVerts(const unsigned short* p, const int nvp) // todo(amos): deduplicate
{
for (int i = 0; i < nvp; ++i)
if (p[i] == DT_MESH_NULL_IDX)
return i;
return nvp;
}
static bool createBVTree(dtNavMeshCreateParams* params, rdTempVector<BVItem>& nodes)
{
BVItem* items = (BVItem*)rdAlloc(sizeof(BVItem)*params->polyCount, RD_ALLOC_TEMP);
@ -210,7 +199,7 @@ static bool createBVTree(dtNavMeshCreateParams* params, rdTempVector<BVItem>& no
const int nvp = params->nvp;
const unsigned short* p = &params->polys[i*nvp * 2];
vertCount = countPolyVerts(p, nvp);
vertCount = rdCountPolyVerts(p, nvp);
for (int j = 0; j < vertCount; ++j)
{
@ -554,7 +543,7 @@ static bool createPolyMeshCells(const dtNavMeshCreateParams* params, rdTempVecto
for (int i = 0; i < params->polyCount; ++i)
{
const unsigned short* p = &params->polys[i*2*nvp];
const int nv = countPolyVerts(p, nvp);
const int nv = rdCountPolyVerts(p, nvp);
if (nv < 3) // Don't generate cells for off-mesh connections.
continue;
@ -816,7 +805,7 @@ bool dtCreateNavMeshData(dtNavMeshCreateParams* params, unsigned char** outData,
const unsigned short* p = &params->polys[i*2*nvp];
for (int j = 0; j < nvp; ++j)
{
if (p[j] == MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
edgeCount++;
if (p[nvp+j] & 0x8000)
@ -844,7 +833,7 @@ bool dtCreateNavMeshData(dtNavMeshCreateParams* params, unsigned char** outData,
int nv = 0;
for (int j = 0; j < nvp; ++j)
{
if (p[j] == MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
nv++;
}
ndv -= nv;
@ -862,7 +851,7 @@ bool dtCreateNavMeshData(dtNavMeshCreateParams* params, unsigned char** outData,
int nv = 0;
for (int j = 0; j < nvp; ++j)
{
if (p[j] == MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
nv++;
}
detailTriCount += nv-2;
@ -1012,7 +1001,7 @@ bool dtCreateNavMeshData(dtNavMeshCreateParams* params, unsigned char** outData,
p->surfaceArea = params->surfAreas[i];
for (int j = 0; j < nvp; ++j)
{
if (src[j] == MESH_NULL_IDX) break;
if (src[j] == RD_MESH_NULL_IDX) break;
p->verts[j] = src[j];
if (src[nvp+j] & 0x8000)
{

View File

@ -621,11 +621,6 @@ enum rcBuildContoursFlags
/// @see rcContour::verts, rcContour::rverts
static const int RC_CONTOUR_REG_MASK = 0xffff;
/// An value which indicates an invalid index within a mesh.
/// @note This does not necessarily indicate an error.
/// @see rcPolyMesh::polys
static const unsigned short RC_MESH_NULL_IDX = 0xffff;
/// Represents the null area.
/// When a data element is given this value it is considered to no longer be
/// assigned to a usable area. (E.g. It is un-walkable.)

View File

@ -353,21 +353,6 @@ void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax,
}
}
static int pointInPoly(int nvert, const float* verts, const float* p) // todo(amos) deduplicate.
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++)
{
const float* vi = &verts[i*3];
const float* vj = &verts[j*3];
if (((vi[1] > p[1]) != (vj[1] > p[1])) &&
(p[0] < (vj[0]-vi[0]) * (p[1]-vi[1]) / (vj[1]-vi[1]) + vi[0]) )
c = !c;
}
return c;
}
/// @par
///
/// The value of spacial parameters are in world units.
@ -432,7 +417,7 @@ void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
p[1] = chf.bmin[1] + (y+0.5f)*chf.cs;
p[2] = 0;
if (pointInPoly(nverts, verts, p))
if (rdPointInPolygon(p, verts, nverts))
{
chf.flags[i] = flags;
chf.areas[i] = areaId;

View File

@ -47,16 +47,16 @@ static bool buildMeshAdjacency(unsigned short* polys, const int npolys,
}
for (int i = 0; i < nverts; i++)
firstEdge[i] = RC_MESH_NULL_IDX;
firstEdge[i] = RD_MESH_NULL_IDX;
for (int i = 0; i < npolys; ++i)
{
unsigned short* t = &polys[i*vertsPerPoly*2];
for (int j = 0; j < vertsPerPoly; ++j)
{
if (t[j] == RC_MESH_NULL_IDX) break;
if (t[j] == RD_MESH_NULL_IDX) break;
unsigned short v0 = t[j];
unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RC_MESH_NULL_IDX) ? t[0] : t[j+1];
unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RD_MESH_NULL_IDX) ? t[0] : t[j+1];
if (v0 < v1)
{
rcEdge& edge = edges[edgeCount];
@ -79,12 +79,12 @@ static bool buildMeshAdjacency(unsigned short* polys, const int npolys,
unsigned short* t = &polys[i*vertsPerPoly*2];
for (int j = 0; j < vertsPerPoly; ++j)
{
if (t[j] == RC_MESH_NULL_IDX) break;
if (t[j] == RD_MESH_NULL_IDX) break;
unsigned short v0 = t[j];
unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RC_MESH_NULL_IDX) ? t[0] : t[j+1];
unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == RD_MESH_NULL_IDX) ? t[0] : t[j+1];
if (v0 > v1)
{
for (unsigned short e = firstEdge[v1]; e != RC_MESH_NULL_IDX; e = nextEdge[e])
for (unsigned short e = firstEdge[v1]; e != RD_MESH_NULL_IDX; e = nextEdge[e])
{
rcEdge& edge = edges[e];
if (edge.vert[1] == v0 && edge.poly[0] == edge.poly[1])
@ -486,14 +486,6 @@ static int triangulate(int n, const int* verts, int* indices, int* tris)
return ntris;
}
static int countPolyVerts(const unsigned short* p, const int nvp)
{
for (int i = 0; i < nvp; ++i)
if (p[i] == RC_MESH_NULL_IDX)
return i;
return nvp;
}
inline bool uleft(const unsigned short* a, const unsigned short* b, const unsigned short* c)
{
return ((int)b[0] - (int)a[0]) * ((int)c[1] - (int)a[1]) -
@ -509,8 +501,8 @@ static int getPolyMergeValue(unsigned short* pa, unsigned short* pb,
const unsigned short* verts, int& ea, int& eb,
const int nvp)
{
const int na = countPolyVerts(pa, nvp);
const int nb = countPolyVerts(pb, nvp);
const int na = rdCountPolyVerts(pa, nvp);
const int nb = rdCountPolyVerts(pb, nvp);
// If the merged polygon would be too big, do not merge.
if (na+nb-2 > nvp)
@ -580,8 +572,8 @@ static int getPolyMergeValue(unsigned short* pa, unsigned short* pb,
static void mergePolyVerts(unsigned short* pa, unsigned short* pb, int ea, int eb,
unsigned short* tmp, const int nvp)
{
const int na = countPolyVerts(pa, nvp);
const int nb = countPolyVerts(pb, nvp);
const int na = rdCountPolyVerts(pa, nvp);
const int nb = rdCountPolyVerts(pb, nvp);
// Merge polygons.
memset(tmp, 0xff, sizeof(unsigned short)*nvp);
@ -621,7 +613,7 @@ static bool canRemoveVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned sho
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*nvp*2];
const int nv = countPolyVerts(p, nvp);
const int nv = rdCountPolyVerts(p, nvp);
int numRemoved = 0;
int numVerts = 0;
for (int j = 0; j < nv; ++j)
@ -660,7 +652,7 @@ static bool canRemoveVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned sho
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*nvp*2];
const int nv = countPolyVerts(p, nvp);
const int nv = rdCountPolyVerts(p, nvp);
// Collect edges which touches the removed vertex.
for (int j = 0, k = nv-1; j < nv; k = j++)
@ -730,7 +722,7 @@ static bool removeVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned short
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*nvp*2];
const int nv = countPolyVerts(p, nvp);
const int nv = rdCountPolyVerts(p, nvp);
for (int j = 0; j < nv; ++j)
{
if (p[j] == rem)
@ -773,7 +765,7 @@ static bool removeVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned short
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*nvp*2];
const int nv = countPolyVerts(p, nvp);
const int nv = rdCountPolyVerts(p, nvp);
bool hasRem = false;
for (int j = 0; j < nv; ++j)
if (p[j] == rem) hasRem = true;
@ -817,7 +809,7 @@ static bool removeVertex(rcContext* ctx, rcPolyMesh& mesh, const unsigned short
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*nvp*2];
const int nv = countPolyVerts(p, nvp);
const int nv = rdCountPolyVerts(p, nvp);
for (int j = 0; j < nv; ++j)
if (p[j] > rem) p[j]--;
}
@ -1337,12 +1329,12 @@ bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, const int nvp, rcPolyMe
unsigned short* p = &mesh.polys[i*2*nvp];
for (int j = 0; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX) break;
if (p[j] == RD_MESH_NULL_IDX) break;
// Skip connected edges.
if (p[nvp+j] != RC_MESH_NULL_IDX)
if (p[nvp+j] != RD_MESH_NULL_IDX)
continue;
int nj = j+1;
if (nj >= nvp || p[nj] == RC_MESH_NULL_IDX) nj = 0;
if (nj >= nvp || p[nj] == RD_MESH_NULL_IDX) nj = 0;
const unsigned short* va = &mesh.verts[p[j]*3];
const unsigned short* vb = &mesh.verts[p[nj]*3];
@ -1378,7 +1370,7 @@ bool rcBuildPolyMesh(rcContext* ctx, rcContourSet& cset, const int nvp, rcPolyMe
float polyArea = 0.0f;
for (int j = 2; j < nvp; ++j)
{
if (p[j] == RC_MESH_NULL_IDX)
if (p[j] == RD_MESH_NULL_IDX)
break;
vi[0] = p[0];
@ -1535,7 +1527,7 @@ bool rcMergePolyMeshes(rcContext* ctx, rcPolyMesh** meshes, const int nmeshes, r
mesh.npolys++;
for (int k = 0; k < mesh.nvp; ++k)
{
if (src[k] == RC_MESH_NULL_IDX) break;
if (src[k] == RD_MESH_NULL_IDX) break;
tgt[k] = vremap[src[k]];
}

View File

@ -1316,7 +1316,7 @@ bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompa
ymax = 0;
for (int j = 0; j < nvp; ++j)
{
if(p[j] == RC_MESH_NULL_IDX) break;
if(p[j] == RD_MESH_NULL_IDX) break;
const unsigned short* v = &mesh.verts[p[j]*3];
xmin = rdMin(xmin, (int)v[0]);
xmax = rdMax(xmax, (int)v[0]);
@ -1376,7 +1376,7 @@ bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompa
int npoly = 0;
for (int j = 0; j < nvp; ++j)
{
if(p[j] == RC_MESH_NULL_IDX) break;
if(p[j] == RD_MESH_NULL_IDX) break;
const unsigned short* v = &mesh.verts[p[j]*3];
poly[j*3+0] = v[0]*cs;
poly[j*3+1] = v[1]*cs;

View File

@ -19,12 +19,10 @@
#ifndef RECASTDETOURCOMMON_H
#define RECASTDETOURCOMMON_H
#include "Shared/Include/SharedConst.h"
#include "Shared/Include/SharedDefs.h"
#include "Shared/Include/SharedMath.h"
/// The total number of bits in an bit cell integer.
static const int RD_BITS_PER_BIT_CELL = 32;
/**
@defgroup shared Shared
@ -625,6 +623,18 @@ inline void rdSwapEndian(float* v)
void rdRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
const float s, const float t, float* out);
/// Counts the number of vertices in the polygon.
/// @param[in] p The polygon.
/// @param[in] nvp The total number of verts per polygon.
/// @return The number of vertices in the polygon.
inline int rdCountPolyVerts(const unsigned short* p, const int nvp)
{
for (int i = 0; i < nvp; ++i)
if (p[i] == RD_MESH_NULL_IDX)
return i;
return nvp;
}
template<typename TypeToRetrieveAs>
TypeToRetrieveAs* rdGetThenAdvanceBufferPointer(const unsigned char*& buffer, const rdSizeType distanceToAdvance)
{

View File

@ -0,0 +1,30 @@
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#ifndef RECASTDETOURCONST_H
#define RECASTDETOURCONST_H
/// The total number of bits in an bit cell integer.
static const int RD_BITS_PER_BIT_CELL = 32;
/// An value which indicates an invalid index within a mesh.
/// @note This does not necessarily indicate an error.
/// @see rcPolyMesh::polys
static const unsigned short RD_MESH_NULL_IDX = 0xffff;
#endif // RECASTDETOURCONST_H