mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-07-20 05:54:25 +08:00
Rewrite xs/t/10_line.t to c++
This commit is contained in:
parent
87e9538993
commit
f78ab3e788
@ -2,6 +2,7 @@ get_filename_component(_TEST_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
|
|||||||
|
|
||||||
add_executable(${_TEST_NAME}_tests
|
add_executable(${_TEST_NAME}_tests
|
||||||
${_TEST_NAME}_tests.cpp
|
${_TEST_NAME}_tests.cpp
|
||||||
|
test_line.cpp
|
||||||
test_3mf.cpp
|
test_3mf.cpp
|
||||||
test_aabbindirect.cpp
|
test_aabbindirect.cpp
|
||||||
test_kdtreeindirect.cpp
|
test_kdtreeindirect.cpp
|
||||||
|
59
tests/libslic3r/test_line.cpp
Normal file
59
tests/libslic3r/test_line.cpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* Ported from xs/t/10_line.t
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
#include <libslic3r/Line.hpp>
|
||||||
|
#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});
|
||||||
|
}
|
@ -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__
|
|
Loading…
x
Reference in New Issue
Block a user