OpenWalnut  1.5.0dev
WDataSetScalar.cpp
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 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "../common/WAssert.h"
30 #include "../common/WLimits.h"
31 #include "WDataSetScalar.h"
32 #include "WDataSetSingle.h"
33 #include "datastructures/WValueSetHistogram.h"
34 
35 // prototype instance as singleton
36 std::shared_ptr< WPrototyped > WDataSetScalar::m_prototype = std::shared_ptr< WPrototyped >();
37 
38 WDataSetScalar::WDataSetScalar( std::shared_ptr< WValueSetBase > newValueSet,
39  std::shared_ptr< WGrid > newGrid )
40  : WDataSetSingle( newValueSet, newGrid )
41 {
42  WAssert( newValueSet, "No value set given." );
43  WAssert( newGrid, "No grid given." );
44  WAssert( newValueSet->size() == newGrid->size(), "Number of values unequal number of positions in grid." );
45  WAssert( newValueSet->order() == 0, "The value set does not contain scalars." );
46 }
47 
49  : WDataSetSingle()
50 {
51  // default constructor used by the prototype mechanism
52 }
53 
55 {
56 }
57 
58 WDataSetSingle::SPtr WDataSetScalar::clone( std::shared_ptr< WValueSetBase > newValueSet, std::shared_ptr< WGrid > newGrid ) const
59 {
60  return WDataSetSingle::SPtr( new WDataSetScalar( newValueSet, newGrid ) );
61 }
62 
63 WDataSetSingle::SPtr WDataSetScalar::clone( std::shared_ptr< WValueSetBase > newValueSet ) const
64 {
65  return WDataSetSingle::SPtr( new WDataSetScalar( newValueSet, getGrid() ) );
66 }
67 
68 WDataSetSingle::SPtr WDataSetScalar::clone( std::shared_ptr< WGrid > newGrid ) const
69 {
70  return WDataSetSingle::SPtr( new WDataSetScalar( getValueSet(), newGrid ) );
71 }
72 
74 {
76 }
77 
78 std::string const WDataSetScalar::getName() const
79 {
80  return "WDataSetScalar";
81 }
82 
83 std::string const WDataSetScalar::getDescription() const
84 {
85  return "A scalar dataset, i.e. one scalar value per voxel.";
86 }
87 
88 double WDataSetScalar::getMax() const
89 {
90  return m_valueSet->getMaximumValue();
91 }
92 
93 double WDataSetScalar::getMin() const
94 {
95  return m_valueSet->getMinimumValue();
96 }
97 
98 std::shared_ptr< WPrototyped > WDataSetScalar::getPrototype()
99 {
100  if( !m_prototype )
101  {
102  m_prototype = std::shared_ptr< WPrototyped >( new WDataSetScalar() );
103  }
104 
105  return m_prototype;
106 }
107 
108 double WDataSetScalar::interpolate( const WPosition& pos, bool* success ) const
109 {
110  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
111 
112  WAssert( grid, "This data set has a grid whose type is not yet supported for interpolation." );
113  WAssert( ( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ),
114  "Only implemented for scalar values so far." );
115 
116  bool isInside = true;
117  size_t cellId = grid->getCellId( pos, &isInside );
118 
119  if( !isInside )
120  {
121  *success = false;
122  return 0.0;
123  }
124 
125  WGridRegular3D::CellVertexArray vertexIds = grid->getCellVertexIds( cellId );
126 
127  WPosition localPos = grid->getTransform().directionToGridSpace( pos - grid->getPosition( vertexIds[0] ) );
128 
129  double lambdaX = localPos[0];
130  double lambdaY = localPos[1];
131  double lambdaZ = localPos[2];
132  std::vector< double > h( 8 );
133 // lZ lY
134 // | /
135 // | 6___/_7
136 // |/: /|
137 // 4_:___5 |
138 // | :...|.|
139 // |.2 | 3
140 // |_____|/ ____lX
141 // 0 1
142  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
143  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
144  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
145  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
146  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
147  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
148  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
149  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
150 
151  double result = 0;
152  for( size_t i = 0; i < 8; ++i )
153  {
154  result += h[i] * WDataSetSingle::getSingleRawValue( vertexIds[i] );
155  }
156 
157  *success = true;
158  return result;
159 }
160 
161 double WDataSetScalar::getValueAt( int x, int y, int z ) const
162 {
163  std::shared_ptr< WGridRegular3D > grid = std::dynamic_pointer_cast< WGridRegular3D >( m_grid );
164  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
165 
167 }
168 
169 double WDataSetScalar::getValueAt( size_t id ) const
170 {
172 }
173 
174 std::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram( size_t buckets )
175 {
176  boost::lock_guard<boost::mutex> lock( m_histogramLock );
177 
178  if( m_histograms.count( buckets ) != 0 )
179  {
180  return m_histograms[ buckets ];
181  }
182 
183  // create if not yet existing
184  m_histograms[ buckets ] = std::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet, buckets ) );
185 
186  return m_histograms[ buckets ];
187 }
virtual ~WDataSetScalar()
Destroys this DataSet instance.
static std::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
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::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< WValueSetBase > getValueSet() const
std::shared_ptr< WGrid > m_grid
Stores the reference of the WGrid of this DataSetSingle instance.
std::shared_ptr< WGrid > getGrid() const
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.
boost::array< size_t, 8 > CellVertexArray
Convenience typedef for a boost::array< size_t, 8 >.
This only is a 3d double vector.
Used to find the occurrence frequencies of values in a value set.