OpenWalnut  1.5.0dev
WDataSetScalar.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 WDATASETSCALAR_H
26 #define WDATASETSCALAR_H
27 
28 #include <map>
29 #include <memory>
30 #include <string>
31 
32 #include <boost/thread.hpp>
33 
34 #include "WDataSetSingle.h"
35 #include "datastructures/WValueSetHistogram.h"
36 
37 
38 /**
39  * This data set type contains scalars as values.
40  * \ingroup dataHandler
41  */
42 class WDataSetScalar : public WDataSetSingle // NOLINT
43 {
44 public:
45  /**
46  * shared_ptr abbreviation
47  */
48  typedef std::shared_ptr< WDataSetScalar > SPtr;
49 
50  /**
51  * const shared_ptr abbreviation
52  */
53  typedef std::shared_ptr< const WDataSetScalar > ConstSPtr;
54 
55  /**
56  * Constructs an instance out of an appropriate value set and a grid.
57  * Computes the maximum an minimum values stored as member variables.
58  *
59  * \param newValueSet the scalar value set to use
60  * \param newGrid the grid which maps world space to the value set
61  */
62  WDataSetScalar( std::shared_ptr< WValueSetBase > newValueSet,
63  std::shared_ptr< WGrid > newGrid );
64 
65  /**
66  * Construct an empty and unusable instance. This is needed for the prototype mechanism.
67  */
69 
70  /**
71  * Destroys this DataSet instance
72  */
73  virtual ~WDataSetScalar();
74 
75  /**
76  * Creates a copy (clone) of this instance but allows one to change the valueset. Unlike copy construction, this is a very useful function if you
77  * want to keep the dynamic type of your dataset.
78  *
79  * \param newValueSet the new valueset.
80  * \param newGrid the new grid.
81  *
82  * \return the clone
83  */
84  virtual WDataSetSingle::SPtr clone( std::shared_ptr< WValueSetBase > newValueSet, std::shared_ptr< WGrid > newGrid ) const;
85 
86  /**
87  * Creates a copy (clone) of this instance but allows one to change the valueset. Unlike copy construction, this is a very useful function if you
88  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
89  *
90  * \param newValueSet the new valueset.
91  *
92  * \return the clone
93  */
94  virtual WDataSetSingle::SPtr clone( std::shared_ptr< WValueSetBase > newValueSet ) const;
95 
96  /**
97  * Creates a copy (clone) of this instance but allows one to change the grid. Unlike copy construction, this is a very useful function if you
98  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
99  *
100  * \param newGrid the new grid.
101  *
102  * \return the clone
103  */
104  virtual WDataSetSingle::SPtr clone( std::shared_ptr< WGrid > newGrid ) const;
105 
106  /**
107  * Creates a copy (clone) of this instance. Unlike copy construction, this is a very useful function if you
108  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
109  *
110  * \return the clone
111  */
112  virtual WDataSetSingle::SPtr clone() const;
113 
114  /**
115  * Returns the largest of the scalars stored in the data set
116  *
117  * \return maximum value in dataset
118  */
119  double getMax() const;
120 
121  /**
122  * Returns the smallest of the scalars stored in the data set
123  *
124  * \return minimum value in dataset
125  */
126  double getMin() const;
127 
128  /**
129  * Gets the name of this prototype.
130  *
131  * \return the name.
132  */
133  virtual const std::string getName() const;
134 
135  /**
136  * Gets the description for this prototype.
137  *
138  * \return the description
139  */
140  virtual const std::string getDescription() const;
141 
142  /**
143  * Returns the histogram of this dataset's valueset. If it does not exist yet, it will be created and cached. It does NOT make use of the
144  * WValueSetHistogram down scaling feature even though the number of buckets might be lower than the default as the down scaling might
145  * introduce errors. To use down-scaling, grab the default histogram and call WValueSetHistogram( getHistogram(), buckets ) manually.
146  *
147  * \param buckets the number of buckets inside the histogram.
148  *
149  * \return the histogram.
150  */
151  std::shared_ptr< const WValueSetHistogram > getHistogram( size_t buckets = 1000 );
152 
153  /**
154  * Interpolate the value for the valueset at the given position.
155  * If interpolation fails, the success parameter will be false
156  * and the value returned zero.
157  *
158  * \param pos The position for which we would like to get a value.
159  * \param success indicates whether the interpolation was successful
160  *
161  * \return Scalar value for that given position
162  */
163  double interpolate( const WPosition& pos, bool* success ) const;
164 
165  /**
166  * Get the value stored at a certain grid position of the data set
167  * \param x index in x direction
168  * \param y index in y direction
169  * \param z index in z direction
170  *
171  * \return the value at the grid position with the given index tuple.
172  */
173  template< typename T > T getValueAt( int x, int y, int z ) const;
174 
175  /**
176  * Get the value stored at a certain grid position of the data set
177  * \param x index in x direction
178  * \param y index in y direction
179  * \param z index in z direction
180  *
181  * \return the double the grid position with the given index tuple.
182  */
183  double getValueAt( int x, int y, int z ) const;
184 
185  /**
186  * Get the value stored at a certain grid position of the data set
187  * \param id
188  *
189  * \return the double the grid position with the given tuple.
190  */
191  double getValueAt( size_t id ) const;
192 
193  /**
194  * Returns a prototype instantiated with the true type of the deriving class.
195  *
196  * \return the prototype.
197  */
198  static std::shared_ptr< WPrototyped > getPrototype();
199 
201 
202 protected:
203  /**
204  * The prototype as singleton.
205  */
206  static std::shared_ptr< WPrototyped > m_prototype;
207 
208 private:
209  /**
210  * The histograms for later use. Each histogram for a requested bucket count gets cached.
211  **/
212  std::map< size_t, std::shared_ptr< WValueSetHistogram > > m_histograms;
213 
214  /**
215  * The lock used for securely creating m_histogram on demand.
216  */
217  boost::mutex m_histogramLock;
218 };
219 
220 template< typename T > T WDataSetScalar::getValueAt( int x, int y, int z ) const
221 {
222  std::shared_ptr< WValueSet< T > > vs = std::dynamic_pointer_cast< WValueSet< T > >( m_valueSet );
223  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
224 
225  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
226 
227  T v = vs->getScalar( id );
228  return v;
229 }
230 
231 #endif // WDATASETSCALAR_H
This data set type contains scalars as values.
virtual ~WDataSetScalar()
Destroys this DataSet instance.
static std::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
std::shared_ptr< const WDataSetScalar > ConstSPtr
const shared_ptr abbreviation
double getMin() const
Returns the smallest of the scalars stored in the data set.
double interpolate(const WPosition &pos, bool *success) const
Interpolate the value for the valueset at the given position.
static std::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
T getValueAt(int x, int y, int z) const
Get the value stored at a certain grid position of the data set.
std::shared_ptr< WDataSetScalar > SPtr
shared_ptr abbreviation
std::map< size_t, std::shared_ptr< WValueSetHistogram > > m_histograms
The histograms for later use.
virtual const std::string getName() const
Gets the name of this prototype.
std::shared_ptr< const WValueSetHistogram > getHistogram(size_t buckets=1000)
Returns the histogram of this dataset's valueset.
virtual WDataSetSingle::SPtr clone() const
Creates a copy (clone) of this instance.
double getMax() const
Returns the largest of the scalars stored in the data set.
boost::mutex m_histogramLock
The lock used for securely creating m_histogram on demand.
virtual const std::string getDescription() const
Gets the description for this prototype.
WDataSetScalar()
Construct an empty and unusable instance.
A data set consisting of a set of values based on a grid.
std::shared_ptr< WGrid > m_grid
Stores the reference of the WGrid of this DataSetSingle instance.
double getSingleRawValue(size_t id) const
Get the raw scalar value stored at id-th position of the raw array of the value set.
std::shared_ptr< WValueSetBase > m_valueSet
Stores the reference of the WValueSet of this DataSetSingle instance.
std::shared_ptr< WDataSetSingle > SPtr
Convenience typedef for a std::shared_ptr.
This only is a 3d double vector.