Implemented avoid_crossing_perimeters with VisiLibity

visilibity
Alessandro Ranellucci 2014-05-13 20:06:01 +02:00
parent a02a7f1a0f
commit 5fe5021fd7
19 changed files with 6216 additions and 13 deletions

View File

@ -111,8 +111,8 @@ sub change_layer {
$self->_layer_islands($layer->islands);
$self->_upper_layer_islands($layer->upper_layer ? $layer->upper_layer->islands : []);
if ($self->print_config->avoid_crossing_perimeters) {
$self->layer_mp(Slic3r::GCode::MotionPlanner->new(
islands => union_ex([ map @$_, @{$layer->slices} ], 1),
$self->layer_mp(Slic3r::MotionPlanner->new(
union_ex([ map @$_, @{$layer->slices} ], 1),
));
}

View File

@ -909,10 +909,7 @@ sub write_gcode {
}
}
}
$gcodegen->external_mp(Slic3r::GCode::MotionPlanner->new(
islands => union_ex([ map @$_, @islands ]),
internal => 0,
));
$gcodegen->external_mp(Slic3r::MotionPlanner->new(union_ex([ map @$_, @islands ])));
}
# calculate wiping points if needed

View File

@ -1676,6 +1676,8 @@ src/Line.cpp
src/Line.hpp
src/Model.cpp
src/Model.hpp
src/MotionPlanner.cpp
src/MotionPlanner.hpp
src/MultiPoint.cpp
src/MultiPoint.hpp
src/myinit.h
@ -1716,6 +1718,8 @@ src/SVG.hpp
src/TriangleMesh.cpp
src/TriangleMesh.hpp
src/utils.cpp
src/visilibity.cpp
src/visilibity.hpp
t/01_trianglemesh.t
t/03_point.t
t/04_expolygon.t

View File

