2013-11-22 01:16:10 +00:00
|
|
|
#include "SurfaceCollection.hpp"
|
2016-09-30 13:23:18 +00:00
|
|
|
#include "BoundingBox.hpp"
|
|
|
|
#include "SVG.hpp"
|
|
|
|
|
2013-11-22 23:07:04 +00:00
|
|
|
#include <map>
|
2013-11-22 01:16:10 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::simplify(double tolerance)
|
2013-11-22 01:16:10 +00:00
|
|
|
{
|
2013-11-22 15:01:50 +00:00
|
|
|
Surfaces ss;
|
|
|
|
for (Surfaces::const_iterator it_s = this->surfaces.begin(); it_s != this->surfaces.end(); ++it_s) {
|
|
|
|
ExPolygons expp;
|
2015-12-21 13:46:35 +00:00
|
|
|
it_s->expolygon.simplify(tolerance, &expp);
|
2013-11-22 15:01:50 +00:00
|
|
|
for (ExPolygons::const_iterator it_e = expp.begin(); it_e != expp.end(); ++it_e) {
|
|
|
|
Surface s = *it_s;
|
|
|
|
s.expolygon = *it_e;
|
|
|
|
ss.push_back(s);
|
|
|
|
}
|
2013-11-22 01:16:10 +00:00
|
|
|
}
|
2013-11-22 15:01:50 +00:00
|
|
|
this->surfaces = ss;
|
2013-11-22 01:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 23:07:04 +00:00
|
|
|
/* group surfaces by common properties */
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::group(std::vector<SurfacesPtr> *retval)
|
2013-11-22 23:07:04 +00:00
|
|
|
{
|
2013-11-23 17:15:59 +00:00
|
|
|
for (Surfaces::iterator it = this->surfaces.begin(); it != this->surfaces.end(); ++it) {
|
2013-12-03 23:10:31 +00:00
|
|
|
// find a group with the same properties
|
|
|
|
SurfacesPtr* group = NULL;
|
2016-11-07 21:49:11 +00:00
|
|
|
for (std::vector<SurfacesPtr>::iterator git = retval->begin(); git != retval->end(); ++git)
|
|
|
|
if (! git->empty() && surfaces_could_merge(*git->front(), *it)) {
|
2013-12-03 23:10:31 +00:00
|
|
|
group = &*git;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// if no group with these properties exists, add one
|
|
|
|
if (group == NULL) {
|
|
|
|
retval->resize(retval->size() + 1);
|
|
|
|
group = &retval->back();
|
2013-11-22 23:07:04 +00:00
|
|
|
}
|
2013-12-03 23:10:31 +00:00
|
|
|
// append surface to group
|
|
|
|
group->push_back(&*it);
|
2013-11-22 23:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
SurfacesPtr SurfaceCollection::filter_by_type(const SurfaceType type)
|
2014-12-24 09:20:55 +00:00
|
|
|
{
|
|
|
|
SurfacesPtr ss;
|
|
|
|
for (Surfaces::iterator surface = this->surfaces.begin(); surface != this->surfaces.end(); ++surface) {
|
|
|
|
if (surface->surface_type == type) ss.push_back(&*surface);
|
|
|
|
}
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
SurfacesPtr SurfaceCollection::filter_by_types(const SurfaceType *types, int ntypes)
|
2016-09-26 10:42:44 +00:00
|
|
|
{
|
|
|
|
SurfacesPtr ss;
|
|
|
|
for (Surfaces::iterator surface = this->surfaces.begin(); surface != this->surfaces.end(); ++surface) {
|
|
|
|
for (int i = 0; i < ntypes; ++ i) {
|
|
|
|
if (surface->surface_type == types[i]) {
|
|
|
|
ss.push_back(&*surface);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::filter_by_type(SurfaceType type, Polygons* polygons)
|
2014-12-24 09:20:55 +00:00
|
|
|
{
|
|
|
|
for (Surfaces::iterator surface = this->surfaces.begin(); surface != this->surfaces.end(); ++surface) {
|
|
|
|
if (surface->surface_type == type) {
|
|
|
|
Polygons pp = surface->expolygon;
|
|
|
|
polygons->insert(polygons->end(), pp.begin(), pp.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::keep_type(const SurfaceType type)
|
2016-09-26 10:42:44 +00:00
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < surfaces.size(); ++ i) {
|
|
|
|
if (surfaces[i].surface_type == type) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(surfaces[i], surfaces[j]);
|
|
|
|
++ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < surfaces.size())
|
|
|
|
surfaces.erase(surfaces.begin() + j, surfaces.end());
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::keep_types(const SurfaceType *types, int ntypes)
|
2016-09-26 10:42:44 +00:00
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < surfaces.size(); ++ i) {
|
|
|
|
bool keep = false;
|
|
|
|
for (int k = 0; k < ntypes; ++ k) {
|
|
|
|
if (surfaces[i].surface_type == types[k]) {
|
|
|
|
keep = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keep) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(surfaces[i], surfaces[j]);
|
|
|
|
++ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < surfaces.size())
|
|
|
|
surfaces.erase(surfaces.begin() + j, surfaces.end());
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::remove_type(const SurfaceType type)
|
2016-09-26 10:42:44 +00:00
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < surfaces.size(); ++ i) {
|
|
|
|
if (surfaces[i].surface_type != type) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(surfaces[i], surfaces[j]);
|
|
|
|
++ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < surfaces.size())
|
|
|
|
surfaces.erase(surfaces.begin() + j, surfaces.end());
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:39:53 +00:00
|
|
|
void SurfaceCollection::remove_types(const SurfaceType *types, int ntypes)
|
2016-09-26 10:42:44 +00:00
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < surfaces.size(); ++ i) {
|
|
|
|
bool remove = false;
|
|
|
|
for (int k = 0; k < ntypes; ++ k) {
|
|
|
|
if (surfaces[i].surface_type == types[k]) {
|
|
|
|
remove = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! remove) {
|
|
|
|
if (j < i)
|
|
|
|
std::swap(surfaces[i], surfaces[j]);
|
|
|
|
++ j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < surfaces.size())
|
|
|
|
surfaces.erase(surfaces.begin() + j, surfaces.end());
|
|
|
|
}
|
|
|
|
|
2016-09-30 13:23:18 +00:00
|
|
|
void SurfaceCollection::export_to_svg(const char *path, bool show_labels)
|
|
|
|
{
|
|
|
|
BoundingBox bbox;
|
|
|
|
for (Surfaces::const_iterator surface = this->surfaces.begin(); surface != this->surfaces.end(); ++surface)
|
|
|
|
bbox.merge(get_extents(surface->expolygon));
|
|
|
|
Point legend_size = export_surface_type_legend_to_svg_box_size();
|
2018-08-17 13:53:43 +00:00
|
|
|
Point legend_pos(bbox.min(0), bbox.max(1));
|
|
|
|
bbox.merge(Point(std::max(bbox.min(0) + legend_size(0), bbox.max(0)), bbox.max(1) + legend_size(1)));
|
2016-09-30 13:23:18 +00:00
|
|
|
|
|
|
|
SVG svg(path, bbox);
|
|
|
|
const float transparency = 0.5f;
|
|
|
|
for (Surfaces::const_iterator surface = this->surfaces.begin(); surface != this->surfaces.end(); ++surface) {
|
|
|
|
svg.draw(surface->expolygon, surface_type_to_color_name(surface->surface_type), transparency);
|
|
|
|
if (show_labels) {
|
2016-10-16 21:05:47 +00:00
|
|
|
int idx = int(surface - this->surfaces.begin());
|
2016-09-30 13:23:18 +00:00
|
|
|
char label[64];
|
|
|
|
sprintf(label, "%d", idx);
|
|
|
|
svg.draw_text(surface->expolygon.contour.points.front(), label, "black");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export_surface_type_legend_to_svg(svg, legend_pos);
|
|
|
|
svg.Close();
|
|
|
|
}
|
|
|
|
|
2013-11-22 01:16:10 +00:00
|
|
|
}
|