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