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