Ported various clipper unit tests from Perl to C++,
the clipper Perl unit tests were removed.
This commit is contained in:
parent
d70b45a51b
commit
f8dc74374c
@ -28,6 +28,8 @@ public:
|
|||||||
explicit ExPolygon(Polygon &&contour, Polygon &&hole) : contour(std::move(contour)) { holes.emplace_back(std::move(hole)); }
|
explicit ExPolygon(Polygon &&contour, Polygon &&hole) : contour(std::move(contour)) { holes.emplace_back(std::move(hole)); }
|
||||||
explicit ExPolygon(const Points &contour, const Points &hole) : contour(contour) { holes.emplace_back(hole); }
|
explicit ExPolygon(const Points &contour, const Points &hole) : contour(contour) { holes.emplace_back(hole); }
|
||||||
explicit ExPolygon(Points &&contour, Polygon &&hole) : contour(std::move(contour)) { holes.emplace_back(std::move(hole)); }
|
explicit ExPolygon(Points &&contour, Polygon &&hole) : contour(std::move(contour)) { holes.emplace_back(std::move(hole)); }
|
||||||
|
ExPolygon(std::initializer_list<Point> contour) : contour(contour) {}
|
||||||
|
ExPolygon(std::initializer_list<Point> contour, std::initializer_list<Point> hole) : contour(contour), holes({ hole }) {}
|
||||||
|
|
||||||
ExPolygon& operator=(const ExPolygon &other) { contour = other.contour; holes = other.holes; return *this; }
|
ExPolygon& operator=(const ExPolygon &other) { contour = other.contour; holes = other.holes; return *this; }
|
||||||
ExPolygon& operator=(ExPolygon &&other) { contour = std::move(other.contour); holes = std::move(other.holes); return *this; }
|
ExPolygon& operator=(ExPolygon &&other) { contour = std::move(other.contour); holes = std::move(other.holes); return *this; }
|
||||||
@ -77,6 +79,9 @@ public:
|
|||||||
Lines lines() const;
|
Lines lines() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline bool operator==(const ExPolygon &lhs, const ExPolygon &rhs) { return lhs.contour == rhs.contour && lhs.holes == rhs.holes; }
|
||||||
|
inline bool operator!=(const ExPolygon &lhs, const ExPolygon &rhs) { return lhs.contour != rhs.contour || lhs.holes != rhs.holes; }
|
||||||
|
|
||||||
// Count a nuber of polygons stored inside the vector of expolygons.
|
// Count a nuber of polygons stored inside the vector of expolygons.
|
||||||
// Useful for allocating space for polygons when converting expolygons to polygons.
|
// Useful for allocating space for polygons when converting expolygons to polygons.
|
||||||
inline size_t number_polygons(const ExPolygons &expolys)
|
inline size_t number_polygons(const ExPolygons &expolys)
|
||||||
|
@ -22,7 +22,8 @@ public:
|
|||||||
const Point& operator[](Points::size_type idx) const { return this->points[idx]; }
|
const Point& operator[](Points::size_type idx) const { return this->points[idx]; }
|
||||||
|
|
||||||
Polygon() {}
|
Polygon() {}
|
||||||
explicit Polygon(const Points &points): MultiPoint(points) {}
|
explicit Polygon(const Points &points) : MultiPoint(points) {}
|
||||||
|
Polygon(std::initializer_list<Point> points) : MultiPoint(points) {}
|
||||||
Polygon(const Polygon &other) : MultiPoint(other.points) {}
|
Polygon(const Polygon &other) : MultiPoint(other.points) {}
|
||||||
Polygon(Polygon &&other) : MultiPoint(std::move(other.points)) {}
|
Polygon(Polygon &&other) : MultiPoint(std::move(other.points)) {}
|
||||||
static Polygon new_scale(const std::vector<Vec2d> &points) {
|
static Polygon new_scale(const std::vector<Vec2d> &points) {
|
||||||
@ -66,6 +67,10 @@ public:
|
|||||||
Point point_projection(const Point &point) const;
|
Point point_projection(const Point &point) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline bool operator==(const Polygon &lhs, const Polygon &rhs) { return lhs.points == rhs.points; }
|
||||||
|
inline bool operator!=(const Polygon &lhs, const Polygon &rhs) { return lhs.points != rhs.points; }
|
||||||
|
|
||||||
|
|
||||||
extern BoundingBox get_extents(const Polygon &poly);
|
extern BoundingBox get_extents(const Polygon &poly);
|
||||||
extern BoundingBox get_extents(const Polygons &polygons);
|
extern BoundingBox get_extents(const Polygons &polygons);
|
||||||
extern BoundingBox get_extents_rotated(const Polygon &poly, double angle);
|
extern BoundingBox get_extents_rotated(const Polygon &poly, double angle);
|
||||||
|
89
t/clipper.t
89
t/clipper.t
@ -1,89 +0,0 @@
|
|||||||
use Test::More;
|
|
||||||
use strict;
|
|
||||||
use warnings;
|
|
||||||
|
|
||||||
plan tests => 6;
|
|
||||||
|
|
||||||
BEGIN {
|
|
||||||
use FindBin;
|
|
||||||
use lib "$FindBin::Bin/../lib";
|
|
||||||
use local::lib "$FindBin::Bin/../local-lib";
|
|
||||||
}
|
|
||||||
|
|
||||||
use List::Util qw(sum);
|
|
||||||
use Slic3r;
|
|
||||||
use Slic3r::Geometry::Clipper qw(intersection_ex union_ex diff_ex diff_pl);
|
|
||||||
|
|
||||||
{
|
|
||||||
my $square = [ # ccw
|
|
||||||
[10, 10],
|
|
||||||
[20, 10],
|
|
||||||
[20, 20],
|
|
||||||
[10, 20],
|
|
||||||
];
|
|
||||||
my $hole_in_square = [ # cw
|
|
||||||
[14, 14],
|
|
||||||
[14, 16],
|
|
||||||
[16, 16],
|
|
||||||
[16, 14],
|
|
||||||
];
|
|
||||||
my $square2 = [ # ccw
|
|
||||||
[5, 12],
|
|
||||||
[25, 12],
|
|
||||||
[25, 18],
|
|
||||||
[5, 18],
|
|
||||||
];
|
|
||||||
my $intersection = intersection_ex([ $square, $hole_in_square ], [ $square2 ]);
|
|
||||||
|
|
||||||
is sum(map $_->area, @$intersection), Slic3r::ExPolygon->new(
|
|
||||||
[
|
|
||||||
[20, 18],
|
|
||||||
[10, 18],
|
|
||||||
[10, 12],
|
|
||||||
[20, 12],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
[14, 16],
|
|
||||||
[16, 16],
|
|
||||||
[16, 14],
|
|
||||||
[14, 14],
|
|
||||||
],
|
|
||||||
)->area, 'hole is preserved after intersection';
|
|
||||||
}
|
|
||||||
|
|
||||||
#==========================================================
|
|
||||||
|
|
||||||
{
|
|
||||||
my $contour1 = [ [0,0], [40,0], [40,40], [0,40] ]; # ccw
|
|
||||||
my $contour2 = [ [10,10], [30,10], [30,30], [10,30] ]; # ccw
|
|
||||||
my $hole = [ [15,15], [15,25], [25,25], [25,15] ]; # cw
|
|
||||||
|
|
||||||
my $union = union_ex([ $contour1, $contour2, $hole ]);
|
|
||||||
|
|
||||||
is_deeply [ map $_->pp, @$union ], [[ [ [40,40], [0,40], [0,0], [40,0] ] ]],
|
|
||||||
'union of two ccw and one cw is a contour with no holes';
|
|
||||||
|
|
||||||
my $diff = diff_ex([ $contour1, $contour2 ], [ $hole ]);
|
|
||||||
is sum(map $_->area, @$diff),
|
|
||||||
Slic3r::ExPolygon->new([ [40,40], [0,40], [0,0], [40,0] ], [ [15,25], [25,25], [25,15], [15,15] ])->area,
|
|
||||||
'difference of a cw from two ccw is a contour with one hole';
|
|
||||||
}
|
|
||||||
|
|
||||||
#==========================================================
|
|
||||||
|
|
||||||
{
|
|
||||||
my $square = Slic3r::Polygon->new_scale( # ccw
|
|
||||||
[10, 10],
|
|
||||||
[20, 10],
|
|
||||||
[20, 20],
|
|
||||||
[10, 20],
|
|
||||||
);
|
|
||||||
my $square_pl = $square->split_at_first_point;
|
|
||||||
|
|
||||||
my $res = diff_pl([$square_pl], []);
|
|
||||||
is scalar(@$res), 1, 'no-op diff_pl returns the right number of polylines';
|
|
||||||
isa_ok $res->[0], 'Slic3r::Polyline', 'no-op diff_pl result';
|
|
||||||
is scalar(@{$res->[0]}), scalar(@$square_pl), 'no-op diff_pl returns the unmodified input polyline';
|
|
||||||
}
|
|
||||||
|
|
||||||
__END__
|
|
@ -3,6 +3,7 @@ add_executable(${_TEST_NAME}_tests
|
|||||||
${_TEST_NAME}_tests.cpp
|
${_TEST_NAME}_tests.cpp
|
||||||
test_3mf.cpp
|
test_3mf.cpp
|
||||||
test_clipper_offset.cpp
|
test_clipper_offset.cpp
|
||||||
|
test_clipper_utils.cpp
|
||||||
test_config.cpp
|
test_config.cpp
|
||||||
# test_elephant_foot_compensation.cpp
|
# test_elephant_foot_compensation.cpp
|
||||||
test_geometry.cpp
|
test_geometry.cpp
|
||||||
|
225
tests/libslic3r/test_clipper_utils.cpp
Normal file
225
tests/libslic3r/test_clipper_utils.cpp
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
#include <catch2/catch.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
|
#include "libslic3r/ClipperUtils.hpp"
|
||||||
|
#include "libslic3r/ExPolygon.hpp"
|
||||||
|
#include "libslic3r/SVG.hpp"
|
||||||
|
|
||||||
|
using namespace Slic3r;
|
||||||
|
|
||||||
|
SCENARIO("Various Clipper operations - xs/t/11_clipper.t", "[ClipperUtils]") {
|
||||||
|
// CCW oriented contour
|
||||||
|
Slic3r::Polygon square{ { 200, 100 }, {200, 200}, {100, 200}, {100, 100} };
|
||||||
|
// CW oriented contour
|
||||||
|
Slic3r::Polygon hole_in_square{ { 160, 140 }, { 140, 140 }, { 140, 160 }, { 160, 160 } };
|
||||||
|
Slic3r::ExPolygon square_with_hole(square, hole_in_square);
|
||||||
|
GIVEN("square_with_hole") {
|
||||||
|
WHEN("offset") {
|
||||||
|
Polygons result = Slic3r::offset(square_with_hole, 5.f);
|
||||||
|
THEN("offset matches") {
|
||||||
|
REQUIRE(result == Polygons {
|
||||||
|
{ { 205, 205 }, { 95, 205 }, { 95, 95 }, { 205, 95 }, },
|
||||||
|
{ { 145, 145 }, { 145, 155 }, { 155, 155 }, { 155, 145 } } });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WHEN("offset_ex") {
|
||||||
|
ExPolygons result = Slic3r::offset_ex(square_with_hole, 5.f);
|
||||||
|
THEN("offset matches") {
|
||||||
|
REQUIRE(result == ExPolygons { {
|
||||||
|
{ { 205, 205 }, { 95, 205 }, { 95, 95 }, { 205, 95 }, },
|
||||||
|
{ { 145, 145 }, { 145, 155 }, { 155, 155 }, { 155, 145 } } } } );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WHEN("offset2_ex") {
|
||||||
|
ExPolygons result = Slic3r::offset2_ex(square_with_hole, 5.f, -2.f);
|
||||||
|
THEN("offset matches") {
|
||||||
|
REQUIRE(result == ExPolygons { {
|
||||||
|
{ { 203, 203 }, { 97, 203 }, { 97, 97 }, { 203, 97 } },
|
||||||
|
{ { 143, 143 }, { 143, 157 }, { 157, 157 }, { 157, 143 } } } } );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("square_with_hole 2") {
|
||||||
|
Slic3r::ExPolygon square_with_hole(
|
||||||
|
{ { 20000000, 20000000 }, { 0, 20000000 }, { 0, 0 }, { 20000000, 0 } },
|
||||||
|
{ { 5000000, 15000000 }, { 15000000, 15000000 }, { 15000000, 5000000 }, { 5000000, 5000000 } });
|
||||||
|
WHEN("offset2_ex") {
|
||||||
|
Slic3r::ExPolygons result = Slic3r::offset2_ex(ExPolygons { square_with_hole }, -1.f, 1.f);
|
||||||
|
THEN("offset matches") {
|
||||||
|
REQUIRE(result.size() == 1);
|
||||||
|
REQUIRE(square_with_hole.area() == result.front().area());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("square and hole") {
|
||||||
|
WHEN("diff_ex") {
|
||||||
|
ExPolygons result = Slic3r::diff_ex({ square }, { hole_in_square });
|
||||||
|
THEN("hole is created") {
|
||||||
|
REQUIRE(result.size() == 1);
|
||||||
|
REQUIRE(square_with_hole.area() == result.front().area());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("polyline") {
|
||||||
|
Polyline polyline { { 50, 150 }, { 300, 150 } };
|
||||||
|
WHEN("intersection_pl") {
|
||||||
|
Polylines result = Slic3r::intersection_pl({ polyline }, { square, hole_in_square });
|
||||||
|
THEN("correct number of result lines") {
|
||||||
|
REQUIRE(result.size() == 2);
|
||||||
|
}
|
||||||
|
THEN("result lines have correct length") {
|
||||||
|
// results are in no particular order
|
||||||
|
REQUIRE(result[0].length() == 40);
|
||||||
|
REQUIRE(result[1].length() == 40);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WHEN("diff_pl") {
|
||||||
|
Polylines result = Slic3r::diff_pl({ polyline }, { square, hole_in_square });
|
||||||
|
THEN("correct number of result lines") {
|
||||||
|
REQUIRE(result.size() == 3);
|
||||||
|
}
|
||||||
|
// results are in no particular order
|
||||||
|
THEN("the left result line has correct length") {
|
||||||
|
REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 50; }) == 1);
|
||||||
|
}
|
||||||
|
THEN("the right result line has correct length") {
|
||||||
|
REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 100; }) == 1);
|
||||||
|
}
|
||||||
|
THEN("the central result line has correct length") {
|
||||||
|
REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 20; }) == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("Clipper bug #96 / Slic3r issue #2028") {
|
||||||
|
Slic3r::Polyline subject{
|
||||||
|
{ 44735000, 31936670 }, { 55270000, 31936670 }, { 55270000, 25270000 }, { 74730000, 25270000 }, { 74730000, 44730000 }, { 68063296, 44730000 }, { 68063296, 55270000 }, { 74730000, 55270000 },
|
||||||
|
{ 74730000, 74730000 }, { 55270000, 74730000 }, { 55270000, 68063296 }, { 44730000, 68063296 }, { 44730000, 74730000 }, { 25270000, 74730000 }, { 25270000, 55270000 }, { 31936670, 55270000 },
|
||||||
|
{ 31936670, 44730000 }, { 25270000, 44730000 }, { 25270000, 25270000 }, { 44730000, 25270000 }, { 44730000, 31936670 } };
|
||||||
|
Slic3r::Polygon clip { {75200000, 45200000}, {54800000, 45200000}, {54800000, 24800000}, {75200000, 24800000} };
|
||||||
|
Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, { clip });
|
||||||
|
THEN("intersection_pl - result is not empty") {
|
||||||
|
REQUIRE(result.size() == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("Clipper bug #122") {
|
||||||
|
Slic3r::Polyline subject { { 1975, 1975 }, { 25, 1975 }, { 25, 25 }, { 1975, 25 }, { 1975, 1975 } };
|
||||||
|
Slic3r::Polygons clip { { { 2025, 2025 }, { -25, 2025 } , { -25, -25 }, { 2025, -25 } },
|
||||||
|
{ { 525, 525 }, { 525, 1475 }, { 1475, 1475 }, { 1475, 525 } } };
|
||||||
|
Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, clip);
|
||||||
|
THEN("intersection_pl - result is not empty") {
|
||||||
|
REQUIRE(result.size() == 1);
|
||||||
|
REQUIRE(result.front().points.size() == 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("Clipper bug #126") {
|
||||||
|
Slic3r::Polyline subject { { 200000, 19799999 }, { 200000, 200000 }, { 24304692, 200000 }, { 15102879, 17506106 }, { 13883200, 19799999 }, { 200000, 19799999 } };
|
||||||
|
Slic3r::Polygon clip { { 15257205, 18493894 }, { 14350057, 20200000 }, { -200000, 20200000 }, { -200000, -200000 }, { 25196917, -200000 } };
|
||||||
|
Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, { clip });
|
||||||
|
THEN("intersection_pl - result is not empty") {
|
||||||
|
REQUIRE(result.size() == 1);
|
||||||
|
}
|
||||||
|
THEN("intersection_pl - result has same length as subject polyline") {
|
||||||
|
REQUIRE(result.front().length() == Approx(subject.length()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
{
|
||||||
|
# Clipper does not preserve polyline orientation
|
||||||
|
my $polyline = Slic3r::Polyline->new([50, 150], [300, 150]);
|
||||||
|
my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
|
||||||
|
is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
|
||||||
|
is_deeply $result->[0]->pp, [[100, 150], [200, 150]], 'clipped line orientation is preserved';
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# Clipper does not preserve polyline orientation
|
||||||
|
my $polyline = Slic3r::Polyline->new([300, 150], [50, 150]);
|
||||||
|
my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
|
||||||
|
is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
|
||||||
|
is_deeply $result->[0]->pp, [[200, 150], [100, 150]], 'clipped line orientation is preserved';
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# Disabled until Clipper bug #127 is fixed
|
||||||
|
my $subject = [
|
||||||
|
Slic3r::Polyline->new([-90000000, -100000000], [-90000000, 100000000]), # vertical
|
||||||
|
Slic3r::Polyline->new([-100000000, -10000000], [100000000, -10000000]), # horizontal
|
||||||
|
Slic3r::Polyline->new([-100000000, 0], [100000000, 0]), # horizontal
|
||||||
|
Slic3r::Polyline->new([-100000000, 10000000], [100000000, 10000000]), # horizontal
|
||||||
|
];
|
||||||
|
my $clip = Slic3r::Polygon->new(# a circular, convex, polygon
|
||||||
|
[99452190, 10452846], [97814760, 20791169], [95105652, 30901699], [91354546, 40673664], [86602540, 50000000],
|
||||||
|
[80901699, 58778525], [74314483, 66913061], [66913061, 74314483], [58778525, 80901699], [50000000, 86602540],
|
||||||
|
[40673664, 91354546], [30901699, 95105652], [20791169, 97814760], [10452846, 99452190], [0, 100000000],
|
||||||
|
[-10452846, 99452190], [-20791169, 97814760], [-30901699, 95105652], [-40673664, 91354546],
|
||||||
|
[-50000000, 86602540], [-58778525, 80901699], [-66913061, 74314483], [-74314483, 66913061],
|
||||||
|
[-80901699, 58778525], [-86602540, 50000000], [-91354546, 40673664], [-95105652, 30901699],
|
||||||
|
[-97814760, 20791169], [-99452190, 10452846], [-100000000, 0], [-99452190, -10452846],
|
||||||
|
[-97814760, -20791169], [-95105652, -30901699], [-91354546, -40673664], [-86602540, -50000000],
|
||||||
|
[-80901699, -58778525], [-74314483, -66913061], [-66913061, -74314483], [-58778525, -80901699],
|
||||||
|
[-50000000, -86602540], [-40673664, -91354546], [-30901699, -95105652], [-20791169, -97814760],
|
||||||
|
[-10452846, -99452190], [0, -100000000], [10452846, -99452190], [20791169, -97814760],
|
||||||
|
[30901699, -95105652], [40673664, -91354546], [50000000, -86602540], [58778525, -80901699],
|
||||||
|
[66913061, -74314483], [74314483, -66913061], [80901699, -58778525], [86602540, -50000000],
|
||||||
|
[91354546, -40673664], [95105652, -30901699], [97814760, -20791169], [99452190, -10452846], [100000000, 0]
|
||||||
|
);
|
||||||
|
my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, [$clip]);
|
||||||
|
is scalar(@$result), scalar(@$subject), 'intersection_pl - expected number of polylines';
|
||||||
|
is sum(map scalar(@$_), @$result), scalar(@$subject) * 2, 'intersection_pl - expected number of points in polylines';
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SCENARIO("Various Clipper operations - t/clipper.t", "[ClipperUtils]") {
|
||||||
|
GIVEN("square with hole") {
|
||||||
|
// CCW oriented contour
|
||||||
|
Slic3r::Polygon square { { 10, 10 }, { 20, 10 }, { 20, 20 }, { 10, 20 } };
|
||||||
|
Slic3r::Polygon square2 { { 5, 12 }, { 25, 12 }, { 25, 18 }, { 5, 18 } };
|
||||||
|
// CW oriented contour
|
||||||
|
Slic3r::Polygon hole_in_square { { 14, 14 }, { 14, 16 }, { 16, 16 }, { 16, 14 } };
|
||||||
|
WHEN("intersection_ex with another square") {
|
||||||
|
ExPolygons intersection = Slic3r::intersection_ex({ square, hole_in_square }, { square2 });
|
||||||
|
THEN("intersection area matches (hole is preserved)") {
|
||||||
|
ExPolygon match({ { 20, 18 }, { 10, 18 }, { 10, 12 }, { 20, 12 } },
|
||||||
|
{ { 14, 16 }, { 16, 16 }, { 16, 14 }, { 14, 14 } });
|
||||||
|
REQUIRE(intersection.size() == 1);
|
||||||
|
REQUIRE(intersection.front().area() == Approx(match.area()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("square with hole 2") {
|
||||||
|
// CCW oriented contour
|
||||||
|
Slic3r::Polygon square { { 0, 0 }, { 40, 0 }, { 40, 40 }, { 0, 40 } };
|
||||||
|
Slic3r::Polygon square2 { { 10, 10 }, { 30, 10 }, { 30, 30 }, { 10, 30 } };
|
||||||
|
// CW oriented contour
|
||||||
|
Slic3r::Polygon hole { { 15, 15 }, { 15, 25 }, { 25, 25 }, {25, 15 } };
|
||||||
|
WHEN("union_ex with another square") {
|
||||||
|
ExPolygons union_ = Slic3r::union_ex({ square, square2, hole });
|
||||||
|
THEN("union of two ccw and one cw is a contour with no holes") {
|
||||||
|
REQUIRE(union_.size() == 1);
|
||||||
|
REQUIRE(union_.front() == ExPolygon { { 40, 40 }, { 0, 40 }, { 0, 0 }, { 40, 0 } } );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
WHEN("diff_ex with another square") {
|
||||||
|
ExPolygons diff = Slic3r::diff_ex({ square, square2 }, { hole });
|
||||||
|
THEN("difference of a cw from two ccw is a contour with one hole") {
|
||||||
|
REQUIRE(diff.size() == 1);
|
||||||
|
REQUIRE(diff.front().area() == Approx(ExPolygon({ {40, 40}, {0, 40}, {0, 0}, {40, 0} }, { {15, 25}, {25, 25}, {25, 15}, {15, 15} }).area()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GIVEN("yet another square") {
|
||||||
|
Slic3r::Polygon square { { 10, 10 }, { 20, 10 }, { 20, 20 }, { 10, 20 } };
|
||||||
|
Slic3r::Polyline square_pl = square.split_at_first_point();
|
||||||
|
WHEN("no-op diff_pl") {
|
||||||
|
Slic3r::Polylines res = Slic3r::diff_pl({ square_pl }, {});
|
||||||
|
THEN("returns the right number of polylines") {
|
||||||
|
REQUIRE(res.size() == 1);
|
||||||
|
}
|
||||||
|
THEN("returns the unmodified input polyline") {
|
||||||
|
REQUIRE(res.front().points.size() == square_pl.points.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,193 +0,0 @@
|
|||||||
#!/usr/bin/perl
|
|
||||||
|
|
||||||
use strict;
|
|
||||||
use warnings;
|
|
||||||
|
|
||||||
use List::Util qw(sum);
|
|
||||||
use Slic3r::XS;
|
|
||||||
use Test::More tests => 16;
|
|
||||||
|
|
||||||
my $square = Slic3r::Polygon->new( # ccw
|
|
||||||
[200, 100],
|
|
||||||
[200, 200],
|
|
||||||
[100, 200],
|
|
||||||
[100, 100],
|
|
||||||
);
|
|
||||||
my $hole_in_square = Slic3r::Polygon->new( # cw
|
|
||||||
[160, 140],
|
|
||||||
[140, 140],
|
|
||||||
[140, 160],
|
|
||||||
[160, 160],
|
|
||||||
);
|
|
||||||
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
|
|
||||||
|
|
||||||
{
|
|
||||||
my $result = Slic3r::Geometry::Clipper::offset([ $square, $hole_in_square ], 5);
|
|
||||||
is_deeply [ map $_->pp, @$result ], [ [
|
|
||||||
[205, 205],
|
|
||||||
[95, 205],
|
|
||||||
[95, 95],
|
|
||||||
[205, 95],
|
|
||||||
], [
|
|
||||||
[145, 145],
|
|
||||||
[145, 155],
|
|
||||||
[155, 155],
|
|
||||||
[155, 145],
|
|
||||||
] ], 'offset';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
my $result = Slic3r::Geometry::Clipper::offset_ex([ @$expolygon ], 5);
|
|
||||||
is_deeply $result->[0]->pp, [ [
|
|
||||||
[205, 205],
|
|
||||||
[95, 205],
|
|
||||||
[95, 95],
|
|
||||||
[205, 95],
|
|
||||||
], [
|
|
||||||
[145, 145],
|
|
||||||
[145, 155],
|
|
||||||
[155, 155],
|
|
||||||
[155, 145],
|
|
||||||
] ], 'offset_ex';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
my $result = Slic3r::Geometry::Clipper::offset2_ex([ @$expolygon ], 5, -2);
|
|
||||||
is_deeply $result->[0]->pp, [ [
|
|
||||||
[203, 203],
|
|
||||||
[97, 203],
|
|
||||||
[97, 97],
|
|
||||||
[203, 97],
|
|
||||||
], [
|
|
||||||
[143, 143],
|
|
||||||
[143, 157],
|
|
||||||
[157, 157],
|
|
||||||
[157, 143],
|
|
||||||
] ], 'offset2_ex';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
my $expolygon2 = Slic3r::ExPolygon->new([
|
|
||||||
[20000000, 20000000],
|
|
||||||
[0, 20000000],
|
|
||||||
[0, 0],
|
|
||||||
[20000000, 0],
|
|
||||||
], [
|
|
||||||
[5000000, 15000000],
|
|
||||||
[15000000, 15000000],
|
|
||||||
[15000000, 5000000],
|
|
||||||
[5000000, 5000000],
|
|
||||||
]);
|
|
||||||
my $result = Slic3r::Geometry::Clipper::offset2_ex([ @$expolygon2 ], -1, +1);
|
|
||||||
is $result->[0]->area, $expolygon2->area, 'offset2_ex';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
my $polygon1 = Slic3r::Polygon->new(@$square);
|
|
||||||
my $polygon2 = Slic3r::Polygon->new(reverse @$hole_in_square);
|
|
||||||
my $result = Slic3r::Geometry::Clipper::diff_ex([$polygon1], [$polygon2]);
|
|
||||||
is $result->[0]->area, $expolygon->area, 'diff_ex';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
my $polyline = Slic3r::Polyline->new([50,150], [300,150]);
|
|
||||||
{
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square, $hole_in_square]);
|
|
||||||
is scalar(@$result), 2, 'intersection_pl - correct number of result lines';
|
|
||||||
# results are in no particular order
|
|
||||||
is scalar(grep $_->length == 40, @$result), 2, 'intersection_pl - result lines have correct length';
|
|
||||||
}
|
|
||||||
{
|
|
||||||
my $result = Slic3r::Geometry::Clipper::diff_pl([$polyline], [$square, $hole_in_square]);
|
|
||||||
is scalar(@$result), 3, 'diff_pl - correct number of result lines';
|
|
||||||
# results are in no particular order
|
|
||||||
is scalar(grep $_->length == 50, @$result), 1, 'diff_pl - the left result line has correct length';
|
|
||||||
is scalar(grep $_->length == 100, @$result), 1, 'diff_pl - two right result line has correct length';
|
|
||||||
is scalar(grep $_->length == 20, @$result), 1, 'diff_pl - the central result line has correct length';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (0) { # Clipper does not preserve polyline orientation
|
|
||||||
my $polyline = Slic3r::Polyline->new([50,150], [300,150]);
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
|
|
||||||
is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
|
|
||||||
is_deeply $result->[0]->pp, [[100,150], [200,150]], 'clipped line orientation is preserved';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (0) { # Clipper does not preserve polyline orientation
|
|
||||||
my $polyline = Slic3r::Polyline->new([300,150], [50,150]);
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
|
|
||||||
is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
|
|
||||||
is_deeply $result->[0]->pp, [[200,150], [100,150]], 'clipped line orientation is preserved';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
# Clipper bug #96 (our issue #2028)
|
|
||||||
my $subject = Slic3r::Polyline->new(
|
|
||||||
[44735000,31936670],[55270000,31936670],[55270000,25270000],[74730000,25270000],[74730000,44730000],[68063296,44730000],[68063296,55270000],[74730000,55270000],[74730000,74730000],[55270000,74730000],[55270000,68063296],[44730000,68063296],[44730000,74730000],[25270000,74730000],[25270000,55270000],[31936670,55270000],[31936670,44730000],[25270000,44730000],[25270000,25270000],[44730000,25270000],[44730000,31936670]
|
|
||||||
);
|
|
||||||
my $clip = [
|
|
||||||
Slic3r::Polygon->new([75200000,45200000],[54800000,45200000],[54800000,24800000],[75200000,24800000]),
|
|
||||||
];
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl([$subject], $clip);
|
|
||||||
is scalar(@$result), 1, 'intersection_pl - result is not empty';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
# Clipper bug #122
|
|
||||||
my $subject = [
|
|
||||||
Slic3r::Polyline->new([1975,1975],[25,1975],[25,25],[1975,25],[1975,1975]),
|
|
||||||
];
|
|
||||||
my $clip = [
|
|
||||||
Slic3r::Polygon->new([2025,2025],[-25,2025],[-25,-25],[2025,-25]),
|
|
||||||
Slic3r::Polygon->new([525,525],[525,1475],[1475,1475],[1475,525]),
|
|
||||||
];
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, $clip);
|
|
||||||
is scalar(@$result), 1, 'intersection_pl - result is not empty';
|
|
||||||
is scalar(@{$result->[0]}), 5, 'intersection_pl - result is not empty';
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
# Clipper bug #126
|
|
||||||
my $subject = Slic3r::Polyline->new(
|
|
||||||
[200000,19799999],[200000,200000],[24304692,200000],[15102879,17506106],[13883200,19799999],[200000,19799999],
|
|
||||||
);
|
|
||||||
my $clip = [
|
|
||||||
Slic3r::Polygon->new([15257205,18493894],[14350057,20200000],[-200000,20200000],[-200000,-200000],[25196917,-200000]),
|
|
||||||
];
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl([$subject], $clip);
|
|
||||||
is scalar(@$result), 1, 'intersection_pl - result is not empty';
|
|
||||||
is $result->[0]->length, $subject->length, 'intersection_pl - result has same length as subject polyline';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (0) {
|
|
||||||
# Disabled until Clipper bug #127 is fixed
|
|
||||||
my $subject = [
|
|
||||||
Slic3r::Polyline->new([-90000000,-100000000],[-90000000,100000000]), # vertical
|
|
||||||
Slic3r::Polyline->new([-100000000,-10000000],[100000000,-10000000]), # horizontal
|
|
||||||
Slic3r::Polyline->new([-100000000,0],[100000000,0]), # horizontal
|
|
||||||
Slic3r::Polyline->new([-100000000,10000000],[100000000,10000000]), # horizontal
|
|
||||||
];
|
|
||||||
my $clip = Slic3r::Polygon->new( # a circular, convex, polygon
|
|
||||||
[99452190,10452846],[97814760,20791169],[95105652,30901699],[91354546,40673664],[86602540,50000000],
|
|
||||||
[80901699,58778525],[74314483,66913061],[66913061,74314483],[58778525,80901699],[50000000,86602540],
|
|
||||||
[40673664,91354546],[30901699,95105652],[20791169,97814760],[10452846,99452190],[0,100000000],
|
|
||||||
[-10452846,99452190],[-20791169,97814760],[-30901699,95105652],[-40673664,91354546],
|
|
||||||
[-50000000,86602540],[-58778525,80901699],[-66913061,74314483],[-74314483,66913061],
|
|
||||||
[-80901699,58778525],[-86602540,50000000],[-91354546,40673664],[-95105652,30901699],
|
|
||||||
[-97814760,20791169],[-99452190,10452846],[-100000000,0],[-99452190,-10452846],
|
|
||||||
[-97814760,-20791169],[-95105652,-30901699],[-91354546,-40673664],[-86602540,-50000000],
|
|
||||||
[-80901699,-58778525],[-74314483,-66913061],[-66913061,-74314483],[-58778525,-80901699],
|
|
||||||
[-50000000,-86602540],[-40673664,-91354546],[-30901699,-95105652],[-20791169,-97814760],
|
|
||||||
[-10452846,-99452190],[0,-100000000],[10452846,-99452190],[20791169,-97814760],
|
|
||||||
[30901699,-95105652],[40673664,-91354546],[50000000,-86602540],[58778525,-80901699],
|
|
||||||
[66913061,-74314483],[74314483,-66913061],[80901699,-58778525],[86602540,-50000000],
|
|
||||||
[91354546,-40673664],[95105652,-30901699],[97814760,-20791169],[99452190,-10452846],[100000000,0]
|
|
||||||
);
|
|
||||||
my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, [$clip]);
|
|
||||||
is scalar(@$result), scalar(@$subject), 'intersection_pl - expected number of polylines';
|
|
||||||
is sum(map scalar(@$_), @$result), scalar(@$subject)*2,
|
|
||||||
'intersection_pl - expected number of points in polylines';
|
|
||||||
}
|
|
||||||
|
|
||||||
__END__
|
|
Loading…
Reference in New Issue
Block a user