aimsalgo  5.0.5
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>
39 #include <aims/data/data_g.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 
56  void setRef( AimsData<T>& ref );
57  void setLevel( int level );
58  const AimsData<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 )
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  _ppItem = new AimsData<T>*[ level ];
89 
90  int dimX = _ref->dimX();
91  int dimY = _ref->dimY();
92  int dimZ = _ref->dimZ();
93  int dimT = _ref->dimT();
94 
95  float sizeX = _ref->sizeX();
96  float sizeY = _ref->sizeY();
97  float sizeZ = _ref->sizeZ();
98  float sizeT = _ref->sizeT();
99 
100  for ( int k = 0; k < level; k++ )
101  {
102  dimX /= 2;
103  dimY /= 2;
104  dimZ /= 2;
105  if( dimX == 0 )
106  dimX = 1 ;
107  if( dimY == 0 )
108  dimY = 1 ;
109  if( dimZ == 0 )
110  dimZ = 1 ;
111 
112  sizeX *= 2.0;
113  sizeY *= 2.0;
114  sizeZ *= 2.0;
115 
116  _ppItem[ k ] = new AimsData<T>( dimX, dimY, dimZ, dimT );
117  _ppItem[ k ]->setSizeXYZT( sizeX, sizeY, sizeZ, sizeT );
118  }
119 }
120 
121 template <class T> inline
123 {
124  if ( _ppItem )
125  {
126  for ( int k = 0; k < _level; k++ )
127  delete _ppItem[ k ];
128  delete[] _ppItem;
129  _ppItem = 0;
130  }
131 }
132 
133 
134 template <class T> inline
135 void Pyramid<T>::setLevel( int level )
136 {
137  ASSERT( _ref );
138 
139  _free_ppItem();
140  _level = level;
141  _new_ppItem( level );
142 
143  _build();
144 }
145 
146 template <class T> inline
148 {
149  int x, y, z, t, k, nx, ny, nz, n;
150 
151  int dimX = _ref->dimX();
152  int dimY = _ref->dimY();
153  int dimZ = _ref->dimZ();
154  int dimT = _ref->dimT();
155 
156  AimsData<T>* pUp;
157  AimsData<T>* pDown = _ref;
158 
159  AimsData<T> tab( 8 );
160 
161  for ( k = 0; k < _level; k++ )
162  {
163  pUp = _ppItem[ k ];
164 
165  int lowerLvlDimX = dimX ;
166  int lowerLvlDimY = dimY ;
167  int lowerLvlDimZ = dimZ ;
168 
169  dimX /= 2;
170  dimY /= 2;
171  dimZ /= 2;
172 
173  if( dimX == 0 )
174  dimX = 1 ;
175  if( dimY == 0 )
176  dimY = 1 ;
177  if( dimZ == 0 )
178  dimZ = 1 ;
179  for ( t = 0; t < dimT; t++ )
180  for ( z = 0; z < dimZ; z++ )
181  for ( y = 0; y < dimY; y++ )
182  for ( x = 0; x < dimX; x++ )
183  {
184  n = 0;
185  for ( nz = 0; nz < 2; nz++ )
186  for ( ny = 0; ny < 2; ny++ )
187  for ( nx = 0; nx < 2; nx++ ){
188  tab(n++) = (*pDown)( std::min(2 * x + nx, lowerLvlDimX - 1 ),
189  std::min(2 * y + ny, lowerLvlDimY - 1 ),
190  std::min(2 * z + nz, lowerLvlDimZ - 1 ), t );
191  }
192  (*pUp)( x, y, z, t ) = _func.doit( tab );
193  }
194  pDown = pUp;
195  }
196 }
197 
198 
199 template <class T> inline
200 const AimsData<T>& Pyramid<T>::item( int level ) const
201 {
202  ASSERT( _ref );
203  ASSERT( level <= _level );
204 
205  if ( level == 0 )
206  return *_ref;
207  else
208  return *_ppItem[ level - 1 ];
209 }
210 
211 
212 template <class T> inline
213 void Pyramid<T>::save( const std::string& name ) const
214 {
215  for ( int k = 0; k < _level; k++ )
216  {
217  char ext[5];
218  sprintf( ext, "%d", k );
219  std::string outName = name + std::string( ext );
220  aims::Writer<AimsData<T> > dataW( outName.c_str() );
221  dataW << item( k );
222  }
223 }
224 
225 
226 #endif
float min(float x, float y)
Definition: thickness.h:105
const AimsData< T > & item(int level) const
Definition: pyramid.h:200
void setLevel(int level)
Definition: pyramid.h:135
const PyramidFunc< T > & _func
Definition: pyramid.h:63
void save(const std::string &name) const
Definition: pyramid.h:213
AimsData< T > * _ref
Definition: pyramid.h:64
virtual ~Pyramid()
Definition: pyramid.h:54
Pyramid(const PyramidFunc< T > &func)
Definition: pyramid.h:52
int _level
Definition: pyramid.h:65
void setRef(AimsData< T > &ref)
Definition: pyramid.h:75
AimsData< T > ** _ppItem
Definition: pyramid.h:66
void _free_ppItem()
Definition: pyramid.h:122
void _new_ppItem(int level)
Definition: pyramid.h:86
void _build()
Definition: pyramid.h:147
#define ASSERT(EX)
reference_wrapper< T > ref(T &ref)