OpenWalnut  1.3.1
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 double WDataSetScalar::getMax() const
74 {
75  return m_valueSet->getMaximumValue();
76 }
77 
78 double WDataSetScalar::getMin() const
79 {
80  return m_valueSet->getMinimumValue();
81 }
82 
83 boost::shared_ptr< WPrototyped > WDataSetScalar::getPrototype()
84 {
85  if( !m_prototype )
86  {
87  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetScalar() );
88  }
89 
90  return m_prototype;
91 }
92 
93 double WDataSetScalar::interpolate( const WPosition& pos, bool* success ) const
94 {
95  boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
96 
97  WAssert( grid, "This data set has a grid whose type is not yet supported for interpolation." );
98  WAssert( ( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ),
99  "Only implemented for scalar values so far." );
100 
101  bool isInside = true;
102  size_t cellId = grid->getCellId( pos, &isInside );
103 
104  if( !isInside )
105  {
106  *success = false;
107  return 0.0;
108  }
109 
110  WGridRegular3D::CellVertexArray vertexIds = grid->getCellVertexIds( cellId );
111 
112  WPosition localPos = grid->getTransform().positionToGridSpace( pos - grid->getPosition( vertexIds[0] ) );
113 
114  double lambdaX = localPos[0];
115  double lambdaY = localPos[1];
116  double lambdaZ = localPos[2];
117  std::vector< double > h( 8 );
118 // lZ lY
119 // | /
120 // | 6___/_7
121 // |/: /|
122 // 4_:___5 |
123 // | :...|.|
124 // |.2 | 3
125 // |_____|/ ____lX
126 // 0 1
127  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
128  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
129  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
130  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
131  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
132  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
133  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
134  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
135 
136  double result = 0;
137  for( size_t i = 0; i < 8; ++i )
138  {
139  result += h[i] * WDataSetSingle::getValueAt( vertexIds[i] );
140  }
141 
142  *success = true;
143  return result;
144 }
145 
146 double WDataSetScalar::getValueAt( int x, int y, int z ) const
147 {
148  boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
149  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
150 
151  return WDataSetSingle::getValueAt( id );
152 }
153 
154 boost::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram( size_t buckets )
155 {
156  boost::lock_guard<boost::mutex> lock( m_histogramLock );
157 
158  if( m_histograms.count( buckets ) != 0 )
159  {
160  return m_histograms[ buckets ];
161  }
162 
163  // create if not yet existing
164  m_histograms[ buckets ] = boost::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet, buckets ) );
165 
166  return m_histograms[ buckets ];
167 }
168