OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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( grid->isNotRotated(), "Only feasible for grids that are only translated or scaled so far." );
99  WAssert( ( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ),
100  "Only implemented for scalar values so far." );
101 
102  bool isInside = true;
103  size_t cellId = grid->getCellId( pos, &isInside );
104 
105  if( !isInside )
106  {
107  *success = false;
108  return 0.0;
109  }
110 
111  std::vector< size_t > vertexIds = grid->getCellVertexIds( cellId );
112 
113  WPosition localPos = pos - grid->getPosition( vertexIds[0] );
114 
115  double lambdaX = localPos[0] / grid->getOffsetX();
116  double lambdaY = localPos[1] / grid->getOffsetY();
117  double lambdaZ = localPos[2] / grid->getOffsetZ();
118  std::vector< double > h( 8 );
119 // lZ lY
120 // | /
121 // | 6___/_7
122 // |/: /|
123 // 4_:___5 |
124 // | :...|.|
125 // |.2 | 3
126 // |_____|/ ____lX
127 // 0 1
128  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
129  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
130  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
131  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
132  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
133  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
134  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
135  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
136 
137  double result = 0;
138  for( size_t i = 0; i < 8; ++i )
139  {
140  result += h[i] * WDataSetSingle::getValueAt( vertexIds[i] );
141  }
142 
143  *success = true;
144  return result;
145 }
146 
147 double WDataSetScalar::getValueAt( int x, int y, int z ) const
148 {
149  boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
150  size_t id = x + y * grid->getNbCoordsX() + z * grid->getNbCoordsX() * grid->getNbCoordsY();
151 
152  return WDataSetSingle::getValueAt( id );
153 }
154 
155 boost::shared_ptr< const WValueSetHistogram > WDataSetScalar::getHistogram( size_t buckets )
156 {
157  boost::lock_guard<boost::mutex> lock( m_histogramLock );
158 
159  if( m_histograms.count( buckets ) != 0 )
160  {
161  return m_histograms[ buckets ];
162  }
163 
164  // create if not yet existing
165  m_histograms[ buckets ] = boost::shared_ptr< WValueSetHistogram >( new WValueSetHistogram( m_valueSet, buckets ) );
166 
167  return m_histograms[ buckets ];
168 }
169