Ported more things to XS

This commit is contained in:
Alessandro Ranellucci 2014-09-21 10:51:36 +02:00
parent 5a96bad8c2
commit 73b3c06361
18 changed files with 383 additions and 297 deletions

View file

@ -16,6 +16,7 @@ BoundingBoxBase<PointClass>::BoundingBoxBase(const std::vector<PointClass> &poin
this->max.x = std::max(it->x, this->max.x);
this->max.y = std::max(it->y, this->max.y);
}
this->defined = true;
}
template BoundingBoxBase<Point>::BoundingBoxBase(const std::vector<Point> &points);
template BoundingBoxBase<Pointf>::BoundingBoxBase(const std::vector<Pointf> &points);
@ -80,10 +81,15 @@ template void BoundingBoxBase<Pointf3>::scale(double factor);
template <class PointClass> void
BoundingBoxBase<PointClass>::merge(const PointClass &point)
{
this->min.x = std::min(point.x, this->min.x);
this->min.y = std::min(point.y, this->min.y);
this->max.x = std::max(point.x, this->max.x);
this->max.y = std::max(point.y, this->max.y);
if (this->defined) {
this->min.x = std::min(point.x, this->min.x);
this->min.y = std::min(point.y, this->min.y);
this->max.x = std::max(point.x, this->max.x);
this->max.y = std::max(point.y, this->max.y);
} else {
this->min = this->max = point;
this->defined = true;
}
}
template void BoundingBoxBase<Point>::merge(const Point &point);
template void BoundingBoxBase<Pointf>::merge(const Pointf &point);
@ -91,10 +97,16 @@ template void BoundingBoxBase<Pointf>::merge(const Pointf &point);
template <class PointClass> void
BoundingBoxBase<PointClass>::merge(const BoundingBoxBase<PointClass> &bb)
{
this->min.x = std::min(bb.min.x, this->min.x);
this->min.y = std::min(bb.min.y, this->min.y);
this->max.x = std::max(bb.max.x, this->max.x);
this->max.y = std::max(bb.max.y, this->max.y);
if (this->defined) {
this->min.x = std::min(bb.min.x, this->min.x);
this->min.y = std::min(bb.min.y, this->min.y);
this->max.x = std::max(bb.max.x, this->max.x);
this->max.y = std::max(bb.max.y, this->max.y);
} else {
this->min = bb.min;
this->max = bb.max;
this->defined = true;
}
}
template void BoundingBoxBase<Point>::merge(const BoundingBoxBase<Point> &bb);
template void BoundingBoxBase<Pointf>::merge(const BoundingBoxBase<Pointf> &bb);
@ -102,18 +114,22 @@ template void BoundingBoxBase<Pointf>::merge(const BoundingBoxBase<Pointf> &bb);
template <class PointClass> void
BoundingBox3Base<PointClass>::merge(const PointClass &point)
{
if (this->defined) {
this->min.z = std::min(point.z, this->min.z);
this->max.z = std::max(point.z, this->max.z);
}
BoundingBoxBase<PointClass>::merge(point);
this->min.z = std::min(point.z, this->min.z);
this->max.z = std::max(point.z, this->max.z);
}
template void BoundingBox3Base<Pointf3>::merge(const Pointf3 &point);
template <class PointClass> void
BoundingBox3Base<PointClass>::merge(const BoundingBox3Base<PointClass> &bb)
{
if (this->defined) {
this->min.z = std::min(bb.min.z, this->min.z);
this->max.z = std::max(bb.max.z, this->max.z);
}
BoundingBoxBase<PointClass>::merge(bb);
this->min.z = std::min(bb.min.z, this->min.z);
this->max.z = std::max(bb.max.z, this->max.z);
}
template void BoundingBox3Base<Pointf3>::merge(const BoundingBox3Base<Pointf3> &bb);

View file

