Ported ExtrusionPath::Collection->chained_path
This commit is contained in:
parent
ea1d138c95
commit
bd7b0e2aed
11 changed files with 170 additions and 57 deletions
|
@ -2,24 +2,36 @@
|
|||
|
||||
namespace Slic3r {
|
||||
|
||||
ExtrusionPath*
|
||||
ExtrusionPath::clone() const
|
||||
{
|
||||
return new ExtrusionPath (*this);
|
||||
}
|
||||
|
||||
void
|
||||
ExtrusionPath::reverse()
|
||||
{
|
||||
this->polyline.reverse();
|
||||
}
|
||||
|
||||
const Point*
|
||||
ExtrusionPath::first_point() const
|
||||
Point*
|
||||
ExtrusionPath::first_point()
|
||||
{
|
||||
return &(this->polyline.points.front());
|
||||
}
|
||||
|
||||
const Point*
|
||||
ExtrusionPath::last_point() const
|
||||
Point*
|
||||
ExtrusionPath::last_point()
|
||||
{
|
||||
return &(this->polyline.points.back());
|
||||
}
|
||||
|
||||
ExtrusionLoop*
|
||||
ExtrusionLoop::clone() const
|
||||
{
|
||||
return new ExtrusionLoop (*this);
|
||||
}
|
||||
|
||||
ExtrusionPath*
|
||||
ExtrusionLoop::split_at_index(int index)
|
||||
{
|
||||
|
@ -47,4 +59,22 @@ ExtrusionLoop::make_counter_clockwise()
|
|||
return this->polygon.make_counter_clockwise();
|
||||
}
|
||||
|
||||
void
|
||||
ExtrusionLoop::reverse()
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
|
||||
Point*
|
||||
ExtrusionLoop::first_point()
|
||||
{
|
||||
return &(this->polygon.points.front());
|
||||
}
|
||||
|
||||
Point*
|
||||
ExtrusionLoop::last_point()
|
||||
{
|
||||
return &(this->polygon.points.front()); // in polygons, first == last
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,10 +25,14 @@ enum ExtrusionRole {
|
|||
class ExtrusionEntity
|
||||
{
|
||||
public:
|
||||
virtual ExtrusionEntity* clone() const = 0;
|
||||
virtual ~ExtrusionEntity() {};
|
||||
ExtrusionRole role;
|
||||
double height; // vertical thickness of the extrusion expressed in mm
|
||||
double flow_spacing;
|
||||
virtual void reverse() = 0;
|
||||
virtual Point* first_point() = 0;
|
||||
virtual Point* last_point() = 0;
|
||||
};
|
||||
|
||||
typedef std::vector<ExtrusionEntity*> ExtrusionEntitiesPtr;
|
||||
|
@ -36,19 +40,24 @@ typedef std::vector<ExtrusionEntity*> ExtrusionEntitiesPtr;
|
|||
class ExtrusionPath : public ExtrusionEntity
|
||||
{
|
||||
public:
|
||||
ExtrusionPath* clone() const;
|
||||
Polyline polyline;
|
||||
void reverse();
|
||||
const Point* first_point() const;
|
||||
const Point* last_point() const;
|
||||
Point* first_point();
|
||||
Point* last_point();
|
||||
};
|
||||
|
||||
class ExtrusionLoop : public ExtrusionEntity
|
||||
{
|
||||
public:
|
||||
ExtrusionLoop* clone() const;
|
||||
Polygon polygon;
|
||||
ExtrusionPath* split_at_index(int index);
|
||||
ExtrusionPath* split_at_first_point();
|
||||
bool make_counter_clockwise();
|
||||
void reverse();
|
||||
Point* first_point();
|
||||
Point* last_point();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
75
xs/src/ExtrusionEntityCollection.cpp
Normal file
75
xs/src/ExtrusionEntityCollection.cpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include "ExtrusionEntityCollection.hpp"
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
ExtrusionEntityCollection*
|
||||
ExtrusionEntityCollection::clone() const
|
||||
{
|
||||
return new ExtrusionEntityCollection (*this);
|
||||
}
|
||||
|
||||
void
|
||||
ExtrusionEntityCollection::reverse()
|
||||
{
|
||||
for (ExtrusionEntitiesPtr::iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
|
||||
(*it)->reverse();
|
||||
}
|
||||
std::reverse(this->entities.begin(), this->entities.end());
|
||||
}
|
||||
|
||||
Point*
|
||||
ExtrusionEntityCollection::first_point()
|
||||
{
|
||||
return this->entities.front()->first_point();
|
||||
}
|
||||
|
||||
Point*
|
||||
ExtrusionEntityCollection::last_point()
|
||||
{
|
||||
return this->entities.back()->last_point();
|
||||
}
|
||||
|
||||
ExtrusionEntityCollection*
|
||||
ExtrusionEntityCollection::chained_path(bool no_reverse) const
|
||||
{
|
||||
if (this->entities.empty()) {
|
||||
return new ExtrusionEntityCollection ();
|
||||
}
|
||||
return this->chained_path_from(this->entities.front()->first_point(), no_reverse);
|
||||
}
|
||||
|
||||
ExtrusionEntityCollection*
|
||||
ExtrusionEntityCollection::chained_path_from(Point* start_near, bool no_reverse) const
|
||||
{
|
||||
if (this->no_sort) return new ExtrusionEntityCollection(*this);
|
||||
ExtrusionEntityCollection* retval = new ExtrusionEntityCollection;
|
||||
ExtrusionEntitiesPtr my_paths = this->entities;
|
||||
|
||||
Points endpoints;
|
||||
for (ExtrusionEntitiesPtr::iterator it = my_paths.begin(); it != my_paths.end(); ++it) {
|
||||
endpoints.push_back(*(*it)->first_point());
|
||||
if (no_reverse) {
|
||||
endpoints.push_back(*(*it)->first_point());
|
||||
} else {
|
||||
endpoints.push_back(*(*it)->last_point());
|
||||
}
|
||||
}
|
||||
|
||||
while (!my_paths.empty()) {
|
||||
// find nearest point
|
||||
int start_index = start_near->nearest_point_index(endpoints);
|
||||
int path_index = start_index/2;
|
||||
if (start_index % 2 && !no_reverse) {
|
||||
my_paths.at(path_index) = my_paths.at(path_index)->clone(); // maybe we should clone them all when building my_paths?
|
||||
my_paths.at(path_index)->reverse();
|
||||
}
|
||||
retval->entities.push_back(my_paths.at(path_index));
|
||||
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();
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
}
|
|
@ -9,9 +9,15 @@ namespace Slic3r {
|
|||
class ExtrusionEntityCollection : public ExtrusionEntity
|
||||
{
|
||||
public:
|
||||
ExtrusionEntityCollection* clone() const;
|
||||
ExtrusionEntitiesPtr entities;
|
||||
bool no_sort;
|
||||
ExtrusionEntityCollection(): no_sort(false) {};
|
||||
ExtrusionEntityCollection* chained_path(bool no_reverse) const;
|
||||
ExtrusionEntityCollection* chained_path_from(Point* start_near, bool no_reverse) const;
|
||||
void reverse();
|
||||
Point* first_point();
|
||||
Point* last_point();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use strict;
|
|||
use warnings;
|
||||
|
||||
use Slic3r::XS;
|
||||
use Test::More tests => 9;
|
||||
use Test::More tests => 12;
|
||||
|
||||
my $points = [
|
||||
[100, 100],
|
||||
|
@ -41,5 +41,32 @@ isa_ok $collection->[3], 'Slic3r::ExtrusionLoop', 'correct object returned for l
|
|||
|
||||
is scalar(@{$collection->[1]}), 1, 'appended collection was duplicated';
|
||||
|
||||
{
|
||||
my $collection = Slic3r::ExtrusionPath::Collection->new(
|
||||
map Slic3r::ExtrusionPath->new(polyline => $_, role => 0),
|
||||
Slic3r::Polyline->new([0,15], [0,18], [0,20]),
|
||||
Slic3r::Polyline->new([0,10], [0,8], [0,5]),
|
||||
);
|
||||
is_deeply
|
||||
[ map $_->y, map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
|
||||
[20, 18, 15, 10, 8, 5],
|
||||
'chained_path_from';
|
||||
is_deeply
|
||||
[ map $_->y, map @{$_->polyline}, @{$collection->chained_path(0)} ],
|
||||
[15, 18, 20, 10, 8, 5],
|
||||
'chained_path';
|
||||
}
|
||||
|
||||
{
|
||||
my $collection = Slic3r::ExtrusionPath::Collection->new(
|
||||
map Slic3r::ExtrusionPath->new(polyline => $_, role => 0),
|
||||
Slic3r::Polyline->new([15,0], [10,0], [4,0]),
|
||||
Slic3r::Polyline->new([10,5], [15,5], [20,5]),
|
||||
);
|
||||
is_deeply
|
||||
[ map $_->x, map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
|
||||
[reverse 4, 10, 15, 10, 15, 20],
|
||||
'chained_path_from';
|
||||
}
|
||||
|
||||
__END__
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
~ExtrusionEntityCollection();
|
||||
void clear()
|
||||
%code{% THIS->entities.clear(); %};
|
||||
ExtrusionEntityCollection* chained_path(bool no_reverse)
|
||||
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->chained_path(no_reverse); %};
|
||||
ExtrusionEntityCollection* chained_path_from(Point* start_near, bool no_reverse)
|
||||
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->chained_path_from(start_near, no_reverse); %};
|
||||
%{
|
||||
|
||||
SV*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue