OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
32 #include "datastructures/WValueSetHistogram.h"
33 
34 #include "WDataSetSingle.h"
35 #include "WExportDataHandler.h"
36 
37 /**
38  * This data set type contains scalars as values.
39  * \ingroup dataHandler
40  */
42 {
43 public:
44 
45  /**
46  * Constructs an instance out of an appropriate value set and a grid.
47  * Computes the maximum an minimum values stored as member variables.
48  *
49  * \param newValueSet the scalar value set to use
50  * \param newGrid the grid which maps world space to the value set
51  */
52  WDataSetScalar( boost::shared_ptr< WValueSetBase > newValueSet,
53  boost::shared_ptr< WGrid > newGrid );
54 
55  /**
56  * Construct an empty and unusable instance. This is needed for the prototype mechanism.
57  */
59 
60  /**
61  * Destroys this DataSet instance
62  */
63  virtual ~WDataSetScalar();
64 
65  /**
66  * Creates a copy (clone) of this instance but allows to change the valueset. Unlike copy construction, this is a very useful function if you
67  * want to keep the dynamic type of your dataset even if you just have a WDataSetSingle.
68  *
69  * \param newValueSet the new valueset.
70  *
71  * \return the clone
72  */
73  virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WValueSetBase > newValueSet ) const;
74 
75  /**
76  * Creates a copy (clone) of this instance but allows to change the grid. 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 newGrid the new grid.
80  *
81  * \return the clone
82  */
83  virtual WDataSetSingle::SPtr clone( boost::shared_ptr< WGrid > newGrid ) const;
84 
85  /**
86  * Creates a copy (clone) of this instance. 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  * \return the clone
90  */
91  virtual WDataSetSingle::SPtr clone() const;
92 
93  /**
94  * Returns the largest of the scalars stored in the data set
95  *
96  * \return maximum value in dataset
97  */
98  double getMax() const;
99 
100  /**
101  * Returns the smallest of the scalars stored in the data set
102  *
103  * \return minimum value in dataset
104  */
105  double getMin() const;
106 
107  /**
108  * 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
109  * WValueSetHistogram down scaling feature even though the number of buckets might be lower than the default as the down scaling might
110  * introduce errors. To use down-scaling, grab the default histogram and call WValueSetHistogram( getHistogram(), buckets ) manually.
111  *
112  * \param buckets the number of buckets inside the histogram.
113  *
114  * \return the histogram.
115  */
116  boost::shared_ptr< const WValueSetHistogram > getHistogram( size_t buckets = 1000 );
117 
118  /**
119  * Interpolate the value fo the valueset at the given position.
120  * If interpolation fails, the success parameter will be false
121  * and the value returned zero.
122  *
123  * \param pos The position for wich we would like to get a value.
124  * \param success indicates whether the interpolation was successful
125  *
126  * \return Scalar value for that given position
127  */
128  double interpolate( const WPosition& pos, bool* success ) const;
129 
130  /**
131  * Get the value stored at a certain grid position of the data set
132  * \param x index in x direction
133  * \param y index in y direction
134  * \param z index in z direction
135  *
136  * \return the value at the grid position with the given index tuple.
137  */
138  template< typename T > T getValueAt( int x, int y, int z ) 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 double the grid position with the given index tuple.
147  */
148  double getValueAt( int x, int y, int z ) const;
149 
150 
151  /**
152  * Returns a prototype instantiated with the true type of the deriving class.
153  *
154  * \return the prototype.
155  */
156  static boost::shared_ptr< WPrototyped > getPrototype();
157 
159 
160 protected:
161 
162  /**
163  * The prototype as singleton.
164  */
165  static boost::shared_ptr< WPrototyped > m_prototype;
166 
167 private:
168 
169  /**
170  * The histograms for later use. Each histogram for a requested bucket count gets cached.
171  **/
172  std::map< size_t, boost::shared_ptr< WValueSetHistogram > > m_histograms;
173 
174  /**
175  * The lock used for securely creating m_histogram on demand.
176  */
177  boost::mutex m_histogramLock;
178 };
179 
180 template< typename T > T WDataSetScalar::getValueAt( int x, int y, int z ) const
181 {
182  boost::shared_ptr< WValueSet< T > > vs = boost::shared_dynamic_cast< WValueSet< T > >( m_valueSet );
183  boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
184 
185  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
186 
187  T v = vs->getScalar( id );
188  return v;
189 }
190 
191 #endif // WDATASETSCALAR_H