Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_curve_fitting.cpp « libslic3r « tests - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: faf7839c7794efd5be5b15442de4eea4299e028d (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <catch2/catch.hpp>
#include <test_utils.hpp>

#include <libslic3r/Geometry/Curves.hpp>
#include <libslic3r/Utils.hpp>
#include <libslic3r/SVG.hpp>

TEST_CASE("Curves: cubic b spline fit test", "[Curves]") {
    using namespace Slic3r;
    using namespace Slic3r::Geometry;

    auto fx = [&](size_t index) {
        return float(index) / 200.0f;
    };

    auto fy = [&](size_t index) {
        return 1.0f;
    };

    std::vector<Vec<1, float>> observations { };
    std::vector<float> observation_points { };
    std::vector<float> weights { };
    for (size_t index = 0; index < 200; ++index) {
        observations.push_back(Vec<1, float> { fy(index) });
        observation_points.push_back(fx(index));
        weights.push_back(1);
    }

    Vec2f fmin { fx(0), fy(0) };
    Vec2f fmax { fx(200), fy(200) };

    auto bspline = fit_cubic_bspline(observations, observation_points, weights, 1);

    Approx ap(1.0f);
    ap.epsilon(0.1f);

    for (int p = 0; p < 200; ++p) {
        float fitted_val = bspline.get_fitted_value(fx(p))(0);
        float expected = fy(p);

        REQUIRE(fitted_val == ap(expected));

    }
}

TEST_CASE("Curves: quadratic f cubic b spline fit test", "[Curves]") {
    using namespace Slic3r;
    using namespace Slic3r::Geometry;

    auto fx = [&](size_t index) {
        return float(index) / 100.0f;
    };

    auto fy = [&](size_t index) {
        return (fx(index) - 1) * (fx(index) - 1);
    };

    std::vector<Vec<1, float>> observations { };
    std::vector<float> observation_points { };
    std::vector<float> weights { };
    for (size_t index = 0; index < 200; ++index) {
        observations.push_back(Vec<1, float> { fy(index) });
        observation_points.push_back(fx(index));
        weights.push_back(1);
    }

    Vec2f fmin { fx(0), fy(0) };
    Vec2f fmax { fx(200), fy(200) };

    auto bspline = fit_cubic_bspline(observations, observation_points, weights, 10);

    for (int p = 0; p < 200; ++p) {
        float fitted_val = bspline.get_fitted_value(fx(p))(0);
        float expected = fy(p);

        auto check = [](float a, float b) {
            return abs(a - b) < 0.2f;
        };
        //Note: checking is problematic, splines will not perfectly align
        REQUIRE(check(fitted_val, expected));

    }
}

TEST_CASE("Curves: polynomial fit test", "[Curves]") {
    using namespace Slic3r;
    using namespace Slic3r::Geometry;

    auto fx = [&](size_t index) {
        return float(index) / 100.0f;
    };

    auto fy = [&](size_t index) {
        return (fx(index) - 1) * (fx(index) - 1);
    };

    std::vector<Vec<1, float>> observations { };
    std::vector<float> observation_points { };
    std::vector<float> weights { };
    for (size_t index = 0; index < 200; ++index) {
        observations.push_back(Vec<1, float> { fy(index) });
        observation_points.push_back(fx(index));
        weights.push_back(1);
    }

    Vec2f fmin { fx(0), fy(0) };
    Vec2f fmax { fx(200), fy(200) };

    Approx ap(1.0f);
    ap.epsilon(0.1f);

    auto poly = fit_polynomial(observations, observation_points, weights, 2);

    REQUIRE(poly.coefficients(0, 0) == ap(1));
    REQUIRE(poly.coefficients(0, 1) == ap(-2));
    REQUIRE(poly.coefficients(0, 2) == ap(1));
}