aimsdata  5.1.2
Neuroimaging data handling
texture.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  * Texture class
36  */
37 #ifndef AIMS_MESH_TEXTURE_H
38 #define AIMS_MESH_TEXTURE_H
39 
40 
41 #include <cartobase/smart/rcptr.h>
43 #include <aims/vector/vector.h>
44 #include <aims/def/general.h>
45 #include <aims/data/pheader.h>
46 #include <aims/math/dtitensor.h>
47 #include <vector>
48 #include <map>
49 #include <iostream>
50 
51 
52 template < class T = float > class Texture;
53 
54 template < class T >
55 AIMSDATA_API std::ostream& operator << ( std::ostream& out,
56  const Texture< T >& thing );
57 
58 //
59 // Basic texture
60 //
61 template < class T >
62 class Texture
63 {
64 public:
65  Texture() { }
66  Texture( size_t n ): _data( n ) { }
67  Texture( size_t n, const T & value ): _data( n, value ) { }
68  virtual ~Texture() { }
69 
70  size_t nItem() const { return _data.size(); }
72  size_t size() const { return _data.size(); }
73 
74  const T& item( int n ) const { return _data[ n ]; }
75  const T & operator [] ( int n ) const { return _data[ n ]; }
76  T & operator [] ( int n ) { return _data[ n ]; }
77  T& item( int n ) { return _data[ n ]; }
78 
79  void reserve( size_t size ) { _data.reserve( size ); }
80  void push_back( const T& item ) { _data.push_back( item ); }
81  const std::vector<T> & data() const { return( _data ); }
82  std::vector<T> & data() { return( _data ); }
83 
84  void erase() { _data.clear(); }
85  bool operator == ( const Texture<T> & x ) const { return _data == x.data(); }
86 
87  friend
88  std::ostream& operator << <>( std::ostream& out,
89  const Texture< T >& thing );
90 
91 protected:
92 
93  std::vector< T > _data;
94 };
95 
96 
97 template < class T > inline
98 std::ostream& operator << ( std::ostream& out, const Texture< T >& thing )
99 {
100  out << "{nItem=" << thing.nItem() << ", data=(";
101  for ( size_t n = 0; n < thing.nItem(); n++ )
102  out << thing.item( n ) << ", ";
103  out << "NULL)}";
104 
105  return out;
106 }
107 
108 
109 //
110 // Mixing surface and time
111 //
112 
113 template < class T = float > class TimeTexture;
114 
115 template < class T >
116 std::ostream& operator << ( std::ostream& out, const TimeTexture< T >& thing );
117 
118 template < class T >
119 class TimeTexture : public virtual carto::RCObject,
120  public std::map< int, Texture< T > >
121 {
122  public:
123  typedef typename std::map<int, Texture< T > >::iterator iterator;
124  typedef typename std::map<int, Texture< T > >::const_iterator
126 
127  TimeTexture() : std::map< int, Texture< T > >() { }
128  TimeTexture(int ntimes, size_t nitems) :
129  std::map< int, Texture< T > >()
130  {
131  for( int i=0; i<ntimes; ++i )
132  (*this)[i] = Texture<T>( nitems );
133  }
134  TimeTexture( int ntimes, size_t nitems, const T & value ) :
135  std::map< int, Texture< T > >()
136  {
137  for( int i=0; i<ntimes; ++i )
138  (*this)[i] = Texture<T>( nitems, value );
139  }
140  virtual ~TimeTexture() {}
141 
143  inline const aims::PythonHeader &header() const { return _header; }
144  inline aims::PythonHeader &header() { return _header; }
145 
147  void setHeader( const aims::PythonHeader &hdr ) { _header = hdr; }
148 
149  size_t nItem() const { return (*(TimeTexture< T >*)this)[0].nItem(); }
150 
151  const T& item( int n ) const
152  { return (*(TimeTexture< T >*)this)[0].item( n ); }
153  T& item( int n ) { return (*this)[0].item( n ); }
154 
155  void reserve( size_t size ) { (*this)[0].reserve( size ); }
156  void push_back( const T& item ) { (*this)[0].push_back( item ); }
157 
158  void erase();
159 
160  bool operator == ( const TimeTexture<T> & x ) const
161  {
162  if( x.nItem() != nItem() ) return false;
163  if( _header != x.header() ) return false;
164  for( int i=0; i<nItem(); ++i )
165  if( item(i) != x.item(i) ) return false;
166  return true;
167  }
168 
169  friend
170  std::ostream& operator << <>( std::ostream& out,
171  const TimeTexture< T >& thing );
172 
173 protected:
174 
177 };
178 
179 
180 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
181 
182 namespace carto
183 {
184 
185  template<class T> class DataTypeCode<TimeTexture<T> >
186  {
187  public:
188  static std::string objectType()
189  { return "Texture"; }
190  static std::string dataType()
191  { return DataTypeCode<T>::dataType(); }
192  static std::string name()
193  {
194  return std::string("texture of ") + DataTypeCode< T >::name();
195  }
196  };
197 
198 }
199 
200 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
201 
202 
203 template < class T > inline
205 {
206  typename TimeTexture< T >::iterator it;
207  for ( it = this->begin(); it != this->end(); it++ )
208  (it->second).erase();
209  std::map< int, Texture< T > >::erase( this->begin(), this->end() );
210 }
211 
212 
213 template < class T > inline
214 std::ostream& operator << ( std::ostream& out, const TimeTexture< T >& thing )
215 {
216  out << "{";
217 
219 
220  for ( it = thing.begin(); it != thing.end(); it++ )
221  {
222  out << "{";
223  out << "t=" << (*it).first << ",";
224  out << (*it).second << "},";
225  }
226 
227  return out << "NULL}" << std::flush;
228 }
229 
230 
231 //
232 // Useful typedefs
233 //
234 
237 
238 namespace carto {
239 
251 #define _TMP_ TimeTexture< AimsVector< short, 2 > >
253 #undef _TMP_
254 
264 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< Texture1d > )
265 DECLARE_GENERIC_OBJECT_TYPE( rc_ptr< Texture2d > )
266 #define _TMP_ rc_ptr< TimeTexture< AimsVector< short, 2 > > >
268 #undef _TMP_
269 
270 } // namespace carto
271 
272 #endif
#define AIMSDATA_API
bool operator==(const Texture< T > &x) const
Definition: texture.h:85
std::vector< T > & data()
Definition: texture.h:82
Texture(size_t n, const T &value)
Definition: texture.h:67
void erase()
Definition: texture.h:84
void reserve(size_t size)
Definition: texture.h:79
virtual ~Texture()
Definition: texture.h:68
T & item(int n)
Definition: texture.h:77
size_t nItem() const
Definition: texture.h:70
const T & operator[](int n) const
Definition: texture.h:75
std::vector< T > _data
Definition: texture.h:93
const std::vector< T > & data() const
Definition: texture.h:81
Texture(size_t n)
Definition: texture.h:66
const T & item(int n) const
Definition: texture.h:74
size_t size() const
alias for nItem()
Definition: texture.h:72
Texture()
Definition: texture.h:65
void push_back(const T &item)
Definition: texture.h:80
const aims::PythonHeader & header() const
Get the header.
Definition: texture.h:143
void reserve(size_t size)
Definition: texture.h:155
void erase()
Definition: texture.h:204
T & item(int n)
Definition: texture.h:153
TimeTexture(int ntimes, size_t nitems, const T &value)
Definition: texture.h:134
std::map< int, Texture< T > >::iterator iterator
Definition: texture.h:123
aims::PythonHeader _header
Header.
Definition: texture.h:176
void push_back(const T &item)
Definition: texture.h:156
virtual ~TimeTexture()
Definition: texture.h:140
TimeTexture(int ntimes, size_t nitems)
Definition: texture.h:128
const T & item(int n) const
Definition: texture.h:151
bool operator==(const TimeTexture< T > &x) const
Definition: texture.h:160
size_t nItem() const
Definition: texture.h:149
TimeTexture()
Definition: texture.h:127
void setHeader(const aims::PythonHeader &hdr)
Set the header.
Definition: texture.h:147
aims::PythonHeader & header()
Definition: texture.h:144
std::map< int, Texture< T > >::const_iterator const_iterator
Definition: texture.h:125
Attributed python-like header, stores all needed information about an object, currently used for volu...
Definition: pheader.h:52
static std::string objectType()
Definition: texture.h:188
std::string name()
std::string dataType()
#define DECLARE_GENERIC_OBJECT_TYPE(T)
#define _TMP_
Definition: texture.h:266
AIMSDATA_API TimeTexture< Point2df > Texture2d
Definition: texture.h:236
AIMSDATA_API std::ostream & operator<<(std::ostream &out, const Texture< T > &thing)
Definition: texture.h:98
AIMSDATA_API TimeTexture< float > Texture1d
Definition: texture.h:235