A.I.M.S algorithms


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 
38 #include <cmath> // std::abs
39 #include <limits> // std::numeric_limits
40 #include <vector> // std::vector
41 
42 namespace aims {
43 
44  //==========================================================================
45  //
46  // B-SPLINE : ORDER AS TEMPLATE ARGUMENT
47  //
48  //==========================================================================
49 
51  template <unsigned Order>
52  struct BSplineOrder
53  {
54  static double spline( double x )
55  {
56  if( std::abs(x) > double(Order+1)/2 )
57  return 0.;
58 
59  return ( dO()/2. + .5 - x ) / dO()
61  + ( dO()/2. + .5 + x ) / dO()
63  }
64 
65  static double derivative( double x )
66  {
67  return BSplineOrder<Order-1>::spline( x + .5 )
69  }
70 
71  static double derivative2( double x )
72  {
73  ASSERT( Order > 1 );
74  return BSplineOrder<Order-1>::derivative( x + .5 )
76  }
77 
78  static unsigned order() { return Order; }
79 
80  private:
81  static double dO() { return (double)Order; }
82  };
83 
85  template <>
86  struct BSplineOrder<0>
87  {
88  static double spline( double value )
89  {
90  if( -.5 <= value && value < .5 )
91  return 1.;
92  else
93  return 0.;
94  }
95 
96  static unsigned order() { return 0; }
97  };
98 
99  //==========================================================================
100  //
101  // B-SPLINE : MULTI ORDER
102  //
103  //==========================================================================
104 
106  class BSpline
107  {
108  public:
109  BSpline( unsigned order = 3 );
110  ~BSpline();
111 
113  double operator() ( double x ) const;
115  double spline( double x ) const;
117  double derivative( double x, unsigned n = 1 ) const;
119  unsigned order() const;
120 
121  private:
122  double dO() const;
123  unsigned _order;
124  };
125 
126  //==========================================================================
127  //
128  // B-SPLINE : TABULATED VALUES
129  //
130  //==========================================================================
131 
136  {
137  public:
138  TabulBSpline( unsigned order = 3, unsigned nder = 1, unsigned length = 65537 );
139  ~TabulBSpline();
140 
142  double operator() ( double x ) const;
144  double spline( double x ) const;
146  double derivative( double x, unsigned n = 1 ) const;
148  unsigned order() const;
150  unsigned nder() const;
152  unsigned length() const;
153 
154  private:
155  unsigned index( double x ) const;
156  bool is_valid( unsigned index ) const;
157  unsigned _order;
158  std::vector<std::vector<double> > _values;
159  };
160 
161 } // namespace aims
162 
163 #endif // AIMSALGO_MATH_BSPLINE_H
static unsigned order()
Definition: bspline.h:78
TabulBSpline(unsigned order=3, unsigned nder=1, unsigned length=65537)
1D B-Spline functions centered on 0, recursively evaluated at run time.
Definition: bspline.h:52
double spline(double x) const
spline value at point x
double spline(double x) const
spline value at point x
static double derivative(double x)
Definition: bspline.h:65
double operator()(double x) const
unsigned order() const
spline order
static unsigned order()
Definition: bspline.h:96
static double spline(double x)
Definition: bspline.h:54
Pre-computed B-Spline values In the "order 0" case, the array is not used (the analytical expression ...
Definition: bspline.h:135
static double spline(double value)
Definition: bspline.h:88
static double derivative2(double x)
Definition: bspline.h:71
unsigned order() const
spline order
double operator()(double x) const
B-Spline function centered on 0, evaluated at run time.
Definition: bspline.h:106
unsigned length() const
length
double derivative(double x, unsigned n=1) const
n-th derivative of the spline at point x
BSpline(unsigned order=3)
#define ASSERT(EX)
double derivative(double x, unsigned n=1) const
n-th derivative of the spline at point x
unsigned nder() const
number of derivative stored