OpenWalnut  1.4.0
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 <string>
26 #include <vector>
27 
28 #include "../common/WAssert.h"
29 #include "../common/WLimits.h"
30 #include "datastructures/WValueSetHistogram.h"
31 #include "WDataSetSingle.h"
32 
33 #include "WDataSetScalar.h"
34 
35 // prototype instance as singleton
36 boost::shared_ptr< WPrototyped > WDataSetScalar::m_prototype = boost::shared_ptr< WPrototyped >();
37 
38 WDataSetScalar::WDataSetScalar( boost::shared_ptr< WValueSetBase > newValueSet,
39  boost::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( boost::shared_ptr< WValueSetBase > newValueSet ) const
59 {
60  return WDataSetSingle::SPtr( new WDataSetScalar( newValueSet, getGrid() ) );
61 }
62 
63 WDataSetSingle::SPtr WDataSetScalar::clone( boost::shared_ptr< WGrid > newGrid ) const
64 {
65  return WDataSetSingle::SPtr( new WDataSetScalar( getValueSet(), newGrid ) );
66 }
67 
69 {
71 }
72 
73 std::string const WDataSetScalar::getName() const
74 {
75  return "WDataSetScalar";
76 }
77 
78 std::string const WDataSetScalar::getDescription() const
79 {
80  return "A scalar dataset, i.e. one scalar value per voxel.";
81 }
82 
83 double WDataSetScalar::getMax() const
84 {
85  return m_valueSet->getMaximumValue();
86 }
87 
88 double WDataSetScalar::getMin() const
89 {
90  return m_valueSet->getMinimumValue();
91 }
92 
93 boost::shared_ptr< WPrototyped > WDataSetScalar::getPrototype()
94 {
95  if( !m_prototype )
96  {
97  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetScalar() );
98  }
99 
100  return m_prototype;
101 }
102 
103 double WDataSetScalar::interpolate( const WPosition& pos, bool* success ) const
104 {
105  boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
106 
107  WAssert( grid, "This data set has a grid whose type is not yet supported for interpolation." );
108  WAssert( ( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ),
109  "Only implemented for scalar values so far." );
110 
111  bool isInside = true;
112  size_t cellId = grid->getCellId( pos, &isInside );
113 
114  if( !isInside )
115  {
116  *success = false;
117  return 0.0;
118  }
119 
120  WGridRegular3D::CellVertexArray vertexIds = grid->getCellVertexIds( cellId );
121 
122  WPosition localPos = grid->getTransform().positionToGridSpace( pos - grid->getPosition( vertexIds[0] ) );
123 
124  double lambdaX = localPos[0];
125  double lambdaY = localPos[1];
126  double lambdaZ = localPos[2];
127  std::vector< double > h( 8 );
128 // lZ lY
129 // | /
130 // | 6___/_7
131 // |/: /|
132 // 4_:___5 |
133 // | :...|.|
134 // |.2 | 3
135 // |_____|/ ____lX
136 // 0 1
137  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
138  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
139  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
140  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
141  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
142  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
143  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
144  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
145 
146  double result = 0;
147  for( size_t i = 0; i < 8; ++i )
148  {
149  result += h[i] * WDataSetSingle::getValueAt( vertexIds[i] );
150  }
151 
152  *success = true;
153  return result;
154 }
155 
156 double WDataSetScalar::getValueAt( int x, int y, int z ) const
157 {
158  boost::shared_ptr< WGridRegular3D > grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_grid );
159  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
160 
161  return WDataSetSingle::getValueAt( id );
162 }
163 
164 boost::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram( size_t buckets )
165 {
166  boost::lock_guard<boost::mutex> lock( m_histogramLock );
167 
168  if( m_histograms.count( buckets ) != 0 )
169  {
170  return m_histograms[ buckets ];
171  }
172 
173  // create if not yet existing
174  m_histograms[ buckets ] = boost::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet, buckets ) );
175 
176  return m_histograms[ buckets ];
177 }
178 
Used to find the occurrence frequencies of values in a value set.
boost::shared_ptr< WGrid > m_grid
Stores the reference of the WGrid of this DataSetSingle instance.
A grid that has parallelepiped cells which all have the same proportion.
static boost::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
virtual ~WDataSetScalar()
Destroys this DataSet instance.
boost::array< size_t, 8 > CellVertexArray
Convenience typedef for a boost::array< size_t, 8 >.
virtual const std::string getName() const
Gets the name of this prototype.
boost::shared_ptr< WValueSetBase > getValueSet() const
double interpolate(const WPosition &pos, bool *success) const
Interpolate the value fo the valueset at the given position.
boost::mutex m_histogramLock
The lock used for securely creating m_histogram on demand.
std::map< size_t, boost::shared_ptr< WValueSetHistogram > > m_histograms
The histograms for later use.
This only is a 3d double vector.
boost::shared_ptr< WGrid > getGrid() const
WDataSetScalar()
Construct an empty and unusable instance.
boost::shared_ptr< WValueSetBase > m_valueSet
Stores the reference of the WValueSet of this DataSetSingle instance.
A data set consisting of a set of values based on a grid.
static boost::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
virtual const std::string getDescription() const
Gets the description for this prototype.
double getMin() const
Returns the smallest of the scalars stored in the data set.
boost::shared_ptr< WDataSetSingle > SPtr
Convenience typedef for a boost::shared_ptr.
T getValueAt(int x, int y, int z) const
Get the value stored at a certain grid position of the data set.
boost::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.
unsigned int getNbCoordsX() const
Returns the number of samples in x direction.
T getValueAt(size_t id)
Get the value stored at position of the value set.