aimsalgo  5.1.2
Neuroimaging image processing
pyramid.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_PYRAMID_PYRAMID_H
36 #define AIMS_PYRAMID_PYRAMID_H
37 
38 #include <aims/def/assert.h>
40 #include <aims/io/writer.h>
41 #include <aims/pyramid/pyr-func.h>
42 
43 
44 //
45 // class Pyramid
46 //
47 template <class T>
48 class Pyramid
49 {
50  public:
51 
52  Pyramid( const PyramidFunc<T>& func )
53  : _func( func ), _ref( 0 ), _level( 0 ), _ppItem ( 0 ) { }
54  virtual ~Pyramid() { _free_ppItem(); }
55 
57  void setLevel( int level );
58  const carto::rc_ptr<carto::Volume<T> > item( int level ) const;
59  void save( const std::string& name ) const;
60 
61  protected:
62 
65  int _level;
67 
68  void _new_ppItem( int level );
69  void _free_ppItem();
70  void _build();
71 };
72 
73 
74 template <class T> inline
76 {
77  if ( _ref.get() )
78  {
79  setLevel( _level );
80  }
81  _ref = ref;
82 }
83 
84 
85 template <class T> inline
86 void Pyramid<T>::_new_ppItem( int level )
87 {
88 // std::cout << "Pyramid: Creating new array of " << carto::toString(level)
89 // << " to store volumes" << std::endl << std::flush;
90  _ppItem = new carto::rc_ptr<carto::Volume<T> >[ level ];
91 
92  int dimX = _ref->getSizeX();
93  int dimY = _ref->getSizeY();
94  int dimZ = _ref->getSizeZ();
95  int dimT = _ref->getSizeT();
96 
97  std::vector<float> vs = _ref->getVoxelSize();
98  float sizeX = vs[0];
99  float sizeY = vs[1];
100  float sizeZ = vs[2];
101  float sizeT = vs[3];
102 
103  for ( int k = 0; k < level; k++ )
104  {
105  dimX /= 2;
106  dimY /= 2;
107  dimZ /= 2;
108  if( dimX == 0 )
109  dimX = 1 ;
110  if( dimY == 0 )
111  dimY = 1 ;
112  if( dimZ == 0 )
113  dimZ = 1 ;
114 
115  sizeX *= 2.0;
116  sizeY *= 2.0;
117  sizeZ *= 2.0;
118 
119 // std::cout << "Pyramid: Creating new volume ("
120 // << carto::toString(dimX) << ","
121 // << carto::toString(dimY) << ","
122 // << carto::toString(dimZ) << ","
123 // << carto::toString(dimT) << ") for level " << carto::toString(k) << std::endl << std::flush;
124  _ppItem[ k ] = carto::VolumeRef<T>( dimX, dimY, dimZ, dimT );
125  _ppItem[ k ]->setVoxelSize( sizeX, sizeY, sizeZ, sizeT );
126  }
127 }
128 
129 template <class T> inline
131 {
132  if ( _ppItem )
133  {
134  delete[] _ppItem;
135  _ppItem = 0;
136  }
137 }
138 
139 
140 template <class T> inline
141 void Pyramid<T>::setLevel( int level )
142 {
143  ASSERT( _ref.get() );
144 
145  _free_ppItem();
146  _level = level;
147  _new_ppItem( level );
148 
149  _build();
150 }
151 
152 template <class T> inline
154 {
155  int x, y, z, t, k, nx, ny, nz, n;
156 
157  int dimX = _ref->getSizeX();
158  int dimY = _ref->getSizeY();
159  int dimZ = _ref->getSizeZ();
160  int dimT = _ref->getSizeT();
161 
163  carto::rc_ptr<carto::Volume<T> > pDown = _ref;
164 
165  carto::VolumeRef<T> tab( 8 );
166 
167  for ( k = 0; k < _level; k++ )
168  {
169  pUp = _ppItem[ k ];
170 
171  int lowerLvlDimX = dimX ;
172  int lowerLvlDimY = dimY ;
173  int lowerLvlDimZ = dimZ ;
174 
175  dimX /= 2;
176  dimY /= 2;
177  dimZ /= 2;
178 
179  if( dimX == 0 )
180  dimX = 1 ;
181  if( dimY == 0 )
182  dimY = 1 ;
183  if( dimZ == 0 )
184  dimZ = 1 ;
185  for ( t = 0; t < dimT; t++ )
186  for ( z = 0; z < dimZ; z++ )
187  for ( y = 0; y < dimY; y++ )
188  for ( x = 0; x < dimX; x++ )
189  {
190  n = 0;
191  for ( nz = 0; nz < 2; nz++ )
192  for ( ny = 0; ny < 2; ny++ )
193  for ( nx = 0; nx < 2; nx++ )
194  {
195  tab(n++) = pDown->at( std::min(2 * x + nx, lowerLvlDimX - 1 ),
196  std::min(2 * y + ny, lowerLvlDimY - 1 ),
197  std::min(2 * z + nz, lowerLvlDimZ - 1 ), t );
198  }
199  pUp->at( x, y, z, t ) = _func.doit( tab );
200  }
201  pDown = pUp;
202  }
203 }
204 
205 
206 template <class T> inline
208 {
209  ASSERT( _ref.get() );
210  ASSERT( level <= _level );
211 
212  if ( level == 0 )
213  return _ref;
214  else
215  return _ppItem[ level - 1 ];
216 }
217 
218 
219 template <class T> inline
220 void Pyramid<T>::save( const std::string& name ) const
221 {
222  for ( int k = 0; k < _level; k++ )
223  {
224  char ext[5];
225  sprintf( ext, "%d", k );
226  std::string outName = name + std::string( ext );
227  aims::Writer<carto::Volume<T> > dataW( outName.c_str() );
228  dataW.write( *item( k ) );
229  }
230 }
231 
232 
233 #endif
#define ASSERT(EX)
carto::rc_ptr< carto::Volume< T > > * _ppItem
Definition: pyramid.h:66
carto::rc_ptr< carto::Volume< T > > _ref
Definition: pyramid.h:64
void setLevel(int level)
Definition: pyramid.h:141
const carto::rc_ptr< carto::Volume< T > > item(int level) const
Definition: pyramid.h:207
void save(const std::string &name) const
Definition: pyramid.h:220
void _new_ppItem(int level)
Definition: pyramid.h:86
void _build()
Definition: pyramid.h:153
virtual ~Pyramid()
Definition: pyramid.h:54
const PyramidFunc< T > & _func
Definition: pyramid.h:63
int _level
Definition: pyramid.h:65
Pyramid(const PyramidFunc< T > &func)
Definition: pyramid.h:52
void _free_ppItem()
Definition: pyramid.h:130
void setRef(carto::rc_ptr< carto::Volume< T > > ref)
Definition: pyramid.h:75
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
float min(float x, float y)
Definition: thickness.h:106
reference_wrapper< T > ref(T &ref)