aimsalgo  5.1.2
Neuroimaging image processing
bspline.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMSALGO_MATH_BSPLINE_H
35 #define AIMSALGO_MATH_BSPLINE_H
36 
37 #include <aims/vector/vector.h> // Point2dd
39 #include <cmath> // std::abs
40 #include <limits> // std::numeric_limits
41 #include <vector> // std::vector
42 
43 namespace aims {
44 
45  //==========================================================================
46  //
47  // B-SPLINE : ORDER AS TEMPLATE ARGUMENT
48  //
49  //==========================================================================
50 
52  template <unsigned Order>
53  struct BSplineOrder
54  {
55  static double at( double x )
56  {
57  return spline(x);
58  }
59 
60  static double spline( double x )
61  {
62  if( std::abs(x) > double(Order+1)/2 )
63  return 0.;
64 
65  return ( dO()/2. + .5 - x ) / dO()
67  + ( dO()/2. + .5 + x ) / dO()
69  }
70 
71  static double derivative( double x )
72  {
73  return BSplineOrder<Order-1>::spline( x + .5 )
75  }
76 
77  static double derivative2( double x )
78  {
79  ASSERT( Order > 1 );
80  return BSplineOrder<Order-1>::derivative( x + .5 )
82  }
83 
84  static unsigned order() { return Order; }
85 
86  private:
87  static double dO() { return (double)Order; }
88  };
89 
91  template <>
92  struct BSplineOrder<0>
93  {
94  static double spline( double value )
95  {
96  if( -.5 <= value && value < .5 )
97  return 1.;
98  else
99  return 0.;
100  }
101 
102  static unsigned order() { return 0; }
103  };
104 
105  //==========================================================================
106  //
107  // B-SPLINE : MULTI ORDER
108  //
109  //==========================================================================
110 
137  class BSpline
138  {
139  public:
140  BSpline( unsigned order = 3, float scale = 1., bool shifted = false );
141  BSpline( const BSpline & other );
142  virtual ~BSpline();
143  BSpline & operator= ( const BSpline & other );
144 
148  virtual
149  double operator() ( double x ) const;
151  virtual
152  double at( double x ) const;
154  virtual
155  double derivative( double x, unsigned n = 1 ) const;
160  virtual
161  const Point2dd & support() const;
163 
167  virtual
168  void reset( unsigned order = 3, float scale = 1., bool shifted = false );
170  virtual
171  void setOrder( unsigned order );
173  virtual
174  void setScale( float scale );
176  virtual
177  void setShifted( bool shifted );
179  virtual
180  void setCentered( bool centered );
182 
186  unsigned order() const { return _order; }
188  float scale() const { return _scale; }
190  bool shifted() const { return _shift; }
192  bool centered() const { return !_shift; }
194 
195  protected:
196  double dO() const { return double( _order ); }
197  virtual void setSupport();
198  unsigned _order;
199  float _scale;
200  bool _shift;
202  };
203 
204  //==========================================================================
205  //
206  // B-SPLINE : FAST
207  //
208  //==========================================================================
209 
216  class FastBSpline: public BSpline
217  {
218  public:
219  FastBSpline( unsigned order = 3, float scale = 1., bool shifted = false );
220  FastBSpline( const FastBSpline & other );
221  virtual ~FastBSpline();
222  FastBSpline & operator= ( const FastBSpline & other );
223 
229  virtual
230  double at( double x ) const;
234  virtual
235  double derivative( double x, unsigned n = 1 ) const;
237 
238  };
239 
240  //==========================================================================
241  //
242  // B-SPLINE : TABULATED VALUES
243  //
244  //==========================================================================
245 
251  class TabulBSpline: public BSpline
252  {
253  public:
254  TabulBSpline( unsigned order = 3,
255  unsigned nder = 1,
256  float scale = 1.,
257  bool shifted = false,
259  TabulBSpline( const TabulBSpline & other );
260  virtual ~TabulBSpline();
262 
266  virtual
267  double at( double x ) const;
269  virtual
270  double derivative( double x, unsigned n = 1 ) const;
272 
281  virtual
282  void reset( unsigned order = 3,
283  unsigned nder = 1,
284  float scale = 1.,
285  bool shifted = false,
288  virtual
289  void setOrder( unsigned order );
291  virtual
292  void setNbDer( unsigned nder );
294  virtual
295  void setLength( size_t length );
297 
301  virtual
302  unsigned nder() const;
304  virtual
305  size_t length() const;
307 
308  protected:
309  size_t index( double x ) const;
310  bool is_valid( size_t index ) const;
311  void setArray( unsigned nder, size_t length );
312  std::vector<std::vector<double> > _values;
313  double _tablescale;
314  double _tableoffset;
315  };
316 
317  //==========================================================================
318  //
319  // DISCRETE B-SPLINE
320  //
321  //==========================================================================
322 
325  class DiscreteBSpline: public BSpline
326  {
327  public:
330  DiscreteBSpline( unsigned order = 3, float scale = 1., bool shifted = false );
332  virtual ~DiscreteBSpline();
335 
338  virtual
339  double operator() ( int x ) const;
340  virtual
341  double at( int x ) const;
343 
352  virtual
353  void reset( unsigned order = 3, float scale = 1., bool shifted = false );
355  virtual
356  void setOrder( unsigned order );
358  virtual
359  void setScale( float scale );
361  virtual
362  void setShifted( bool shifted = true );
364  virtual
365  void setCentered( bool centered = true );
367 
368  protected:
369  void setArray();
370  void setSupport();
371  std::vector<double> _values;
372  };
373 
374 } // namespace aims
375 
376 #endif // AIMSALGO_MATH_BSPLINE_H
#define ASSERT(EX)
Centered and scaled B-Spline function, evaluated at run time.
Definition: bspline.h:138
virtual void setScale(float scale)
scale
bool centered() const
is centered ?
Definition: bspline.h:192
double dO() const
Definition: bspline.h:196
bool shifted() const
is shifted ?
Definition: bspline.h:190
BSpline & operator=(const BSpline &other)
virtual double operator()(double x) const
Compute value.
virtual double at(double x) const
spline value at point x
bool _shift
Definition: bspline.h:200
BSpline(unsigned order=3, float scale=1., bool shifted=false)
float scale() const
spline scale
Definition: bspline.h:188
unsigned order() const
Get parameters &nbsp;{ spline order.
Definition: bspline.h:186
virtual void setCentered(bool centered)
centered
virtual const Point2dd & support() const
Get the inferior and superior boundaries of the function support (i.e.
unsigned _order
Definition: bspline.h:198
virtual double derivative(double x, unsigned n=1) const
n-th derivative of the spline at point x
float _scale
Definition: bspline.h:199
virtual ~BSpline()
virtual void setSupport()
BSpline(const BSpline &other)
virtual void setShifted(bool shifted)
shifted
Point2dd _support
Definition: bspline.h:201
virtual void reset(unsigned order=3, float scale=1., bool shifted=false)
Set parameters &nbsp;{ (Re)set all parameters.
virtual void setOrder(unsigned order)
order
Discrete B-Splines B-Spline pre sampled only on integral values.
Definition: bspline.h:326
virtual void setScale(float scale)
scale
virtual double operator()(int x) const
Get value.
virtual void reset(unsigned order=3, float scale=1., bool shifted=false)
Set parameters Be aware that a call to reset, setOrder, setNbDer and setLength will trigger a realloc...
DiscreteBSpline & operator=(const DiscreteBSpline &other)
virtual double at(int x) const
virtual void setCentered(bool centered=true)
centered
DiscreteBSpline(const DiscreteBSpline &other)
std::vector< double > _values
Definition: bspline.h:371
DiscreteBSpline(unsigned order=3, float scale=1., bool shifted=false)
Constructor/Destructor/Copy.
virtual void setShifted(bool shifted=true)
shifted
virtual void setOrder(unsigned order)
order
virtual ~DiscreteBSpline()
*Centered and *scaled B-Spline function, evaluated at run time.
Definition: bspline.h:217
virtual double at(double x) const
Compute valuespline value at point x.
virtual ~FastBSpline()
FastBSpline(const FastBSpline &other)
FastBSpline(unsigned order=3, float scale=1., bool shifted=false)
virtual double derivative(double x, unsigned n=1) const
n-th derivative of the spline at point x
FastBSpline & operator=(const FastBSpline &other)
Pre-computed B-Spline values In the "order 0" case, the array is not used (the analytical expression ...
Definition: bspline.h:252
TabulBSpline & operator=(const TabulBSpline &other)
virtual void setOrder(unsigned order)
order
void setArray(unsigned nder, size_t length)
std::vector< std::vector< double > > _values
Definition: bspline.h:312
size_t index(double x) const
TabulBSpline(const TabulBSpline &other)
virtual unsigned nder() const
Get parametersnumber of derivative stored.
virtual double at(double x) const
Get valuespline value at point x.
virtual void setNbDer(unsigned nder)
number of derivative stored
double _tablescale
Definition: bspline.h:313
bool is_valid(size_t index) const
double _tableoffset
Definition: bspline.h:314
TabulBSpline(unsigned order=3, unsigned nder=1, float scale=1., bool shifted=false, size_t length=std::numeric_limits< unsigned short >::max() - 1)
virtual ~TabulBSpline()
virtual size_t length() const
array length
virtual double derivative(double x, unsigned n=1) const
n-th derivative of the spline at point x
virtual void reset(unsigned order=3, unsigned nder=1, float scale=1., bool shifted=false, size_t length=std::numeric_limits< unsigned short >::max() - 1)
Set parameters Be aware that a call to reset, setOrder, setNbDer and setLength will trigger a realloc...
virtual void setLength(size_t length)
array length
static unsigned order()
Definition: bspline.h:102
static double spline(double value)
Definition: bspline.h:94
1D B-Spline functions centered on 0, recursively evaluated at run time.
Definition: bspline.h:54
static double derivative2(double x)
Definition: bspline.h:77
static unsigned order()
Definition: bspline.h:84
static double at(double x)
Definition: bspline.h:55
static double derivative(double x)
Definition: bspline.h:71
static double spline(double x)
Definition: bspline.h:60