-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Neizvestnyj/obj-convert-support
Obj convert support
- Loading branch information
Showing
37 changed files
with
746 additions
and
99 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
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
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 +1 @@ | ||
__version__ = "0.2.2" | ||
__version__ = "0.3" |
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,103 @@ | ||
#include <string> | ||
|
||
#include "stl_to_obj/vectornd.h" | ||
#include "stl_to_obj/geometry.h" | ||
#include "stl_to_obj/importstl.h" | ||
#include "stl_to_obj/exportobj.h" | ||
|
||
#include "obj_to_stl/convert_file.h" | ||
|
||
#include <iostream> | ||
#include <exception> | ||
#include <sstream> | ||
#include <vector> | ||
#include <cstring> | ||
|
||
#include "convert.h" | ||
#include "mode.h" | ||
#include "paths.h" | ||
|
||
void convert( | ||
string src, | ||
string dst, | ||
bool debug = true, | ||
void(*callback)(int, void*) = NULL, | ||
void* py_callback = NULL, | ||
void* py_progress = NULL) { | ||
/* | ||
`src` - full path to stl file in binary format | ||
`dst` - full path to obj file | ||
`callback` - just function, that call pointer like `py_object` | ||
*/ | ||
|
||
int code = 0; | ||
int progress_part = 1; // how much progress must pass to trigger (how many iterations of the loop should go through) `py_progress(1)` | ||
string tmp_src = ""; | ||
string type = get_extention(src); // stl/obj | ||
|
||
if (debug == false) { | ||
streambuf* old = cout.rdbuf(); | ||
cout.rdbuf(0); | ||
} | ||
|
||
if (type == "stl") { | ||
cout << "Convert stl to obj" << endl; | ||
string mode = get_stl_mode(src); | ||
|
||
if (mode == "ASCII") { | ||
cout << "Convert ASCII to BIN" << endl; | ||
progress_part = 2; | ||
|
||
tmp_src = split_filename(src, "dir") + "tmp.stl"; | ||
int res = stl_mode_converter(src, tmp_src, "ASCII", progress_part, callback, py_callback, py_progress, true); | ||
|
||
if (res != 0) { | ||
code = 1; | ||
} | ||
else { | ||
src = tmp_src; | ||
} | ||
} | ||
|
||
if (code == 0) { | ||
// create a geometry tesselation object | ||
Geometry tessel; | ||
|
||
// fill up the tesselation object with STL data (load STL) | ||
tessel.visit(ImportSTL(src, callback, py_progress, progress_part)); | ||
|
||
// write down the tesselation object into OBJ file (save OBJ) | ||
tessel.visit(ExportOBJ(dst)); | ||
|
||
if (tmp_src != "") { | ||
remove(tmp_src.c_str()); | ||
cout << tmp_src << " removed" << endl; | ||
} | ||
} | ||
} | ||
else { | ||
cout << "Convert obj to stl" << endl; | ||
|
||
Coord3TR tr; | ||
ObjModel model; | ||
StlModel stlModel; | ||
|
||
model = ObjModel(callback, py_progress); | ||
|
||
try { | ||
read_stl(model, src); | ||
convert_stl2obj(model, stlModel, tr); | ||
write_obj(stlModel, dst); | ||
} | ||
catch (const exception& e) | ||
{ | ||
cerr << e.what() << "\n"; | ||
code = -1; | ||
} | ||
} | ||
|
||
if (callback && py_callback) { | ||
// for python lib `callback` written in c, `py_object` - pointer to python function | ||
callback(code, py_callback); | ||
} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,78 @@ | ||
#include "Converter.h" | ||
|
||
#include <stdexcept> | ||
#include <algorithm> | ||
#include <cfloat> | ||
|
||
using namespace std; | ||
|
||
void Converter::Convert(const ObjModel& objModel, StlModel& stlModel) | ||
{ | ||
// collect coordinates for polygon faces | ||
for (const auto& f : objModel.GetFaces()) | ||
{ | ||
CreatePolygonsWithNorms(f, objModel.GetVertices(), objModel.GetNorms()); | ||
} | ||
|
||
// convert polygons to triangles | ||
for (const auto& poly : polygons_) | ||
{ | ||
Coord3 translate = trMatix_.ApplyTransformation(objModel.GetCoordMin()); | ||
CreateTrianglesWithNormsAndTransform(poly, translate); | ||
} | ||
|
||
// sort triangles by z axis (not strictly enforced) | ||
sort(triangles_.begin(), triangles_.end(), | ||
[](const array<Coord3, 3>& a, const array<Coord3, 3>& b) -> bool | ||
{ | ||
return min({ a[0].z, a[1].z, a[2].z }) > min({ b[0].z, b[1].z, b[2].z }); | ||
}); | ||
|
||
// move triangles to stl | ||
for (const auto& t : triangles_) | ||
{ | ||
// generate normal which should be perpendicular to the triangle plane | ||
Coord3 nv = Coord3::CrossProduct(t[1] - t[0], t[2] - t[0]).Normalize(); | ||
|
||
stlModel.AddTriangle({ t[0], t[1], t[2], nv }); | ||
} | ||
} | ||
|
||
void Converter::CreatePolygonsWithNorms(const vector<FaceVertex>& faceVertices, const vector<Coord3>& objVertices, const vector<Coord3>& objNorms) | ||
{ | ||
// collect face vertices with norms | ||
vector<Coord3N> face; | ||
for (auto const& v : faceVertices) | ||
{ | ||
const Coord3& vt = v.vIndex() ? objVertices[static_cast<size_t>(v.vIndex() - 1)] : Coord3(); | ||
const Coord3& vn = v.vnIndex() ? objNorms[static_cast<size_t>(v.vnIndex() - 1)] : Coord3(); | ||
|
||
face.push_back({ vt, vn }); | ||
} | ||
polygons_.push_back(face); | ||
} | ||
|
||
void Converter::CreateTrianglesWithNormsAndTransform(const vector<Coord3N>& poly, const Coord3& translate) | ||
{ | ||
if (poly.size() < 3) | ||
{ | ||
throw runtime_error("Polygon has less than 3 vertices"); | ||
}; | ||
|
||
// traverse polygon and make triangles | ||
for (vector<Coord3N>::const_iterator it = poly.begin(); it+2 != poly.end(); ++it) | ||
{ | ||
const Coord3N& p0 = *poly.begin(); | ||
const Coord3N& p1 = *(it+1); | ||
const Coord3N& p2 = *(it+2); | ||
|
||
array<Coord3, 3> triangle | ||
{ | ||
trMatix_.ApplyTransformation(p0.vt - translate), | ||
trMatix_.ApplyTransformation(p1.vt - translate), | ||
trMatix_.ApplyTransformation(p2.vt - translate) | ||
}; | ||
|
||
triangles_.push_back(triangle); | ||
} | ||
} |
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,28 @@ | ||
#pragma once | ||
|
||
#include "ObjModel.h" | ||
#include "StlModel.h" | ||
|
||
#include <vector> | ||
#include <array> | ||
|
||
using namespace std; | ||
|
||
class Converter | ||
{ | ||
public: | ||
Converter(Coord3TR trMatix) : trMatix_(trMatix) {} | ||
void Convert(const ObjModel& objModel, StlModel& stlModel); | ||
|
||
private: | ||
vector<vector<Coord3N>> polygons_; // list of faces -> list of coordinate pairs (vertex, norm) | ||
vector<array<Coord3,3>> triangles_; // list of faces -> 3 * coordinates | ||
|
||
Coord3TR trMatix_; | ||
|
||
void CreatePolygonsWithNorms(const vector<FaceVertex>& faceVertices, const vector<Coord3>& objVertices, const vector<Coord3>& objNorms); | ||
void CreateTrianglesWithNormsAndTransform(const vector<Coord3N>& poly, const Coord3& translate); | ||
}; | ||
|
||
|
||
|
Oops, something went wrong.