-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
3,682 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#pragma once | ||
#include <fstream> | ||
#include <string> | ||
|
||
namespace objwriter | ||
{ | ||
template<class TVertex, class TIndex, class TString, class TStream> | ||
class ObjWriterT | ||
{ | ||
public: | ||
explicit ObjWriterT(TStream& stream) : | ||
m_file(stream) | ||
{} | ||
|
||
void comment(const TString& comment) | ||
{ | ||
m_file << "# " << comment << std::endl; | ||
} | ||
void materialLib(const TString& materialLib) | ||
{ | ||
m_file << "mtllib " << materialLib << std::endl; | ||
} | ||
void objectName(const TString& objName) | ||
{ | ||
m_file << "o " << objName << std::endl; | ||
} | ||
void vertex(TVertex v1, TVertex v2, TVertex v3) | ||
{ | ||
m_file << "v " | ||
<< v1 << " " | ||
<< v2 << " " | ||
<< v3 << std::endl; | ||
} | ||
void vertex(TVertex v1, TVertex v2, TVertex v3, TVertex v4) | ||
{ | ||
m_file << "v " | ||
<< v1 << " " | ||
<< v2 << " " | ||
<< v3 << " " | ||
<< v4 << std::endl; | ||
} | ||
void texcoord(TVertex t1, TVertex t2) | ||
{ | ||
m_file << "vt " | ||
<< t1 << " " | ||
<< t2 << std::endl; | ||
} | ||
void texcoord(TVertex t1, TVertex t2, TVertex t3) | ||
{ | ||
m_file << "vt " | ||
<< t1 << " " | ||
<< t2 << " " | ||
<< t3 << std::endl; | ||
} | ||
void normal(TVertex n1, TVertex n2, TVertex n3) | ||
{ | ||
m_file << "vn " | ||
<< n1 << " " | ||
<< n2 << " " | ||
<< n3 << std::endl; | ||
} | ||
void group(const TString& groupName) | ||
{ | ||
m_file << "g " << groupName << std::endl; | ||
} | ||
void material(const TString& materialName) | ||
{ | ||
m_file << "usemtl " << materialName << std::endl; | ||
} | ||
|
||
/** | ||
* \param level smoothing level between 1 and 32 | ||
*/ | ||
void smoothLevel(int level) | ||
{ | ||
m_file << "s " << level << std::endl; | ||
} | ||
void smoothOff() | ||
{ | ||
m_file << "s off" << std::endl; | ||
} | ||
void face(TIndex i1, TIndex i2, TIndex i3) | ||
{ | ||
m_file << "f " | ||
<< i1 << " " | ||
<< i2 << " " | ||
<< i3 << std::endl; | ||
} | ||
// vertex + normal indices | ||
void face_vn(TIndex i1, TIndex n1, TIndex i2, TIndex n2, TIndex i3, TIndex n3) | ||
{ | ||
m_file << "f " | ||
<< i1 << "//" << n1 << " " | ||
<< i2 << "//" << n2 << " " | ||
<< i3 << "//" << n3 << std::endl; | ||
} | ||
// vertex + texture indices | ||
void face_vt(TIndex i1, TIndex t1, TIndex i2, TIndex t2, TIndex i3, TIndex t3) | ||
{ | ||
m_file << "f " | ||
<< i1 << "/" << t1 << " " | ||
<< i2 << "/" << t2 << " " | ||
<< i3 << "/" << t3 << std::endl; | ||
} | ||
// vertex + texture + normal indices | ||
void face_vtn(TIndex i1, TIndex t1, TIndex n1, TIndex i2, TIndex t2, TIndex n2, TIndex i3, TIndex t3, TIndex n3) | ||
{ | ||
m_file << "f " | ||
<< i1 << "/" << t1 << "/" << n1 << " " | ||
<< i2 << "/" << t2 << "/" << n2 << " " | ||
<< i3 << "/" << t3 << "/" << n3 << std::endl; | ||
} | ||
void parameterVertex(TVertex u, TVertex v) | ||
{ | ||
m_file << "vp " | ||
<< u << " " | ||
<< v << std::endl; | ||
} | ||
void parameterVertex(TVertex u, TVertex v, TVertex w) | ||
{ | ||
m_file << "vp " | ||
<< u << " " | ||
<< v << " " | ||
<< w << std::endl; | ||
} | ||
private: | ||
TStream& m_file; | ||
}; | ||
|
||
using ObjWriter = ObjWriterT<float, int, std::string, std::fstream>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,92 @@ | ||
int main ( ){ | ||
|
||
#include "tiny_obj_loader.h" | ||
#include "ObjWriter.hpp" | ||
#include "force-manifold.h" | ||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
|
||
void ReadOBJ(const std::string &obj_file, | ||
std::vector<float> &vert_pos, | ||
std::vector<int> &tri_ind){ | ||
tinyobj::attrib_t attrib; | ||
std::vector<tinyobj::shape_t> shapes; | ||
std::vector<tinyobj::material_t> materials; | ||
std::string warn; | ||
std::string err; | ||
|
||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, obj_file.c_str()); | ||
|
||
for (double vertice : attrib.vertices){ | ||
vert_pos.emplace_back(vertice); | ||
|
||
} | ||
|
||
for (auto &shape : shapes){ | ||
for (auto &indice : shape.mesh.indices){ | ||
tri_ind.emplace_back(indice.vertex_index); | ||
} | ||
} | ||
} | ||
|
||
|
||
int write_obj(std::string path, force_mani::MeshData mdata) | ||
{ | ||
std::fstream file; | ||
file.open(path, std::ios::out); | ||
objwriter::ObjWriter w(file); | ||
|
||
|
||
for (size_t i = 0; i < mdata.triangles.size(); i+= 3) | ||
{ | ||
w.face( | ||
mdata.triangles[i + 0], | ||
mdata.triangles[i + 1], | ||
mdata.triangles[i + 2]); | ||
} | ||
|
||
for (size_t i = 0; i < mdata.points.size(); i+= 3) | ||
{ | ||
w.vertex( | ||
mdata.points[i + 0], | ||
mdata.points[i + 1], | ||
mdata.points[i + 2]); | ||
} | ||
// make a cube | ||
|
||
|
||
file.close(); | ||
return 1; | ||
} | ||
|
||
int main(int argc, char *argv[]){ | ||
|
||
std::vector<float> vert_pos; | ||
std::vector<int> tri_ind; | ||
|
||
|
||
if (argc < 3) | ||
{ | ||
std::cerr << "provide [input path] [output path]\n"; | ||
return -1; | ||
} | ||
|
||
char * inpath =argv[1]; | ||
char * outpath =argv[2]; | ||
ReadOBJ(inpath, vert_pos, tri_ind); | ||
|
||
force_mani::MeshData input; | ||
input.triangles = std::move(tri_ind); | ||
input.points = std::move(vert_pos); | ||
|
||
force_mani::MeshData output = force_mani::SDFManifold(input); | ||
|
||
if (!write_obj(outpath, output)) | ||
{ | ||
return -1; | ||
} | ||
|
||
|
||
return 0; | ||
} |
Oops, something went wrong.