diff --git a/tests/libslic3r/CMakeLists.txt b/tests/libslic3r/CMakeLists.txt index d10494461a..bb53fb9b1e 100644 --- a/tests/libslic3r/CMakeLists.txt +++ b/tests/libslic3r/CMakeLists.txt @@ -2,6 +2,7 @@ get_filename_component(_TEST_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) add_executable(${_TEST_NAME}_tests ${_TEST_NAME}_tests.cpp + test_line.cpp test_3mf.cpp test_aabbindirect.cpp test_kdtreeindirect.cpp diff --git a/tests/libslic3r/test_line.cpp b/tests/libslic3r/test_line.cpp new file mode 100644 index 0000000000..4a8edf224f --- /dev/null +++ b/tests/libslic3r/test_line.cpp @@ -0,0 +1,59 @@ +/** + * Ported from xs/t/10_line.t + */ + +#include +#include +#include "test_utils.hpp" + +using namespace Slic3r; + +TEST_CASE("Line can be translated", "[Line]") { + Line line{{100, 100}, {200, 100}}; + + line.translate(10, -5); + CHECK(Points{line.a, line.b} == Points{{110, 95}, {210, 95}}); +} + +TEST_CASE("Check if lines are parallel", "[Line]") { + CHECK(Line{{0, 0}, {100, 0}}.parallel_to(Line{{200, 200}, {0, 200}})); +} + +TEST_CASE("Parallel lines under angles", "[Line]") { + auto base_angle = GENERATE(0, M_PI/3, M_PI/2, M_PI); + + Line line{{0, 0}, {100, 0}}; + line.rotate(base_angle, {0, 0}); + Line clone{line}; + + INFO("Line is parallel to self"); + CHECK(line.parallel_to(clone)); + + clone.reverse(); + INFO("Line is parallel to self + PI"); + CHECK(line.parallel_to(clone)); + + INFO("Line is parallel to its direction"); + CHECK(line.parallel_to(line.direction())); + INFO("Line is parallel to its direction + PI"); + line.parallel_to(line.direction() + M_PI); + INFO("line is parallel to its direction - PI") + line.parallel_to(line.direction() - M_PI); + + SECTION("Line is parallel within epsilon") { + clone = line; + clone.rotate(EPSILON/2, {0, 0}); + CHECK(line.parallel_to(clone)); + clone = line; + clone.rotate(-EPSILON/2, {0, 0}); + CHECK(line.parallel_to(clone)); + } +} + +TEST_CASE("Intersection infinite", "[Line]") { + const Line a{{100, 0}, {200, 0}}; + const Line b{{300, 300}, {300, 100}}; + Point r; + a.intersection_infinite(b, &r); + CHECK(r == Point{300, 0}); +} diff --git a/xs/t/10_line.t b/xs/t/10_line.t deleted file mode 100644 index 886573f7b6..0000000000 --- a/xs/t/10_line.t +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Slic3r::XS; -use Test::More tests => 35; - -use constant PI => 4 * atan2(1, 1); -use constant EPSILON => 1E-4; - -my $points = [ - [100, 100], - [200, 100], -]; - -my $line = Slic3r::Line->new(@$points); - -{ - my $clone = $line->clone; - $clone->translate(10, -5); - is_deeply $clone->pp, [ - [110, 95], - [210, 95], - ], 'translate'; -} - -{ - ok +Slic3r::Line->new([0,0],[200,0])->parallel_to_line(Slic3r::Line->new([200,200],[0,200])), 'parallel_to'; -} - -foreach my $base_angle (0, PI/4, PI/2, PI) { - my $line = Slic3r::Line->new([0,0], [100,0]); - $line->rotate($base_angle, [0,0]); - my $clone = $line->clone; - ok $line->parallel_to_line($clone), 'line is parallel to self'; - $clone->reverse; - ok $line->parallel_to_line($clone), 'line is parallel to self + PI'; - ok $line->parallel_to($line->direction), 'line is parallel to its direction'; - ok $line->parallel_to($line->direction + PI), 'line is parallel to its direction + PI'; - ok $line->parallel_to($line->direction - PI), 'line is parallel to its direction - PI'; - { - my $line2 = $line->clone; - $line2->reverse; - ok $line->parallel_to_line($line2), 'line is parallel to its opposite'; - } - { - my $line2 = $line->clone; - $line2->rotate(+(EPSILON)/2, [0,0]); - ok $line->parallel_to_line($line2), 'line is parallel within epsilon'; - } - { - my $line2 = $line->clone; - $line2->rotate(-(EPSILON)/2, [0,0]); - ok $line->parallel_to_line($line2), 'line is parallel within epsilon'; - } -} - -{ - my $a = Slic3r::Line->new([100, 0], [200, 0]); - my $b = Slic3r::Line->new([300, 300], [300, 100]); - my $r = $a->intersection_infinite($b); - is_deeply $r->pp, [300, 0], 'intersection_infinite'; -} - -__END__