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