aimsalgo 6.0.0
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 <cartodata/volume/volume.h>
40#include <aims/io/writer.h>
42
43
44//
45// class Pyramid
46//
47template <class T>
49{
50 public:
52
53 Pyramid( const PyramidFunc<T>& func )
54 : _func( func ), _ref( 0 ), _level( 0 ), _ppItem ( 0 ), _axes(true, true, true) { }
55 virtual ~Pyramid() { _free_ppItem(); }
56
57 void setAxes(const AxesType & axes);
59 void setLevel( int level );
60 const carto::rc_ptr<carto::Volume<T> > item( int level ) const;
61 void save( const std::string& name ) const;
62
63 protected:
64
67 int _level;
69
70 void _new_ppItem( int level );
71 void _free_ppItem();
72 void _build();
73
74 // Axes along which pyramid is built
76};
77
78template <class T> inline
79void Pyramid<T>::setAxes(const AxesType & axes) {
80 _axes = axes;
81}
82
83template <class T> inline
85{
86 if ( _ref.get() )
87 {
89 }
90 _ref = ref;
91}
92
93
94template <class T> inline
95void Pyramid<T>::_new_ppItem( int level )
96{
97// std::cout << "Pyramid: Creating new array of " << carto::toString(level)
98// << " to store volumes" << std::endl << std::flush;
100
101 int dimX = _ref->getSizeX();
102 int dimY = _ref->getSizeY();
103 int dimZ = _ref->getSizeZ();
104 int dimT = _ref->getSizeT();
105
106 std::vector<float> vs = _ref->getVoxelSize();
107 float sizeX = vs[0];
108 float sizeY = vs[1];
109 float sizeZ = vs[2];
110 float sizeT = vs[3];
111
112 for ( int k = 0; k < level; k++ )
113 {
114 if (_axes[0]) {
115 dimX /= 2;
116 sizeX *= 2.0;
117 }
118 if (_axes[1]) {
119 dimY /= 2;
120 sizeY *= 2.0;
121 }
122 if (_axes[2]) {
123 dimZ /= 2;
124 sizeZ *= 2.0;
125 }
126 if( dimX == 0 )
127 dimX = 1 ;
128 if( dimY == 0 )
129 dimY = 1 ;
130 if( dimZ == 0 )
131 dimZ = 1 ;
132
133// std::cout << "Pyramid: Creating new volume ("
134// << carto::toString(dimX) << ","
135// << carto::toString(dimY) << ","
136// << carto::toString(dimZ) << ","
137// << carto::toString(dimT) << ") for level " << carto::toString(k) << std::endl << std::flush;
138 _ppItem[ k ] = carto::VolumeRef<T>( dimX, dimY, dimZ, dimT );
139 _ppItem[ k ]->setVoxelSize( sizeX, sizeY, sizeZ, sizeT );
140 }
141}
142
143template <class T> inline
145{
146 if ( _ppItem )
147 {
148 delete[] _ppItem;
149 _ppItem = 0;
150 }
151}
152
153
154template <class T> inline
155void Pyramid<T>::setLevel( int level )
156{
157 ASSERT( _ref.get() );
158
159 _free_ppItem();
160 _level = level;
161 _new_ppItem( level );
162
163 _build();
164}
165
166template <class T> inline
168{
169 int x, y, z, t, k, nx, ny, nz, n;
170
171 int dimX = _ref->getSizeX();
172 int dimY = _ref->getSizeY();
173 int dimZ = _ref->getSizeZ();
174 int dimT = _ref->getSizeT();
175
178
179 int count = 0;
180 for (int i = 0; i < 3; ++i)
181 if (_axes[i])
182 count++;
183
184 carto::VolumeRef<T> tab(pow(2, count));
185
186 for (k = 0; k < _level; k++)
187 {
188 pUp = _ppItem[k];
189
190 int lowerLvlDimX = dimX;
191 int lowerLvlDimY = dimY;
192 int lowerLvlDimZ = dimZ;
193
194 if (_axes[0])
195 dimX /= 2;
196 if (_axes[1])
197 dimY /= 2;
198 if (_axes[2])
199 dimZ /= 2;
200
201 if( dimX == 0 )
202 dimX = 1 ;
203 if( dimY == 0 )
204 dimY = 1 ;
205 if( dimZ == 0 )
206 dimZ = 1 ;
207
208 // std::cout << "Pyramid level " << carto::toString(k) << " dimensions: ["
209 // << carto::toString(dimX) << ", "
210 // << carto::toString(dimY) << ", "
211 // << carto::toString(dimZ) << "]"
212 // << std::endl << std::flush;
213
214 for (t = 0; t < dimT; t++)
215 for (z = 0; z < dimZ; z++)
216 for (y = 0; y < dimY; y++)
217 for (x = 0; x < dimX; x++)
218 {
219 n = 0;
220 for (nz = 0; nz < 2; nz++)
221 for (ny = 0; ny < 2; ny++)
222 for (nx = 0; nx < 2; nx++) {
223 // Add only values along selected axes
224 if ((!_axes[0] && nx > 0) ||
225 (!_axes[1] && ny > 0) ||
226 (!_axes[2] && nz > 0))
227 continue;
228
229 Point3d src;
230 src[0] = (_axes[0] ? std::min(2 * x + nx, lowerLvlDimX - 1) : x);
231 src[1] = (_axes[1] ? std::min(2 * y + ny, lowerLvlDimY - 1) : y);
232 src[2] = (_axes[2] ? std::min(2 * z + nz, lowerLvlDimZ - 1) : z);
233
234 tab(n++) = pDown->at(src[0], src[1], src[2], t);
235 }
236 pUp->at(x, y, z, t) = _func.doit(tab);
237 }
238 pDown = pUp;
239 }
240}
241
242
243template <class T> inline
245{
246 ASSERT( _ref.get() );
247 ASSERT( level <= _level );
248
249 if ( level == 0 )
250 return _ref;
251 else
252 return _ppItem[ level - 1 ];
253}
254
255
256template <class T> inline
257void Pyramid<T>::save( const std::string& name ) const
258{
259 for ( int k = 0; k < _level; k++ )
260 {
261 char ext[5];
262 sprintf( ext, "%d", k );
263 std::string outName = name + std::string( ext );
264 aims::Writer<carto::Volume<T> > dataW( outName.c_str() );
265 dataW.write( *item( k ) );
266 }
267}
268
269
270#endif
#define ASSERT(EX)
carto::rc_ptr< carto::Volume< T > > * _ppItem
Definition pyramid.h:68
carto::rc_ptr< carto::Volume< T > > _ref
Definition pyramid.h:66
void setLevel(int level)
Definition pyramid.h:155
AxesType _axes
Definition pyramid.h:75
const carto::rc_ptr< carto::Volume< T > > item(int level) const
Definition pyramid.h:244
void save(const std::string &name) const
Definition pyramid.h:257
void _new_ppItem(int level)
Definition pyramid.h:95
void _build()
Definition pyramid.h:167
virtual ~Pyramid()
Definition pyramid.h:55
const PyramidFunc< T > & _func
Definition pyramid.h:65
int _level
Definition pyramid.h:67
Pyramid(const PyramidFunc< T > &func)
Definition pyramid.h:53
AimsVector< bool, 3 > AxesType
Definition pyramid.h:51
void _free_ppItem()
Definition pyramid.h:144
void setRef(carto::rc_ptr< carto::Volume< T > > ref)
Definition pyramid.h:84
void setAxes(const AxesType &axes)
Definition pyramid.h:79
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
AimsVector< int16_t, 3 > Point3d