diff --git a/components/wmtk_components/embedding/embedding.cpp b/components/wmtk_components/embedding/embedding.cpp index 9023afd7e1..7fd1dccef0 100644 --- a/components/wmtk_components/embedding/embedding.cpp +++ b/components/wmtk_components/embedding/embedding.cpp @@ -26,11 +26,15 @@ void embedding(const nlohmann::json& j, std::map edges; + Eigen::MatrixXd W; + Eigen::MatrixXd VT; + Eigen::MatrixXd VN; + Eigen::MatrixXd VP; // EdgeMeshReader reader(); EdgeMesh mesh; EdgeMeshReader reader(files[options.input_file], EdgeMeshReader::OBJ); - reader.read(edges, vertices_); + reader.read(edges, vertices_, W, VT, VN, VP); vertices.resize(vertices_.rows(), 2); // assume we have the data diff --git a/components/wmtk_components/input/input.cpp b/components/wmtk_components/input/input.cpp index 510214714c..6f83a1f3e1 100644 --- a/components/wmtk_components/input/input.cpp +++ b/components/wmtk_components/input/input.cpp @@ -69,6 +69,10 @@ void input(const nlohmann::json& j, std::map } else if (options.file.extension() == ".obj" || options.file.extension() == ".off") { Eigen::MatrixXd V; Eigen::Matrix E; + Eigen::MatrixXd W; + Eigen::MatrixXd VT; + Eigen::MatrixXd VN; + Eigen::MatrixXd VP; // read E // assume only have l and v EdgeMeshReader::file_type type; @@ -79,7 +83,7 @@ void input(const nlohmann::json& j, std::map } // EdgeMeshReader reader(, ); EdgeMeshReader reader(options.file.string(), EdgeMeshReader::file_type::OBJ); - reader.read(E, V); + reader.read(E, V, W, VT, VN, VP); mesh.initialize(E); // for debugging // spdlog::info("E:{} V:{}", E.rows(), V.rows()); diff --git a/src/wmtk/io/EdgeMeshReader.cpp b/src/wmtk/io/EdgeMeshReader.cpp index b6ad7275c8..5de84c6ce3 100644 --- a/src/wmtk/io/EdgeMeshReader.cpp +++ b/src/wmtk/io/EdgeMeshReader.cpp @@ -1,66 +1,109 @@ #include "EdgeMeshReader.hpp" #include +#include namespace wmtk { EdgeMeshReader::EdgeMeshReader(const std::string& filename, const file_type type) : m_filename(filename) , m_type(type) {} -void EdgeMeshReader::read(Eigen::Matrix& E, Eigen::MatrixXd& V) +void EdgeMeshReader::read( + Eigen::Matrix& edges, + Eigen::MatrixXd& vertices, + Eigen::MatrixXd& vertices_w, + Eigen::MatrixXd& vertices_texture, + Eigen::MatrixXd& vertices_normal, + Eigen::MatrixXd& vertices_parameter) { - std::vector> edges; - std::vector> vertices; - + std::vector> vec_edges; + std::vector> vec_vertices; + std::vector vec_vertices_w; + std::vector> vec_vertices_texture; + std::vector> vec_vertices_normal; + std::vector> vec_vertices_parameter; switch (m_type) { - case OBJ: read_obj(edges, vertices); break; - case OFF: read_off(edges, vertices); break; + case OBJ: + read_obj( + vec_edges, + vec_vertices, + vec_vertices_w, + vec_vertices_texture, + vec_vertices_normal, + vec_vertices_parameter); + break; + case OFF: read_off(vec_edges, vec_vertices); break; default: throw std::runtime_error("Unknown data type!"); break; } - E.resize(edges.size(), 2); - V.resize(vertices.size(), 3); + edges.resize(vec_edges.size(), 2); + vertices.resize(vec_vertices.size(), 3); + vertices_w.resize(vec_vertices.size(), 1); + vertices_texture.resize(vec_vertices_texture.size(), 3); + vertices_normal.resize(vec_vertices_normal.size(), 3); + vertices_parameter.resize(vec_vertices_parameter.size(), 3); - for (size_t i = 0; i < E.rows(); i++) { - E(i, 0) = edges[i].first; - E(i, 1) = edges[i].second; + for (size_t i = 0; i < edges.rows(); i++) { + edges(i, 0) = vec_edges[i].first; + edges(i, 1) = vec_edges[i].second; + } + for (size_t i = 0; i < vertices.rows(); i++) { + vertices(i, 0) = vec_vertices[i][0]; + vertices(i, 1) = vec_vertices[i][1]; + vertices(i, 2) = vec_vertices[i][2]; + vertices_w(i, 0) = vec_vertices[i][3]; + } + for (size_t i = 0; i < vertices_texture.rows(); i++) { + vertices_texture(i, 0) = vec_vertices_texture[i][0]; + vertices_texture(i, 1) = vec_vertices_texture[i][1]; + vertices_texture(i, 2) = vec_vertices_texture[i][2]; + } + for (size_t i = 0; i < vertices_normal.rows(); i++) { + vertices_normal(i, 0) = vec_vertices_normal[i][0]; + vertices_normal(i, 1) = vec_vertices_normal[i][1]; + vertices_normal(i, 2) = vec_vertices_normal[i][2]; } - for (size_t i = 0; i < V.rows(); i++) { - V(i, 0) = vertices[i][0]; - V(i, 1) = vertices[i][1]; - V(i, 2) = vertices[i][2]; + for (size_t i = 0; i < vertices_parameter.rows(); i++) { + vertices_parameter(i, 0) = vec_vertices_parameter[i][0]; + vertices_parameter(i, 1) = vec_vertices_parameter[i][1]; + vertices_parameter(i, 2) = vec_vertices_parameter[i][2]; } } void EdgeMeshReader::read_obj( std::vector>& edges, - std::vector>& vertices) + std::vector>& vertices, + std::vector& vertices_w, + std::vector>& vertices_texture, + std::vector> vertices_normal, + std::vector>& vertices_parameter) { std::ifstream f(m_filename); if (!f.is_open()) { throw std::runtime_error("can't open file!"); } + std::string buffer; while (!f.eof()) { getline(f, buffer); std::vector tokens; - // here is for debugging - // spdlog::info("lineinfo: {}", buffer[0]); - // read obj - if (buffer[0] == 'v') { + std::stringstream line(buffer); + std::string token; + getline(line, token, ' '); + switch (str_to_type(token)) { + case V: { std::vector positions; - std::stringstream line(buffer); - std::string token; - getline(line, token, ' '); while (getline(line, token, ' ')) { // spdlog::info("lineinfo: {}", token); positions.push_back(std::atof(token.c_str())); } + assert(positions.size() >= 3); + if (positions.size() == 4) { + vertices_w.push_back(positions[3]); + } vertices.push_back(positions); - } else if (buffer[0] == 'l') { + }; break; + case L: { std::vector segment; - std::stringstream line(buffer); - std::string token; - getline(line, token, ' '); while (getline(line, token, ' ')) { segment.push_back(std::atol(token.c_str())); } @@ -76,6 +119,45 @@ void EdgeMeshReader::read_obj( v0 = v1; } } + }; break; + case VT: { + std::vector texture; + while (getline(line, token, ' ')) { + if (token[0] == '[') { + token.erase(std::remove(token.begin(), token.end(), ']'), token.end()); + texture.push_back(std::atof(token.c_str() + 1)); + } else { + texture.push_back(std::atof(token.c_str())); + } + } + assert(texture.size() >= 2); + if (texture.size() == 2) { + texture.push_back(0); + } + vertices_texture.push_back(texture); + }; break; + case VN: { + std::vector normal; + while (getline(line, token, ' ')) { + normal.push_back(std::atof(token.c_str())); + } + assert(normal.size() == 3); + double standard = + sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]); + normal[0] /= standard; + normal[1] /= standard; + normal[2] /= standard; + vertices_normal.push_back(normal); + }; break; + case VP: { + std::vector parameters; + while (getline(line, token, ' ')) { + parameters.push_back(std::atof(token.c_str())); + } + assert(parameters.size() == 3); + vertices_parameter.push_back(parameters); + }; break; + default: continue; } } } diff --git a/src/wmtk/io/EdgeMeshReader.hpp b/src/wmtk/io/EdgeMeshReader.hpp index 78cee457e4..2c03ec57c4 100644 --- a/src/wmtk/io/EdgeMeshReader.hpp +++ b/src/wmtk/io/EdgeMeshReader.hpp @@ -8,15 +8,42 @@ class Mesh; class EdgeMeshReader { public: + enum data_type { V, L, VT, VN, VP, COMMENT }; enum file_type { OBJ, OFF }; EdgeMeshReader(const std::string& filename, const file_type type); - void read(Eigen::Matrix& E, Eigen::MatrixXd& V); + void read( + Eigen::Matrix& edges, + Eigen::MatrixXd& vertices, + Eigen::MatrixXd& vertices_w, + Eigen::MatrixXd& vertices_texture, + Eigen::MatrixXd& vertices_normal, + Eigen::MatrixXd& vertices_parameter); void read_obj( std::vector>& edges, - std::vector>& vertices); + std::vector>& vertices, + std::vector& vertices_w, + std::vector>& vertices_texture, + std::vector> vertices_normal, + std::vector>& vertices_parameter); void read_off( std::vector>& edges, std::vector>& vertices); + data_type str_to_type(std::string str) + { + if (str == "v") { + return V; + } else if (str == "l") { + return L; + } else if (str == "vt") { + return VT; + } else if (str == "vn") { + return VN; + } else if (str == "vp") { + return VP; + } else { + return COMMENT; + } + } std::string m_filename; file_type m_type;