2013-08-29 09:47:59 +00:00
|
|
|
#include "ExtrusionEntityCollection.hpp"
|
2019-09-27 16:17:21 +00:00
|
|
|
#include "ShortestPath.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 {
|
|
|
|
|
2020-01-10 14:51:35 +00:00
|
|
|
void filter_by_extrusion_role_in_place(ExtrusionEntitiesPtr &extrusion_entities, ExtrusionRole role)
|
|
|
|
{
|
|
|
|
if (role != erMixed) {
|
|
|
|
auto first = extrusion_entities.begin();
|
|
|
|
auto last = extrusion_entities.end();
|
2020-02-17 14:41:35 +00:00
|
|
|
extrusion_entities.erase(
|
|
|
|
std::remove_if(first, last, [&role](const ExtrusionEntity* ee) {
|
|
|
|
return ee->role() != role; }),
|
|
|
|
last);
|
2020-01-10 14:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-28 08:45:19 +00:00
|
|
|
ExtrusionEntity *ExtrusionEntityCollection::clone() const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2022-01-28 08:45:19 +00:00
|
|
|
return new ExtrusionEntityCollection(*this);
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-09 09:00:38 +00:00
|
|
|
ExtrusionEntityCollection ExtrusionEntityCollection::chained_path_from(const ExtrusionEntitiesPtr& extrusion_entities, const Point &start_near, ExtrusionRole role)
|
2019-09-27 16:17:21 +00:00
|
|
|
{
|
2020-01-10 14:51:35 +00:00
|
|
|
// Return a filtered copy of the collection.
|
|
|
|
ExtrusionEntityCollection out;
|
|
|
|
out.entities = filter_by_extrusion_role(extrusion_entities, role);
|
|
|
|
// Clone the extrusion entities.
|
|
|
|
for (auto &ptr : out.entities)
|
|
|
|
ptr = ptr->clone();
|
2020-01-09 09:00:38 +00:00
|
|
|
chain_and_reorder_extrusion_entities(out.entities, &start_near);
|
|
|
|
return out;
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2019-10-17 17:09:24 +00:00
|
|
|
ExtrusionEntityCollection ExtrusionEntityCollection::flatten(bool preserve_ordering) const
|
2015-07-06 23:17:31 +00:00
|
|
|
{
|
2019-09-16 14:35:08 +00:00
|
|
|
struct Flatten {
|
2019-10-17 17:09:24 +00:00
|
|
|
Flatten(bool preserve_ordering) : preserve_ordering(preserve_ordering) {}
|
2019-09-16 14:35:08 +00:00
|
|
|
ExtrusionEntityCollection out;
|
2019-10-17 17:09:24 +00:00
|
|
|
bool preserve_ordering;
|
2019-09-16 14:35:08 +00:00
|
|
|
void recursive_do(const ExtrusionEntityCollection &collection) {
|
2019-10-17 17:09:24 +00:00
|
|
|
if (collection.no_sort && preserve_ordering) {
|
|
|
|
// Don't flatten whatever happens below this level.
|
|
|
|
out.append(collection);
|
|
|
|
} else {
|
|
|
|
for (const ExtrusionEntity *entity : collection.entities)
|
|
|
|
if (entity->is_collection())
|
|
|
|
this->recursive_do(*static_cast<const ExtrusionEntityCollection*>(entity));
|
|
|
|
else
|
|
|
|
out.append(*entity);
|
|
|
|
}
|
2019-09-16 14:35:08 +00:00
|
|
|
}
|
2019-10-17 17:09:24 +00:00
|
|
|
} flatten(preserve_ordering);
|
|
|
|
|
2019-09-16 14:35:08 +00:00
|
|
|
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
|
|
|
}
|