@ -18,8 +18,9 @@ class BoundingBoxBase
public:
PointClass min;
PointClass max;
bool defined;
BoundingBoxBase() {};
BoundingBoxBase() : defined(false) {};
BoundingBoxBase(const std::vector<PointClass> &points);
void merge(const PointClass &point);
void merge(const BoundingBoxBase<PointClass> &bb);
@ -34,7 +35,7 @@ template <class PointClass>
class BoundingBox3Base : public BoundingBoxBase<PointClass>
{
public:
BoundingBox3Base() {};
BoundingBox3Base() : BoundingBoxBase<PointClass>() {};
BoundingBox3Base(const std::vector<PointClass> &points);
void merge(const PointClass &point);
void merge(const BoundingBox3Base<PointClass> &bb);
@ -50,7 +51,7 @@ class BoundingBox : public BoundingBoxBase<Point>
void polygon(Polygon* polygon) const;
Polygon polygon() const;
BoundingBox() {};
BoundingBox() : BoundingBoxBase<Point>() {};
BoundingBox(const Points &points) : BoundingBoxBase<Point>(points) {};
BoundingBox(const Lines &lines);
};
@ -61,13 +62,13 @@ class BoundingBox3 : public BoundingBox3Base<Point3> {};
class BoundingBoxf : public BoundingBoxBase<Pointf> {
public:
BoundingBoxf() {};
BoundingBoxf() : BoundingBoxBase<Pointf>() {};
BoundingBoxf(const std::vector<Pointf> &points) : BoundingBoxBase<Pointf>(points) {};
};
class BoundingBoxf3 : public BoundingBox3Base<Pointf3> {
public:
BoundingBoxf3() {};
BoundingBoxf3() : BoundingBox3Base<Pointf3>() {};
BoundingBoxf3(const std::vector<Pointf3> &points) : BoundingBox3Base<Pointf3>(points) {};
};

View file

