OpenWalnut  1.4.0
WValueSet.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WVALUESET_H
26 #define WVALUESET_H
27 
28 #include <cmath>
29 #include <cstddef>
30 #include <limits>
31 #include <vector>
32 
33 #ifndef Q_MOC_RUN
34 #include <boost/shared_ptr.hpp>
35 #endif
36 
37 #include "../common/math/linearAlgebra/WVectorFixed.h"
38 #include "../common/math/WValue.h"
39 #include "../common/WAssert.h"
40 #include "../common/WLimits.h"
41 #include "WDataHandlerEnums.h"
42 #include "WValueSetBase.h"
43 
44 /**
45  * Base Class for all value set types.
46  * \ingroup dataHandler
47  */
48 template< typename T > class WValueSet : public WValueSetBase
49 {
50 /**
51  * Only UnitTests are allowed to be friends
52  */
53 friend class WValueSetTest;
54 
55 public:
56  /**
57  * The type of the single value in this value set.
58  */
59  typedef T ValueT;
60 
61  /**
62  * \class SubArray
63  *
64  * A helper class granting safe access to a certain part of the valueset.
65  */
66  class SubArray
67  {
68  public:
69  //! make the valueset a friend
70  friend class WValueSet;
71 
72  /**
73  * Destructor.
74  */
76  {
77  }
78 
79  /**
80  * Safe access. Only the const version is allowed.
81  *
82  * \param i The relative position of the element in the subarray's range.
83  *
84  * \note If i is not in ( 0, size - 1 ), the first element will be returned.
85  *
86  * \return the value
87  */
88  T const& operator[] ( std::size_t i ) const
89  {
90  return *( m_ptr + i * static_cast< std::size_t >( i < m_size ) );
91  }
92 
93  // use the standard copy constructor and operator
94  private:
95  /**
96  * Construct an object that allows safe access.
97  * (no access to elements not in the subarray's range).
98  * Only a valueset may construct a SubArray.
99  *
100  * \param p A pointer to the first element.
101  * \param size The size of the subarray.
102  */
103  SubArray( T const* const p, std::size_t size )
104  : m_ptr( p ),
105  m_size( size )
106  {
107  }
108 
109  //! the pointer to the first element
110  T const* const m_ptr;
111 
112  //! the size of the subarray
113  std::size_t const m_size;
114  };
115 
116  /**
117  * Constructs a value set with values of type T. Sets order and dimension
118  * to allow to interpret the values as tensors of a certain order and dimension.
119  * \param order tensor order of values stored in the value set
120  * \param dimension tensor dimension of values stored in the value set
121  * \param data the vector holding the raw data
122  * \param inDataType indicator telling us which dataType comes in
123  */
124  WValueSet( size_t order, size_t dimension, const boost::shared_ptr< std::vector< T > > data, dataType inDataType )
125  : WValueSetBase( order, dimension, inDataType ),
126  m_data( data )
127  {
128  // calculate min and max
129  // Calculating this once simply ensures that it does not need to be recalculated in textures, histograms ...
130  m_minimum = std::numeric_limits< T >::max();
131  m_maximum = std::numeric_limits< T >::min();
132  for( typename std::vector< T >::const_iterator iter = data->begin(); iter != data->end(); ++iter )
133  {
134  m_minimum = m_minimum > *iter ? *iter : m_minimum;
135  m_maximum = m_maximum < *iter ? *iter : m_maximum;
136  }
137  }
138 
139  /**
140  * Constructs a value set with values of type T. Sets order and dimension
141  * to allow to interpret the values as tensors of a certain order and dimension.
142  * \param order tensor order of values stored in the value set
143  * \param dimension tensor dimension of values stored in the value set
144  * \param data the vector holding the raw data
145  */
146  WValueSet( size_t order, size_t dimension, const boost::shared_ptr< std::vector< T > > data )
147  : WValueSetBase( order, dimension, DataType< T >::type ),
148  m_data( data )
149  {
150  // calculate min and max
151  // Calculating this once simply ensures that it does not need to be recalculated in textures, histograms ...
152  m_minimum = std::numeric_limits< T >::max();
153  m_maximum = std::numeric_limits< T >::min();
154  for( typename std::vector< T >::const_iterator iter = data->begin(); iter != data->end(); ++iter )
155  {
156  m_minimum = m_minimum > *iter ? *iter : m_minimum;
157  m_maximum = m_maximum < *iter ? *iter : m_maximum;
158  }
159  }
160 
161  /**
162  * \return The number of tensors stored in this set.
163  */
164  virtual size_t size() const
165  {
166  switch( m_order )
167  {
168  case 0 : // scalar
169  WAssert( m_dimension == 1, "Although order zero, (dimension != 1) was found." );
170  return rawSize();
171  case 1 : // vector
172  WAssert( rawSize() % m_dimension == 0, "Raw size and dimension don't fit." );
173  return rawSize() / m_dimension;
174  case 2 : // matrix
175  WAssert( rawSize() % ( m_dimension * m_dimension ) == 0, "Raw size and dimension don't fit." );
176  return rawSize() / ( m_dimension * m_dimension );
177  default : // other
178  WAssert( false, "Unsupported tensor order." );
179  return 0;
180  }
181  }
182 
183  /**
184  * \return The number of integral types stored in this set.
185  */
186  virtual size_t rawSize() const
187  {
188  return (*m_data.get()).size();
189  }
190 
191  /**
192  * \param i id of the scalar to retrieve
193  * \return The i-th scalar stored in this value set. There are rawSize() such scalars.
194  */
195  virtual T getScalar( size_t i ) const
196  {
197  return (*m_data.get())[i];
198  }
199 
200  /**
201  * \param i id of the scalar to retrieve
202  * \return The i-th scalar stored in this value set. There are rawSize() such scalars.
203  */
204  virtual double getScalarDouble( size_t i ) const
205  {
206  return static_cast< double >( (*m_data.get())[i] );
207  }
208 
209  /**
210  * \param i id of the WValue to retrieve
211  * \return The i-th WValue stored in this value set. There are size() such scalars.
212  */
213  virtual WValue< double > getWValueDouble( size_t i ) const
214  {
215  return WValue< double >( getWValue( i ) );
216  }
217 
218  /**
219  * Get the i'th vector
220  *
221  * \param index the index number of the vector
222  *
223  * \return the vector
224  */
225  WVector3d getVector3D( size_t index ) const;
226 
227 
228  /**
229  * Get the i'th WValue with the dimension of WValueSet
230  *
231  * \param index the index number of the WValue
232  *
233  * \return a WValue with the dimension WValueSet
234  */
235  WValue< T > getWValue( size_t index ) const;
236 
237  /**
238  * Sometimes we need raw access to the data array, for e.g. OpenGL.
239  *
240  * \return the raw data pointer
241  */
242  const T * rawData() const
243  {
244  return &(*m_data.get())[0];
245  }
246 
247  /**
248  * Sometimes we need raw access to the data vector.
249  *
250  * \return the data vector
251  */
252  const std::vector< T >* rawDataVectorPointer() const
253  {
254  return &(*m_data.get());
255  }
256 
257  /**
258  * Request (read-) access object to a subarray of this valueset.
259  * The object returned by this function can be used as an array
260  * ( starting at index 0 ), whose elements are the data elements
261  * at positions start to ( including ) start + size - 1 of the valueset.
262  *
263  * \param start The position of the first element of the subarray.
264  * \param size The number of elements in the subarray.
265  * \return The subarray.
266  */
267  SubArray const getSubArray( std::size_t start, std::size_t size ) const
268  {
269  WAssert( start + size <= rawSize(), "" );
270  WAssert( size != 0, "" );
271  return SubArray( rawData() + start, size );
272  }
273 
274  /**
275  * This method returns the smallest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
276  * smallest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
277  *
278  * \return the smallest value in the data.
279  */
280  virtual double getMinimumValue() const
281  {
282  return m_minimum;
283  }
284 
285  /**
286  * This method returns the largest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
287  * largest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
288  *
289  * \return the largest value in the data.
290  */
291  virtual double getMaximumValue() const
292  {
293  return m_maximum;
294  }
295 
296  /**
297  * Calculates the needed number of integral values for a valueset with specified order and dimension for one voxel. The whole dataset will
298  * then be as large as the number of voxels multiplied by this value.
299  *
300  * \param oder desired tensor order.
301  * \param dimension desired dimension.
302  *
303  * \return the number of values needed
304  */
305  static size_t getRequiredRawSizePerVoxel( size_t oder, size_t dimension );
306 protected:
307  /**
308  * The smallest value in m_data.
309  */
311 
312  /**
313  * The largest value in m_data.
314  */
316 
317 private:
318  /**
319  * Stores the values of type T as simple array which never should be modified.
320  */
321  const boost::shared_ptr< std::vector< T > > m_data; // WARNING: don't remove constness since &m_data[0] won't work anymore!
322 
323  /**
324  * Get a variant reference to this valueset (the reference is stored in the variant).
325  * \note Use this as a temporary object inside a function or something like that.
326  * \return var A variant reference.
327  */
328  virtual WValueSetVariant const getVariant() const
329  {
330  return WValueSetVariant( this );
331  }
332 };
333 
334 template< typename T > WVector3d WValueSet< T >::getVector3D( size_t index ) const
335 {
336  WAssert( m_order == 1 && m_dimension == 3, "WValueSet<T>::getVector3D only implemented for order==1, dim==3 value sets" );
337  WAssert( ( index + 1 ) * 3 <= m_data->size(), "index in WValueSet<T>::getVector3D too big" );
338  size_t offset = index * 3;
339  return WVector3d( ( *m_data )[offset], ( *m_data )[offset + 1], ( *m_data )[offset + 2] );
340 }
341 
342 template< typename T > WValue< T > WValueSet< T >::getWValue( size_t index ) const
343 {
344  WAssert( m_order == 1, "WValueSet<T>::getWValue only implemented for order==1 value sets" );
345  WAssert( ( index + 1 ) * m_dimension <= m_data->size(), "index in WValueSet<T>::getWValue too big" );
346 
347  size_t offset = index * m_dimension;
348 
349  WValue< T > result( m_dimension );
350 
351  // copying values
352  for( std::size_t i = 0; i < m_dimension; i++ )
353  result[i] = ( *m_data )[offset+i];
354 
355  return result;
356 }
357 
358 template< typename T >
360 {
361  // NOTE: std::pow works only for floating point types
362  size_t pow = 1;
363  for( size_t v = 0; v < oder; ++v )
364  {
365  pow *= dimension;
366  }
367 
368  return pow;
369 }
370 
371 #endif // WVALUESET_H
static size_t getRequiredRawSizePerVoxel(size_t oder, size_t dimension)
Calculates the needed number of integral values for a valueset with specified order and dimension for...
Definition: WValueSet.h:359
virtual size_t order() const
const size_t m_dimension
The dimension of the tensors for this ValueSet.
const T * rawData() const
Sometimes we need raw access to the data array, for e.g.
Definition: WValueSet.h:242
~SubArray()
Destructor.
Definition: WValueSet.h:75
T m_minimum
The smallest value in m_data.
Definition: WValueSet.h:310
WValueSet(size_t order, size_t dimension, const boost::shared_ptr< std::vector< T > > data)
Constructs a value set with values of type T.
Definition: WValueSet.h:146
SubArray const getSubArray(std::size_t start, std::size_t size) const
Request (read-) access object to a subarray of this valueset.
Definition: WValueSet.h:267
A helper class granting safe access to a certain part of the valueset.
Definition: WValueSet.h:66
virtual double getScalarDouble(size_t i) const
Definition: WValueSet.h:204
WValue< T > getWValue(size_t index) const
Get the i'th WValue with the dimension of WValueSet.
Definition: WValueSet.h:342
virtual T getScalar(size_t i) const
Definition: WValueSet.h:195
T ValueT
The type of the single value in this value set.
Definition: WValueSet.h:59
An object that knows an appropriate dataType flag for the typename T.
std::size_t const m_size
the size of the subarray
Definition: WValueSet.h:113
A fixed size matrix class.
Definition: WMatrixFixed.h:153
virtual WValueSetVariant const getVariant() const
Get a variant reference to this valueset (the reference is stored in the variant).
Definition: WValueSet.h:328
virtual size_t rawSize() const
Definition: WValueSet.h:186
UnitTests the WValueSet class.
dataType
Data types and number values taken from the nifti1.h, at this point it's unknown if it makes sense to...
virtual double getMinimumValue() const
This method returns the smallest value in the valueset.
Definition: WValueSet.h:280
virtual double getMaximumValue() const
This method returns the largest value in the valueset.
Definition: WValueSet.h:291
WValueSet(size_t order, size_t dimension, const boost::shared_ptr< std::vector< T > > data, dataType inDataType)
Constructs a value set with values of type T.
Definition: WValueSet.h:124
SubArray(T const *const p, std::size_t size)
Construct an object that allows safe access.
Definition: WValueSet.h:103
virtual size_t size() const
Definition: WValueSet.h:164
Base Class for all value set types.
Definition: WValueSet.h:48
T m_maximum
The largest value in m_data.
Definition: WValueSet.h:315
Abstract base class to all WValueSets.
Definition: WValueSetBase.h:63
virtual WValue< double > getWValueDouble(size_t i) const
Definition: WValueSet.h:213
const boost::shared_ptr< std::vector< T > > m_data
Stores the values of type T as simple array which never should be modified.
Definition: WValueSet.h:321
virtual size_t dimension() const
const std::vector< T > * rawDataVectorPointer() const
Sometimes we need raw access to the data vector.
Definition: WValueSet.h:252
T const & operator[](std::size_t i) const
Safe access.
Definition: WValueSet.h:88
WVector3d getVector3D(size_t index) const
Get the i'th vector.
Definition: WValueSet.h:334
const size_t m_order
The order of the tensors for this ValueSet.
T const *const m_ptr
the pointer to the first element
Definition: WValueSet.h:110