2013-08-29 09:47:59 +00:00
|
|
|
#include "ExtrusionEntityCollection.hpp"
|
2014-04-24 14:40:10 +00:00
|
|
|
#include <algorithm>
|
2015-05-31 20:04:32 +00:00
|
|
|
#include <cmath>
|
2014-03-15 01:16:04 +00:00
|
|
|
#include <map>
|
2013-08-29 09:47:59 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2015-07-06 23:17:31 +00:00
|
|
|
ExtrusionEntityCollection::ExtrusionEntityCollection(const ExtrusionPaths &paths)
|
|
|
|
: no_sort(false)
|
|
|
|
{
|
|
|
|
this->append(paths);
|
2014-04-24 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 14:35:08 +00:00
|
|
|
ExtrusionEntityCollection& ExtrusionEntityCollection::operator=(const ExtrusionEntityCollection &other)
|
2014-04-24 14:40:10 +00:00
|
|
|
{
|
2016-11-02 09:47:00 +00:00
|
|
|
this->entities = other.entities;
|
|
|
|
for (size_t i = 0; i < this->entities.size(); ++i)
|
|
|
|
this->entities[i] = this->entities[i]->clone();
|
|
|
|
this->no_sort = other.no_sort;
|
2014-04-24 14:40:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
void ExtrusionEntityCollection::swap(ExtrusionEntityCollection &c)
|
2014-04-24 14:40:10 +00:00
|
|
|
{
|
|
|
|
std::swap(this->entities, c.entities);
|
|
|
|
std::swap(this->no_sort, c.no_sort);
|
|
|
|
}
|
|
|
|
|
2016-11-02 09:47:00 +00:00
|
|
|
void ExtrusionEntityCollection::clear()
|
2015-07-23 13:53:02 +00:00
|
|
|
{
|
2016-11-02 09:47:00 +00:00
|
|
|
for (size_t i = 0; i < this->entities.size(); ++i)
|
|
|
|
delete this->entities[i];
|
|
|
|
this->entities.clear();
|
2015-07-23 13:53:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 23:17:31 +00:00
|
|
|
ExtrusionEntityCollection::operator ExtrusionPaths() const
|
|
|
|
{
|
|
|
|
ExtrusionPaths paths;
|
2019-09-11 11:25:50 +00:00
|
|
|
for (const ExtrusionEntity *ptr : this->entities) {
|
|
|
|
if (const ExtrusionPath *path = dynamic_cast<const ExtrusionPath*>(ptr))
|
2015-07-06 23:17:31 +00:00
|
|
|
paths.push_back(*path);
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
ExtrusionEntity* ExtrusionEntityCollection::clone() const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2015-07-23 13:53:02 +00:00
|
|
|
ExtrusionEntityCollection* coll = new ExtrusionEntityCollection(*this);
|
2016-11-02 09:47:00 +00:00
|
|
|
for (size_t i = 0; i < coll->entities.size(); ++i)
|
2015-07-23 13:53:02 +00:00
|
|
|
coll->entities[i] = this->entities[i]->clone();
|
|
|
|
return coll;
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
void ExtrusionEntityCollection::reverse()
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2019-09-11 11:25:50 +00:00
|
|
|
for (ExtrusionEntity *ptr : this->entities)
|
2015-01-08 14:19:56 +00:00
|
|
|
// Don't reverse it if it's a loop, as it doesn't change anything in terms of elements ordering
|
|
|
|
// and caller might rely on winding order
|
2019-09-11 11:25:50 +00:00
|
|
|
if (! ptr->is_loop())
|
|
|
|
ptr->reverse();
|
2013-08-29 09:47:59 +00:00
|
|
|
std::reverse(this->entities.begin(), this->entities.end());
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
void ExtrusionEntityCollection::replace(size_t i, const ExtrusionEntity &entity)
|
2016-03-19 14:33:58 +00:00
|
|
|
{
|
|
|
|
delete this->entities[i];
|
|
|
|
this->entities[i] = entity.clone();
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
void ExtrusionEntityCollection::remove(size_t i)
|
2016-03-19 14:33:58 +00:00
|
|
|
{
|
|
|
|
delete this->entities[i];
|
|
|
|
this->entities.erase(this->entities.begin() + i);
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
ExtrusionEntityCollection ExtrusionEntityCollection::chained_path(bool no_reverse, ExtrusionRole role) const
|
2015-07-06 23:17:31 +00:00
|
|
|
{
|
|
|
|
ExtrusionEntityCollection coll;
|
2017-04-07 15:37:30 +00:00
|
|
|
this->chained_path(&coll, no_reverse, role);
|
2015-07-06 23:17:31 +00:00
|
|
|
return coll;
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:44:38 +00:00
|
|
|
void ExtrusionEntityCollection::chained_path(ExtrusionEntityCollection* retval, bool no_reverse, ExtrusionRole role) const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
if (this->entities.empty()) return;
|
2019-09-26 07:44:38 +00:00
|
|
|
this->chained_path_from(this->entities.front()->first_point(), retval, no_reverse, role);
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:28:22 +00:00
|
|
|
ExtrusionEntityCollection ExtrusionEntityCollection::chained_path_from(Point start_near, bool no_reverse, ExtrusionRole role) const
|
|
|
|
{
|
|
|
|
ExtrusionEntityCollection coll;
|
|
|
|
this->chained_path_from(start_near, &coll, no_reverse, role);
|
|
|
|
return coll;
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:44:38 +00:00
|
|
|
void ExtrusionEntityCollection::chained_path_from(Point start_near, ExtrusionEntityCollection* retval, bool no_reverse, ExtrusionRole role) const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
if (this->no_sort) {
|
|
|
|
*retval = *this;
|
|
|
|
return;
|
|
|
|
}
|
2019-09-11 11:25:50 +00:00
|
|
|
|
2014-03-09 19:19:30 +00:00
|
|
|
retval->entities.reserve(this->entities.size());
|
2013-08-30 22:37:17 +00:00
|
|
|
|
2014-03-15 01:16:04 +00:00
|
|
|
// if we're asked to return the original indices, build a map
|
|
|
|
std::map<ExtrusionEntity*,size_t> indices_map;
|
|
|
|
|
2013-08-30 22:37:17 +00:00
|
|
|
ExtrusionEntitiesPtr my_paths;
|
2019-09-11 11:25:50 +00:00
|
|
|
for (ExtrusionEntity * const &entity_src : this->entities) {
|
2017-04-07 15:37:30 +00:00
|
|
|
if (role != erMixed) {
|
|
|
|
// The caller wants only paths with a specific extrusion role.
|
2019-09-11 11:25:50 +00:00
|
|
|
auto role2 = entity_src->role();
|
2017-04-07 15:37:30 +00:00
|
|
|
if (role != role2) {
|
|
|
|
// This extrusion entity does not match the role asked.
|
|
|
|
assert(role2 != erMixed);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 12:05:51 +00:00
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
ExtrusionEntity *entity = entity_src->clone();
|
2014-03-15 01:16:04 +00:00
|
|
|
my_paths.push_back(entity);
|
2019-09-26 07:44:38 +00:00
|
|
|
// if (orig_indices != nullptr)
|
|
|
|
// indices_map[entity] = &entity_src - &this->entities.front();
|
2013-08-30 22:37:17 +00:00
|
|
|
}
|
2013-08-29 09:47:59 +00:00
|
|
|
|
|
|
|
Points endpoints;
|
2019-09-11 11:25:50 +00:00
|
|
|
for (const ExtrusionEntity *entity : my_paths) {
|
|
|
|
endpoints.push_back(entity->first_point());
|
|
|
|
endpoints.push_back((no_reverse || ! entity->can_reverse()) ?
|
|
|
|
entity->first_point() : entity->last_point());
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
while (! my_paths.empty()) {
|
2013-08-29 09:47:59 +00:00
|
|
|
// find nearest point
|
2014-04-24 14:40:10 +00:00
|
|
|
int start_index = start_near.nearest_point_index(endpoints);
|
2013-08-29 09:47:59 +00:00
|
|
|
int path_index = start_index/2;
|
2014-03-15 01:16:04 +00:00
|
|
|
ExtrusionEntity* entity = my_paths.at(path_index);
|
2015-01-08 14:19:56 +00:00
|
|
|
// never reverse loops, since it's pointless for chained path and callers might depend on orientation
|
2019-09-11 11:25:50 +00:00
|
|
|
if (start_index % 2 && !no_reverse && entity->can_reverse())
|
2014-03-15 01:16:04 +00:00
|
|
|
entity->reverse();
|
2013-08-29 09:47:59 +00:00
|
|
|
retval->entities.push_back(my_paths.at(path_index));
|
2019-09-26 07:44:38 +00:00
|
|
|
// if (orig_indices != nullptr)
|
|
|
|
// orig_indices->push_back(indices_map[entity]);
|
2013-08-29 09:47:59 +00:00
|
|
|
my_paths.erase(my_paths.begin() + path_index);
|
|
|
|
endpoints.erase(endpoints.begin() + 2*path_index, endpoints.begin() + 2*path_index + 2);
|
|
|
|
start_near = retval->entities.back()->last_point();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 23:10:35 +00:00
|
|
|
void ExtrusionEntityCollection::polygons_covered_by_width(Polygons &out, const float scaled_epsilon) const
|
2015-01-15 19:06:30 +00:00
|
|
|
{
|
2019-09-11 11:25:50 +00:00
|
|
|
for (const ExtrusionEntity *entity : this->entities)
|
|
|
|
entity->polygons_covered_by_width(out, scaled_epsilon);
|
2016-11-03 23:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExtrusionEntityCollection::polygons_covered_by_spacing(Polygons &out, const float scaled_epsilon) const
|
|
|
|
{
|
2019-09-11 11:25:50 +00:00
|
|
|
for (const ExtrusionEntity *entity : this->entities)
|
|
|
|
entity->polygons_covered_by_spacing(out, scaled_epsilon);
|
2015-01-15 19:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
// Recursively count paths and loops contained in this collection.
|
|
|
|
size_t ExtrusionEntityCollection::items_count() const
|
2015-04-12 18:16:27 +00:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
2019-09-11 11:25:50 +00:00
|
|
|
for (const ExtrusionEntity *entity : this->entities)
|
|
|
|
if (entity->is_collection())
|
|
|
|
count += static_cast<const ExtrusionEntityCollection*>(entity)->items_count();
|
|
|
|
else
|
|
|
|
++ count;
|
2015-04-12 18:16:27 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
// Returns a single vector of pointers to all non-collection items contained in this one.
|
|
|
|
ExtrusionEntityCollection ExtrusionEntityCollection::flatten() const
|
2015-07-06 23:17:31 +00:00
|
|
|
{
|
2019-09-16 14:35:08 +00:00
|
|
|
struct Flatten {
|
|
|
|
ExtrusionEntityCollection out;
|
|
|
|
void recursive_do(const ExtrusionEntityCollection &collection) {
|
|
|
|
for (const ExtrusionEntity* entity : collection.entities)
|
|
|
|
if (entity->is_collection())
|
|
|
|
this->recursive_do(*static_cast<const ExtrusionEntityCollection*>(entity));
|
|
|
|
else
|
|
|
|
out.append(*entity);
|
|
|
|
}
|
|
|
|
} flatten;
|
|
|
|
flatten.recursive_do(*this);
|
|
|
|
return flatten.out;
|
2015-07-06 23:17:31 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:25:50 +00:00
|
|
|
double ExtrusionEntityCollection::min_mm3_per_mm() const
|
2015-05-31 20:04:32 +00:00
|
|
|
{
|
2016-11-03 23:10:35 +00:00
|
|
|
double min_mm3_per_mm = std::numeric_limits<double>::max();
|
2019-09-11 11:25:50 +00:00
|
|
|
for (const ExtrusionEntity *entity : this->entities)
|
|
|
|
min_mm3_per_mm = std::min(min_mm3_per_mm, entity->min_mm3_per_mm());
|
2015-05-31 20:04:32 +00:00
|
|
|
return min_mm3_per_mm;
|
|
|
|
}
|
|
|
|
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|