OpenWalnut  1.3.1
WValueSetBase.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 WVALUESETBASE_H
26 #define WVALUESETBASE_H
27 
28 #include <cstddef>
29 #include <cmath>
30 
31 #include <boost/shared_ptr.hpp>
32 #include <boost/variant.hpp>
33 
34 #include "../common/math/WValue.h"
35 #include "WDataHandlerEnums.h"
36 
37 
38 //! forward declaration
39 template< typename T >
40 class WValueSet;
41 
42 //! declare a boost::variant of all possible valuesets
43 typedef boost::variant< WValueSet< uint8_t > const*,
44  WValueSet< int8_t > const*,
45  WValueSet< uint16_t > const*,
46  WValueSet< int16_t > const*,
47  WValueSet< uint32_t > const*,
48  WValueSet< int32_t > const*,
49  WValueSet< uint64_t > const*,
50  WValueSet< int64_t > const*,
51  WValueSet< float > const*,
52  WValueSet< double > const*,
53  WValueSet< long double > const* > WValueSetVariant;
54 
55 /**
56  * Abstract base class to all WValueSets. This class doesn't provide any genericness.
57  * \ingroup dataHandler
58  */
59 class WValueSetBase // NOLINT
60 {
61 public:
62  /**
63  * Shared pointer to an instance of this class.
64  */
65  typedef boost::shared_ptr< WValueSetBase > SPtr;
66 
67  /**
68  * Shared pointer to an const instance of this class.
69  */
70  typedef boost::shared_ptr< const WValueSetBase > ConstSPtr;
71 
72  /**
73  * Despite this is an abstract class all subclasses should have an order
74  * and dimension.
75  * \param order the tensor order of the values stored in this WValueSet
76  * \param dimension the tensor dimension of the values stored in this WValueSet
77  * \param inDataType indication of the primitive data type used to store the values
78  */
79  WValueSetBase( size_t order, size_t dimension, dataType inDataType );
80 
81  /**
82  * Dummy since each class with virtual member functions needs one.
83  */
84  virtual ~WValueSetBase() = 0;
85 
86  /**
87  * \return The number of tensors in this ValueSet.
88  */
89  virtual size_t size() const = 0;
90 
91  /**
92  * \return The number of integrals (POD like int, double) in this ValueSet.
93  */
94  virtual size_t rawSize() const = 0;
95 
96  /**
97  * \param i id of the scalar to retrieve
98  * \return The i-th scalar stored in this value set. There are rawSize() such scalars.
99  */
100  virtual double getScalarDouble( size_t i ) const = 0;
101 
102  /**
103  * \param i id of the WValue to retrieve
104  * \return The i-th WValue stored in this value set. There are size() such scalars.
105  */
106  virtual WValue< double > getWValueDouble( size_t i ) const = 0;
107 
108  /**
109  * \param i id of the WVector to retrieve
110  * \return The i-th WValue (stored in this value set) as WVector. There are size() such scalars.
111  */
112  virtual WVector_2 getWVector( size_t i ) const = 0;
113 
114  /**
115  * \return Dimension of the values in this ValueSet
116  */
117  virtual size_t dimension() const
118  {
119  return m_dimension;
120  }
121 
122  /**
123  * \return Order of the values in this ValueSet
124  */
125  virtual size_t order() const
126  {
127  return m_order;
128  }
129 
130  /**
131  * Returns the number of elements of type T per value.
132  * \note this is dimension to the power of order.
133  * \return number of elements per value
134  */
135  virtual size_t elementsPerValue() const
136  {
137  return static_cast< size_t >( std::pow( static_cast< double >( m_dimension ), static_cast< int >( m_order ) ) );
138  }
139 
140  /**
141  * \return Dimension of the values in this ValueSet
142  */
143  virtual dataType getDataType() const
144  {
145  return m_dataType;
146  }
147 
148  /**
149  * This method returns the smallest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
150  * smallest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
151  *
152  * \return the smallest value in the data.
153  */
154  virtual double getMinimumValue() const = 0;
155 
156  /**
157  * This method returns the largest value in the valueset. It does not handle vectors, matrices and so on well. It simply returns the
158  * largest value in the data array. This is especially useful for texture scaling or other statistic tools (histograms).
159  *
160  * \return the largest value in the data.
161  */
162  virtual double getMaximumValue() const = 0;
163 
164  /**
165  * Apply a function object to this valueset.
166  *
167  * \tparam Func_T The type of the function object, should be derived from the boost::static_visitor template.
168  *
169  * \param func The function object to apply.
170  * \return The result of the operation.
171  */
172  template< typename Func_T >
173  typename Func_T::result_type applyFunction( Func_T const& func )
174  {
175  return boost::apply_visitor( func, getVariant() );
176  }
177 
178 protected:
179  /**
180  * The order of the tensors for this ValueSet
181  */
182  const size_t m_order;
183 
184  /**
185  * The dimension of the tensors for this ValueSet
186  */
187  const size_t m_dimension;
188 
189  /**
190  * The data type of the values' elements.
191  */
193 
194 private:
195  /**
196  * Creates a boost::variant reference.
197  *
198  * \return var A pointer to a variant reference to the valueset.
199  */
200  virtual WValueSetVariant const getVariant() const
201  {
202  return WValueSetVariant();
203  }
204 };
205 
206 #endif // WVALUESETBASE_H