2013-08-29 09:47:59 +00:00
|
|
|
#include "ExtrusionEntityCollection.hpp"
|
2014-04-24 14:40:10 +00:00
|
|
|
#include <algorithm>
|
2014-03-15 01:16:04 +00:00
|
|
|
#include <map>
|
2013-08-29 09:47:59 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
ExtrusionEntityCollection::ExtrusionEntityCollection(const ExtrusionEntityCollection& collection)
|
2014-12-16 23:52:01 +00:00
|
|
|
: no_sort(collection.no_sort), orig_indices(collection.orig_indices)
|
2014-04-24 14:40:10 +00:00
|
|
|
{
|
|
|
|
this->entities.reserve(collection.entities.size());
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = collection.entities.begin(); it != collection.entities.end(); ++it)
|
|
|
|
this->entities.push_back((*it)->clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtrusionEntityCollection& ExtrusionEntityCollection::operator= (const ExtrusionEntityCollection &other)
|
|
|
|
{
|
|
|
|
ExtrusionEntityCollection tmp(other);
|
|
|
|
this->swap(tmp);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-29 09:47:59 +00:00
|
|
|
ExtrusionEntityCollection*
|
|
|
|
ExtrusionEntityCollection::clone() const
|
|
|
|
{
|
2014-04-24 14:40:10 +00:00
|
|
|
return new ExtrusionEntityCollection(*this);
|
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());
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
Point
|
2013-09-02 20:33:03 +00:00
|
|
|
ExtrusionEntityCollection::first_point() const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
|
|
|
return this->entities.front()->first_point();
|
|
|
|
}
|
|
|
|
|
2014-04-24 14:40:10 +00:00
|
|
|
Point
|
2013-09-02 20:33:03 +00:00
|
|
|
ExtrusionEntityCollection::last_point() const
|
2013-08-29 09:47:59 +00:00
|
|
|
{
|
|
|
|
return this->entities.back()->last_point();
|
|
|
|
}
|
|
|
|
|
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());
|
2013-08-29 09:47:59 +00:00
|
|
|
if (no_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
|
|
|
|
if (start_index % 2 && !no_reverse && !entity->is_loop()) {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:06:30 +00:00
|
|
|
Polygons
|
|
|
|
ExtrusionEntityCollection::grow() const
|
|
|
|
{
|
|
|
|
Polygons pp;
|
|
|
|
for (ExtrusionEntitiesPtr::const_iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
|
|
|
Polygons entity_pp = (*it)->grow();
|
|
|
|
pp.insert(pp.end(), entity_pp.begin(), entity_pp.end());
|
|
|
|
}
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
|
2014-04-27 17:18:53 +00:00
|
|
|
#ifdef SLIC3RXS
|
|
|
|
// there is no ExtrusionLoop::Collection or ExtrusionEntity::Collection
|
|
|
|
REGISTER_CLASS(ExtrusionEntityCollection, "ExtrusionPath::Collection");
|
|
|
|
#endif
|
|
|
|
|
2013-08-29 09:47:59 +00:00
|
|
|
}
|