Skip to content

Commit

Permalink
load and write obj
Browse files Browse the repository at this point in the history
  • Loading branch information
yetigit committed Mar 31, 2023
1 parent ce75ca1 commit 7d5f66f
Show file tree
Hide file tree
Showing 5 changed files with 3,682 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ target_link_libraries(${PROJECT_NAME} OpenVDB::openvdb)

if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /openmp)
endif()
endif()

target_compile_definitions(${PROJECT_NAME} PRIVATE TINYOBJLOADER_IMPLEMENTATION)
131 changes: 131 additions & 0 deletions src/ObjWriter.hpp
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>;
}
1 change: 0 additions & 1 deletion src/force-manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <openvdb/tools/VolumeToMesh.h>
#include <openvdb/util/Util.h>
#include <vector>
#include <string>

namespace force_mani{

Expand Down
90 changes: 89 additions & 1 deletion src/main.cpp
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;
}
Loading

0 comments on commit 7d5f66f

Please sign in to comment.