vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
extrapolate.h
Go to the documentation of this file.
1/************************************************************************/
2/* */
3/* vspline - a set of generic tools for creation and evaluation */
4/* of uniform b-splines */
5/* */
6/* Copyright 2015 - 2023 by Kay F. Jahnke */
7/* */
8/* The git repository for this software is at */
9/* */
10/* https://bitbucket.org/kfj/vspline */
11/* */
12/* Please direct questions, bug reports, and contributions to */
13/* */
14/* kfjahnke+vspline@gmail.com */
15/* */
16/* Permission is hereby granted, free of charge, to any person */
17/* obtaining a copy of this software and associated documentation */
18/* files (the "Software"), to deal in the Software without */
19/* restriction, including without limitation the rights to use, */
20/* copy, modify, merge, publish, distribute, sublicense, and/or */
21/* sell copies of the Software, and to permit persons to whom the */
22/* Software is furnished to do so, subject to the following */
23/* conditions: */
24/* */
25/* The above copyright notice and this permission notice shall be */
26/* included in all copies or substantial portions of the */
27/* Software. */
28/* */
29/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
30/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
31/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
32/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
33/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
34/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
35/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
36/* OTHER DEALINGS IN THE SOFTWARE. */
37/* */
38/************************************************************************/
39
40/*! \file extrapolate.h
41
42 \brief extrapolation of 1D data sets with specific boundary conditions
43*/
44
45#ifndef VSPLINE_EXTRAPOLATE_H
46#define VSPLINE_EXTRAPOLATE_H
47
48#include "common.h"
49
50namespace vspline
51{
52
53/// struct extrapolator is a helper class providing extrapolated
54/// values for a 1D buffer indexed with possibly out-of-range indices.
55/// The extrapolated value is returned by value. boundary conditions
56/// PERIODIC , MIRROR , REFLECT, NATURAL and CONSTANT are currently
57/// supported.
58/// An extrapolator is set up by passing the boundary condition code
59/// (see common.h) and a const reference to the 1D data set, coded
60/// as a 1D vigra::MultiArrayView. The view has to refer to valid data
61/// for the time the extrapolator is in use.
62/// Now the extrapolator object can be indexed with arbitrary indices,
63/// and it will return extrapolated values. The indexing is done with
64/// operator() rather than operator[] to mark the semantic difference.
65/// Note how buffers with size 1 are treated specially for some
66/// boundary conditions: here we simply return the value at index 0.
67
68template < class buffer_type >
70{
71 const buffer_type & buffer ;
72 typedef typename buffer_type::value_type value_type ;
73
74 // we handle the polymorphism by calling the specific extrapolation
75 // routine via a method pointer. This enables us to provide a uniform
76 // interface without having to set up a virtual base class and inherit
77 // from it.
78
79 typedef value_type ( extrapolator::*p_xtr ) ( int i ) const ;
81
83 {
84 int w = buffer.size() - 1 ;
85
86 if ( w == 0 )
87 return buffer[0] ;
88
89 i = std::abs ( i ) ;
90 if ( i >= w )
91 {
92 i %= 2 * w ;
93 i -= w ;
94 i = std::abs ( i ) ;
95 i = w - i ;
96 }
97 return buffer [ i ] ;
98 }
99
101 {
102 int w = buffer.size() - 1 ;
103
104 if ( w == 0 )
105 return buffer[0] ;
106
107 if ( i >= 0 && i <= w )
108 return buffer[i] ;
109
110 int sign = i < 0 ? -1 : 1 ;
111 i = std::abs ( i ) ;
112
113 int p = 2 * w ;
114 int np = i / p ;
115 int r = i % p ;
116 value_type help ;
117
118 if ( r <= w )
119 {
120 help = buffer[r] - buffer[0] ;
121 help += np * 2 * ( buffer[w] - buffer[0] ) ;
122 help *= sign ;
123 help += buffer [ 0 ] ;
124 return help ;
125 }
126
127 r = 2 * w - r ;
128 help = 2 * ( buffer [ w ] - buffer [ 0 ] );
129 help -= ( buffer[r] - buffer [ 0 ] ) ;
130 help += np * 2 * ( buffer[w] - buffer[0] ) ;
131 help *= sign ;
132 help += buffer [ 0 ] ;
133 return help ;
134 }
135
137 {
138 int w = buffer.size() ;
139 if ( i < 0 )
140 i = -1 - i ;
141 if ( i >= w )
142 {
143 i %= 2 * w ;
144 if ( i >= w )
145 i = 2 * w - i - 1 ;
146 }
147 return buffer [ i ] ;
148 }
149
151 {
152 int w = buffer.size() ;
153
154 if ( w == 1 )
155 return buffer[0] ;
156
157 if ( i < 0 || i >= w )
158 {
159 i %= w ;
160 if ( i < 0 )
161 i += w ;
162 }
163 return buffer [ i ] ;
164 }
165
167 {
168 if ( i < 0 )
169 return buffer [ 0 ] ;
170 int w = buffer.size() - 1 ;
171 if ( i >= w )
172 return buffer [ w ] ;
173 return buffer [ i ] ;
174 }
175
177 {
178 return value_type ( 0 ) ;
179 }
180
181 /// class extrapolator's constructor takes the boundary
182 /// condition code and a const reference to the buffer.
183 /// the specific extrapolation routine is picked in the
184 /// case switch and assigned to the method pointer which
185 /// will be invoked by operator().
186
187 extrapolator ( vspline::bc_code bc , const buffer_type & _buffer )
188 : buffer ( _buffer )
189 {
190 switch ( bc )
191 {
192 case vspline::PERIODIC :
194 break ;
195 case vspline::REFLECT :
197 break ;
198 case vspline::NATURAL :
200 break ;
201 case vspline::MIRROR :
203 break ;
204 case vspline::GUESS :
205 case vspline::CONSTANT :
207 break ;
208 case vspline::ZEROPAD :
210 break ;
211 default:
213 ( "extrapolator: unknown boundary condition" ) ;
214 break ;
215 }
216 }
217
218 /// operator() uses the specific extrapolation method to provide
219 /// a value for position i.
220
221 value_type operator() ( const int & i ) const
222 {
223 return (this->*_p_xtr) ( i ) ;
224 }
225} ;
226
227} ; // namespace vspline
228
229#endif // #define VSPLINE_EXTRAPOLATE_H
definitions common to all files in this project, utility code
Definition: basis.h:79
bc_code
This enumeration is used for codes connected to boundary conditions. There are two aspects to boundar...
Definition: common.h:71
@ CONSTANT
Definition: common.h:76
@ GUESS
Definition: common.h:78
@ NATURAL
Definition: common.h:75
@ REFLECT
Definition: common.h:74
@ PERIODIC
Definition: common.h:73
@ MIRROR
Definition: common.h:72
@ ZEROPAD
Definition: common.h:77
struct extrapolator is a helper class providing extrapolated values for a 1D buffer indexed with poss...
Definition: extrapolate.h:70
buffer_type::value_type value_type
Definition: extrapolate.h:72
value_type extrapolate_reflect(int i) const
Definition: extrapolate.h:136
value_type extrapolate_periodic(int i) const
Definition: extrapolate.h:150
value_type extrapolate_clamp(int i) const
Definition: extrapolate.h:166
value_type(extrapolator::* p_xtr)(int i) const
Definition: extrapolate.h:79
value_type operator()(const int &i) const
operator() uses the specific extrapolation method to provide a value for position i.
Definition: extrapolate.h:221
value_type extrapolate_natural(int i) const
Definition: extrapolate.h:100
value_type extrapolate_mirror(int i) const
Definition: extrapolate.h:82
const buffer_type & buffer
Definition: extrapolate.h:71
value_type extrapolate_zeropad(int i) const
Definition: extrapolate.h:176
extrapolator(vspline::bc_code bc, const buffer_type &_buffer)
class extrapolator's constructor takes the boundary condition code and a const reference to the buffe...
Definition: extrapolate.h:187
for interfaces which need specific implementations we use:
Definition: common.h:277