@ -58,6 +58,14 @@ BoundingBox::polygon(Polygon* polygon) const
polygon->points[3].y = this->max.y;
}
Polygon
BoundingBox::polygon() const
{
Polygon p;
this->polygon(&p);
return p;
}
template <class PointClass> void
BoundingBoxBase<PointClass>::scale(double factor)
{

View File

@ -46,6 +46,7 @@ class BoundingBox : public BoundingBoxBase<Point>
{
public:
void polygon(Polygon* polygon) const;
Polygon polygon() const;
BoundingBox() {};
BoundingBox(const Points &points) : BoundingBoxBase<Point>(points) {};

View File

@ -84,11 +84,14 @@ ExPolygon::is_valid() const
bool
ExPolygon::contains_line(const Line &line) const
{
Polylines pl;
pl.push_back(line);
return this->contains_polyline(line);
}
bool
ExPolygon::contains_polyline(const Polyline &polyline) const
{
Polylines pl_out;
diff(pl, *this, pl_out);
diff((Polylines)polyline, *this, pl_out);
return pl_out.empty();
}

View File

@ -2,6 +2,7 @@
#define slic3r_ExPolygon_hpp_
#include "Polygon.hpp"
#include "Polyline.hpp"
#include <vector>
namespace Slic3r {
@ -22,6 +23,7 @@ class ExPolygon
double area() const;
bool is_valid() const;
bool contains_line(const Line &line) const;
bool contains_polyline(const Polyline &polyline) const;
bool contains_point(const Point &point) const;
Polygons simplify_p(double tolerance) const;
ExPolygons simplify(double tolerance) const;

View File

@ -3,6 +3,17 @@
namespace Slic3r {
ExPolygonCollection::operator Points() const
{
Points points;
Polygons pp = *this;
for (Polygons::const_iterator poly = pp.begin(); poly != pp.end(); ++poly) {
for (Points::const_iterator point = poly->points.begin(); point != poly->points.end(); ++point)
points.push_back(*point);
}
return points;
}
ExPolygonCollection::operator Polygons() const
{
Polygons polygons;
@ -15,6 +26,11 @@ ExPolygonCollection::operator Polygons() const
return polygons;
}
ExPolygonCollection::operator ExPolygons&()
{
return this->expolygons;
}
void
ExPolygonCollection::scale(double factor)
{

View File

@ -6,11 +6,19 @@
namespace Slic3r {
class ExPolygonCollection;
typedef std::vector<ExPolygonCollection> ExPolygonCollections;
class ExPolygonCollection
{
public:
ExPolygons expolygons;
ExPolygonCollection() {};
ExPolygonCollection(const ExPolygons &expolygons) : expolygons(expolygons) {};
operator Points() const;
operator Polygons() const;
operator ExPolygons&();
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, const Point &center);

181
xs/src/MotionPlanner.cpp Normal file
View File

@ -0,0 +1,181 @@
#include "BoundingBox.hpp"
#include "MotionPlanner.hpp"
namespace Slic3r {
MotionPlanner::MotionPlanner(const ExPolygons &islands)
: islands(islands), initialized(false)
{}
MotionPlanner::~MotionPlanner()
{
for (std::vector<VisiLibity::Environment*>::iterator env = this->envs.begin(); env != this->envs.end(); ++env)
delete *env;
for (std::vector<VisiLibity::Visibility_Graph*>::iterator graph = this->graphs.begin(); graph != this->graphs.end(); ++graph)
delete *graph;
}
void
MotionPlanner::initialize()
{
if (this->initialized) return;
// loop through islands in order to create inner expolygons and collect their contours
this->inner.reserve(this->islands.size());
Polygons outer_holes;
for (ExPolygons::const_iterator island = this->islands.begin(); island != this->islands.end(); ++island) {
this->inner.push_back(ExPolygonCollection());
offset_ex(*island, this->inner.back(), -MP_INNER_MARGIN);
outer_holes.push_back(island->contour);
}
// grow island contours in order to prepare holes of the outer environment
// This is actually wrong because it might merge contours that are close,
// thus confusing the island check in shortest_path() below
//offset(outer_holes, outer_holes, +MP_OUTER_MARGIN);
// generate outer contour as bounding box of everything
Points points;
for (Polygons::const_iterator contour = outer_holes.begin(); contour != outer_holes.end(); ++contour)
points.insert(points.end(), contour->points.begin(), contour->points.end());
BoundingBox bb(points);
// grow outer contour
Polygons contour;
offset(bb.polygon(), contour, +MP_OUTER_MARGIN);
assert(contour.size() == 1);
// make expolygon for outer environment
ExPolygons outer;
diff(contour, outer_holes, outer);
assert(outer.size() == 1);
this->outer = outer.front();
this->envs.resize(this->islands.size() + 1, NULL);
this->graphs.resize(this->islands.size() + 1, NULL);
this->initialized = true;
}
void
MotionPlanner::shortest_path(const Point &from, const Point &to, Polyline* polyline)
{
if (!this->initialized) this->initialize();
// Are both points in the same island?
int island_idx = -1;
for (ExPolygons::const_iterator island = this->islands.begin(); island != this->islands.end(); ++island) {
if (island->contains_point(from) && island->contains_point(to)) {
island_idx = island - this->islands.begin();
break;
}
}
// Generate environment.
this->generate_environment(island_idx);
// Now check whether points are inside the environment.
Point inner_from = from;
Point inner_to = to;
bool from_is_inside, to_is_inside;
if (island_idx == -1) {
if (!(from_is_inside = this->outer.contains_point(from))) {
// Find the closest inner point to start from.
from.nearest_point(this->outer, &inner_from);
}
if (!(to_is_inside = this->outer.contains_point(to))) {
// Find the closest inner point to start from.
to.nearest_point(this->outer, &inner_to);
}
} else {
if (!(from_is_inside = this->inner[island_idx].contains_point(from))) {
// Find the closest inner point to start from.
from.nearest_point(this->inner[island_idx], &inner_from);
}
if (!(to_is_inside = this->inner[island_idx].contains_point(to))) {
// Find the closest inner point to start from.
to.nearest_point(this->inner[island_idx], &inner_to);
}
}
// perform actual path search
*polyline = convert_polyline(this->envs[island_idx + 1]->shortest_path(convert_point(inner_from),
convert_point(inner_to), *this->graphs[island_idx + 1], SCALED_EPSILON));
// if start point was outside the environment, prepend it
if (!from_is_inside) polyline->points.insert(polyline->points.begin(), from);
// if end point was outside the environment, append it
if (!to_is_inside) polyline->points.push_back(to);
}
void
MotionPlanner::generate_environment(int island_idx)
{
if (this->envs[island_idx + 1] != NULL) return;
Polygons pp;
if (island_idx == -1) {
pp = this->outer;
} else {
pp = this->inner[island_idx];
}
// populate VisiLibity polygons
std::vector<VisiLibity::Polygon> v_polygons;
for (Polygons::const_iterator p = pp.begin(); p != pp.end(); ++p)
v_polygons.push_back(convert_polygon(*p));
// generate graph and environment
this->envs[island_idx + 1] = new VisiLibity::Environment(v_polygons);
this->graphs[island_idx + 1] = new VisiLibity::Visibility_Graph(*this->envs[island_idx + 1], SCALED_EPSILON);
}
VisiLibity::Polyline
MotionPlanner::convert_polyline(const Polyline &polyline)
{
VisiLibity::Polyline v_polyline;
for (Points::const_iterator point = polyline.points.begin(); point != polyline.points.end(); ++point) {
v_polyline.push_back(convert_point(*point));
}
return v_polyline;
}
Polyline
MotionPlanner::convert_polyline(const VisiLibity::Polyline &v_polyline)
{
Polyline polyline;
polyline.points.reserve(v_polyline.size());
for (size_t i = 0; i < v_polyline.size(); ++i) {
polyline.points.push_back(convert_point(v_polyline[i]));
}
return polyline;
}
VisiLibity::Polygon
MotionPlanner::convert_polygon(const Polygon &polygon)
{
VisiLibity::Polygon v_polygon;
for (Points::const_iterator point = polygon.points.begin(); point != polygon.points.end(); ++point) {
v_polygon.push_back(convert_point(*point));
}
return v_polygon;
}
VisiLibity::Point
MotionPlanner::convert_point(const Point &point)
{
return VisiLibity::Point(point.x, point.y);
}
Point
MotionPlanner::convert_point(const VisiLibity::Point &v_point)
{
return Point((coord_t)v_point.x(), (coord_t)v_point.y());
}
#ifdef SLIC3RXS
REGISTER_CLASS(MotionPlanner, "MotionPlanner");
#endif
}

42
xs/src/MotionPlanner.hpp Normal file
View File

@ -0,0 +1,42 @@
#ifndef slic3r_MotionPlanner_hpp_
#define slic3r_MotionPlanner_hpp_
#include <myinit.h>
#include "ClipperUtils.hpp"
#include "ExPolygonCollection.hpp"
#include "Polyline.hpp"
#include "visilibity.hpp"
#include <vector>
#define MP_INNER_MARGIN scale_(1.0)
#define MP_OUTER_MARGIN scale_(2.0)
namespace Slic3r {
class MotionPlanner
{
public:
MotionPlanner(const ExPolygons &islands);
~MotionPlanner();
void shortest_path(const Point &from, const Point &to, Polyline* polyline);
private:
ExPolygons islands;
bool initialized;
ExPolygon outer;
ExPolygonCollections inner;
std::vector<VisiLibity::Environment*> envs;
std::vector<VisiLibity::Visibility_Graph*> graphs;
void initialize();
void generate_environment(int island_idx);
static VisiLibity::Polyline convert_polyline(const Polyline &polyline);
static Polyline convert_polyline(const VisiLibity::Polyline &v_polyline);
static VisiLibity::Polygon convert_polygon(const Polygon &polygon);
static VisiLibity::Point convert_point(const Point &point);
static Point convert_point(const VisiLibity::Point &v_point);
};
}
#endif

View File

@ -30,9 +30,11 @@ Lines
Polyline::lines() const
{
Lines lines;
lines.reserve(this->points.size() - 1);
for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) {
lines.push_back(Line(*it, *(it + 1)));
if (this->points.size() >= 2) {
lines.reserve(this->points.size() - 1);
for (Points::const_iterator it = this->points.begin(); it != this->points.end()-1; ++it) {
lines.push_back(Line(*it, *(it + 1)));
}
}
return lines;
}

3659
xs/src/visilibity.cpp Normal file

File diff suppressed because it is too large Load Diff

2169
xs/src/visilibity.hpp Normal file

File diff suppressed because it is too large Load Diff

89
xs/t/18_motionplanner.t Normal file
View File

@ -0,0 +1,89 @@
#!/usr/bin/perl
use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 17;
my $square = Slic3r::Polygon->new( # ccw
[100, 100],
[200, 100],
[200, 200],
[100, 200],
);
my $hole_in_square = Slic3r::Polygon->new( # cw
[140, 140],
[140, 160],
[160, 160],
[160, 140],
);
$_->scale(1/0.000001) for $square, $hole_in_square;
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
{
my $mp = Slic3r::MotionPlanner->new([ $expolygon ]);
isa_ok $mp, 'Slic3r::MotionPlanner';
my $from = Slic3r::Point->new(120, 120);
my $to = Slic3r::Point->new(180,180);
$_->scale(1/0.000001) for $from, $to;
my $path = $mp->shortest_path($from, $to);
ok $path->is_valid(), 'return path is valid';
ok $path->length > Slic3r::Line->new($from, $to)->length, 'path length is greater than straight line';
ok $path->first_point->coincides_with($from), 'first path point coincides with initial point';
ok $path->last_point->coincides_with($to), 'last path point coincides with destination point';
ok $expolygon->contains_polyline($path), 'path is fully contained in expolygon';
}
{
my $mp = Slic3r::MotionPlanner->new([ $expolygon ]);
isa_ok $mp, 'Slic3r::MotionPlanner';
my $from = Slic3r::Point->new(80, 100);
my $to = Slic3r::Point->new(220,200);
$_->scale(1/0.000001) for $from, $to;
my $path = $mp->shortest_path($from, $to);
ok $path->is_valid(), 'return path is valid';
ok $path->length > Slic3r::Line->new($from, $to)->length, 'path length is greater than straight line';
ok $path->first_point->coincides_with($from), 'first path point coincides with initial point';
ok $path->last_point->coincides_with($to), 'last path point coincides with destination point';
is scalar(@{ Slic3r::Geometry::Clipper::intersection_pl([$path], [@$expolygon]) }), 0, 'path has no intersection with expolygon';
}
{
my $expolygon2 = $expolygon->clone;
$expolygon2->translate(300/0.000001, 0);
my $mp = Slic3r::MotionPlanner->new([ $expolygon, $expolygon2 ]);
isa_ok $mp, 'Slic3r::MotionPlanner';
my $from = Slic3r::Point->new(120, 120);
my $to = Slic3r::Point->new(120 + 300, 120);
$_->scale(1/0.000001) for $from, $to;
ok $expolygon->contains_point($from), 'start point is contained in first expolygon';
ok $expolygon2->contains_point($to), 'end point is contained in second expolygon';
my $path = $mp->shortest_path($from, $to);
ok $path->is_valid(), 'return path is valid';
ok $path->length > Slic3r::Line->new($from, $to)->length, 'path length is greater than straight line';
}
{
my $expolygons = [
Slic3r::ExPolygon->new([[123800962,89330311],[123959159,89699438],[124000004,89898430],[124000012,110116427],[123946510,110343065],[123767391,110701303],[123284087,111000001],[102585791,111000009],[102000004,110414223],[102000004,89585787],[102585790,89000000],[123300022,88999993]]),
Slic3r::ExPolygon->new([[97800954,89330311],[97959151,89699438],[97999996,89898430],[98000004,110116427],[97946502,110343065],[97767383,110701303],[97284079,111000001],[76585783,111000009],[75999996,110414223],[75999996,89585787],[76585782,89000000],[97300014,88999993]]),
];
my $mp = Slic3r::MotionPlanner->new($expolygons);
isa_ok $mp, 'Slic3r::MotionPlanner';
my $from = Slic3r::Point->new(79120520, 107839491);
my $to = Slic3r::Point->new(104664164, 108335852);
ok $expolygons->[1]->contains_point($from), 'start point is contained in second expolygon';
ok $expolygons->[0]->contains_point($to), 'end point is contained in first expolygon';
my $path = $mp->shortest_path($from, $to);
ok $path->is_valid(), 'return path is valid';
ok $path->length > Slic3r::Line->new($from, $to)->length, 'path length is greater than straight line';
}
__END__

View File

@ -23,6 +23,8 @@
bool is_valid();
bool contains_line(Line* line)
%code{% RETVAL = THIS->contains_line(*line); %};
bool contains_polyline(Polyline* polyline)
%code{% RETVAL = THIS->contains_polyline(*polyline); %};
bool contains_point(Point* point)
%code{% RETVAL = THIS->contains_point(*point); %};
ExPolygons simplify(double tolerance);

14
xs/xsp/MotionPlanner.xsp Normal file
View File

@ -0,0 +1,14 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "MotionPlanner.hpp"
%}
%name{Slic3r::MotionPlanner} class MotionPlanner {
MotionPlanner(ExPolygons islands);
~MotionPlanner();
Polyline* shortest_path(Point* from, Point* to)
%code%{ RETVAL = new Polyline(); THIS->shortest_path(*from, *to, RETVAL); %};
};

View File

@ -110,6 +110,9 @@ ModelInstance* O_OBJECT_SLIC3R
Ref<ModelInstance> O_OBJECT_SLIC3R_T
Clone<ModelInstance> O_OBJECT_SLIC3R_T
MotionPlanner* O_OBJECT_SLIC3R
Ref<MotionPlanner> O_OBJECT_SLIC3R_T
Clone<MotionPlanner> O_OBJECT_SLIC3R_T
ExtrusionRole T_UV
FlowRole T_UV

View File

@ -67,6 +67,9 @@
%typemap{PolylineCollection*};
%typemap{Ref<PolylineCollection>}{simple};
%typemap{Clone<PolylineCollection>}{simple};
%typemap{MotionPlanner*};
%typemap{Ref<MotionPlanner>}{simple};
%typemap{Clone<MotionPlanner>}{simple};
%typemap{Points};
%typemap{Pointfs};