diff --git a/RecastDemo/Source/InputGeom.cpp b/RecastDemo/Source/InputGeom.cpp index 5f8ccb7c..55f901c9 100644 --- a/RecastDemo/Source/InputGeom.cpp +++ b/RecastDemo/Source/InputGeom.cpp @@ -16,23 +16,22 @@ // 3. This notice may not be removed or altered from any source distribution. // -#include -#include -#include -#include -#include -#include "Recast.h" #include "InputGeom.h" + #include "ChunkyTriMesh.h" -#include "MeshLoaderObj.h" #include "DebugDraw.h" -#include "RecastDebugDraw.h" -#include "DetourNavMesh.h" +#include "MeshLoaderObj.h" +#include "Recast.h" #include "Sample.h" -static bool intersectSegmentTriangle(const float* sp, const float* sq, - const float* a, const float* b, const float* c, - float &t) +#include +#include +#include +#include + +#include + +static bool intersectSegmentTriangle(const float* sp, const float* sq, const float* a, const float* b, const float* c, float& t) { float v; float w; @@ -54,22 +53,37 @@ static bool intersectSegmentTriangle(const float* sp, const float* sq, // Compute denominator d. If d <= 0, segment is parallel to or points // away from triangle, so exit early float d = rcVdot(qp, norm); - if (d <= 0.0f) { return false; } + if (d <= 0.0f) + { + return false; + } // Compute intersection t value of pq with plane of triangle. A ray // intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay // dividing by d until intersection has been found to pierce triangle rcVsub(ap, sp, a); t = rcVdot(ap, norm); - if (t < 0.0f) { return false; } - if (t > d) { return false; } // For segment; exclude this code line for a ray test + if (t < 0.0f) + { + return false; + } + if (t > d) + { + return false; + } // For segment; exclude this code line for a ray test // Compute barycentric coordinate components and test if within bounds rcVcross(e, qp, ap); v = rcVdot(ac, e); - if (v < 0.0f || v > d) { return false; } + if (v < 0.0f || v > d) + { + return false; + } w = -rcVdot(ab, e); - if (w < 0.0f || v + w > d) { return false; } + if (w < 0.0f || v + w > d) + { + return false; + } // Segment/ray intersects triangle. Perform delayed division t /= d; @@ -89,24 +103,30 @@ static char* parseRow(char* buf, char* bufEnd, char* row, int len) // multirow switch (c) { - case '\n': - if (start) { break; } + case '\n': + if (start) + { + break; + } + done = true; + break; + case '\r': + break; + case '\t': + case ' ': + if (start) + { + break; + } + // else falls through + default: + start = false; + row[n++] = c; + if (n >= len - 1) + { done = true; - break; - case '\r': - break; - case '\t': - case ' ': - if (start) { break; } - // else falls through - default: - start = false; - row[n++] = c; - if (n >= len-1) - { - done = true; - } - break; + } + break; } } row[n] = '\0'; @@ -117,8 +137,10 @@ bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath) { if (m_mesh) { - delete m_chunkyMesh; m_chunkyMesh = nullptr; - delete m_mesh; m_mesh = nullptr; + delete m_chunkyMesh; + m_chunkyMesh = nullptr; + delete m_mesh; + m_mesh = nullptr; } m_offMeshConCount = 0; m_volumeCount = 0; @@ -155,7 +177,10 @@ bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath) bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) { FILE* fp = fopen(filepath.c_str(), "rb"); - if (!fp) { return false; } + if (!fp) + { + return false; + } if (fseek(fp, 0, SEEK_END) != 0) { @@ -191,7 +216,8 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) m_offMeshConCount = 0; m_volumeCount = 0; - delete m_mesh; m_mesh = nullptr; + delete m_mesh; + m_mesh = nullptr; char* src = buf; char* srcEnd = buf + bufSize; @@ -206,12 +232,14 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) // File name. const char* name = row + 1; // Skip white spaces - for (;*name && isspace(*name); ++name) {} + for (; *name && isspace(*name); ++name) + { + } if (*name) { if (!loadMesh(ctx, name)) { - delete [] buf; + delete[] buf; return false; } } @@ -226,8 +254,19 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) int area; int flags; float rad; - sscanf(row + 1, "%f %f %f %f %f %f %f %d %d %d", - &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &rad, &bidir, &area, &flags); + sscanf( + row + 1, + "%f %f %f %f %f %f %f %d %d %d", + &v[0], + &v[1], + &v[2], + &v[3], + &v[4], + &v[5], + &rad, + &bidir, + &area, + &flags); m_offMeshConRads[m_offMeshConCount] = rad; m_offMeshConDirs[m_offMeshConCount] = (unsigned char)bidir; m_offMeshConAreas[m_offMeshConCount] = (unsigned char)area; @@ -254,7 +293,8 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) { // Settings m_hasBuildSettings = true; - sscanf(row + 1, + sscanf( + row + 1, "%f %f %f %f %f %f %f %f %f %f %f %f %f %d %f %f %f %f %f %f %f", &m_buildSettings.cellSize, &m_buildSettings.cellHeight, @@ -280,14 +320,17 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) } } - delete [] buf; + delete[] buf; return true; } bool InputGeom::load(rcContext* ctx, const std::string& filepath) { size_t extensionPos = filepath.find_last_of('.'); - if (extensionPos == std::string::npos) { return false; } + if (extensionPos == std::string::npos) + { + return false; + } std::string extension = filepath.substr(extensionPos); std::transform(extension.begin(), extension.end(), extension.begin(), tolower); @@ -306,7 +349,10 @@ bool InputGeom::load(rcContext* ctx, const std::string& filepath) bool InputGeom::saveGeomSet(const BuildSettings* settings) { - if (!m_mesh) { return false; } + if (!m_mesh) + { + return false; + } // Change extension std::string filepath = m_mesh->getFileName(); @@ -319,7 +365,10 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings) filepath += ".gset"; FILE* fp = fopen(filepath.c_str(), "w"); - if (!fp) { return false; } + if (!fp) + { + return false; + } // Store mesh filename. fprintf(fp, "f %s\n", m_mesh->getFileName().c_str()); @@ -327,7 +376,8 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings) // Store settings if any if (settings) { - fprintf(fp, + fprintf( + fp, "s %f %f %f %f %f %f %f %f %f %f %f %f %f %d %f %f %f %f %f %f %f\n", settings->cellSize, settings->cellHeight, @@ -360,9 +410,7 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings) const int bidir = m_offMeshConDirs[i]; const int area = m_offMeshConAreas[i]; const int flags = m_offMeshConFlags[i]; - fprintf(fp, - "c %f %f %f %f %f %f %f %d %d %d\n", - v[0], v[1], v[2], v[3], v[4], v[5], rad, bidir, area, flags); + fprintf(fp, "c %f %f %f %f %f %f %f %d %d %d\n", v[0], v[1], v[2], v[3], v[4], v[5], rad, bidir, area, flags); } // Convex volumes @@ -381,22 +429,11 @@ bool InputGeom::saveGeomSet(const BuildSettings* settings) return true; } -static bool isectSegAABB( - const float* sp, - const float* sq, - const float* amin, - const float* amax, - float& tmin, - float& tmax) +static bool isectSegAABB(const float* sp, const float* sq, const float* amin, const float* amax, float& tmin, float& tmax) { static const float EPS = 1e-6f; - float d[] - { - sq[0] - sp[0], - sq[1] - sp[1], - sq[2] - sp[2] - }; + float d[]{sq[0] - sp[0], sq[1] - sp[1], sq[2] - sp[2]}; tmin = 0.0; tmax = 1.0f; @@ -414,10 +451,24 @@ static bool isectSegAABB( const float ood = 1.0f / d[i]; float t1 = (amin[i] - sp[i]) * ood; float t2 = (amax[i] - sp[i]) * ood; - if (t1 > t2) { float tmp = t1; t1 = t2; t2 = tmp; } - if (t1 > tmin) { tmin = t1; } - if (t2 < tmax) { tmax = t2; } - if (tmin > tmax) { return false; } + if (t1 > t2) + { + float tmp = t1; + t1 = t2; + t2 = tmp; + } + if (t1 > tmin) + { + tmin = t1; + } + if (t2 < tmax) + { + tmax = t2; + } + if (tmin > tmax) + { + return false; + } } } @@ -429,22 +480,20 @@ bool InputGeom::raycastMesh(float* src, float* dst, float& tmin) // Prune hit ray. float btmin; float btmax; - if (!isectSegAABB(src, dst, m_meshBMin, m_meshBMax, btmin, btmax)) { return false; } + if (!isectSegAABB(src, dst, m_meshBMin, m_meshBMax, btmin, btmax)) + { + return false; + } - float p[] - { - p[0] = src[0] + (dst[0] - src[0]) * btmin, - p[1] = src[2] + (dst[2] - src[2]) * btmin - }; - float q[] - { - src[0] + (dst[0] - src[0]) * btmax, - src[2] + (dst[2] - src[2]) * btmax - }; + float p[]{p[0] = src[0] + (dst[0] - src[0]) * btmin, p[1] = src[2] + (dst[2] - src[2]) * btmin}; + float q[]{src[0] + (dst[0] - src[0]) * btmax, src[2] + (dst[2] - src[2]) * btmax}; int cid[512]; const int ncid = m_chunkyMesh->GetChunksOverlappingSegment(p, q, cid, 512); - if (!ncid) { return false; } + if (!ncid) + { + return false; + } tmin = 1.0f; bool hit = false; @@ -459,11 +508,7 @@ bool InputGeom::raycastMesh(float* src, float* dst, float& tmin) for (int j = 0; j < ntris * 3; j += 3) { float t = 1; - if (intersectSegmentTriangle( - src, dst, - &verts[tris[j] * 3], - &verts[tris[j + 1] * 3], - &verts[tris[j + 2] * 3], t)) + if (intersectSegmentTriangle(src, dst, &verts[tris[j] * 3], &verts[tris[j + 1] * 3], &verts[tris[j + 2] * 3], t)) { if (t < tmin) { @@ -485,7 +530,10 @@ void InputGeom::addOffMeshConnection( unsigned char area, unsigned short flags) { - if (m_offMeshConCount >= MAX_OFFMESH_CONNECTIONS) { return; } + if (m_offMeshConCount >= MAX_OFFMESH_CONNECTIONS) + { + return; + } float* v = &m_offMeshConVerts[m_offMeshConCount * 3 * 2]; m_offMeshConRads[m_offMeshConCount] = rad; m_offMeshConDirs[m_offMeshConCount] = bidir; @@ -532,22 +580,19 @@ void InputGeom::drawOffMeshConnections(duDebugDraw* dd, bool hilight) if (hilight) { - duAppendArc(dd, v[0], v[1], v[2], v[3], v[4], v[5], 0.25f, - (m_offMeshConDirs[i]&1) ? 0.6f : 0.0f, 0.6f, conColor); + duAppendArc(dd, v[0], v[1], v[2], v[3], v[4], v[5], 0.25f, (m_offMeshConDirs[i] & 1) ? 0.6f : 0.0f, 0.6f, conColor); } } dd->end(); dd->depthMask(true); } -void InputGeom::addConvexVolume( - const float* verts, - const int nverts, - const float minh, - const float maxh, - unsigned char area) +void InputGeom::addConvexVolume(const float* verts, const int nverts, const float minh, const float maxh, unsigned char area) { - if (m_volumeCount >= MAX_VOLUMES) { return; } + if (m_volumeCount >= MAX_VOLUMES) + { + return; + } ConvexVolume* vol = &m_volumes[m_volumeCount++]; memset(vol, 0, sizeof(ConvexVolume)); memcpy(vol->verts, verts, sizeof(float) * 3 * nverts); @@ -572,7 +617,7 @@ void InputGeom::drawConvexVolumes(struct duDebugDraw* dd, bool /*hilight*/) { const ConvexVolume* vol = &m_volumes[i]; unsigned int col = duTransCol(dd->areaToCol(vol->area), 32); - for (int j = 0, k = vol->nverts-1; j < vol->nverts; k = j++) + for (int j = 0, k = vol->nverts - 1; j < vol->nverts; k = j++) { const float* va = &vol->verts[k * 3]; const float* vb = &vol->verts[j * 3]; @@ -598,7 +643,7 @@ void InputGeom::drawConvexVolumes(struct duDebugDraw* dd, bool /*hilight*/) { const ConvexVolume* vol = &m_volumes[i]; unsigned int col = duTransCol(dd->areaToCol(vol->area), 220); - for (int j = 0, k = vol->nverts-1; j < vol->nverts; k = j++) + for (int j = 0, k = vol->nverts - 1; j < vol->nverts; k = j++) { const float* va = &vol->verts[k * 3]; const float* vb = &vol->verts[j * 3];