mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-10-21 15:51:10 -06:00
Ported some ModelObject methods from Perl to C++.
Added some utility functions to TriangleMesh, thanks to @alexrj Some porting to C++ based on work by @alexrj.
This commit is contained in:
parent
777023c7a8
commit
31085fb1d7
7 changed files with 191 additions and 81 deletions
|
@ -1,8 +1,16 @@
|
|||
#include "Model.hpp"
|
||||
#include "Geometry.hpp"
|
||||
|
||||
#include "Format/AMF.hpp"
|
||||
#include "Format/OBJ.hpp"
|
||||
#include "Format/PRUS.hpp"
|
||||
#include "Format/STL.hpp"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
Model::Model(const Model &other)
|
||||
|
@ -28,10 +36,36 @@ void Model::swap(Model &other)
|
|||
std::swap(this->objects, other.objects);
|
||||
}
|
||||
|
||||
Model::~Model()
|
||||
Model Model::read_from_file(const std::string &input_file, bool add_default_instances)
|
||||
{
|
||||
this->clear_objects();
|
||||
this->clear_materials();
|
||||
Model model;
|
||||
|
||||
bool result = false;
|
||||
if (boost::algorithm::iends_with(input_file, ".stl"))
|
||||
result = load_stl(input_file.c_str(), &model);
|
||||
else if (boost::algorithm::iends_with(input_file, ".obj"))
|
||||
result = load_obj(input_file.c_str(), &model);
|
||||
else if (boost::algorithm::iends_with(input_file, ".amf") ||
|
||||
boost::algorithm::iends_with(input_file, ".amf.xml"))
|
||||
result = load_amf(input_file.c_str(), &model);
|
||||
else if (boost::algorithm::iends_with(input_file, ".prusa"))
|
||||
result = load_prus(input_file.c_str(), &model);
|
||||
else
|
||||
throw std::runtime_error("Unknown file format. Input file must have .stl, .obj, .amf(.xml) or .prusa extension.");
|
||||
|
||||
if (! result)
|
||||
throw std::runtime_error("Loading of a model file failed.");
|
||||
|
||||
if (model.objects.empty())
|
||||
throw std::runtime_error("The supplied file couldn't be read because it's empty");
|
||||
|
||||
for (ModelObject *o : model.objects)
|
||||
o->input_file = input_file;
|
||||
|
||||
if (add_default_instances)
|
||||
model.add_default_instances();
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
ModelObject* Model::add_object()
|
||||
|
@ -279,6 +313,45 @@ void Model::duplicate_objects_grid(size_t x, size_t y, coordf_t dist)
|
|||
}
|
||||
}
|
||||
|
||||
bool Model::looks_like_multipart_object() const
|
||||
{
|
||||
if (this->objects.size() <= 1)
|
||||
return false;
|
||||
double zmin = std::numeric_limits<double>::max();
|
||||
for (const ModelObject *obj : this->objects) {
|
||||
if (obj->volumes.size() > 1 || obj->config.keys().size() > 1)
|
||||
return false;
|
||||
for (const ModelVolume *vol : obj->volumes) {
|
||||
double zmin_this = vol->mesh.bounding_box().min.z;
|
||||
if (zmin == std::numeric_limits<double>::max())
|
||||
zmin = zmin_this;
|
||||
else if (std::abs(zmin - zmin_this) > EPSILON)
|
||||
// The volumes don't share zmin.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Model::convert_multipart_object()
|
||||
{
|
||||
if (this->objects.empty())
|
||||
return;
|
||||
|
||||
ModelObject* object = this->add_object();
|
||||
object->input_file = this->objects.front()->input_file;
|
||||
|
||||
for (const ModelObject* o : this->objects)
|
||||
for (const ModelVolume* v : o->volumes)
|
||||
object->add_volume(*v)->name = o->name;
|
||||
|
||||
for (const ModelInstance* i : this->objects.front()->instances)
|
||||
object->add_instance(*i);
|
||||
|
||||
while (this->objects.size() > 1)
|
||||
this->delete_object(0);
|
||||
}
|
||||
|
||||
ModelObject::ModelObject(Model *model, const ModelObject &other, bool copy_volumes) :
|
||||
name(other.name),
|
||||
input_file(other.input_file),
|
||||
|
@ -662,6 +735,48 @@ void ModelObject::split(ModelObjectPtrs* new_objects)
|
|||
return;
|
||||
}
|
||||
|
||||
void ModelObject::print_info() const
|
||||
{
|
||||
using namespace std;
|
||||
cout << fixed;
|
||||
cout << "[" << boost::filesystem::path(this->input_file).filename().string() << "]" << endl;
|
||||
|
||||
TriangleMesh mesh = this->raw_mesh();
|
||||
mesh.check_topology();
|
||||
BoundingBoxf3 bb = mesh.bounding_box();
|
||||
Sizef3 size = bb.size();
|
||||
cout << "size_x = " << size.x << endl;
|
||||
cout << "size_y = " << size.y << endl;
|
||||
cout << "size_z = " << size.z << endl;
|
||||
cout << "min_x = " << bb.min.x << endl;
|
||||
cout << "min_y = " << bb.min.y << endl;
|
||||
cout << "min_z = " << bb.min.z << endl;
|
||||
cout << "max_x = " << bb.max.x << endl;
|
||||
cout << "max_y = " << bb.max.y << endl;
|
||||
cout << "max_z = " << bb.max.z << endl;
|
||||
cout << "number_of_facets = " << mesh.stl.stats.number_of_facets << endl;
|
||||
cout << "manifold = " << (mesh.is_manifold() ? "yes" : "no") << endl;
|
||||
|
||||
mesh.repair(); // this calculates number_of_parts
|
||||
if (mesh.needed_repair()) {
|
||||
mesh.repair();
|
||||
if (mesh.stl.stats.degenerate_facets > 0)
|
||||
cout << "degenerate_facets = " << mesh.stl.stats.degenerate_facets << endl;
|
||||
if (mesh.stl.stats.edges_fixed > 0)
|
||||
cout << "edges_fixed = " << mesh.stl.stats.edges_fixed << endl;
|
||||
if (mesh.stl.stats.facets_removed > 0)
|
||||
cout << "facets_removed = " << mesh.stl.stats.facets_removed << endl;
|
||||
if (mesh.stl.stats.facets_added > 0)
|
||||
cout << "facets_added = " << mesh.stl.stats.facets_added << endl;
|
||||
if (mesh.stl.stats.facets_reversed > 0)
|
||||
cout << "facets_reversed = " << mesh.stl.stats.facets_reversed << endl;
|
||||
if (mesh.stl.stats.backwards_edges > 0)
|
||||
cout << "backwards_edges = " << mesh.stl.stats.backwards_edges << endl;
|
||||
}
|
||||
cout << "number_of_parts = " << mesh.stl.stats.number_of_parts << endl;
|
||||
cout << "volume = " << mesh.volume() << endl;
|
||||
}
|
||||
|
||||
void ModelVolume::material_id(t_model_material_id material_id)
|
||||
{
|
||||
this->_material_id = material_id;
|
||||
|
|
|
@ -125,6 +125,9 @@ public:
|
|||
bool needed_repair() const;
|
||||
void cut(coordf_t z, Model* model) const;
|
||||
void split(ModelObjectPtrs* new_objects);
|
||||
|
||||
// Print object statistics to console.
|
||||
void print_info() const;
|
||||
|
||||
private:
|
||||
ModelObject(Model *model) : m_model(model), m_bounding_box_valid(false), layer_height_profile_valid(false) {}
|
||||
|
@ -232,7 +235,10 @@ public:
|
|||
Model(const Model &other);
|
||||
Model& operator= (Model other);
|
||||
void swap(Model &other);
|
||||
~Model();
|
||||
~Model() { this->clear_objects(); this->clear_materials(); }
|
||||
|
||||
static Model read_from_file(const std::string &input_file, bool add_default_instances = true);
|
||||
|
||||
ModelObject* add_object();
|
||||
ModelObject* add_object(const char *name, const char *path, const TriangleMesh &mesh);
|
||||
ModelObject* add_object(const char *name, const char *path, TriangleMesh &&mesh);
|
||||
|
@ -259,6 +265,11 @@ public:
|
|||
void duplicate(size_t copies_num, coordf_t dist, const BoundingBoxf* bb = NULL);
|
||||
void duplicate_objects(size_t copies_num, coordf_t dist, const BoundingBoxf* bb = NULL);
|
||||
void duplicate_objects_grid(size_t x, size_t y, coordf_t dist);
|
||||
|
||||
bool looks_like_multipart_object() const;
|
||||
void convert_multipart_object();
|
||||
|
||||
void print_info() const { for (const ModelObject *o : this->objects) o->print_info(); }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -215,6 +215,47 @@ TriangleMesh::repair() {
|
|||
BOOST_LOG_TRIVIAL(debug) << "TriangleMesh::repair() finished";
|
||||
}
|
||||
|
||||
|
||||
float TriangleMesh::volume()
|
||||
{
|
||||
if (this->stl.stats.volume == -1)
|
||||
stl_calculate_volume(&this->stl);
|
||||
return this->stl.stats.volume;
|
||||
}
|
||||
|
||||
void TriangleMesh::check_topology()
|
||||
{
|
||||
// checking exact
|
||||
stl_check_facets_exact(&stl);
|
||||
stl.stats.facets_w_1_bad_edge = (stl.stats.connected_facets_2_edge - stl.stats.connected_facets_3_edge);
|
||||
stl.stats.facets_w_2_bad_edge = (stl.stats.connected_facets_1_edge - stl.stats.connected_facets_2_edge);
|
||||
stl.stats.facets_w_3_bad_edge = (stl.stats.number_of_facets - stl.stats.connected_facets_1_edge);
|
||||
|
||||
// checking nearby
|
||||
//int last_edges_fixed = 0;
|
||||
float tolerance = stl.stats.shortest_edge;
|
||||
float increment = stl.stats.bounding_diameter / 10000.0;
|
||||
int iterations = 2;
|
||||
if (stl.stats.connected_facets_3_edge < stl.stats.number_of_facets) {
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
if (stl.stats.connected_facets_3_edge < stl.stats.number_of_facets) {
|
||||
//printf("Checking nearby. Tolerance= %f Iteration=%d of %d...", tolerance, i + 1, iterations);
|
||||
stl_check_facets_nearby(&stl, tolerance);
|
||||
//printf(" Fixed %d edges.\n", stl.stats.edges_fixed - last_edges_fixed);
|
||||
//last_edges_fixed = stl.stats.edges_fixed;
|
||||
tolerance += increment;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TriangleMesh::is_manifold() const
|
||||
{
|
||||
return this->stl.stats.connected_facets_3_edge == this->stl.stats.number_of_facets;
|
||||
}
|
||||
|
||||
void
|
||||
TriangleMesh::reset_repair_stats() {
|
||||
this->stl.stats.degenerate_facets = 0;
|
||||
|
|
|
@ -32,6 +32,9 @@ public:
|
|||
void write_ascii(const char* output_file);
|
||||
void write_binary(const char* output_file);
|
||||
void repair();
|
||||
float volume();
|
||||
void check_topology();
|
||||
bool is_manifold() const;
|
||||
void WriteOBJFile(char* output_file);
|
||||
void scale(float factor);
|
||||
void scale(const Pointf3 &versor);
|
||||
|
|
|
@ -15,6 +15,15 @@
|
|||
Model();
|
||||
~Model();
|
||||
|
||||
%name{read_from_file} Model(std::string input_file, bool add_default_instances = true)
|
||||
%code%{
|
||||
try {
|
||||
RETVAL = new Model(Model::read_from_file(input_file, add_default_instances));
|
||||
} catch (std::exception& e) {
|
||||
croak("Error while opening %s: %s\n", input_file.c_str(), e.what());
|
||||
}
|
||||
%};
|
||||
|
||||
Clone<Model> clone()
|
||||
%code%{ RETVAL = THIS; %};
|
||||
|
||||
|
@ -73,6 +82,11 @@
|
|||
void duplicate_objects(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
|
||||
void duplicate_objects_grid(unsigned int x, unsigned int y, double dist);
|
||||
|
||||
bool looks_like_multipart_object() const;
|
||||
void convert_multipart_object();
|
||||
|
||||
void print_info() const;
|
||||
|
||||
bool store_stl(char *path, bool binary)
|
||||
%code%{ TriangleMesh mesh = THIS->mesh(); RETVAL = Slic3r::store_stl(path, &mesh, binary); %};
|
||||
bool store_amf(char *path)
|
||||
|
@ -272,6 +286,8 @@ ModelMaterial::attributes()
|
|||
RETVAL = new ModelObjectPtrs(); // leak?
|
||||
THIS->split(RETVAL);
|
||||
%};
|
||||
|
||||
void print_info() const;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue