A.I.M.S algorithms


resampler_d.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 #ifndef AIMS_RESAMPLING_RESAMPLER_D_H
36 #define AIMS_RESAMPLING_RESAMPLER_D_H
37 
40 #include <iomanip>
41 #include <stdexcept>
42 
43 template < typename T >
44 void
46  const Motion& transform3d,
47  const T& outBackground,
48  AimsData< T >& outVolume,
49  bool verbose )
50 {
51 
52  Point3df inResolution;
53  inResolution[0] = inVolume.sizeX();
54  inResolution[1] = inVolume.sizeY();
55  inResolution[2] = inVolume.sizeZ();
56 
57  Point3df outResolution;
58  outResolution[0] = outVolume.sizeX();
59  outResolution[1] = outVolume.sizeY();
60  outResolution[2] = outVolume.sizeZ();
61 
62  Motion normTransform3d;
63 
64  normTransform3d.scale( inResolution, Point3df( 1, 1, 1 ) );
65  normTransform3d = transform3d * normTransform3d;
66  normTransform3d = normTransform3d.inverse();
67 
68  int outSizeX = outVolume.dimX();
69  int outSizeY = outVolume.dimY();
70  int outSizeZ = outVolume.dimZ();
71  int outSizeT = outVolume.dimT();
72  if( outSizeT > inVolume.dimT() )
73  outSizeT = inVolume.dimT();
74 
75  typename AimsData< T >::iterator o;
76 
77  if ( verbose )
78  {
79 
80  std::cout << std::setw( 4 ) << 0;
81  if( outSizeT > 1 )
82  std::cout << ", t: " << std::setw( 4 ) << 0;
83  std::cout << std::flush;
84 
85  }
86  Point3df outLoc;
87  int x, y, z, t;
88  for ( t = 0; t < outSizeT; t++ )
89  {
90  updateParameters( inVolume, t, verbose );
91  outLoc = Point3df( 0.0, 0.0, 0.0 );
92 
93  for ( z = 0; z < outSizeZ; z++ )
94  {
95 
96  for ( y = 0; y < outSizeY; y++ )
97  {
98  o = &outVolume( 0, y, z, t );
99 
100  for ( x = 0; x < outSizeX; x++ )
101  {
102 
103  doResample( inVolume, normTransform3d, outBackground,
104  outLoc, *o, t );
105  ++ o;
106  outLoc[0] += outResolution[0];
107 
108  }
109  outLoc[1] += outResolution[1];
110  outLoc[0] = 0.0;
111 
112  }
113  outLoc[2] += outResolution[2];
114  outLoc[1] = 0.0;
115 
116  if ( verbose )
117  {
118 
119  if( outSizeT > 1 )
120  std::cout << "\b\b\b\b\b\b\b\b\b";
121  std::cout << "\b\b\b\b" << std::setw( 4 ) << z + 1;
122  if( outSizeT > 1 )
123  std::cout << ", t: " << std::setw( 4 ) << t;
124  std::cout << std::flush;
125 
126  }
127 
128  }
129 
130  }
131 
132 }
133 
134 
135 template < typename T >
136 void
138  const Motion& transform3d,
139  const T& outBackground,
140  const Point3df& outLocation,
141  T& outValue, int t )
142 {
143 
144  Point3df inResolution;
145  inResolution[0] = inVolume.sizeX();
146  inResolution[1] = inVolume.sizeY();
147  inResolution[2] = inVolume.sizeZ();
148 
149  Motion normTransform3d;
150 
151  normTransform3d.scale( inResolution, Point3df( 1, 1, 1 ) );
152  normTransform3d = transform3d * normTransform3d;
153  normTransform3d = normTransform3d.inverse();
154 
155  doResample( inVolume, normTransform3d, outBackground, outLocation,
156  outValue, t );
157 
158 }
159 
160 
161 template <typename T>
163  bool )
164 {
165 }
166 
167 
168 template <typename T>
169 void Resampler<T>::doit( const Motion& motion, AimsData<T>& thing )
170 {
171  if( !_ref )
172  throw std::runtime_error( "Resampler used without a ref volme to resample"
173  );
174  resample( *_ref, motion, _defval, thing, carto::verbose );
175 }
176 
177 
178 template <typename T>
179 AimsData<T> Resampler<T>::doit( const Motion& motion, int dimX, int dimY,
180  int dimZ, const Point3df& resolution )
181 {
182  if( !_ref )
183  throw std::runtime_error( "Resampler used without a ref volme to resample"
184  );
185  AimsData<T> thing( dimX, dimY, dimZ, _ref->dimT() );
186  thing.setSizeXYZT( resolution[0], resolution[1], resolution[2],
187  _ref->sizeT() );
188 
189  resample( *_ref, motion, _defval, thing, carto::verbose );
190  thing.setHeader( _ref->header()->cloneHeader() );
191  thing.setSizeXYZT( resolution[0], resolution[1], resolution[2],
192  _ref->sizeT() );
193  if( !motion.isIdentity() )
194  {
196  *ph = dynamic_cast<aims::PythonHeader *>( thing.header() );
197  try
198  {
199  // remove any referential ID since we are no longer in the same ref
200  ph->removeProperty( "referential" );
201  }
202  catch( ... )
203  {
204  }
205  try
206  {
207  if( ph )
208  {
209  carto::Object trs = ph->getProperty( "transformations" );
210  carto::Object tit = trs->objectIterator();
211  Motion motioninv = motion.inverse();
212  std::vector<std::vector<float> > trout;
213  trout.reserve( trs->size() );
214  for( ; tit->isValid(); tit->next() )
215  {
216  Motion m( tit->currentValue() );
217  m *= motioninv;
218  trout.push_back( m.toVector() );
219  }
220  ph->setProperty( "transformations", trout );
221  }
222  }
223  catch( ... )
224  {
225  // setup a new transformations list
226  std::vector<std::vector<float> > trout;
227  std::vector<std::string> refsout;
228  const aims::PythonHeader
229  *iph = dynamic_cast<const aims::PythonHeader *>( _ref->header() );
230  if( iph )
231  try
232  {
233  carto::Object iref = iph->getProperty( "referential" );
234  std::string refid = iref->getString();
235  refsout.push_back( refid );
236  }
237  catch( ... )
238  {
239  }
240  if( refsout.empty() )
241  refsout.push_back( "Coordinates aligned to another file or to "
242  "anatomical truth" );
243 
244  trout.push_back( motion.inverse().toVector() );
245  ph->setProperty( "transformations", trout );
246  ph->setProperty( "referentials", refsout );
247  }
248  }
249  return thing;
250 }
251 
252 #endif
253 
virtual void scale(const Point3df &sizeFrom, const Point3df &sizeTo)
int dimX() const
float sizeY() const
T * iterator
int dimT() const
virtual bool isIdentity() const
virtual bool removeProperty(const std::string &)
int verbose
virtual void resample(const AimsData< T > &input_data, const aims::AffineTransformation3d &transform, const T &background, AimsData< T > &output_data, bool verbose=false)
Resample the input data into an existing output data.
Definition: resampler_d.h:45
virtual Object objectIterator() const =0
virtual void doit(const aims::AffineTransformation3d &transform, AimsData< T > &output_data)
Resample the reference input data (set via setRef()) into an existing output data.
Definition: resampler_d.h:169
float sizeZ() const
virtual bool getProperty(const std::string &, Object &) const
virtual Object currentValue() const =0
virtual void updateParameters(const AimsData< T > &inVolume, int time, bool verbose)
called before each time step, used in spline resamplers
Definition: resampler_d.h:162
float sizeX() const
int dimZ() const
virtual std::string getString() const =0
virtual void setProperty(const std::string &, Object)
void setSizeXYZT(float sizex=1.0f, float sizey=1.0f, float sizez=1.0f, float sizet=1.0f)
int dimY() const
std::vector< float > toVector() const
virtual size_t size() const =0
AffineTransformation3d inverse() const
virtual bool isValid() const =0