@ -173,6 +173,76 @@ Model::add_default_instances()
return true;
}
// this returns the bounding box of the *transformed* instances
void
Model::bounding_box(BoundingBoxf3* bb)
{
for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) {
BoundingBoxf3 obb;
(*o)->bounding_box(&obb);
bb->merge(obb);
}
}
void
Model::center_instances_around_point(const Pointf &point)
{
BoundingBoxf3 bb;
this->bounding_box(&bb);
Sizef3 size = bb.size();
double shift_x = -bb.min.x + point.x - size.x/2;
double shift_y = -bb.min.y + point.y - size.y/2;
for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) {
for (ModelInstancePtrs::const_iterator i = (*o)->instances.begin(); i != (*o)->instances.end(); ++i) {
(*i)->offset.translate(shift_x, shift_y);
}
(*o)->update_bounding_box();
}
}
void
Model::align_instances_to_origin()
{
BoundingBoxf3 bb;
this->bounding_box(&bb);
Pointf new_center = (Pointf)bb.size();
new_center.translate(-new_center.x/2, -new_center.y/2);
this->center_instances_around_point(new_center);
}
void
Model::translate(coordf_t x, coordf_t y, coordf_t z)
{
for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) {
(*o)->translate(x, y, z);
}
}
// flattens everything to a single mesh
void
Model::mesh(TriangleMesh* mesh) const
{
for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) {
TriangleMesh omesh;
(*o)->mesh(&omesh);
mesh->merge(omesh);
}
}
// flattens everything to a single mesh
void
Model::raw_mesh(TriangleMesh* mesh) const
{
for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) {
TriangleMesh omesh;
(*o)->raw_mesh(&omesh);
mesh->merge(omesh);
}
}
#ifdef SLIC3RXS
REGISTER_CLASS(Model, "Model");
#endif
@ -321,12 +391,45 @@ ModelObject::clear_instances()
this->delete_instance(i);
}
// this returns the bounding box of the *transformed* instances
void
ModelObject::bounding_box(BoundingBoxf3* bb)
{
if (!this->_bounding_box_valid) this->update_bounding_box();
*bb = this->_bounding_box;
}
void
ModelObject::invalidate_bounding_box()
{
this->_bounding_box_valid = false;
}
void
ModelObject::update_bounding_box()
{
TriangleMesh mesh;
this->mesh(&mesh);
mesh.bounding_box(&this->_bounding_box);
this->_bounding_box_valid = true;
}
// flattens all volumes and instances into a single mesh
void
ModelObject::mesh(TriangleMesh* mesh) const
{
TriangleMesh raw_mesh;
this->raw_mesh(&raw_mesh);
for (ModelInstancePtrs::const_iterator i = this->instances.begin(); i != this->instances.end(); ++i) {
TriangleMesh m = raw_mesh;
(*i)->transform_mesh(&m);
mesh->merge(m);
}
}
void
ModelObject::raw_mesh(TriangleMesh* mesh) const
{
@ -336,6 +439,94 @@ ModelObject::raw_mesh(TriangleMesh* mesh) const
}
}
void
ModelObject::raw_bounding_box(BoundingBoxf3* bb) const
{
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
if ((*v)->modifier) continue;
TriangleMesh mesh = (*v)->mesh;
if (this->instances.empty()) CONFESS("Can't call raw_bounding_box() with no instances");
this->instances.front()->transform_mesh(&mesh, true);
BoundingBoxf3 mbb;
mesh.bounding_box(&mbb);
bb->merge(mbb);
}
}
// this returns the bounding box of the *transformed* given instance
void
ModelObject::instance_bounding_box(size_t instance_idx, BoundingBoxf3* bb) const
{
TriangleMesh mesh;
this->raw_mesh(&mesh);
this->instances[instance_idx]->transform_mesh(&mesh);
mesh.bounding_box(bb);
}
void
ModelObject::center_around_origin()
{
// calculate the displacements needed to
// center this object around the origin
BoundingBoxf3 bb;
{
TriangleMesh mesh;
this->raw_mesh(&mesh);
mesh.bounding_box(&bb);
}
// first align to origin on XY
double shift_x = -bb.min.x;
double shift_y = -bb.min.y;
// then center it on XY
Sizef3 size = bb.size();
shift_x -= size.x/2;
shift_y -= size.y/2;
this->translate(shift_x, shift_y, 0);
this->origin_translation.translate(shift_x, shift_y);
if (!this->instances.empty()) {
for (ModelInstancePtrs::const_iterator i = this->instances.begin(); i != this->instances.end(); ++i) {
(*i)->offset.translate(-shift_x, -shift_y);
}
this->update_bounding_box();
}
}
void
ModelObject::translate(coordf_t x, coordf_t y, coordf_t z)
{
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
(*v)->mesh.translate(x, y, z);
}
if (this->_bounding_box_valid) this->_bounding_box.translate(x, y, z);
}
void
ModelObject::scale(const Pointf3 &versor)
{
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
(*v)->mesh.scale(versor);
}
this->invalidate_bounding_box();
}
size_t
ModelObject::materials_count() const
{
std::set<t_model_material_id> material_ids;
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
material_ids.insert((*v)->material_id());
}
return material_ids.size();
}
size_t
ModelObject::facets_count() const
{
@ -357,6 +548,47 @@ ModelObject::needed_repair() const
return false;
}
void
ModelObject::cut(coordf_t z, Model* model) const
{
// clone this one to duplicate instances, materials etc.
ModelObject* upper = model->add_object(*this);
ModelObject* lower = model->add_object(*this);
upper->clear_volumes();
lower->clear_volumes();
for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) {
ModelVolume* volume = *v;
if (volume->modifier) {
// don't cut modifiers
upper->add_volume(*volume);
lower->add_volume(*volume);
} else {
TriangleMeshSlicer tms(&volume->mesh);
TriangleMesh upper_mesh, lower_mesh;
// TODO: shouldn't we use object bounding box instead of per-volume bb?
tms.cut(z + volume->mesh.bounding_box().min.z, &upper_mesh, &lower_mesh);
upper_mesh.repair();
lower_mesh.repair();
upper_mesh.reset_repair_stats();
lower_mesh.reset_repair_stats();
if (upper_mesh.facets_count() > 0) {
ModelVolume* vol = upper->add_volume(upper_mesh);
vol->name = volume->name;
vol->config = volume->config;
vol->set_material(volume->material_id(), *volume->material());
}
if (lower_mesh.facets_count() > 0) {
ModelVolume* vol = lower->add_volume(lower_mesh);
vol->name = volume->name;
vol->config = volume->config;
vol->set_material(volume->material_id(), *volume->material());
}
}
}
}
#ifdef SLIC3RXS
REGISTER_CLASS(ModelObject, "Model::Object");
#endif
@ -394,6 +626,13 @@ ModelVolume::material() const
return this->object->get_model()->get_material(this->_material_id);
}
void
ModelVolume::set_material(t_model_material_id material_id, const ModelMaterial &material)
{
this->_material_id = material_id;
(void)this->object->get_model()->add_material(material_id, material);
}
ModelMaterial*
ModelVolume::assign_unique_material()
{

View file

@ -54,11 +54,12 @@ class Model
// void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb);
bool has_objects_with_no_instances() const;
bool add_default_instances();
// void bounding_box(BoundingBox* bb) const;
// void align_to_origin();
// void center_instances_around_point(const Pointf &point);
// void translate(coordf_t x, coordf_t y, coordf_t z);
// void mesh(TriangleMesh* mesh) const;
void bounding_box(BoundingBoxf3* bb);
void center_instances_around_point(const Pointf &point);
void align_instances_to_origin();
void translate(coordf_t x, coordf_t y, coordf_t z);
void mesh(TriangleMesh* mesh) const;
void raw_mesh(TriangleMesh* mesh) const;
// void split_meshes();
// std::string get_material_name(t_model_material_id material_id);
@ -113,17 +114,21 @@ class ModelObject
void delete_last_instance();
void clear_instances();
void bounding_box(BoundingBoxf3* bb);
void invalidate_bounding_box();
void mesh(TriangleMesh* mesh) const;
void raw_mesh(TriangleMesh* mesh) const;
//void mesh(TriangleMesh* mesh) const;
//void instance_bounding_box(size_t instance_idx, BoundingBox* bb) const;
//void center_around_origin();
//void translate(coordf_t x, coordf_t y, coordf_t z);
//size_t materials_count() const;
//void unique_materials(std::vector<t_model_material_id>* materials) const;
void raw_bounding_box(BoundingBoxf3* bb) const;
void instance_bounding_box(size_t instance_idx, BoundingBoxf3* bb) const;
void center_around_origin();
void translate(coordf_t x, coordf_t y, coordf_t z);
void scale(const Pointf3 &versor);
size_t materials_count() const;
size_t facets_count() const;
bool needed_repair() const;
void cut(coordf_t z, Model* model) const;
void update_bounding_box(); // this is a private method but we expose it until we need to expose it via XS
private:
Model* model;
@ -133,7 +138,6 @@ class ModelObject
ModelObject& operator= (ModelObject other);
void swap(ModelObject &other);
~ModelObject();
void update_bounding_box();
};
class ModelVolume
@ -149,6 +153,7 @@ class ModelVolume
t_model_material_id material_id() const;
void material_id(t_model_material_id material_id);
ModelMaterial* material() const;
void set_material(t_model_material_id material_id, const ModelMaterial &material);
ModelMaterial* assign_unique_material();

