Make loading of obj files into TriangleMesh possible.

This commit is contained in:
tamasmeszaros 2019-09-24 15:07:22 +02:00
parent e4247f9856
commit 9d775d0a43
2 changed files with 45 additions and 29 deletions

View file

@ -15,39 +15,41 @@
namespace Slic3r { namespace Slic3r {
bool load_obj(const char *path, Model *model, const char *object_name_in) bool load_obj(const char *path, TriangleMesh *meshptr)
{ {
if(meshptr == nullptr) return false;
// Parse the OBJ file. // Parse the OBJ file.
ObjParser::ObjData data; ObjParser::ObjData data;
if (! ObjParser::objparse(path, data)) { if (! ObjParser::objparse(path, data)) {
// die "Failed to parse $file\n" if !-e $path; // die "Failed to parse $file\n" if !-e $path;
return false; return false;
} }
// Count the faces and verify, that all faces are triangular. // Count the faces and verify, that all faces are triangular.
size_t num_faces = 0; size_t num_faces = 0;
size_t num_quads = 0; size_t num_quads = 0;
for (size_t i = 0; i < data.vertices.size(); ) { for (size_t i = 0; i < data.vertices.size(); ) {
size_t j = i; size_t j = i;
for (; j < data.vertices.size() && data.vertices[j].coordIdx != -1; ++ j) ; for (; j < data.vertices.size() && data.vertices[j].coordIdx != -1; ++ j) ;
if (i == j) if (i == j)
continue; continue;
size_t face_vertices = j - i; size_t face_vertices = j - i;
if (face_vertices != 3 && face_vertices != 4) { if (face_vertices != 3 && face_vertices != 4) {
// Non-triangular and non-quad faces are not supported as of now. // Non-triangular and non-quad faces are not supported as of now.
return false; return false;
} }
if (face_vertices == 4) if (face_vertices == 4)
++ num_quads; ++ num_quads;
++ num_faces; ++ num_faces;
i = j + 1; i = j + 1;
} }
// Convert ObjData into STL. // Convert ObjData into STL.
TriangleMesh mesh; TriangleMesh &mesh = *meshptr;
stl_file &stl = mesh.stl; stl_file &stl = mesh.stl;
stl.stats.type = inmemory; stl.stats.type = inmemory;
stl.stats.number_of_facets = int(num_faces + num_quads); stl.stats.number_of_facets = uint32_t(num_faces + num_quads);
stl.stats.original_num_facets = int(num_faces + num_quads); stl.stats.original_num_facets = int(num_faces + num_quads);
// stl_allocate clears all the allocated data to zero, all normals are set to zeros as well. // stl_allocate clears all the allocated data to zero, all normals are set to zeros as well.
stl_allocate(&stl); stl_allocate(&stl);
@ -68,14 +70,14 @@ bool load_obj(const char *path, Model *model, const char *object_name_in)
++ num_normals; ++ num_normals;
} }
} }
if (data.vertices[i].coordIdx != -1) { if (data.vertices[i].coordIdx != -1) {
// This is a quad. Produce the other triangle. // This is a quad. Produce the other triangle.
stl_facet &facet2 = stl.facet_start[i_face++]; stl_facet &facet2 = stl.facet_start[i_face++];
facet2.vertex[0] = facet.vertex[0]; facet2.vertex[0] = facet.vertex[0];
facet2.vertex[1] = facet.vertex[2]; facet2.vertex[1] = facet.vertex[2];
const ObjParser::ObjVertex &vertex = data.vertices[i++]; const ObjParser::ObjVertex &vertex = data.vertices[i++];
memcpy(facet2.vertex[2].data(), &data.coordinates[vertex.coordIdx * 4], 3 * sizeof(float)); memcpy(facet2.vertex[2].data(), &data.coordinates[vertex.coordIdx * 4], 3 * sizeof(float));
if (vertex.normalIdx != -1) { if (vertex.normalIdx != -1) {
normal(0) += data.normals[vertex.normalIdx*3]; normal(0) += data.normals[vertex.normalIdx*3];
normal(1) += data.normals[vertex.normalIdx*3+1]; normal(1) += data.normals[vertex.normalIdx*3+1];
normal(2) += data.normals[vertex.normalIdx*3+2]; normal(2) += data.normals[vertex.normalIdx*3+2];
@ -96,25 +98,37 @@ bool load_obj(const char *path, Model *model, const char *object_name_in)
if (len > EPSILON) if (len > EPSILON)
facet.normal = normal / len; facet.normal = normal / len;
} }
} }
stl_get_size(&stl); stl_get_size(&stl);
mesh.repair(); mesh.repair();
if (mesh.facets_count() == 0) { if (mesh.facets_count() == 0) {
// die "This STL file couldn't be read because it's empty.\n" // die "This OBJ file couldn't be read because it's empty.\n"
return false; return false;
} }
std::string object_name;
if (object_name_in == nullptr) {
const char *last_slash = strrchr(path, DIR_SEPARATOR);
object_name.assign((last_slash == nullptr) ? path : last_slash + 1);
} else
object_name.assign(object_name_in);
model->add_object(object_name.c_str(), path, std::move(mesh));
return true; return true;
} }
bool load_obj(const char *path, Model *model, const char *object_name_in)
{
TriangleMesh mesh;
bool ret = load_obj(path, &mesh);
if (ret) {
std::string object_name;
if (object_name_in == nullptr) {
const char *last_slash = strrchr(path, DIR_SEPARATOR);
object_name.assign((last_slash == nullptr) ? path : last_slash + 1);
} else
object_name.assign(object_name_in);
model->add_object(object_name.c_str(), path, std::move(mesh));
}
return ret;
}
bool store_obj(const char *path, TriangleMesh *mesh) bool store_obj(const char *path, TriangleMesh *mesh)
{ {
//FIXME returning false even if write failed. //FIXME returning false even if write failed.

View file

@ -5,8 +5,10 @@ namespace Slic3r {
class TriangleMesh; class TriangleMesh;
class Model; class Model;
class ModelObject;
// Load an OBJ file into a provided model. // Load an OBJ file into a provided model.
extern bool load_obj(const char *path, TriangleMesh *mesh);
extern bool load_obj(const char *path, Model *model, const char *object_name = nullptr); extern bool load_obj(const char *path, Model *model, const char *object_name = nullptr);
extern bool store_obj(const char *path, TriangleMesh *mesh); extern bool store_obj(const char *path, TriangleMesh *mesh);