Skip to content

Commit

Permalink
Merge pull request recastnavigation#160 from Janiels/settings-presets
Browse files Browse the repository at this point in the history
Add settings storage to geometry sets
  • Loading branch information
grahamboree committed Jan 18, 2016
2 parents f8d6d39 + a31da92 commit 451c87b
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 216 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ RecastDemo/Bin/Tests
# Build directory
RecastDemo/Build

# Ignore some meshes based on name
RecastDemo/Bin/Meshes/_*
# Ignore meshes
RecastDemo/Bin/Meshes/*

## Logs and databases #
*.log
Expand Down
3 changes: 2 additions & 1 deletion RecastDemo/Include/Filelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <vector>
#include <string>

void scanDirectory(std::string path, std::string ext, std::vector<std::string>& fileList);
void scanDirectoryAppend(const std::string& path, const std::string& ext, std::vector<std::string>& fileList);
void scanDirectory(const std::string& path, const std::string& ext, std::vector<std::string>& fileList);

#endif // FILELIST_H
58 changes: 51 additions & 7 deletions RecastDemo/Include/InputGeom.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,51 @@ struct ConvexVolume
int area;
};

struct BuildSettings
{
// Cell size in world units
float cellSize;
// Cell height in world units
float cellHeight;
// Agent height in world units
float agentHeight;
// Agent radius in world units
float agentRadius;
// Agent max climb in world units
float agentMaxClimb;
// Agent max slope in degrees
float agentMaxSlope;
// Region minimum size in voxels.
// regionMinSize = sqrt(regionMinArea)
float regionMinSize;
// Region merge size in voxels.
// regionMergeSize = sqrt(regionMergeArea)
float regionMergeSize;
// Edge max length in world units
float edgeMaxLen;
// Edge max error in voxels
float edgeMaxError;
float vertsPerPoly;
// Detail sample distance in voxels
float detailSampleDist;
// Detail sample max error in voxel heights.
float detailSampleMaxError;
// Partition type, see SamplePartitionType
int partitionType;
// Bounds of the area to mesh
float navMeshBMin[3];
float navMeshBMax[3];
// Size of the tiles in voxels
float tileSize;
};

class InputGeom
{
rcChunkyTriMesh* m_chunkyMesh;
rcMeshLoaderObj* m_mesh;
float m_meshBMin[3], m_meshBMax[3];
BuildSettings m_buildSettings;
bool m_hasBuildSettings;

/// @name Off-Mesh connections.
///@{
Expand All @@ -56,20 +96,24 @@ class InputGeom
int m_volumeCount;
///@}

bool loadMesh(class rcContext* ctx, const std::string& filepath);
bool loadGeomSet(class rcContext* ctx, const std::string& filepath);
public:
InputGeom();
~InputGeom();

bool loadMesh(class rcContext* ctx, const char* filepath);

bool load(class rcContext* ctx, const char* filepath);
bool save(const char* filepath);
bool load(class rcContext* ctx, const std::string& filepath);
bool saveGeomSet(const BuildSettings* settings);

/// Method to return static mesh data.
inline const rcMeshLoaderObj* getMesh() const { return m_mesh; }
inline const float* getMeshBoundsMin() const { return m_meshBMin; }
inline const float* getMeshBoundsMax() const { return m_meshBMax; }
inline const rcChunkyTriMesh* getChunkyMesh() const { return m_chunkyMesh; }
const rcMeshLoaderObj* getMesh() const { return m_mesh; }
const float* getMeshBoundsMin() const { return m_meshBMin; }
const float* getMeshBoundsMax() const { return m_meshBMax; }
const float* getNavMeshBoundsMin() const { return m_hasBuildSettings ? m_buildSettings.navMeshBMin : m_meshBMin; }
const float* getNavMeshBoundsMax() const { return m_hasBuildSettings ? m_buildSettings.navMeshBMax : m_meshBMax; }
const rcChunkyTriMesh* getChunkyMesh() const { return m_chunkyMesh; }
const BuildSettings* getBuildSettings() const { return m_hasBuildSettings ? &m_buildSettings : 0; }
bool raycastMesh(float* src, float* dst, float& tmin);

/// @name Off-Mesh connections.
Expand Down
18 changes: 10 additions & 8 deletions RecastDemo/Include/MeshLoaderObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@
#ifndef MESHLOADER_OBJ
#define MESHLOADER_OBJ

#include <string>

class rcMeshLoaderObj
{
public:
rcMeshLoaderObj();
~rcMeshLoaderObj();

bool load(const char* fileName);
bool load(const std::string& fileName);

inline const float* getVerts() const { return m_verts; }
inline const float* getNormals() const { return m_normals; }
inline const int* getTris() const { return m_tris; }
inline int getVertCount() const { return m_vertCount; }
inline int getTriCount() const { return m_triCount; }
inline const char* getFileName() const { return m_filename; }
const float* getVerts() const { return m_verts; }
const float* getNormals() const { return m_normals; }
const int* getTris() const { return m_tris; }
int getVertCount() const { return m_vertCount; }
int getTriCount() const { return m_triCount; }
const std::string& getFileName() const { return m_filename; }

private:

void addVertex(float x, float y, float z, int& cap);
void addTriangle(int a, int b, int c, int& cap);

char m_filename[260];
std::string m_filename;
float m_scale;
float* m_verts;
int* m_tris;
Expand Down
7 changes: 3 additions & 4 deletions RecastDemo/Include/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class Sample
virtual void handleMeshChanged(class InputGeom* geom);
virtual bool handleBuild();
virtual void handleUpdate(const float dt);
virtual void collectSettings(struct BuildSettings& settings);

virtual class InputGeom* getInputGeom() { return m_geom; }
virtual class dtNavMesh* getNavMesh() { return m_navMesh; }
Expand All @@ -149,11 +150,9 @@ class Sample
virtual float getAgentRadius() { return m_agentRadius; }
virtual float getAgentHeight() { return m_agentHeight; }
virtual float getAgentClimb() { return m_agentMaxClimb; }
virtual const float* getBoundsMin();
virtual const float* getBoundsMax();

inline unsigned char getNavMeshDrawFlags() const { return m_navMeshDrawFlags; }
inline void setNavMeshDrawFlags(unsigned char flags) { m_navMeshDrawFlags = flags; }
unsigned char getNavMeshDrawFlags() const { return m_navMeshDrawFlags; }
void setNavMeshDrawFlags(unsigned char flags) { m_navMeshDrawFlags = flags; }

void updateToolStates(const float dt);
void initToolStates(Sample* sample);
Expand Down
5 changes: 3 additions & 2 deletions RecastDemo/Include/Sample_TileMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class Sample_TileMesh : public Sample
float m_tileSize;

unsigned int m_tileCol;
float m_tileBmin[3];
float m_tileBmax[3];
float m_lastBuiltTileBmin[3];
float m_lastBuiltTileBmax[3];
float m_tileBuildTime;
float m_tileMemUsage;
int m_tileTriCount;
Expand All @@ -93,6 +93,7 @@ class Sample_TileMesh : public Sample
virtual void handleRenderOverlay(double* proj, double* model, int* view);
virtual void handleMeshChanged(class InputGeom* geom);
virtual bool handleBuild();
virtual void collectSettings(struct BuildSettings& settings);

void getTilePos(const float* pos, int& tx, int& ty);

Expand Down
11 changes: 6 additions & 5 deletions RecastDemo/Include/TestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef TESTCASE_H
#define TESTCASE_H

#include <string>
#include "DetourNavMesh.h"

class TestCase
Expand Down Expand Up @@ -60,8 +61,8 @@ class TestCase
Test* next;
};

char m_sampleName[256];
char m_geomFileName[256];
std::string m_sampleName;
std::string m_geomFileName;
Test* m_tests;

void resetTimes();
Expand All @@ -70,10 +71,10 @@ class TestCase
TestCase();
~TestCase();

bool load(const char* filePath);
bool load(const std::string& filePath);

inline const char* getSampleName() const { return m_sampleName; }
inline const char* getGeomFileName() const { return m_geomFileName; }
const std::string& getSampleName() const { return m_sampleName; }
const std::string& getGeomFileName() const { return m_geomFileName; }

void doTests(class dtNavMesh* navmesh, class dtNavMeshQuery* navquery);

Expand Down
10 changes: 7 additions & 3 deletions RecastDemo/Source/Filelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
using std::vector;
using std::string;

void scanDirectory(string path, string ext, vector<string>& filelist)
void scanDirectoryAppend(const string& path, const string& ext, vector<string>& filelist)
{
filelist.clear();

#ifdef WIN32
string pathWithExt = path + "/*" + ext;

Expand Down Expand Up @@ -71,3 +69,9 @@ void scanDirectory(string path, string ext, vector<string>& filelist)
// Sort the list of files alphabetically.
std::sort(filelist.begin(), filelist.end());
}

void scanDirectory(const string& path, const string& ext, vector<string>& filelist)
{
filelist.clear();
scanDirectoryAppend(path, ext, filelist);
}
96 changes: 89 additions & 7 deletions RecastDemo/Source/InputGeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <algorithm>
#include "Recast.h"
#include "InputGeom.h"
#include "ChunkyTriMesh.h"
Expand Down Expand Up @@ -107,6 +108,7 @@ static char* parseRow(char* buf, char* bufEnd, char* row, int len)
InputGeom::InputGeom() :
m_chunkyMesh(0),
m_mesh(0),
m_hasBuildSettings(false),
m_offMeshConCount(0),
m_volumeCount(0)
{
Expand All @@ -118,7 +120,7 @@ InputGeom::~InputGeom()
delete m_mesh;
}

bool InputGeom::loadMesh(rcContext* ctx, const char* filepath)
bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath)
{
if (m_mesh)
{
Expand All @@ -138,7 +140,7 @@ bool InputGeom::loadMesh(rcContext* ctx, const char* filepath)
}
if (!m_mesh->load(filepath))
{
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath);
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath.c_str());
return false;
}

Expand All @@ -159,10 +161,10 @@ bool InputGeom::loadMesh(rcContext* ctx, const char* filepath)
return true;
}

bool InputGeom::load(rcContext* ctx, const char* filePath)
bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath)
{
char* buf = 0;
FILE* fp = fopen(filePath, "rb");
FILE* fp = fopen(filepath.c_str(), "rb");
if (!fp)
return false;
fseek(fp, 0, SEEK_END);
Expand Down Expand Up @@ -243,22 +245,102 @@ bool InputGeom::load(rcContext* ctx, const char* filePath)
}
}
}
else if (row[0] == 's')
{
// Settings
m_hasBuildSettings = true;
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,
&m_buildSettings.agentHeight,
&m_buildSettings.agentRadius,
&m_buildSettings.agentMaxClimb,
&m_buildSettings.agentMaxSlope,
&m_buildSettings.regionMinSize,
&m_buildSettings.regionMergeSize,
&m_buildSettings.edgeMaxLen,
&m_buildSettings.edgeMaxError,
&m_buildSettings.vertsPerPoly,
&m_buildSettings.detailSampleDist,
&m_buildSettings.detailSampleMaxError,
&m_buildSettings.partitionType,
&m_buildSettings.navMeshBMin[0],
&m_buildSettings.navMeshBMin[1],
&m_buildSettings.navMeshBMin[2],
&m_buildSettings.navMeshBMax[0],
&m_buildSettings.navMeshBMax[1],
&m_buildSettings.navMeshBMax[2],
&m_buildSettings.tileSize);
}
}

delete [] buf;

return true;
}

bool InputGeom::save(const char* filepath)
bool InputGeom::load(rcContext* ctx, const std::string& filepath)
{
size_t extensionPos = filepath.find_last_of('.');
if (extensionPos == std::string::npos)
return false;

std::string extension = filepath.substr(extensionPos);
std::transform(extension.begin(), extension.end(), extension.begin(), tolower);

if (extension == ".gset")
return loadGeomSet(ctx, filepath);
if (extension == ".obj")
return loadMesh(ctx, filepath);

return false;
}

bool InputGeom::saveGeomSet(const BuildSettings* settings)
{
if (!m_mesh) return false;

FILE* fp = fopen(filepath, "w");
// Change extension
std::string filepath = m_mesh->getFileName();
size_t extPos = filepath.find_last_of('.');
if (extPos != std::string::npos)
filepath = filepath.substr(0, extPos);

filepath += ".gset";

FILE* fp = fopen(filepath.c_str(), "w");
if (!fp) return false;

// Store mesh filename.
fprintf(fp, "f %s\n", m_mesh->getFileName());
fprintf(fp, "f %s\n", m_mesh->getFileName().c_str());

// Store settings if any
if (settings)
{
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,
settings->agentHeight,
settings->agentRadius,
settings->agentMaxClimb,
settings->agentMaxSlope,
settings->regionMinSize,
settings->regionMergeSize,
settings->edgeMaxLen,
settings->edgeMaxError,
settings->vertsPerPoly,
settings->detailSampleDist,
settings->detailSampleMaxError,
settings->partitionType,
settings->navMeshBMin[0],
settings->navMeshBMin[1],
settings->navMeshBMin[2],
settings->navMeshBMax[0],
settings->navMeshBMax[1],
settings->navMeshBMax[2],
settings->tileSize);
}

// Store off-mesh links.
for (int i = 0; i < m_offMeshConCount; ++i)
Expand Down
Loading

0 comments on commit 451c87b

Please sign in to comment.