OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <cstddef>
29 #include <vector>
30 #include <boost/shared_ptr.hpp>
31 
32 #include "../common/math/WValue.h"
33 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
34 #include "../common/WAssert.h"
35 #include "../common/WLimits.h"
36 #include "WDataHandlerEnums.h"
37 #include "WValueSetBase.h"
38 
39 /**
40  * Base Class for all value set types.
41  * \ingroup dataHandler
42  */
43 template< typename T > class WValueSet : public WValueSetBase
44 {
45 /**
46  * Only UnitTests are allowed to be friends
47  */
48 friend class WValueSetTest;
49 
50 public:
51  /**
52  * The type of the single value in this value set.
53  */
54  typedef T ValueT;
55 
56  /**
57  * \class SubArray
58  *
59  * A helper class granting safe access to a certain part of the valueset.
60  */
61  class SubArray
62  {
63  public:
64  //! make the valueset a friend
65  friend class WValueSet;
66 
67  /**
68  * Destructor.
69  */
71  {
72  }
73 
74  /**
75  * Safe access. Only the const version is allowed.
76  *
77  * \param i The relative position of the element in the subarray's range.
78  *
79  * \note If i is not in ( 0, size - 1 ), the first element will be returned.
80  *
81  * \return the value
82  */
83  T const& operator[] ( std::size_t i ) const
84  {
85  return *( m_ptr + i * static_cast< std::size_t >( i < m_size ) );
86  }
87 
88  // use the standard copy constructor and operator
89  private:
90  /**
91  * Construct an object that allows safe access.
92  * (no access to elements not in the subarray's range).
93  * Only a valueset may construct a SubArray.
94  *
95  * \param p A pointer to the first element.
96  * \param size The size of the subarray.
97  */
98  SubArray( T const* const p, std::size_t size )
99  : m_ptr( p ),
100  m_size( size )
101  {
102  }
103 
104  //! the pointer to the first element
105  T const* const m_ptr;
106 
107  //! the size of the subarray
108  std::size_t const m_size;
109  };
110 
111  /**
112  * Constructs a value set with values of type T. Sets order and dimension
113  * to allow to interprete the values as tensors of a certain order and dimension.
114  * \param order tensor order of values stored in the value set
115  * \param dimension tensor dimension of values stored in the value set
116  * \param data the vector holding the raw data
117  * \param inDataType indicator telling us which dataType comes in
118  */
119  WValueSet( size_t order, size_t dimension, const boost::shared_ptr< std::vector< T > > data, dataType inDataType )
120  : WValueSetBase( order, dimension, inDataType ),
121  m_data( data )
122  {
123  // calculate min and max
124  // Calculating this once simply ensures that it does not need to be recalculated in textures, histograms ...
127  for( typename std::vector< T >::const_iterator iter = data->begin(); iter != data->end(); ++iter )
128  {
129  m_minimum = m_minimum > *iter ? *iter : m_minimum;
130  m_maximum = m_maximum < *iter ? *iter : m_maximum;
131  }
132  }
133 
134  /**
135  * \return The number of tensors stored in this set.
136  */
137  virtual size_t size() const
138  {
139  switch( m_order )
140  {
141  case 0 : // scalar
142  WAssert( m_dimension == 1, "Although order zero, (dimension != 1) was found." );
143  return rawSize();
144  case 1 : // vector
145  WAssert( rawSize() % m_dimension == 0, "Raw size and dimension don't fit." );
146  return rawSize() / m_dimension;
147  case 2 : // matrix
148  WAssert( rawSize() % ( m_dimension * m_dimension ) == 0, "Raw size and dimension don't fit." );
149  return rawSize() / ( m_dimension * m_dimension );
150  default : // other
151  WAssert( false, "Unsupported tensor order." );
152  return 0;
153  }
154  }
155 
156  /**
157  * \return The number of integral types stored in this set.
158  */
159  virtual size_t rawSize() const
160  {
161  return (*m_data.get()).size();
162  }
163 
164  /**
165  * \param i id of the scalar to retrieve
166  * \return The i-th scalar stored in this value set. There are rawSize() such scalars.
167  */
168  virtual T getScalar( size_t i ) const
169  {
170  return (*m_data.get())[i];
171  }
172 
173  /**
174  * \param i id of the scalar to retrieve
175  * \return The i-th scalar stored in this value set. There are rawSize() such scalars.
176  */
177  virtual double getScalarDouble( size_t i ) const
178  {
179  return static_cast< double >( (*m_data.get())[i] );
180  }
181 
182  /**
183  * \param i id of the WValue to retrieve
184  * \return The i-th WValue stored in this value set. There are size() such scalars.
185  */
186  virtual WValue< double > getWValueDouble( size_t i ) const
187  {
188  return WValue< double >( getWValue( i ) );
189  }
190 
191  /**
192  * \param i id of the WVector to retrieve
193  * \return The i-th WValue (stored in this value set) as WVector. There are size() such scalars.
194  */
195  virtual WVector_2 getWVector( size_t i ) const
196  {
197  return ( WValue< double >( getWValue( i ) ) ).toWVector();
198  }
199 
200  /**
201  * Get the i'th vector
202  *
203  * \param index the index number of the vector
204  *
205  * \return the vector
206  */
207  WVector3d getVector3D( size_t index ) const;
208 
209 
210  /**
211  * Get the i'th WValue with the dimension of WValueSet
212  *
213  * \param index the index number of the WValue
214  *
215  * \return a WValue with the dimension WValueSet
216  */
217  WValue< T > getWValue( size_t index ) const;
218 
219  /**
220  * Sometimes we need raw access to the data array, for e.g. OpenGL.
221  *
222  * \return the raw data pointer
223  */
224  const T * rawData() const
225  {
226  return &(*m_data.get())[0];
227  }
228 
229  /**
230  * Sometimes we need raw access to the data vector.
231  *
232  * \return the data vector
233  */
234  const std::vector< T >* rawDataVectorPointer() const
235  {
236  return &(*m_data.get());
237  }
238 
239  /**
240  * Request (read-) access object to a subarray of this valueset.
241  * The object returned by this function can be used as an array
242  * ( starting at index 0 ), whose elements are the data elements
243  * at positions start to ( including ) start + size - 1 of the valueset.
244  *
245  * \param start The position of the first element of the subarray.
246  * \param size The number of elements in the subarray.
247  * \return The subarray.
248  */
249  SubArray const getSubArray( std::size_t start, std::size_t size ) const
250  {
251  WAssert( start + size <= rawSize(), "" );
252  WAssert( size != 0, "" );
253  return SubArray( rawData() + start, size );
254  }
255 
256  /**
257  * This method returns the smallest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
258  * smallest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
259  *
260  * \return the smallest value in the data.
261  */
262  virtual double getMinimumValue() const
263  {
264  return m_minimum;
265  }
266 
267  /**
268  * This method returns the largest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
269  * largest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
270  *
271  * \return the largest value in the data.
272  */
273  virtual double getMaximumValue() const
274  {
275  return m_maximum;
276  }
277 
278 protected:
279  /**
280  * The smallest value in m_data.
281  */
283 
284  /**
285  * The largest value in m_data.
286  */
288 
289 private:
290  /**
291  * Stores the values of type T as simple array which never should be modified.
292  */
293  const boost::shared_ptr< std::vector< T > > m_data; // WARNING: don't remove constness since &m_data[0] won't work anymore!
294 
295  /**
296  * Get a variant reference to this valueset (the reference is stored in the variant).
297  * \note Use this as a temporary object inside a function or something like that.
298  * \return var A variant reference.
299  */
300  virtual WValueSetVariant const getVariant() const
301  {
302  return WValueSetVariant( this );
303  }
304 };
305 
306 template< typename T > WVector3d WValueSet< T >::getVector3D( size_t index ) const
307 {
308  WAssert( m_order == 1 && m_dimension == 3, "WValueSet<T>::getVector3D only implemented for order==1, dim==3 value sets" );
309  WAssert( ( index + 1 ) * 3 <= m_data->size(), "index in WValueSet<T>::getVector3D too big" );
310  size_t offset = index * 3;
311  return WVector3d( ( *m_data )[offset], ( *m_data )[offset + 1], ( *m_data )[offset + 2] );
312 }
313 
314 template< typename T > WValue< T > WValueSet< T >::getWValue( size_t index ) const
315 {
316  WAssert( m_order == 1, "WValueSet<T>::getWValue only implemented for order==1 value sets" );
317  WAssert( ( index + 1 ) * m_dimension <= m_data->size(), "index in WValueSet<T>::getWValue too big" );
318 
319  size_t offset = index * m_dimension;
320 
321  WValue< T > result( m_dimension );
322 
323  // copying values
324  for( std::size_t i = 0; i < m_dimension; i++ )
325  result[i] = ( *m_data )[offset+i];
326 
327  return result;
328 }
329 
330 #endif // WVALUESET_H