Skip to content

Commit

Permalink
Merge pull request #3610 from alexrj/new-rectilinear
Browse files Browse the repository at this point in the history
New Rectilinear implementation
  • Loading branch information
alranel committed Dec 15, 2016
2 parents 3a3b24e + 3d2742e commit 43c62d4
Show file tree
Hide file tree
Showing 22 changed files with 628 additions and 1,998 deletions.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ add_library(libslic3r STATIC
${LIBDIR}/libslic3r/Fill/FillHoneycomb.cpp
${LIBDIR}/libslic3r/Fill/FillPlanePath.cpp
${LIBDIR}/libslic3r/Fill/FillRectilinear.cpp
${LIBDIR}/libslic3r/Fill/FillRectilinear2.cpp
${LIBDIR}/libslic3r/Flow.cpp
${LIBDIR}/libslic3r/GCode.cpp
${LIBDIR}/libslic3r/GCodeSender.cpp
Expand Down
69 changes: 66 additions & 3 deletions t/fill.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use Test::More;
use strict;
use warnings;

plan tests => 43;
plan tests => 92;

BEGIN {
use FindBin;
Expand All @@ -11,8 +11,8 @@ BEGIN {

use List::Util qw(first sum);
use Slic3r;
use Slic3r::Geometry qw(PI X Y scale unscale convex_hull);
use Slic3r::Geometry::Clipper qw(union diff diff_ex offset offset2_ex);
use Slic3r::Geometry qw(PI X Y scaled_epsilon scale unscale convex_hull);
use Slic3r::Geometry::Clipper qw(union diff diff_ex offset offset2_ex diff_pl);
use Slic3r::Surface qw(:types);
use Slic3r::Test;

Expand All @@ -26,6 +26,67 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
is $surface_width % $distance, 0, 'adjusted solid distance';
}

{
my $filler = Slic3r::Filler->new_from_type('rectilinear');
$filler->set_angle(-(PI)/2);
$filler->set_spacing(5);
$filler->set_dont_adjust(1);
$filler->set_endpoints_overlap(0);

my $test = sub {
my ($expolygon) = @_;
my $surface = Slic3r::Surface->new(
surface_type => S_TYPE_TOP,
expolygon => $expolygon,
);
return $filler->fill_surface($surface);
};

# square
$filler->set_density($filler->spacing / 50);
for my $i (0..3) {
# check that it works regardless of the points order
my @points = ([0,0], [100,0], [100,100], [0,100]);
@points = (@points[$i..$#points], @points[0..($i-1)]);
my $paths = $test->(my $e = Slic3r::ExPolygon->new([ scale_points @points ]));

is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
}

# diamond with endpoints on grid
{
my $paths = $test->(my $e = Slic3r::ExPolygon->new([ scale_points [0,0], [100,0], [150,50], [100,100], [0,100], [-50,50] ]));
is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
}

# square with hole
for my $angle (-(PI/2), -(PI/4), -(PI), PI/2, PI) {
for my $spacing (25, 5, 7.5, 8.5) {
$filler->set_density($filler->spacing / $spacing);
$filler->set_angle($angle);
my $paths = $test->(my $e = Slic3r::ExPolygon->new(
[ scale_points [0,0], [100,0], [100,100], [0,100] ],
[ scale_points reverse [25,25], [75,25], [75,75], [25,75] ],
));

if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"fill.svg",
no_arrows => 1,
expolygons => [$e],
polylines => $paths,
);
}

ok(@$paths >= 2 && @$paths <= 3, '2 or 3 continuous paths') or done_testing, exit;
ok(!@{diff_pl($paths->arrayref, offset(\@$e, +scaled_epsilon*10))},
'paths don\'t cross hole') or done_testing, exit;
}
}
}

{
my $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [50,0], [50,50], [0,50] ]);
my $filler = Slic3r::Filler->new_from_type('rectilinear');
Expand All @@ -41,6 +102,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
nozzle_diameter => 0.50,
);
$filler->set_spacing($flow->spacing);
$filler->set_density(1);
foreach my $angle (0, 45) {
$surface->expolygon->rotate(Slic3r::Geometry::deg2rad($angle), [0,0]);
my $paths = $filler->fill_surface($surface, layer_height => 0.4, density => 0.4);
Expand All @@ -55,6 +117,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
my $filler = Slic3r::Filler->new_from_type('rectilinear');
$filler->set_bounding_box($expolygon->bounding_box);
$filler->set_angle($angle // 0);
$filler->set_dont_adjust(0);
my $surface = Slic3r::Surface->new(
surface_type => S_TYPE_BOTTOM,
expolygon => $expolygon,
Expand Down
2 changes: 0 additions & 2 deletions xs/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ src/libslic3r/Fill/FillPlanePath.cpp
src/libslic3r/Fill/FillPlanePath.hpp
src/libslic3r/Fill/FillRectilinear.cpp
src/libslic3r/Fill/FillRectilinear.hpp
src/libslic3r/Fill/FillRectilinear2.cpp
src/libslic3r/Fill/FillRectilinear2.hpp
src/libslic3r/Flow.cpp
src/libslic3r/Flow.hpp
src/libslic3r/GCode.cpp
Expand Down
21 changes: 9 additions & 12 deletions xs/src/libslic3r/Fill/Fill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Fill3DHoneycomb.hpp"
#include "FillPlanePath.hpp"
#include "FillRectilinear.hpp"
#include "FillRectilinear2.hpp"

namespace Slic3r {

Expand All @@ -24,12 +23,9 @@ Fill::new_from_type(const InfillPattern type)
case ip3DHoneycomb: return new Fill3DHoneycomb();

case ipRectilinear: return new FillRectilinear();
case ipLine: return new FillLine();
case ipGrid: return new FillGrid();
case ipAlignedRectilinear: return new FillAlignedRectilinear();
case ipGrid: return new FillGrid();

case ipRectilinear2: return new FillRectilinear2();
case ipGrid2: return new FillGrid2();
case ipTriangles: return new FillTriangles();
case ipStars: return new FillStars();
case ipCubic: return new FillCubic();
Expand Down Expand Up @@ -80,11 +76,10 @@ Fill::adjust_solid_spacing(const coord_t width, const coord_t distance)
{
assert(width >= 0);
assert(distance > 0);
// floor(width / distance)
coord_t number_of_intervals = floor(width / distance);
coord_t distance_new = (number_of_intervals == 0)
? distance
: (width / number_of_intervals);
const int number_of_intervals = floor(width / distance);
if (number_of_intervals == 0) return distance;

coord_t distance_new = (width / number_of_intervals);

const coordf_t factor = coordf_t(distance_new) / coordf_t(distance);
assert(factor > 1. - 1e-5);
Expand All @@ -94,12 +89,14 @@ Fill::adjust_solid_spacing(const coord_t width, const coord_t distance)
if (factor > factor_max)
distance_new = floor((double)distance * factor_max + 0.5);

assert((distance_new * number_of_intervals) <= width);

return distance_new;
}

// Returns orientation of the infill and the reference point of the infill pattern.
// For a normal print, the reference point is the center of a bounding box of the STL.
std::pair<float, Point>
Fill::direction_t
Fill::_infill_direction(const Surface &surface) const
{
// set infill angle
Expand Down Expand Up @@ -133,7 +130,7 @@ Fill::_infill_direction(const Surface &surface) const
}

out_angle += float(M_PI/2.);
return std::pair<float, Point>(out_angle, out_shift);
return direction_t(out_angle, out_shift);
}

} // namespace Slic3r
10 changes: 8 additions & 2 deletions xs/src/libslic3r/Fill/Fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Fill
// in unscaled coordinates
coordf_t spacing;

// overlap over spacing for extrusion endpoints
float endpoints_overlap;

// in radians, ccw, 0 = East
float angle;

Expand Down Expand Up @@ -80,6 +83,7 @@ class Fill
layer_id(size_t(-1)),
z(0.f),
spacing(0.f),
endpoints_overlap(0.3f),
angle(0),
link_max_length(0),
loop_clipping(0),
Expand All @@ -89,10 +93,12 @@ class Fill
complete(false)
{};

typedef std::pair<float, Point> direction_t;

// The expolygon may be modified by the method to avoid a copy.
virtual void _fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out) {};

Expand All @@ -101,7 +107,7 @@ class Fill
return (idx % 2) == 0 ? (M_PI/2.) : 0;
};

