File: test_point.cpp

package info (click to toggle)
slic3r-prusa 2.9.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 196,524 kB
  • sloc: cpp: 534,736; ansic: 71,269; yacc: 1,311; makefile: 256; lex: 241; sh: 113
file content (55 lines) | stat: -rw-r--r-- 1,921 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/**
 * Ported from xs/t/03_point.t
 *  - it used to check ccw() but it does not exist anymore
 *  and cross product uses doubles
 */

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#include <libslic3r/Point.hpp>
#include "test_utils.hpp"

using namespace Slic3r;
using namespace Catch;

TEST_CASE("Nearest point", "[Point]") {
    const Point point{10, 15};
    const Point point2{30, 15};

    const Point nearest{nearest_point({point2, Point{100, 200}}, point).first};
    CHECK(nearest == point2);
}

TEST_CASE("Distance to line", "[Point]") {
    const Line line{{0, 0}, {100, 0}};
    CHECK(line.distance_to(Point{0, 0}) == Approx(0));
    CHECK(line.distance_to(Point{100, 0}) == Approx(0));
    CHECK(line.distance_to(Point{50, 0}) == Approx(0));
    CHECK(line.distance_to(Point{150, 0}) == Approx(50));
    CHECK(line.distance_to(Point{0, 50}) == Approx(50));
    CHECK(line.distance_to(Point{50, 50}) == Approx(50));
    CHECK(line.perp_distance_to(Point{50, 50}) == Approx(50));
    CHECK(line.perp_distance_to(Point{150, 50}) == Approx(50));
    
    // possitive values are on the left side WRT line direction
    CHECK(line.perp_signed_distance_to(Point{50, 50}) == Approx(50));
    CHECK(line.perp_signed_distance_to(Point{50, -50}) == Approx(-50));
    const Line line2{{0, 0}, {0, 100}};
    CHECK(line2.perp_signed_distance_to(Point{50, 50}) == Approx(-50));
    CHECK(line2.perp_signed_distance_to(Point{-50, 50}) == Approx(50));
}

TEST_CASE("Distance to diagonal line", "[Point]") {
    const Line line{{50, 50}, {125, -25}};
    CHECK_THAT(std::abs(line.distance_to(Point{100, 0})), Catch::Matchers::WithinAbs(0, 1e-6));
}

TEST_CASE("Perp distance to line does not overflow", "[Point]") {
    const Line line{
        {18335846, 18335845},
        {18335846, 1664160},
    };

    CHECK(line.distance_to(Point{1664161, 18335848}) == Approx(16671685));
}