diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..839c2a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ + +TARGET := PhyObj.o +SOURCES := $(wildcard *.cc) +HEADERS := $(wildcard *.h) + +all: $(TARGET) + +$(TARGET): $(SOURCES) $(HEADERS) + $(CXX) -c -o $@ $(CXXFLAGS) $(SOURCES) + +clean: + -rm -f *~ *.o diff --git a/PhyObj.cc b/PhyObj.cc new file mode 100644 index 0000000..7f1b41b --- /dev/null +++ b/PhyObj.cc @@ -0,0 +1,106 @@ + +#include "PhyObj.h" +#include +#include +#include +using namespace std; + +#define WHITESPACE " \t\r\n\f" + +/********** static utility functions **********/ +static string trim(const string & orig) +{ + string result = orig; + size_t pos = result.find_first_not_of(WHITESPACE); + if (pos == string::npos) + { + result = ""; + } + else + { + if (pos > 0) + result = result.substr(pos, result.length() - pos); + pos = result.find_last_not_of(WHITESPACE); + if (pos < result.length() - 1) + result = result.substr(0, pos + 1); + } + return result; +} + +/********** PhyObj::Geom functions **********/ +PhyObj::Geom::Geom() +{ + m_type = NONE; +} + +PhyObj::Geom::Geom(PhyObj::GeomType type, refptr< vector > args) +{ + m_type = type; + m_args = args; +} + + +/********** PhyObj functions **********/ +void PhyObj::load(FileLoader * fileLoader, const FileLoader::Path & path) +{ + FileLoader::Buffer buff = fileLoader->load(path); + if (buff.size <= 0) + return; + string str(buff.data, buff.size); + stringstream istr(str, ios_base::in); + while (!istr.eof()) + { + string line; + getline(istr, line); + line = trim(line); + if (line == "" || line[0] == '#') + continue; + size_t pos = line.find_first_of(WHITESPACE); + if (pos == string::npos) + continue; + string type = line.substr(0, pos); + pos = line.find("\"", pos); + if (pos == string::npos) + continue; + size_t pos2 = line.find("\"", pos + 1); + if (pos2 == string::npos) + continue; + string name = line.substr(pos + 1, pos2 - pos - 1); + pos = pos2 + 1; + refptr< vector > args = new vector(); + for (;;) + { + pos = line.find_first_not_of(WHITESPACE, pos); + if (pos == string::npos) + break; + pos2 = line.find_first_of(WHITESPACE, pos); + string n = line.substr(pos, pos2 - pos); + float f = atof(n.c_str()); + args->push_back(f); + if (pos2 == string::npos) + break; + pos = pos2 + 1; + } + GeomType geom_type = NONE; + if (type == "cube") + { + geom_type = BOX; + } + else if (type == "sphere") + { + geom_type = SPHERE; + } + else if (type == "capsule") + { + geom_type = CAPSULE; + } + else if (type == "plane") + { + geom_type = PLANE; + } + if (geom_type != NONE) + { + m_geoms.push_back(new Geom(geom_type, args)); + } + } +} diff --git a/PhyObj.h b/PhyObj.h new file mode 100644 index 0000000..2fe17ec --- /dev/null +++ b/PhyObj.h @@ -0,0 +1,40 @@ + +#ifndef PHYOBJ_H +#define PHYOBJ_H + +#include "refptr/refptr.h" +#include "FileLoader/FileLoader.h" +#include + +class PhyObj +{ + public: + /* Types */ + enum GeomType { NONE, BOX, SPHERE, PLANE, CAPSULE }; + + class Geom + { + public: + /* Constructors */ + Geom(); + Geom(GeomType type, refptr< std::vector > args); + + /* Methods */ + GeomType getType() { return m_type; } + refptr< std::vector > getArgs() { return m_args; } + + protected: + GeomType m_type; + refptr< std::vector > m_args; + }; + + /* Methods */ + void load(FileLoader * fileLoader, const FileLoader::Path & path); + size_t getNumGeoms() { return m_geoms.size(); } + refptr getGeom(int i) { return m_geoms[i]; } + + protected: + std::vector< refptr > m_geoms; +}; + +#endif