std::pair<float, Point> _infill_direction(const Surface &surface) const;
direction_t _infill_direction(const Surface &surface) const;
};

} // namespace Slic3r
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/Fill3DHoneycomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ makeGrid(coord_t z, coord_t gridSize, size_t gridWidth, size_t gridHeight, size_
void
Fill3DHoneycomb::_fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out)
{
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/Fill3DHoneycomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Fill3DHoneycomb : public Fill
protected:
virtual void _fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out);
};
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/FillConcentric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Slic3r {
void
FillConcentric::_fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out)
{
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/FillConcentric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FillConcentric : public Fill
virtual Fill* clone() const { return new FillConcentric(*this); };
virtual void _fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out);

Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/FillHoneycomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Slic3r {
void
FillHoneycomb::_fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out)
{
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/FillHoneycomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FillHoneycomb : public Fill
virtual Fill* clone() const { return new FillHoneycomb(*this); };
virtual void _fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out
);
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/FillPlanePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Slic3r {

void FillPlanePath::_fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out)
{
Expand Down
2 changes: 1 addition & 1 deletion xs/src/libslic3r/Fill/FillPlanePath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FillPlanePath : public Fill
protected:
virtual void _fill_surface_single(
unsigned int thickness_layers,
const std::pair<float, Point> &direction,
const direction_t &direction,
ExPolygon &expolygon,
Polylines* polylines_out);

Expand Down
Loading

0 comments on commit 43c62d4

Please sign in to comment.