27#ifndef BSPLINE_SPLINE_H
28#define BSPLINE_SPLINE_H
30#include <bspline/exceptions/BSplineException.h>
31#include <bspline/internal/misc.h>
32#include <bspline/support/Support.h>
45using namespace support;
58template <
typename T,
size_t order>
62 static constexpr size_t ARRAY_SIZE =
order + 1;
66 std::vector<std::array<T, ARRAY_SIZE>> _coefficients;
77 std::optional<size_t> findInterval(
const T &
x)
const {
81 const auto begin = _support.
begin();
82 const auto it = std::lower_bound(begin, _support.
end(),
x);
99 const std::vector<std::array<T, ARRAY_SIZE>> &
coefficients)
const {
102 (support.
size() >= 2 &&
112 void checkValidity()
const { checkValidity(_support, _coefficients); };
124 _support = std::move(support);
148 checkValidity(_support, _coefficients);
178 return _coefficients;
211 return _support.
front();
224 return _support.
back();
235 template <
size_t order2>
238 if (!_support.containsIntervals() || !
m2.getSupport().containsIntervals())
256 static const T ZERO =
static_cast<T>(0);
257 if (!_support.containsIntervals())
return true;
258 for (
const auto &
cs : _coefficients) {
259 for (
const auto &
c :
cs) {
260 if (
c !=
ZERO)
return false;
278 return (*
this) * (
static_cast<T>(1) /
d);
291 for (
auto &
cs :
ret._coefficients) {
309 for (
auto &
cs : _coefficients) {
327 (*this) *= (
static_cast<T>(1) /
d);
337 return (*
this) *
static_cast<T>(-1);
350 template <
size_t ordera>
357 "The assignment operator is only defined if the order of the rhs "
358 "spline is lower than or equal to that of the lhs spline.");
361 a.getCoefficients().
size(),
362 internal::make_array<T, ARRAY_SIZE>(
static_cast<T>(0)));
363 for (
size_t i = 0;
i <
a.getCoefficients().size();
i++) {
364 const auto &
coeffsi =
a.getCoefficients()[
i];
382 template <
size_t ordera>
396 nintervals, internal::make_array<T, NEW_ARRAY_SIZE>(
static_cast<T>(0)));
401 auto thisRelIndex = _support.intervalIndexFromAbsolute(
ai).value();
402 auto aRelIndex =
a.getSupport().intervalIndexFromAbsolute(
ai).value();
408 for (
size_t j = 0;
j <
order + 1;
j++) {
409 for (
size_t k = 0;
k <
ordera + 1;
k++) {
427 template <
size_t ordera>
447 internal::changearraysize<T, order + 1, NEW_ARRAY_SIZE>(
451 internal::changearraysize<T, ordera + 1, NEW_ARRAY_SIZE>(
458 internal::make_array<T, NEW_ARRAY_SIZE>(
static_cast<T>(0));
476 template <
size_t ordera>
481 "The operators += and -= are only defined if the order of the rhs "
482 "spline is lower than or equal to that of the lhs spline.");
483 (*this) = (*this) +
a;
498 template <
size_t ordera>
500 (*this) += (
static_cast<T>(-1) *
a);
514 template <
size_t ordera>
517 return (*
this) + (
static_cast<T>(-1) *
a);
527 return _support ==
other._support && _coefficients ==
other._coefficients;
542template <
typename T,
size_t ARRAY_SIZE>
558template <
typename T,
size_t order>
582template <
typename CoeffIter,
typename SplineIter>
586 using Spline =
typename std::remove_cv_t<
587 typename std::iterator_traits<SplineIter>::value_type>;
590 using T =
typename std::remove_cv_t<
591 typename std::iterator_traits<CoeffIter>::value_type>;
598 std::is_same<typename Spline::data_type, T>::value,
599 "Coefficients must be of the same type as the data type of the spline.");
609 ErrorCode::INCONSISTENT_DATA,
610 "The number of coefficients and splines must coincide.");
616 ErrorCode::MISSING_DATA,
617 "The number of coefficients and splines may not be zero.");
627 if (!
it->getSupport().hasSameGrid(
support0)) {
631 if (!
it->getSupport().
empty()) {
632 const size_t si =
it->getSupport().getStartIndex();
637 const size_t ei =
it->getSupport().getEndIndex();
649 internal::make_array<T, order + 1>(
static_cast<T>(0)));
658 for (
size_t j = 0;
j <
spline.getSupport().numberOfIntervals();
j++) {
670 for (
size_t k = 0;
k <
order + 1;
k++) {
698template <
typename CoeffCollection,
typename SplineCollection>
705#ifndef BSPLINE_DOXYGEN_IGNORE
722template <
typename T,
size_t order>
The central Spline class of the library.
Definition Spline.h:59
Spline< T, order > & operator*=(const T &d)
In-place scalar-multiplication operator.
Definition Spline.h:307
bool checkOverlap(const Spline< T, order2 > &m2) const
Checks whether the supports of the two splines overlap.
Definition Spline.h:236
T operator()(const T &x) const
Evaluates the spline at point x.
Definition Spline.h:189
bool isZero() const
Checks whether this spline is a zero over the whole number line.
Definition Spline.h:254
const std::vector< std::array< T, ARRAY_SIZE > > & getCoefficients() const noexcept
The spline's polynomial coefficients.
Definition Spline.h:175
bool operator==(const Spline &other) const
Compares two splines for equality.
Definition Spline.h:526
static constexpr size_t spline_order
The order of the spline.
Definition Spline.h:137
Spline< T, order > & operator=(const Spline< T, ordera > &a)
Copy assign operator.
Definition Spline.h:351
Spline< T, std::max(order, ordera)> operator+(const Spline< T, ordera > &a) const
Spline addition operator.
Definition Spline.h:428
const T & back() const
Returns the end of the spline's support.
Definition Spline.h:222
Spline< T, order > & operator/=(const T &d)
In-place scalar-division operator.
Definition Spline.h:325
const Support< T > & getSupport() const noexcept
Returns the spline's support.
Definition Spline.h:163
Spline< T, order > & operator+=(const Spline< T, ordera > &a)
In-place addition operator.
Definition Spline.h:477
const T & front() const
Returns the begin of the spline's support.
Definition Spline.h:209
bool operator!=(const Spline &other) const
Compares two splines for inequality.
Definition Spline.h:536
Spline< T, order+ordera > operator*(const Spline< T, ordera > &a) const
Spline-spline multiplication operator.
Definition Spline.h:383
Spline(Grid< T > grid)
Constructs an empty spline on the global grid.
Definition Spline.h:156
Spline< T, order > operator-() const
Unary minus operator.
Definition Spline.h:335
Spline< T, order > & operator-=(const Spline< T, ordera > &a)
Binary in-place subtraction operator.
Definition Spline.h:499
Spline< T, order > operator/(const T &d) const
Scalar-division operator.
Definition Spline.h:276
Spline< T, order > operator*(const T &d) const
Scalar-multiplication operator.
Definition Spline.h:288
Spline< T, std::max(order, ordera)> operator-(const Spline< T, ordera > &a) const
Binary subtraction operator.
Definition Spline.h:515
Spline(Support< T > support, std::vector< std::array< T, ARRAY_SIZE > > coefficients)
Constructor setting the data. Performs sanity checks.
Definition Spline.h:145
The main exception class.
Definition BSplineException.h:84
Represents a global Grid.
Definition Grid.h:27
const T & at(size_t i) const
Gives access to the i-th element of the Grid.
Definition Grid.h:218
const_iterator end() const
Returns the end iterator of the Grid..
Definition Grid.h:265
size_t size() const
Returns the number of elements of the Grid.
Definition Grid.h:169
bool empty() const
Checks whether the spline is empty.
Definition Grid.h:192
const_iterator begin() const
Returns the begin iterator of the Grid.
Definition Grid.h:256
const T & back() const
Returns a reference to the last element of the Grid.
Definition Grid.h:244
const T & front() const
Returns a reference to the first element of the Grid.
Definition Grid.h:231
Represents the Spline's Support.
Definition Support.h:33
Exceptions and error codes.
Definition BSplineException.h:19
Main namespace for this library.
Definition BSplineGenerator.h:21
Spline< T, order > operator*(const T &d, const Spline< T, order > &b)
Commutation of spline scalar multiplication operator.
Definition Spline.h:559
constexpr bool is_spline_v
Definition Spline.h:731
auto linearCombination(CoeffIter coeffsBegin, CoeffIter coeffsEnd, SplineIter splinesBegin, SplineIter splinesEnd)
Calculates the linear combination.
Definition Spline.h:583