aimsdata 6.0.0
Neuroimaging data handling
texdata.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#ifndef AIMS_TEXDATA_TEXDATA_H
35#define AIMS_TEXDATA_TEXDATA_H
36
37#include <aims/mesh/surface.h>
39#include <aims/mesh/texture.h>
40#include <cartodata/volume/volume.h>
43#include <aims/io/writer.h>
44#include <aims/io/reader.h>
45#include <vector>
46#include <set>
47#include <utility>
48
49//=================== types ======================================
50
51template<typename T> struct SiteType {
52 typedef T type;
53};
54
55template<typename T> struct TexType {
56 typedef T type;
57};
58
59// specialisations de SiteType
60
61template<typename T> class SiteType<carto::VolumeRef<T> > { // Images
62 public:
63 typedef Point3d type;
64};
65
66template<int D> class SiteType<AimsSurface<D, Void> > { // surface
67 public:
68 typedef std::pair<Point3df,uint> type;
69};
70
71template<typename T> class TexType<carto::VolumeRef<T> > { // texture image
72 public:
73 typedef T type;
74};
75
76template<typename T> class TexType<Texture<T> > {// texture surface
77 public:
78 typedef T type;
79};
80
81//========== iterateur g��ique ==================================
82
83template<typename S> class SiteIterator {
84 public:
85 typedef typename SiteType<S>::type Site;
87 //const Site & operator * () const;
89 SiteIterator<S> & operator --(); // optionnel
90 bool operator == ( const SiteIterator<S> & other ) const;
91 bool operator != ( const SiteIterator<S> & other ) const;
92 // etc
93};
94
95// iterateur semi-specialise pour images
96
97template<typename T> class SiteIterator<carto::VolumeRef<T> > {
98 public:
100 Site operator *() { return _pos; }
101 //const Site & operator *() const { return (*_data)( _pos ); }
104 SiteIterator( const carto::VolumeRef<T> *data, const Site & pos );
105 bool operator == ( const SiteIterator<carto::VolumeRef<T> > & other ) const;
106 bool operator != ( const SiteIterator<carto::VolumeRef<T> > & other ) const;
107 private:
108 const carto::VolumeRef<T> *_data;
109 Site _pos;
110};
111
112// iterateur semi-specialise pour surfaces
113
114template<int D> class SiteIterator<AimsSurface<D,Void> > {
115 public:
117
118 const Site operator *() { return std::pair<Point3df,uint>((*_coordinates)[ _index], _index); }
119 //const Site & operator *() const { return _data->vertex()[ _index ]; }
122 SiteIterator( const AimsSurface<D,Void> *data, uint index );
123 SiteIterator( const AimsSurface<D,Void> * data, std::vector<Point3df> *coords, uint index );
124 bool operator == ( const SiteIterator<AimsSurface<D,Void> > & other ) const;
125 bool operator != ( const SiteIterator<AimsSurface<D,Void> > & other ) const;
126 private:
127 const AimsSurface<D,Void> *_data;
128 uint _index;
129 std::vector<Point3df> *_coordinates;
130};
131
132
133
134//======== Classe g��ale TexturedData ============================
135
136// La classe generique qui nous interesse
137
138template<typename Geom, typename Text> class TexturedData {
139 // Geom est la geometrie (AimsSurface ou carto::VolumeRef)
140 // Tex est la texture (Texture ou carto::VolumeRef)
141
142 public:
143 typedef typename SiteType<Geom>::type Site; // j'espere que cette ruse marche
144 typedef typename TexType<Text>::type Tex;
145 SiteIterator<Geom> siteBegin(); // pointe sur le premier site
146 SiteIterator<Geom> siteEnd(); // pointe juste apres le dernier site
147 std::vector<Site> neighbours( const Site & pos ); // acces aux voisins de <pos>
148 Tex & intensity( const Site & pos );
149 int NbSites(); // nb of sites (comes handy sometime)
150};
151
152
153
154//=============================================================================
155// Specialisation de TexturedData pour images
156//=============================================================================
157
158template<typename T> class TexturedData<carto::VolumeRef<T>, carto::VolumeRef<T> >
159{
160public:
161 // interface standard, commune
163 typedef T Tex;
164 SiteIterator<carto::VolumeRef<T> > siteBegin(); // pointe sur le premier site
165 SiteIterator<carto::VolumeRef<T> > siteEnd(); // pointe juste apres le dernier site
166 std::vector<Site> neighbours( const Site & pos ); // acces aux voisins de <pos>
167 Tex & intensity( const Site & pos );
168 const Tex & intensity( const Site & pos ) const;
169
171 TexturedData( carto::VolumeRef<T> *data ){_data=data;} // pass an image
173 // copy constructor
174 TexturedData(int dimx, int dimy, int dimz, int dimt = 1,
175 int borderw = 0);
176
177 // Writer
178
179 void write(char *name) {aims::Writer<carto::VolumeRef<T> > dataW(name); dataW.write(*_data);}
180
181 // Getting the image
182
183 carto::VolumeRef<T> *GetImage() {return _data;}
184
185 // nb of sites (comes handy sometime)
186
187 int NbSites() {return (_data->getSizeX()*_data->getSizeY()*_data->getSizeZ());}
188
190
191
192private:
193 carto::VolumeRef<T> *_data; // ou compteur de reference rc_ptr (cf libcartobase)
194};
195
196
197//=============================================================================
198// Specialisation de TexturedData pour surfaces
199//=============================================================================
200
201template<int D, class T> class TexturedData<AimsSurface<D,Void>, Texture<T> > {
202 public:
203 // interface standard, commune
205 typedef T Tex;
206 SiteIterator<AimsSurface<D,Void> > siteBegin(); // pointe sur le premier site
207 SiteIterator<AimsSurface<D,Void> > siteEnd(); // pointe juste apres le dernier site
208 std::vector<Site> neighbours( const Site & pos ); // acces aux voisins de <pos>
209 Tex & intensity( const Site & pos );
210 const Tex & intensity( const Site & pos ) const;
213 _mesh = mesh;
214 _tex = tex;
215 _coordinates = NULL;
216 }
217 // pass mesh and texture
218 TexturedData ( AimsSurface<D, Void> *mesh, Texture<T> *tex, std::vector<Point3df> *coords ) :
219 _mesh(mesh), _tex(tex), _coordinates(coords) { }
220
222 //copy constructor
223 // Writer
224 void write (char *name) {
225 TimeTexture<T> timetext;
226 timetext[0] = *_tex;
227 aims::Writer<TimeTexture<T> > writerT(name);
228 writerT.write(timetext);
229 }
230
231 // Getting the texture and geometry
232 AimsSurface<D,Void> *GetMesh() { return _mesh; }
233 Texture<T> *GetTexture() { return _tex; }
234
236
237 // nb of sites (comes handy sometime)
238
239 int NbSites() { return _mesh->vertex().size(); }
240
241 private:
242 AimsSurface<D,Void> *_mesh;
243 Texture<T> *_tex;
244 std::vector<std::set<uint> > _allNeighbours;
245 std::vector<Point3df> *_coordinates;
246
247};
248
249
250//=================================================================================
251// Definitions
252//=================================================================================
253
254template<typename T> bool
256{
257 if ((_pos[0]==other._pos[0]) && (_pos[1]==other._pos[1]) && (_pos[2]==other._pos[2])) return true;
258 else return false;
259}
260
261template<typename T> bool
263{
264 if ((this->_pos[0] != other._pos[0]) || (this->_pos[1] != other._pos[1]) || (this->_pos[2] != other._pos[2])) return true;
265 else return false;
266}
267
268template<int D> bool
270{
271 if (this->_index == other._index) return true;
272 else return false;
273}
274
275template<int D> bool
277{
278 if (this->_index != other._index) return true;
279 else return false;
280}
281
282
283template<typename T>
285{
286 _data=data;
287 _pos=pos;
288}
289
290template<typename T> SiteIterator<carto::VolumeRef<T> > &
292{
293 if ( _pos[0] < (_data->getSizeX() -1) )
294 _pos[0]++;
295 else if ( _pos[1] < (_data->getSizeY() -1) )
296 {
297 _pos[0]=0;
298 _pos[1]++;
299 }
300 else
301 {
302 _pos[0]=0;
303 _pos[1]=0;
304 _pos[2]++;
305 }
306 return(*this);
307}
308
309template<typename T> SiteIterator<carto::VolumeRef<T> > &
311{
312 if ( _pos[0] > 0 )
313 _pos[0]--;
314 else if ( _pos[1] > 0 )
315 {
316 _pos[0]=(_data->getSizeX() -1);
317 _pos[1]--;
318 }
319 else
320 {
321 _pos[0]=(_data->getSizeX() -1);
322 _pos[1]=(_data->getSizeY() -1);
323 _pos[2]--;
324 }
325 return(*this);
326}
327
328//----------------------------
329
330template<int D>
332{
333 _data=data;
334 _index=index;
335 _coordinates = (std::vector<Point3df> *)(&(data->vertex()));
336}
337
338template<int D>
339 SiteIterator<AimsSurface<D,Void> >::SiteIterator( const AimsSurface<D,Void> * data, std::vector<Point3df> *coords, uint index )
340{
341 _data=data;
342 _index=index;
343 _coordinates = coords;
344
345}
346
347template<int D> SiteIterator<AimsSurface<D,Void> > &
349{
350 _index++;
351 return(*this);
352}
353
354template<int D> SiteIterator<AimsSurface<D,Void> > &
356{
357 _index--;
358 return(*this);
359}
360
361//----------------------------
362
363template<typename T>
365{
366 (*this)._data=new carto::VolumeRef<T>;
367 *((*this)._data)=(*(other._data)).copy();
368}
369
370template<typename T>
371TexturedData<carto::VolumeRef<T>, carto::VolumeRef<T> >::TexturedData(int dimx, int dimy, int dimz, int dimt, int borderw)
372{
373 _data=new carto::VolumeRef<T>(dimx, dimy, dimz, dimt, borderw);
374}
375
376template<typename T> SiteIterator<carto::VolumeRef<T> >
378{
379 Point3d point(0,0,0);
380 SiteIterator<carto::VolumeRef<T> > iter((*this)._data, point);
381
382 return iter;
383}
384
385template<typename T> SiteIterator<carto::VolumeRef<T> >
387{
388 Point3d point(0, 0, _data->getSizeZ());
389 SiteIterator<carto::VolumeRef<T> > iter((*this)._data, point);
390
391 return iter;
392}
393
394template<typename T> std::vector<typename SiteType<carto::VolumeRef<T> >::type>
396{
397 // 26-connectivity so far ...
398 std::vector<Point3d> neigh;
399 int x=pos[0], y=pos[1], z=pos[2];
400 Point3d point;
401 int i, j, k;
402
403 for (i=-1; i<=1; i=i+1)
404 for (j=-1; j<=1; j=j+1)
405 for (k=-1; k<=1; k=k+1)
406 {
407 if ((i!=0) || (j!=0) || (k!=0))
408 {
409 if ( ((x+i)>=0) && ((x+i)<_data->getSizeX()) && ((y+j)>=0) && ((y+j)<_data->getSizeY()) && ((z+k)>=0) && ((z+k)<_data->getSizeZ()) )
410 {
411 point[0]=x+i;
412 point[1]=y+j;
413 point[2]=z+k;
414 neigh.push_back(point);
415 }
416 }
417 }
418 return neigh;
419}
420
421template<typename T> T &
423{
424 return (*_data)(pos);
425}
426
427template<typename T> const T &
429{
430 return (*_data)(pos);
431}
432
433//----------------------------
434
435template<int D, class T>
437{
438 (*this)._tex=new Texture<T>(*(other._tex));
439 (*this)._mesh=new AimsSurface<D,Void>(*(other._mesh));
440 if (other._coordinates != NULL ){
441 (*this)._coordinates = other._coordinates;
442 }
443 else {
444 (*this)._coordinates=NULL;
445 }
446}
447
448
449template<int D, class T> SiteIterator<AimsSurface<D,Void> >
451{
452 if (_coordinates == NULL) {
453 SiteIterator<AimsSurface<D,Void> > iter(_mesh, 0);
454 return iter;
455 }
456 else{
457 SiteIterator<AimsSurface<D,Void> > iter(_mesh, _coordinates, 0);
458 return iter;
459 }
460}
461
462template<int D, class T> SiteIterator<AimsSurface<D,Void> >
464{
465 if (_coordinates == NULL ) {
466 SiteIterator<AimsSurface<D,Void> > iter(_mesh, _mesh->vertex().size());
467 return iter;
468 }
469 else {
470 SiteIterator<AimsSurface<D,Void> > iter(_mesh, _coordinates, _mesh->vertex().size());
471 return iter;
472 }
473}
474
475template<int D, class T>
476std::vector<typename SiteType<AimsSurface<D,Void> >::type>
478{
479 int i,j,k,n;
480
481 if (_allNeighbours.size()==0)
482 {
483 _allNeighbours=std::vector<std::set<uint> >( (*_mesh).vertex().size());
484 n=(*_mesh).polygon().size();
485 for( i=0; i<n; ++i )
486 for( j=0; j<D; ++j )
487 for( k=0; k<D; ++k )
488 if( j != k )
489 _allNeighbours[(*_mesh).polygon()[i][j]].insert( (*_mesh).polygon()[i][k] );
490 }
491
492 std::vector<Site> out;
493 std::set<uint>::iterator ptNeigh=_allNeighbours[pos.second].begin();
494
495 for ( ; ptNeigh != _allNeighbours[pos.second].end() ; ++ptNeigh)
496 out.push_back(std::pair<Point3df,uint>(_mesh->vertex()[*ptNeigh], *ptNeigh));
497
498 return(out);
499}
500
501
502template<int D, class T> T &
504{
505 return((*_tex).item(pos.second));
506}
507
508template<int D, class T> const T &
510{
511 return((*_tex).item(pos.second));
512}
513
514template <typename T> inline TexturedData<carto::VolumeRef<T>, carto::VolumeRef<T> > &
516{
517 (*this)._data=new carto::VolumeRef<T>;
518 (*(*this)._data)=(*(other._data)).copy();
519 return *this;
520}
521
522template < int D, class T> inline TexturedData<AimsSurface<D,Void>, Texture<T> > &
524{
525 (*this)._mesh=other._mesh;
526 (*this)._tex=other._tex;
527 if (other._coordinates != NULL){
528 (*this)._coordinates=other._coordinates;
529 }
530 else {
531 (*this)._coordinates=NULL;
532 }
533 return *this;
534}
535
536#endif
The template class to manage a mesh.
Definition surface.h:69
const std::vector< Point3df > & vertex() const
Get a const reference to the vector of vertices.
Definition surface.h:93
SiteType< AimsSurface< D, Void > >::type Site
Definition texdata.h:116
SiteIterator(const AimsSurface< D, Void > *data, uint index)
Definition texdata.h:331
SiteType< carto::VolumeRef< T > >::type Site
Definition texdata.h:99
SiteIterator(const carto::VolumeRef< T > *data, const Site &pos)
Definition texdata.h:284
SiteIterator< S > & operator--()
bool operator!=(const SiteIterator< S > &other) const
bool operator==(const SiteIterator< S > &other) const
SiteIterator< S > & operator++()
SiteType< S >::type Site
Definition texdata.h:85
Site & operator*()
std::pair< Point3df, uint > type
Definition texdata.h:68
TexturedData(AimsSurface< D, Void > *mesh, Texture< T > *tex)
Definition texdata.h:212
SiteType< AimsSurface< D, Void > >::type Site
Definition texdata.h:204
SiteIterator< AimsSurface< D, Void > > siteBegin()
Definition texdata.h:450
std::vector< Site > neighbours(const Site &pos)
Definition texdata.h:477
TexturedData(AimsSurface< D, Void > *mesh, Texture< T > *tex, std::vector< Point3df > *coords)
Definition texdata.h:218
SiteIterator< AimsSurface< D, Void > > siteEnd()
Definition texdata.h:463
SiteIterator< carto::VolumeRef< T > > siteEnd()
Definition texdata.h:386
SiteType< carto::VolumeRef< T > >::type Site
Definition texdata.h:162
std::vector< Site > neighbours(const Site &pos)
Definition texdata.h:395
SiteIterator< carto::VolumeRef< T > > siteBegin()
Definition texdata.h:377
TexType< Text >::type Tex
Definition texdata.h:144
SiteIterator< Geom > siteEnd()
SiteType< Geom >::type Site
Definition texdata.h:143
Tex & intensity(const Site &pos)
std::vector< Site > neighbours(const Site &pos)
SiteIterator< Geom > siteBegin()
Generic writer for every format of Aims object.
Definition writer.h:94
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
Finds the correct format and writes the object.
Definition writer_d.h:108
VolumeRef< T > copy() const
T type
Definition texdata.h:56
unsigned int uint
AimsVector< int16_t, 3 > Point3d