Tatooine
polynomial_line.h
Go to the documentation of this file.
1#ifndef TATOOINE_POLYNOMIAL_LINE_H
2#define TATOOINE_POLYNOMIAL_LINE_H
3//==============================================================================
4#include <tatooine/available_libraries.h>
5#include <tatooine/line.h>
6#include <tatooine/linspace.h>
7#include <tatooine/math.h>
9
10#include <array>
11//==============================================================================
12namespace tatooine {
13//==============================================================================
14template <typename Real, std::size_t N, std::size_t Degree>
16 //----------------------------------------------------------------------------
17 // typedefs
18 //----------------------------------------------------------------------------
19 public:
22
23 //----------------------------------------------------------------------------
24 // static methods
25 //----------------------------------------------------------------------------
26 public:
27 static constexpr auto num_dimensions() -> std::size_t { return N; }
28 static constexpr auto degree() { return Degree; }
29
30 //----------------------------------------------------------------------------
31 // members
32 //----------------------------------------------------------------------------
33 private:
34 std::array<polynomial_type, N> m_polynomials;
35
36 //----------------------------------------------------------------------------
37 // ctors
38 //----------------------------------------------------------------------------
39 public:
40 constexpr polynomial_line()
42 constexpr polynomial_line(const polynomial_line& other) = default;
43 constexpr polynomial_line(polynomial_line&& other) = default;
44 constexpr polynomial_line& operator=(const polynomial_line& other) = default;
45 constexpr polynomial_line& operator=(polynomial_line&& other) = default;
46 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47 template <typename... Polynomials>
48 requires(is_polynomial<Polynomials> && ...)
49 constexpr polynomial_line(Polynomials&&... polynomials)
50 : m_polynomials{std::forward<Polynomials>(polynomials)...} {}
51
52 //----------------------------------------------------------------------------
53 // methods
54 //----------------------------------------------------------------------------
55 public:
56 auto& polynomial(std::size_t i) { return m_polynomials[i]; }
57 const auto& polynomial(std::size_t i) const { return m_polynomials[i]; }
58 //----------------------------------------------------------------------------
59 auto& polynomials() { return m_polynomials; }
60 const auto& polynomials() const { return m_polynomials; }
61 //----------------------------------------------------------------------------
62 private:
63 template <std::size_t... Is>
64 constexpr auto evaluate(Real t, std::index_sequence<Is...> /*is*/) const {
65 return vec_type{m_polynomials[Is](t)...};
66 }
67 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 public:
69 constexpr auto evaluate(Real t) const {
70 return evaluate(t, std::make_index_sequence<N>{});
71 }
72 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 constexpr auto operator()(Real t) const { return evaluate(t); }
74 //----------------------------------------------------------------------------
75 private:
76 template <std::size_t... Is>
77 constexpr auto diff(std::index_sequence<Is...> /*is*/) const {
78 return polynomial_line{diff(m_polynomials[Is])...};
79 }
80 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81 public:
82 constexpr auto diff() const { return diff(std::make_index_sequence<N>{}); }
83 //----------------------------------------------------------------------------
84 private:
85 template <std::size_t... Is>
86 constexpr auto tangent(Real t, std::index_sequence<Is...> /*is*/) const {
87 return vec_type{m_polynomials[Is].diff()(t)...};
88 }
89 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90 public:
91 constexpr auto tangent(Real t) const {
92 return tangent(t, std::make_index_sequence<N>{});
93 }
94 //----------------------------------------------------------------------------
95 private:
96 template <std::size_t... Is>
97 constexpr auto second_derivative(Real t,
98 std::index_sequence<Is...> /*is*/) const {
99 return vec_type{m_polynomials[Is].diff().diff()(t)...};
100 }
101 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 public:
103 constexpr auto second_derivative(Real t) const {
104 return second_derivative(t, std::make_index_sequence<N>{});
105 }
106 //----------------------------------------------------------------------------
107 constexpr auto curvature(Real t) const {
108 return curvature(tangent(t), second_derivative(t));
109 }
110 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 constexpr auto curvature(Real t, const vec_type& tang) const {
112 return curvature(tang, second_derivative(t));
113 }
114 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115 constexpr auto curvature(const vec_type& tang, const vec_type& snd_der) const
116 -> Real {
117 const auto ltang = length(tang);
118 if (std::abs(ltang) < 1e-10) {
119 return 0;
120 }
121 if constexpr (N == 2) {
122 return std::abs(tang(0) * snd_der(1) - tang(1) * snd_der(0)) /
123 (ltang * ltang * ltang);
124 } else if constexpr (N == 3) {
125 return length(cross(tang, snd_der)) / (ltang * ltang * ltang);
126 }
127 }
128 //----------------------------------------------------------------------------
129 template <template <typename> typename InterpolationKernel,
130 floating_point Real_>
131 constexpr auto evaluate(linspace<Real_> const& ts) const {
132 auto discretized = line<Real, N>{};
133 auto& param = discretized.parameterization();
134 auto& tang = discretized.tangents();
135 auto& snd_der =
136 discretized.template vertex_property<vec<Real, N>>("second_derivative");
137 auto& curv = discretized.scalar_vertex_property("curvature");
138 for (auto const t : ts) {
139 discretized.push_back(evaluate(t));
140 param.back() = t;
141 tang.back() = tangent(t);
142 snd_der.back() = second_derivative(t);
143 curv.back() = curvature(tang.back(), snd_der.back());
144 }
145 return discretized;
146 }
147 //----------------------------------------------------------------------------
148 constexpr auto arc_length(const linspace<Real>& range) const {
149 Real l = 0;
150 for (std::size_t i = 0; i < size(range) - 1; ++i) {
151 l += distance(evaluate(range[i]), evaluate(range[i + 1]));
152 }
153 return l;
154 }
155};
156//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157template <typename... Polynomials>
158polynomial_line(Polynomials&&...)
159 -> polynomial_line<common_type<typename Polynomials::real_type...>,
160 sizeof...(Polynomials), max(Polynomials::degree()...)>;
161//==============================================================================
162template <typename Real, std::size_t N, std::size_t Degree>
163auto operator<<(std::ostream& out, const polynomial_line<Real, N, Degree>& line)
164 -> std::ostream& {
165 out << "[(" << line.polynomial(0) << ")";
166 for (std::size_t i = 1; i < N; ++i) {
167 out << ", (" << line.polynomial(i) << ")";
168 }
169 out << "]";
170 return out;
171}
172//==============================================================================
173} // namespace tatooine
174//==============================================================================
175#endif
Definition: polynomial_line.h:15
auto & polynomials()
Definition: polynomial_line.h:59
constexpr auto operator()(Real t) const
Definition: polynomial_line.h:73
constexpr polynomial_line & operator=(polynomial_line &&other)=default
constexpr auto curvature(Real t, const vec_type &tang) const
Definition: polynomial_line.h:111
constexpr auto arc_length(const linspace< Real > &range) const
Definition: polynomial_line.h:148
const auto & polynomials() const
Definition: polynomial_line.h:60
constexpr auto tangent(Real t, std::index_sequence< Is... >) const
Definition: polynomial_line.h:86
auto & polynomial(std::size_t i)
Definition: polynomial_line.h:56
static constexpr auto degree()
Definition: polynomial_line.h:28
constexpr auto second_derivative(Real t) const
Definition: polynomial_line.h:103
constexpr auto evaluate(linspace< Real_ > const &ts) const
Definition: polynomial_line.h:131
constexpr auto evaluate(Real t, std::index_sequence< Is... >) const
Definition: polynomial_line.h:64
constexpr auto evaluate(Real t) const
Definition: polynomial_line.h:69
constexpr polynomial_line(polynomial_line &&other)=default
constexpr auto tangent(Real t) const
Definition: polynomial_line.h:91
constexpr polynomial_line(const polynomial_line &other)=default
std::array< polynomial_type, N > m_polynomials
Definition: polynomial_line.h:34
constexpr auto curvature(Real t) const
Definition: polynomial_line.h:107
static constexpr auto num_dimensions() -> std::size_t
Definition: polynomial_line.h:27
constexpr polynomial_line()
Definition: polynomial_line.h:40
const auto & polynomial(std::size_t i) const
Definition: polynomial_line.h:57
constexpr auto curvature(const vec_type &tang, const vec_type &snd_der) const -> Real
Definition: polynomial_line.h:115
constexpr auto second_derivative(Real t, std::index_sequence< Is... >) const
Definition: polynomial_line.h:97
constexpr auto diff(std::index_sequence< Is... >) const
Definition: polynomial_line.h:77
constexpr polynomial_line & operator=(const polynomial_line &other)=default
constexpr auto diff() const
Definition: polynomial_line.h:82
Definition: concepts.h:30
Definition: concepts.h:84
Definition: algorithm.h:6
auto operator<<(std::ostream &out, linspace< Real > const &l) -> auto &
Definition: linspace.h:165
constexpr auto distance(Iter const &it0, Iter const &it1)
Definition: iterator_facade.h:372
typename common_type_impl< Ts... >::type common_type
Definition: common_type.h:23
constexpr auto make_array()
Definition: make_array.h:22
constexpr auto max(A &&a, B &&b)
Definition: math.h:20
auto size(vec< ValueType, N > const &v)
Definition: vec.h:148
constexpr auto cross(base_tensor< Tensor0, T0, 3 > const &lhs, base_tensor< Tensor1, T1, 3 > const &rhs)
Definition: cross.h:9
Definition: line.h:35
auto parameterization() -> auto &
Definition: line.h:502
Definition: linspace.h:26
Definition: polynomial.h:14