From 6c55d262edb28cbaf37988107e3ba51a2faa8f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Sun, 28 Jan 2018 23:03:57 +0100 Subject: [PATCH] Add parser for obj mesh format --- Makefile | 3 ++- util/ObjParser.cpp | 32 ++++++++++++++++++++++++++++++++ util/ObjParser.hpp | 16 ++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 util/ObjParser.cpp create mode 100644 util/ObjParser.hpp diff --git a/Makefile b/Makefile index 2074d0d..7b08b8f 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,8 @@ CXXLIBS= TARGETS= OBJS=Implicit.o \ - Mesh.o + Mesh.o \ + util/ObjParser.o all: $(TARGETS:=.bin) diff --git a/util/ObjParser.cpp b/util/ObjParser.cpp new file mode 100644 index 0000000..c62bfc1 --- /dev/null +++ b/util/ObjParser.cpp @@ -0,0 +1,32 @@ +#include "ObjParser.hpp" + +ObjParser::ObjParser(const std::string& path) + : path(path) +{} + +Mesh ObjParser::parse() { + std::ifstream handle(path); + Mesh output; + + if(!handle.is_open()) + throw std::runtime_error(std::string("Cannot open the file ") + path); + + while(!handle.eof()) { + char type; + handle >> type; + if(type == 'v') { // vertice + double x, y, z; + handle >> x >> y >> z; + output.add_vertice(Point(x, y, z)); + } + else { + int v1, v2, v3; + handle >> v1 >> v2 >> v3; + output.add_face(Face(v1, v2, v3)); + } + + handle.ignore('\n'); + } + + return output; +} diff --git a/util/ObjParser.hpp b/util/ObjParser.hpp new file mode 100644 index 0000000..5fa697b --- /dev/null +++ b/util/ObjParser.hpp @@ -0,0 +1,16 @@ +/** Parses .obj mesh files, outputting a `Mesh` */ + +#include +#include +#include + +#include "../Mesh.hpp" + +class ObjParser { + public: + ObjParser(const std::string& path); + Mesh parse(); + + private: + std::string path; +};