View file

@ -164,6 +164,12 @@ TriangleMesh::needed_repair() const
|| this->stl.stats.backwards_edges > 0;
}
size_t
TriangleMesh::facets_count() const
{
return this->stl.stats.number_of_facets;
}
void
TriangleMesh::WriteOBJFile(char* output_file) {
stl_generate_shared_vertices(&stl);
@ -175,12 +181,12 @@ void TriangleMesh::scale(float factor)
stl_scale(&(this->stl), factor);
}
void TriangleMesh::scale(std::vector<double> versor)
void TriangleMesh::scale(const Pointf3 &versor)
{
float fversor[3];
fversor[0] = versor[0];
fversor[1] = versor[1];
fversor[2] = versor[2];
fversor[0] = versor.x;
fversor[1] = versor.y;
fversor[2] = versor.z;
stl_scale_versor(&this->stl, fversor);
}
@ -355,6 +361,14 @@ TriangleMesh::bounding_box(BoundingBoxf3* bb) const
bb->max.z = this->stl.stats.max.z;
}
BoundingBoxf3
TriangleMesh::bounding_box() const
{
BoundingBoxf3 bb;
this->bounding_box(&bb);
return bb;
}
void
TriangleMesh::require_shared_vertices()
{

View file

@ -29,7 +29,7 @@ class TriangleMesh
void repair();
void WriteOBJFile(char* output_file);
void scale(float factor);
void scale(std::vector<double> versor);
void scale(const Pointf3 &versor);
void translate(float x, float y, float z);
void rotate_x(float angle);
void rotate_y(float angle);
@ -44,8 +44,10 @@ class TriangleMesh
void horizontal_projection(ExPolygons &retval) const;
void convex_hull(Polygon* hull);
void bounding_box(BoundingBoxf3* bb) const;
BoundingBoxf3 bounding_box() const;
void reset_repair_stats();
bool needed_repair() const;
size_t facets_count() const;
stl_file stl;
bool repaired;