BSplinebasis
Loading...
Searching...
No Matches
Support.h
1/*
2 * ########################################################################
3 * The contents of this file is free and unencumbered software released into the
4 * public domain. For more information, please refer to <http://unlicense.org/>
5 * ########################################################################
6 */
7
8#ifndef BSPLINE_SUPPORT_SUPPORT_H
9#define BSPLINE_SUPPORT_SUPPORT_H
10
11#include <bspline/exceptions/BSplineException.h>
12#include <bspline/support/Grid.h>
13
14#include <optional>
15
22namespace bspline::support {
23using namespace bspline::exceptions;
24
32template <typename T>
34 public:
40
46
51
52 private:
54 Grid<T> _grid;
56 AbsoluteIndex _startIndex;
61 AbsoluteIndex _endIndex;
62
70 void checkValidity() const {
71 const bool isEmpty = _startIndex == 0 && _endIndex == 0;
72 const bool containsElement = _endIndex > _startIndex;
73 const bool withinBounds = _endIndex <= _grid.size();
74
75 const bool valid = isEmpty || (containsElement && withinBounds);
76
77 if (!valid) {
78 throw BSplineException(ErrorCode::INCONSISTENT_DATA);
79 }
80 }
81
82 public:
92 : _grid(grid), _startIndex(startIndex), _endIndex(endIndex) {
93 checkValidity();
94 };
95
100 Support(const Support &s) noexcept = default;
101
106 Support &operator=(const Support &s) noexcept = default;
107
111 ~Support() = default;
112
121 : _grid{s._grid}, _startIndex{s._startIndex}, _endIndex{s._endIndex} {
122 s._startIndex = 0;
123 s._endIndex = 0;
124
127 }
128
136 Support &operator=(Support &&s) noexcept {
137 _grid = s._grid;
138 _startIndex = s._startIndex;
139 _endIndex = s._endIndex;
140
141 s._startIndex = 0;
142 s._endIndex = 0;
145
146 return *this;
147 }
148
154 return Support<T>{grid, 0, 0};
155 };
156
162 const auto gridSize = grid.size();
163 return Support<T>{grid, 0, gridSize};
164 };
165
170 size_t size() const {
172 return _endIndex - _startIndex;
173 };
174
181 bool empty() const {
183 return (_startIndex == _endIndex);
184 };
185
193 bool containsIntervals() const {
195 return (size() > 1);
196 };
197
209 std::optional<RelativeIndex> relativeFromAbsolute(AbsoluteIndex index) const {
211 if (index >= _startIndex && index < _endIndex) {
212 return index - _startIndex;
213 } else {
214 return std::nullopt;
215 }
216 };
217
228 std::optional<RelativeIndex> intervalIndexFromAbsolute(
229 AbsoluteIndex index) const {
231 if (index >= _startIndex && index + 1 < _endIndex) {
232 return index - _startIndex;
233 } else {
234 return std::nullopt;
235 }
236 };
237
250 if (index >= size()) {
251 throw BSplineException(ErrorCode::UNDETERMINED);
252 }
253 return index + _startIndex;
254 };
255
260 size_t numberOfIntervals() const {
262 const size_t si = size();
263 if (si == 0) {
264 return 0;
265 } else {
266 return si - 1;
267 }
268 };
269
274 const Grid<T> &getGrid() const {
276 return _grid;
277 };
278
285 return _startIndex;
286 };
287
294 return _endIndex;
295 }
296
306 const T &operator[](RelativeIndex index) const {
308 return _grid[_startIndex + index];
309 };
310
321 const T &at(RelativeIndex index) const {
323 if (_startIndex + index >= _endIndex) {
324 throw BSplineException(ErrorCode::INVALID_ACCESS);
325 }
326 return _grid.at(_startIndex + index);
327 };
328
336 const T &front() const {
338 if (empty()) {
339 throw BSplineException(ErrorCode::INVALID_ACCESS);
340 }
341 return _grid[_startIndex];
342 };
343
352 const T &back() const {
354 if (empty()) {
355 throw BSplineException(ErrorCode::INVALID_ACCESS);
356 }
357 return _grid[_endIndex - 1];
358 };
359
366 return _grid.begin() + _startIndex;
367 };
368
375 return _grid.begin() + _endIndex;
376 };
377
387 bool hasSameGrid(const Support &s) const {
389 return _grid == s._grid;
390 };
391
402 bool operator==(const Support &s) const {
405 return hasSameGrid(s) &&
406 ((_startIndex == s._startIndex && _endIndex == s._endIndex) ||
407 (empty() && s.empty()));
408 };
409
420 bool operator!=(const Support &s) const {
423 return !(*this == s);
424 };
425
438 Support calcUnion(const Support &s) const {
441 if (!hasSameGrid(s)) {
442 throw BSplineException(ErrorCode::DIFFERING_GRIDS);
443 }
444
445 const bool thisEmpty = empty();
446 const bool sEmpty = s.empty();
447
448 if (thisEmpty && sEmpty) {
449 // Both Supports are empty, return empty Support.
450 return createEmpty(_grid);
451 } else if (thisEmpty && !sEmpty) {
452 return s;
453 } else if (!thisEmpty && sEmpty) {
454 return *this;
455 }
456
457 const size_t newStartIndex = std::min(_startIndex, s._startIndex);
458 const size_t newEndIndex = std::max(_endIndex, s._endIndex);
459 return Support(_grid, newStartIndex, newEndIndex);
460 };
461
474 if (!hasSameGrid(s)) {
475 throw BSplineException(ErrorCode::DIFFERING_GRIDS);
476 }
477
478 const size_t newStartIndex = std::max(_startIndex, s._startIndex);
479 const size_t newEndIndex = std::min(_endIndex, s._endIndex);
480
481 if (newStartIndex >= newEndIndex) {
482 // No overlap, return empty Support.
483 return createEmpty(_grid);
484 } else {
485 return Support(_grid, newStartIndex, newEndIndex);
486 }
487 };
488}; // end class Support
489} // namespace bspline::support
490#endif // BSPLINE_SUPPORT_SUPPORT_H
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
typename std::vector< T >::const_iterator const_iterator
Iterator type.
Definition Grid.h:66
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
Represents the Spline's Support.
Definition Support.h:33
bool containsIntervals() const
Returns false if this Support is empty or point-like.
Definition Support.h:193
Support(const Support &s) noexcept=default
Default copy constructor.
const_iterator begin() const
Returns the begin iterator of the Support.
Definition Support.h:364
std::optional< RelativeIndex > relativeFromAbsolute(AbsoluteIndex index) const
Converts an AbsoluteIndex into a RelativeIndex.
Definition Support.h:209
std::optional< RelativeIndex > intervalIndexFromAbsolute(AbsoluteIndex index) const
Converts an AbsoluteIndex into a RelativeIndex.
Definition Support.h:228
typename Grid< T >::const_iterator const_iterator
The iterator type.
Definition Support.h:50
Support(const Grid< T > &grid, AbsoluteIndex startIndex, AbsoluteIndex endIndex)
Constructs a Support relative to the global Grid grid.
Definition Support.h:91
AbsoluteIndex getEndIndex() const
Returns the _endIndex.
Definition Support.h:292
~Support()=default
Default destructor.
size_t numberOfIntervals() const
Returns the number of intervals represented by this Support.
Definition Support.h:260
Support calcIntersection(const Support &s) const
Calculates the intersection of the two Supports.
Definition Support.h:471
static Support< T > createEmpty(const Grid< T > &grid)
Constructs an empty Support relative to the global Grid grid.
Definition Support.h:153
const_iterator end() const
Returns the end iterator of the Support.
Definition Support.h:373
const T & at(RelativeIndex index) const
Returns the indexed element.
Definition Support.h:321
const T & operator[](RelativeIndex index) const
Returns the indexed element.
Definition Support.h:306
bool operator!=(const Support &s) const
Logically compares to Supports.
Definition Support.h:420
const T & front() const
Returns the begin of this Support.
Definition Support.h:336
Support & operator=(Support &&s) noexcept
Move assignment operator.
Definition Support.h:136
size_t size() const
Returns the number of grid points contained in this Support.
Definition Support.h:170
AbsoluteIndex absoluteFromRelative(RelativeIndex index) const
Converts a RelativeIndex into an AbsoluteIndex.
Definition Support.h:248
Support calcUnion(const Support &s) const
Calculates the union of the two Supports.
Definition Support.h:438
bool operator==(const Support &s) const
Logically compares to Supports.
Definition Support.h:402
bool hasSameGrid(const Support &s) const
Checks whether two Supports are defined on the same Grids.
Definition Support.h:387
const Grid< T > & getGrid() const
Returns the global Grid.
Definition Support.h:274
const T & back() const
Returns the end of this Support.
Definition Support.h:352
Support & operator=(const Support &s) noexcept=default
Default copy assignment operator.
bool empty() const
Returns whether this Support is empty.
Definition Support.h:181
AbsoluteIndex getStartIndex() const
Returns the _startIndex.
Definition Support.h:283
static Support< T > createWholeGrid(const Grid< T > &grid)
Constructs a Support representing the complete global grid grid.
Definition Support.h:161
Support(Support &&s) noexcept
Move constructor.
Definition Support.h:120
Exceptions and error codes.
Definition BSplineException.h:19
Namespace for the Spline's Grid and Support.
Definition Grid.h:19