Volume Cartographer 2.27.0
Spline.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cassert>
6#include <cstddef>
7#include <vector>
8
9#include <unsupported/Eigen/Splines>
10
12
14{
21template <typename Scalar = double, int Degree = 3>
22class Spline
23{
24public:
25 using ScalarVector = std::vector<Scalar>;
26 using SplineType = Eigen::Spline<Scalar, 2>;
27
28 Spline() = default;
29
36 Spline(const ScalarVector& xs, const ScalarVector& ys) : npoints_{xs.size()}
37 {
38 assert(xs.size() == ys.size() && "xs and ys must be same length");
39 auto points = make_wide_matrix_(xs, ys);
40 spline_ = Eigen::SplineFitting<SplineType>::Interpolate(points, Degree);
41 }
42
46 Pixel operator()(Scalar t) const
47 {
48 assert(t >= 0 && t <= 1 && "t must be in range [0, 1]");
49 Eigen::Vector2d val = spline_(t);
50 return {val(0), val(1)};
51 }
52
53private:
55 std::size_t npoints_;
56
57 SplineType spline_;
58
62 Eigen::MatrixXd make_wide_matrix_(
63 const ScalarVector& xs, const ScalarVector& ys)
64 {
65 Eigen::MatrixXd mat{2, xs.size()};
66 mat.row(0) = Eigen::VectorXd::Map(xs.data(), xs.size());
67 mat.row(1) = Eigen::VectorXd::Map(ys.data(), ys.size());
68 return mat;
69 }
70};
71
77template <typename Scalar>
79} // namespace volcart::segmentation
Simple spline wrapper around Eigen::Spline.
Definition: Spline.hpp:23
Pixel operator()(Scalar t) const
Spline evaluation at t-space value t in [0, 1]
Definition: Spline.hpp:46
Spline(const ScalarVector &xs, const ScalarVector &ys)
Construct a spline by fitting to a set of points.
Definition: Spline.hpp:36
Eigen::MatrixXd make_wide_matrix_(const ScalarVector &xs, const ScalarVector &ys)
Combine X and Y values into an npoints_ x 2 matrix.
Definition: Spline.hpp:62
Segmentation algorithms and utilities library