added support for multiple obj-pool folders

This commit is contained in:
k-a-z-u
2018-04-03 15:03:55 +02:00
parent 705763f03c
commit 5d089f4b86

View File

@@ -13,6 +13,8 @@
//#include <stdio.h> //#include <stdio.h>
#include <experimental/filesystem> #include <experimental/filesystem>
#include "../../../misc/Debug.h"
namespace Ray3D { namespace Ray3D {
@@ -23,6 +25,8 @@ namespace Ray3D {
private: private:
static constexpr const char* name = "OBJ-Pool";
/** singleton */ /** singleton */
OBJPool() {;} OBJPool() {;}
@@ -37,32 +41,23 @@ namespace Ray3D {
return instance; return instance;
} }
/** data folder */ /** init with *.obj data folder */
void init(const std::string& folder) { void init(const std::string& folder) {
scanFolder(folder);
initDone = true; initDone = true;
// LINUX ONLY! }
//DIR* d = opendir(folder.c_str());
//if (!d) {throw Exception("OBJPool: folder not found: " + folder);}
std::experimental::filesystem::path d(folder);
if (!std::experimental::filesystem::exists(d)) {
throw Exception("OBJPool: folder not found: " + folder);
}
//struct dirent *dir; /** init with multiple *.obj data folder */
//while ((dir = readdir(d)) != NULL) { void init(const std::initializer_list<std::string>& folders) {
for (std::experimental::filesystem::directory_entry entry : std::experimental::filesystem::directory_iterator(d)) {
//const std::string absFile = folder + "/" + dir->d_name; for (const std::string& folder : folders) {
const std::string absFile = entry.path().native(); try {
if (endsWith(absFile, ".obj")) { scanFolder(folder);
//std::string name = std::string(dir->d_name); } catch (...) {;}
std::string name = entry.path().filename();
name = name.substr(0, name.length() - 4); // without extension
load(absFile, name);
}
} }
//closedir(d); initDone = true;
} }
@@ -85,6 +80,25 @@ namespace Ray3D {
private: private:
/** scan the given folder for all *.obj files */
void scanFolder(const std::string& folder) {
std::experimental::filesystem::path d(folder);
if (!std::experimental::filesystem::exists(d)) {
throw Exception("OBJPool: folder not found: " + folder);
}
for (std::experimental::filesystem::directory_entry entry : std::experimental::filesystem::directory_iterator(d)) {
const std::string absFile = entry.path().native();
if (endsWith(absFile, ".obj")) {
std::string name = entry.path().filename();
name = name.substr(0, name.length() - 4); // without extension
load(absFile, name);
}
}
}
inline bool endsWith(std::string const & value, std::string const & ending) { inline bool endsWith(std::string const & value, std::string const & ending) {
if (ending.size() > value.size()) return false; if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
@@ -104,6 +118,8 @@ namespace Ray3D {
obs.triangles.push_back(tria); obs.triangles.push_back(tria);
} }
Log::add(this->name, "loaded: " + absName + " [" + std::to_string(obs.triangles.size()) + " triangles]", true);
// store // store
cache[name] = obs; cache[name] = obs;