OpenWalnut  1.3.1
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 
30 #include <boost/thread.hpp>
31 #include <boost/shared_ptr.hpp>
32 
33 #include "datastructures/WValueSetHistogram.h"
34 
35 #include "WDataSetSingle.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 boost::shared_ptr< WDataSetScalar > SPtr;
49 
50  /**
51  * const shared_ptr abbreviation
52  */
53  typedef boost::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( boost::shared_ptr< WValueSetBase > newValueSet,
63  boost::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 even if you just have a WDataSetSingle.
78  *
79  * \param newValueSet the new valueset.
80  *
81  * \return the clone
82  */
83  virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WValueSetBase > newValueSet ) const;
84 
85  /**
86  * 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
87  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
88  *
89  * \param newGrid the new grid.
90  *
91  * \return the clone
92  */
93  virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WGrid > newGrid ) const;
94 
95  /**
96  * Creates a copy (clone) of this instance. Unlike copy construction, this is a very useful function if you
97  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
98  *
99  * \return the clone
100  */
101  virtual WDataSetSingle::SPtr clone() const;
102 
103  /**
104  * Returns the largest of the scalars stored in the data set
105  *
106  * \return maximum value in dataset
107  */
108  double getMax() const;
109 
110  /**
111  * Returns the smallest of the scalars stored in the data set
112  *
113  * \return minimum value in dataset
114  */
115  double getMin() const;
116 
117  /**
118  * 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
119  * WValueSetHistogram down scaling feature even though the number of buckets might be lower than the default as the down scaling might
120  * introduce errors. To use down-scaling, grab the default histogram and call WValueSetHistogram( getHistogram(), buckets ) manually.
121  *
122  * \param buckets the number of buckets inside the histogram.
123  *
124  * \return the histogram.
125  */
126  boost::shared_ptr< const WValueSetHistogram > getHistogram( size_t buckets = 1000 );
127 
128  /**
129  * Interpolate the value fo the valueset at the given position.
130  * If interpolation fails, the success parameter will be false
131  * and the value returned zero.
132  *
133  * \param pos The position for wich we would like to get a value.
134  * \param success indicates whether the interpolation was successful
135  *
136  * \return Scalar value for that given position
137  */
138  double interpolate( const WPosition& pos, bool* success ) const;
139 
140  /**
141  * Get the value stored at a certain grid position of the data set
142  * \param x index in x direction
143  * \param y index in y direction
144  * \param z index in z direction
145  *
146  * \return the value at the grid position with the given index tuple.
147  */
148  template< typename T > T getValueAt( int x, int y, int z ) const;
149 
150  /**
151  * Get the value stored at a certain grid position of the data set
152  * \param x index in x direction
153  * \param y index in y direction
154  * \param z index in z direction
155  *
156  * \return the double the grid position with the given index tuple.
157  */
158  double getValueAt( int x, int y, int z ) const;
159 
160 
161  /**
162  * Returns a prototype instantiated with the true type of the deriving class.
163  *
164  * \return the prototype.
165  */
166  static boost::shared_ptr< WPrototyped > getPrototype();
167 
169 
170 protected:
171  /**
172  * The prototype as singleton.
173  */
174  static boost::shared_ptr< WPrototyped > m_prototype;
175 
176 private:
177  /**
178  * The histograms for later use. Each histogram for a requested bucket count gets cached.
179  **/
180  std::map< size_t, boost::shared_ptr< WValueSetHistogram > > m_histograms;
181 
182  /**
183  * The lock used for securely creating m_histogram on demand.
184  */
185  boost::mutex m_histogramLock;
186 };
187 
188 template< typename T > T WDataSetScalar::getValueAt( int x, int y, int z ) const
189 {
190  boost::shared_ptr< WValueSet< T > > vs = boost::shared_dynamic_cast< WValueSet< T > >( m_valueSet );
191  boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
192 
193  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
194 
195  T v = vs->getScalar( id );
196  return v;
197 }
198 
199 #endif // WDATASETSCALAR_H