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
|
|
|
}
|
|
|
|
|
|
|
|
ExtrusionEntityCollection& ExtrusionEntityCollection::operator= (const ExtrusionEntityCollection &other)
|
|
|
|
{
|
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->orig_indices = other.orig_indices;
|
|
|
|
this->no_sort = other.no_sort;
|
2014-04-24 14:40:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::swap (ExtrusionEntityCollection &c)
|
|
|
|
{
|
|
|
|
std::swap(this->entities, c.entities);
|
|
|
|
std::swap(this->orig_indices, c.orig_indices);
|
|
|
|
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;
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
|
|
|
if (const ExtrusionPath* path = dynamic_cast<const ExtrusionPath*>(*it))
|
|
|
|
paths.push_back(*path);
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2013-08-29 09:47:59 +00:00
|
|
|
ExtrusionEntityCollection*
|
|
|
|
ExtrusionEntityCollection::clone() const
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::reverse()
|
|
|
|
{
|
|
|
|
for (ExtrusionEntitiesPtr::iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
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
|
|
|
|
if (!(*it)->is_loop()) (*it)->reverse();
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
std::reverse(this->entities.begin(), this->entities.end());
|
|
|
|
}
|
|
|
|
|
2016-03-19 14:33:58 +00:00
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::replace(size_t i, const ExtrusionEntity &entity)
|
|
|
|
{
|
|
|
|
delete this->entities[i];
|
|
|
|
this->entities[i] = entity.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::remove(size_t i)
|
|
|
|
{
|
|
|
|
delete this->entities[i];
|
|
|
|
this->entities.erase(this->entities.begin() + i);
|
|
|
|
}
|
|
|
|
|
2015-07-06 23:17:31 +00:00
|
|
|
ExtrusionEntityCollection
|
|
|
|
ExtrusionEntityCollection::chained_path(bool no_reverse, std::vector<size_t>* orig_indices) const
|
|
|
|
{
|
|
|
|
ExtrusionEntityCollection coll;
|
|
|
|
this->chained_path(&coll, no_reverse, orig_indices);
|
|
|
|
return coll;
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::chained_path(ExtrusionEntityCollection* retval, bool no_reverse, std::vector<size_t>* orig_indices) const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
if (this->entities.empty()) return;
|
|
|
|
this->chained_path_from(this->entities.front()->first_point(), retval, no_reverse, orig_indices);
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::chained_path_from(Point start_near, ExtrusionEntityCollection* retval, bool no_reverse, std::vector<size_t>* orig_indices) const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
if (this->no_sort) {
|
|
|
|
*retval = *this;
|
|
|
|
return;
|
|
|
|
}
|
2014-03-09 19:19:30 +00:00
|
|
|
retval->entities.reserve(this->entities.size());
|
|
|
|
retval->orig_indices.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;
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
2014-03-15 01:16:04 +00:00
|
|
|
ExtrusionEntity* entity = (*it)->clone();
|
|
|
|
my_paths.push_back(entity);
|
|
|
|
if (orig_indices != NULL) indices_map[entity] = it - this->entities.begin();
|
2013-08-30 22:37:17 +00:00
|
|
|
}
|
2013-08-29 09:47:59 +00:00
|
|
|
|
|
|
|
Points endpoints;
|
|
|
|
for (ExtrusionEntitiesPtr::iterator it = my_paths.begin(); it != my_paths.end(); ++it) {
|
2014-04-24 14:40:10 +00:00
|
|
|
endpoints.push_back((*it)->first_point());
|
2015-03-09 17:28:07 +00:00
|
|
|
if (no_reverse || !(*it)->can_reverse()) {
|
2014-04-24 14:40:10 +00:00
|
|
|
endpoints.push_back((*it)->first_point());
|
2013-08-29 09:47:59 +00:00
|
|
|
} else {
|
2014-04-24 14:40:10 +00:00
|
|
|
endpoints.push_back((*it)->last_point());
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!my_paths.empty()) {
|
|
|
|
// 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
|
2015-03-09 17:28:07 +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));
|
2014-03-15 01:16:04 +00:00
|
|
|
if (orig_indices != NULL) 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
|
|
|
{
|
2016-11-03 09:24:32 +00:00
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it)
|
2016-11-03 23:10:35 +00:00
|
|
|
(*it)->polygons_covered_by_width(out, scaled_epsilon);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExtrusionEntityCollection::polygons_covered_by_spacing(Polygons &out, const float scaled_epsilon) const
|
|
|
|
{
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it)
|
|
|
|
(*it)->polygons_covered_by_spacing(out, scaled_epsilon);
|
2015-01-15 19:06:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-12 18:16:27 +00:00
|
|
|
/* Recursively count paths and loops contained in this collection */
|
|
|
|
size_t
|
|
|
|
ExtrusionEntityCollection::items_count() const
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
|
|
|
if ((*it)->is_collection()) {
|
|
|
|
ExtrusionEntityCollection* collection = dynamic_cast<ExtrusionEntityCollection*>(*it);
|
|
|
|
count += collection->items_count();
|
|
|
|
} else {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-05-31 20:04:32 +00:00
|
|
|
/* Returns a single vector of pointers to all non-collection items contained in this one */
|
|
|
|
void
|
|
|
|
ExtrusionEntityCollection::flatten(ExtrusionEntityCollection* retval) const
|
|
|
|
{
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
|
|
|
if ((*it)->is_collection()) {
|
|
|
|
ExtrusionEntityCollection* collection = dynamic_cast<ExtrusionEntityCollection*>(*it);
|
2015-07-23 13:53:02 +00:00
|
|
|
retval->append(collection->flatten().entities);
|
2015-05-31 20:04:32 +00:00
|
|
|
} else {
|
2015-07-06 23:17:31 +00:00
|
|
|
retval->append(**it);
|
2015-05-31 20:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 23:17:31 +00:00
|
|
|
ExtrusionEntityCollection
|
|
|
|
ExtrusionEntityCollection::flatten() const
|
|
|
|
{
|
|
|
|
ExtrusionEntityCollection coll;
|
|
|
|
this->flatten(&coll);
|
|
|
|
return coll;
|
|
|
|
}
|
|
|
|
|
2015-05-31 20:04:32 +00:00
|
|
|
double
|
|
|
|
ExtrusionEntityCollection::min_mm3_per_mm() const
|
|
|
|
{
|
2016-11-03 23:10:35 +00:00
|
|
|
double min_mm3_per_mm = std::numeric_limits<double>::max();
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it)
|
|
|
|
min_mm3_per_mm = std::min(min_mm3_per_mm, (*it)->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
|
|
|
}
|