Skip to content

Commit

Permalink
merge the newest input part code
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhouyuan-Chen committed Oct 1, 2023
1 parent 8b8320b commit b681de1
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 31 deletions.
6 changes: 5 additions & 1 deletion components/wmtk_components/embedding/embedding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ void embedding(const nlohmann::json& j, std::map<std::string, std::filesystem::p

Eigen::MatrixXd vertices_, vertices;
Eigen::Matrix<long, -1, -1> 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
Expand Down
6 changes: 5 additions & 1 deletion components/wmtk_components/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ void input(const nlohmann::json& j, std::map<std::string, std::filesystem::path>
} else if (options.file.extension() == ".obj" || options.file.extension() == ".off") {
Eigen::MatrixXd V;
Eigen::Matrix<long, -1, -1> 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;
Expand All @@ -79,7 +83,7 @@ void input(const nlohmann::json& j, std::map<std::string, std::filesystem::path>
}
// 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());
Expand Down
136 changes: 109 additions & 27 deletions src/wmtk/io/EdgeMeshReader.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,109 @@
#include "EdgeMeshReader.hpp"
#include <spdlog/spdlog.h>
#include <map>

namespace wmtk {
EdgeMeshReader::EdgeMeshReader(const std::string& filename, const file_type type)
: m_filename(filename)
, m_type(type)
{}
void EdgeMeshReader::read(Eigen::Matrix<long, -1, -1>& E, Eigen::MatrixXd& V)
void EdgeMeshReader::read(
Eigen::Matrix<long, -1, -1>& edges,
Eigen::MatrixXd& vertices,
Eigen::MatrixXd& vertices_w,
Eigen::MatrixXd& vertices_texture,
Eigen::MatrixXd& vertices_normal,
Eigen::MatrixXd& vertices_parameter)
{
std::vector<std::pair<long, long>> edges;
std::vector<std::vector<double>> vertices;

std::vector<std::pair<long, long>> vec_edges;
std::vector<std::vector<double>> vec_vertices;
std::vector<double> vec_vertices_w;
std::vector<std::vector<double>> vec_vertices_texture;
std::vector<std::vector<double>> vec_vertices_normal;
std::vector<std::vector<double>> 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<std::pair<long, long>>& edges,
std::vector<std::vector<double>>& vertices)
std::vector<std::vector<double>>& vertices,
std::vector<double>& vertices_w,
std::vector<std::vector<double>>& vertices_texture,
std::vector<std::vector<double>> vertices_normal,
std::vector<std::vector<double>>& 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<std::string> 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<double> 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<long> segment;
std::stringstream line(buffer);
std::string token;
getline(line, token, ' ');
while (getline(line, token, ' ')) {
segment.push_back(std::atol(token.c_str()));
}
Expand All @@ -76,6 +119,45 @@ void EdgeMeshReader::read_obj(
v0 = v1;
}
}
}; break;
case VT: {
std::vector<double> 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<double> 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<double> 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;
}
}
}
Expand Down
31 changes: 29 additions & 2 deletions src/wmtk/io/EdgeMeshReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<long, -1, -1>& E, Eigen::MatrixXd& V);
void read(
Eigen::Matrix<long, -1, -1>& 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<std::pair<long, long>>& edges,
std::vector<std::vector<double>>& vertices);
std::vector<std::vector<double>>& vertices,
std::vector<double>& vertices_w,
std::vector<std::vector<double>>& vertices_texture,
std::vector<std::vector<double>> vertices_normal,
std::vector<std::vector<double>>& vertices_parameter);
void read_off(
std::vector<std::pair<long, long>>& edges,
std::vector<std::vector<double>>& 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;
Expand Down

0 comments on commit b681de1

Please sign in to comment.