aimsdata  5.0.5
Neuroimaging data handling
linearInterpolator.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 /*
35  * 3D linear image interpolation
36  */
37 
38 #ifndef AIMS_LINEAR_INTERPOLATOR
39 #define AIMS_LINEAR_INTERPOLATOR
40 
41 #include <cassert>
42 
43 #include <aims/data/data.h>
44 #include <cartobase/smart/rcptr.h>
45 #include <vector>
46 
47 namespace aims {
48 
49 
50  //----------------//
51  // Interpolator //
52 //----------------//
53 
54 //-----------------------------------------------------------------------------
56 {
57 public:
58 
59  typedef float Coordinate_t;
60  typedef Point3df Point_t;
61  typedef double Scalar_t;
62 
63  virtual ~Interpolator();
64 
68  virtual bool isValid( Coordinate_t x, Coordinate_t y,
69  Coordinate_t z ) const = 0;
73  inline bool isValid( const Interpolator::Point_t &point ) const
74  {
75  return isValid( point[ 0 ], point[ 1 ], point[ 2 ] );
76  }
77 
79  inline Scalar_t operator()( Coordinate_t x, Coordinate_t y,
80  Coordinate_t z ) const
81  __attribute__((__deprecated__("use value() method instead")))
82  {
83  return do_interpolation( x, y, z );
84  }
86  inline Scalar_t operator()( const Interpolator::Point_t &point ) const
87  __attribute__((__deprecated__("use value() method instead")))
88  {
89  return do_interpolation( point[ 0 ], point[ 1 ], point[ 2 ] );
90  }
92  inline void operator()( Coordinate_t x, Coordinate_t y, Coordinate_t z,
93  std::vector<Scalar_t> & v ) const
94  __attribute__((__deprecated__("use values() method instead")))
95  {
96  do_interpolations( x, y, z, v );
97  }
99  inline void operator()( const Interpolator::Point_t &point,
100  std::vector<Scalar_t> &v ) const
101  __attribute__((__deprecated__("use values() method instead")))
102  {
103  do_interpolations( point[ 0 ], point[ 1 ], point[ 2 ], v );
104  }
105 
107  inline Scalar_t value( Coordinate_t x, Coordinate_t y,
108  Coordinate_t z ) const
109  {
110  return do_interpolation(x, y, z);
111  }
113  inline Scalar_t value( const Interpolator::Point_t &point ) const
114  {
115  return do_interpolation( point[ 0 ], point[ 1 ], point[ 2 ] );
116  }
118  inline void values( Coordinate_t x, Coordinate_t y, Coordinate_t z,
119  std::vector<Scalar_t> &v ) const
120  {
121  do_interpolations(x, y, z, v);
122  }
124  inline void values( const Interpolator::Point_t &point,
125  std::vector<Scalar_t> &v ) const
126  {
127  do_interpolations( point[ 0 ], point[ 1 ], point[ 2 ], v );
128  }
131  virtual const carto::PropertySet &header() const = 0;
132 private:
133  virtual Scalar_t do_interpolation( Coordinate_t x, Coordinate_t y,
134  Coordinate_t z ) const = 0;
135  virtual void do_interpolations( Coordinate_t x,
136  Coordinate_t y,
137  Coordinate_t z,
138  std::vector<Scalar_t> &v ) const = 0;
139 };
140 
141 
142  //--------------------------------//
143  // linear interpolator factories //
144 //--------------------------------//
145 //-----------------------------------------------------------------------------
147 template <typename T>
149 
150 
151  //--------------------------//
152  // LinearInterpolator<T> //
153 //--------------------------//
154 
155 class LinearInterpolatorFactory;
156 
157 //-----------------------------------------------------------------------------
158 template <typename T>
160 {
161 
162 public:
163  LinearInterpolator( const AimsData<T> & image );
164  virtual ~LinearInterpolator();
165 
166  bool isValid( Coordinate_t x, Coordinate_t y, Coordinate_t z ) const;
167 
168  virtual const carto::PropertySet &header() const;
169 
170 private:
171  const AimsData<T> _image;
172 
173  Scalar_t do_interpolation( Coordinate_t x, Coordinate_t y,
174  Coordinate_t z ) const;
175  void do_interpolations( Coordinate_t x, Coordinate_t y, Coordinate_t z,
176  std::vector<Scalar_t> & ) const;
177 
178  inline static
179  void _interpolationCoefficients( Coordinate_t xx,
180  int &x,
181  int &X,
182  Coordinate_t &ax,
183  int dx, float sx );
184 
185  inline static
186  Scalar_t linint(Scalar_t a, Scalar_t b, Scalar_t x) { return a + x*(b-a); }
187 
188 
189  int _dimX, _dimY, _dimZ;
190  float _invsizeX, _invsizeY, _invsizeZ;
191 };
192 
193 //-----------------------------------------------------------------------------
194 extern template class LinearInterpolator<uint8_t>;
195 extern template class LinearInterpolator<int8_t>;
196 extern template class LinearInterpolator<uint16_t>;
197 extern template class LinearInterpolator<int16_t>;
198 extern template class LinearInterpolator<uint32_t>;
199 extern template class LinearInterpolator<int32_t>;
200 extern template class LinearInterpolator<float>;
201 extern template class LinearInterpolator<double>;
202 
203 
204  //--------------------------------//
205  // linear interpolator factories //
206 //--------------------------------//
207 
208 //-----------------------------------------------------------------------------
209 template <typename T>
210 inline
212 {
214 }
215 
216 
217 //-----------------------------------------------------------------------------
218 extern template carto::rc_ptr< Interpolator >
220 extern template carto::rc_ptr< Interpolator >
222 extern template carto::rc_ptr< Interpolator >
224 extern template carto::rc_ptr< Interpolator >
226 extern template carto::rc_ptr< Interpolator >
228 extern template carto::rc_ptr< Interpolator >
230 extern template carto::rc_ptr< Interpolator >
232 extern template carto::rc_ptr< Interpolator >
234 extern template carto::rc_ptr< Interpolator >
236 
237 
238  //--------------------------//
239  // LinearInterpolator<T> //
240 //--------------------------//
241 
242 //-----------------------------------------------------------------------------
243 template <typename T>
244 inline
246  _image( image )
247 {
248  _dimX = _image.dimX();
249  _dimY = _image.dimY();
250  _dimZ = _image.dimZ();
251 
252  assert(_image.sizeX() > 0);
253  _invsizeX = float(1.0 / _image.sizeX());
254  assert(_image.sizeY() > 0);
255  _invsizeY = float(1.0 / _image.sizeY());
256  assert(_image.sizeZ() > 0);
257  _invsizeZ = float(1.0 / _image.sizeZ());
258 }
259 
260 
261 //-----------------------------------------------------------------------------
262 template <typename T>
263 inline
265 {
266 }
267 
268 //-----------------------------------------------------------------------------
269 template <typename T>
270 inline
274 {
275  const Interpolator::Coordinate_t hx = - _image.sizeX() / 2;
276  const Interpolator::Coordinate_t hy = - _image.sizeY() / 2;
277  const Interpolator::Coordinate_t hz = - _image.sizeZ() / 2;
278  return x >= hx && y >= hy && z >= hz &&
279  x < _image.dimX() * _image.sizeX() + hx &&
280  y < _image.dimY() * _image.sizeY() + hy &&
281  z < _image.dimZ() * _image.sizeZ() + hz;
282 }
283 
284 
285 //-----------------------------------------------------------------------------
286 template <typename T>
287 inline
290  int & x,
291  int & X,
293  int dx, float isx)
294 {
295  const Interpolator::Coordinate_t px = xx * isx;
296 
297  x = int(px);
298  X = x+1;
299  if ( px < 0 )
300  {
301  x = X = 0;
302  ax = 1;
303  }
304  else if (X >= dx)
305  {
306  X = x = dx - 1;
307  ax = 0;
308  }
309  else
310  {
311  ax = px - x;
312  assert(ax >= 0);
313  assert(ax <= 1);
314  }
315 }
316 
317 
318 //-----------------------------------------------------------------------------
319 template <typename T>
323  Interpolator::Coordinate_t zz ) const
324 {
325  int x, X, y, Y, z, Z;
326  Interpolator::Coordinate_t ax, ay, az;
327  _interpolationCoefficients(xx, x, X, ax, _dimX, _invsizeX);
328  _interpolationCoefficients(yy, y, Y, ay, _dimY, _invsizeY);
329  _interpolationCoefficients(zz, z, Z, az, _dimZ, _invsizeZ);
330 
331  return
332  linint(
333  linint(
334  linint(_image( x, y, z ), _image( X, y, z ), ax),
335  linint(_image( x, Y, z ), _image( X, Y, z ), ax), ay),
336  linint(
337  linint(_image( x, y, Z ), _image( X, y, Z ), ax),
338  linint(_image( x, Y, Z ), _image( X, Y, Z ), ax), ay), az);
339 }
340 
341 
342 //-----------------------------------------------------------------------------
343 template <typename T>
348  std::vector< Interpolator::Scalar_t > &values ) const
349 {
350  values.resize( _image.dimT() );
351  int x, X, y, Y, z, Z;
352  Interpolator::Coordinate_t ax, ay, az;
353  _interpolationCoefficients(xx, x, X, ax, _dimX, _invsizeX);
354  _interpolationCoefficients(yy, y, Y, ay, _dimY, _invsizeY);
355  _interpolationCoefficients(zz, z, Z, az, _dimZ, _invsizeZ);
356 
357  for( int t = 0; t < _image.dimT(); ++t ) {
358  values[ t ] =
359  linint(
360  linint(
361  linint(_image( x, y, z, t ), _image( X, y, z, t ), ax),
362  linint(_image( x, Y, z, t ), _image( X, Y, z, t ), ax),
363  ay),
364  linint(
365  linint(_image( x, y, Z, t ), _image( X, y, Z, t ), ax),
366  linint(_image( x, Y, Z, t ), _image( X, Y, Z, t ), ax),
367  ay), az);
368  }
369 }
370 
371 
372 //-----------------------------------------------------------------------------
373 template <typename T>
374 inline
376 {
377  return _image.volume()->header();
378 }
379 
380 } // namespace aims
381 
382 #endif // ifdef AIMS_LINEAR_INTERPOLATOR
carto::rc_ptr< Interpolator > getLinearInterpolator(const std::string &)
#define __deprecated__(msg)
void operator()(const Interpolator::Point_t &point, std::vector< Scalar_t > &v) const __attribute__((__deprecated__("use values() method instead")))
Scalar_t value(const Interpolator::Point_t &point) const
Interpolate to get a value from point.
void operator()(Coordinate_t x, Coordinate_t y, Coordinate_t z, std::vector< Scalar_t > &v) const __attribute__((__deprecated__("use values() method instead")))
bool isValid(const Interpolator::Point_t &point) const
Return true if point can be used for interpolation.
virtual bool isValid(Coordinate_t x, Coordinate_t y, Coordinate_t z) const =0
Return true if point ( x, y, z ) can be used for interpolation.
The class for EcatSino data write operation.
Definition: border.h:44
void values(Coordinate_t x, Coordinate_t y, Coordinate_t z, std::vector< Scalar_t > &v) const
Interpolate to get a series of values from point ( x, y ,z )
Scalar_t operator()(Coordinate_t x, Coordinate_t y, Coordinate_t z) const __attribute__((__deprecated__("use value() method instead")))
class __attribute__((__deprecated__("Use Volume, which now manages views, instead."))) VolumeView
bool isValid(Coordinate_t x, Coordinate_t y, Coordinate_t z) const
Return true if point ( x, y, z ) can be used for interpolation.
virtual const carto::PropertySet & header() const =0
Return the header of the image.
void values(const Interpolator::Point_t &point, std::vector< Scalar_t > &v) const
Interpolate to get a series of values from point ( x, y ,z )
LinearInterpolator(const AimsData< T > &image)
Scalar_t value(Coordinate_t x, Coordinate_t y, Coordinate_t z) const
Interpolate to get a value from point ( x, y ,z ).
virtual ~Interpolator()
Scalar_t operator()(const Interpolator::Point_t &point) const __attribute__((__deprecated__("use value() method instead")))
virtual const carto::PropertySet & header() const
Return the header of the image.