OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WDataSetVector.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 <stdint.h>
26 
27 #include <string>
28 #include <vector>
29 
30 #include <boost/array.hpp>
31 
32 #include "../common/WAssert.h"
33 #include "WDataSetSingle.h"
34 #include "WDataSetVector.h"
35 
36 // prototype instance as singleton
37 boost::shared_ptr< WPrototyped > WDataSetVector::m_prototype = boost::shared_ptr< WPrototyped >();
38 
39 WDataSetVector::WDataSetVector( boost::shared_ptr< WValueSetBase > newValueSet,
40  boost::shared_ptr< WGrid > newGrid )
41  : WDataSetSingle( newValueSet, newGrid )
42 {
43  WAssert( newValueSet, "No value set given." );
44  WAssert( newGrid, "No grid given." );
45  WAssert( newValueSet->size() == newGrid->size(), "Number of values unequal number of positions in grid." );
46  WAssert( newValueSet->order() == 1, "The value set does not contain vectors." );
47 }
48 
50  : WDataSetSingle()
51 {
52 }
53 
55 {
56 }
57 
58 WDataSetSingle::SPtr WDataSetVector::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
59 {
60  return WDataSetSingle::SPtr( new WDataSetVector( newValueSet, getGrid() ) );
61 }
62 
63 WDataSetSingle::SPtr WDataSetVector::clone( boost::shared_ptr< WGrid > newGrid ) const
64 {
65  return WDataSetSingle::SPtr( new WDataSetVector( getValueSet(), newGrid ) );
66 }
67 
69 {
71 }
72 
73 boost::shared_ptr< WPrototyped > WDataSetVector::getPrototype()
74 {
75  if( !m_prototype )
76  {
77  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetVector() );
78  }
79 
80  return m_prototype;
81 }
82 
83 namespace
84 {
85  boost::array< double, 8 > computePrefactors( const WPosition& pos, boost::shared_ptr< const WGrid > i_grid,
86  boost::shared_ptr< const WValueSetBase > i_valueSet, bool *success, boost::shared_ptr< std::vector< size_t > > vertexIds )
87  {
88  boost::shared_ptr< const WGridRegular3D > grid = boost::shared_dynamic_cast< const WGridRegular3D >( i_grid );
89 
90  WAssert( grid, "This data set has a grid whose type is not yet supported for interpolation." );
91  WAssert( grid->isNotRotated(), "Only feasible for grids that are only translated or scaled so far." );
92  WAssert( ( i_valueSet->order() == 1 && i_valueSet->dimension() == 3 ),
93  "Only implemented for 3D Vectors so far." );
94  boost::array< double, 8 > h;
95 
96  bool isInside = true;
97  size_t cellId = grid->getCellId( pos, &isInside );
98 
99  if( !isInside )
100  {
101  *success = false;
102  return h;
103  }
104  *success = true; // set it here, before the real work is done, because this cannot fail anymore.
105 
106  *vertexIds = grid->getCellVertexIds( cellId );
107 
108  WPosition localPos = pos - grid->getPosition( ( *vertexIds )[0] );
109 
110  double lambdaX = localPos[0] / grid->getOffsetX();
111  double lambdaY = localPos[1] / grid->getOffsetY();
112  double lambdaZ = localPos[2] / grid->getOffsetZ();
113 
114  // lZ lY
115  // | /
116  // | 6___/_7
117  // |/: /|
118  // 4_:___5 |
119  // | :...|.|
120  // |.2 | 3
121  // |_____|/ ____lX
122  // 0 1
123  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
124  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
125  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
126  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
127  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
128  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
129  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
130  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
131 
132  return h;
133  }
134 }
135 
136 WVector3d WDataSetVector::interpolate( const WPosition& pos, bool *success ) const
137 {
138  boost::shared_ptr< std::vector< size_t > > vertexIds( new std::vector< size_t > );
139  boost::array< double, 8 > h = computePrefactors( pos, m_grid, m_valueSet, success, vertexIds );
140  WVector3d result( 0.0, 0.0, 0.0 );
141 
142  if( *success ) // only if pos was iniside the grid, we proivde a result different to 0.0, 0.0, 0.0
143  {
144  for( size_t i = 0; i < 8; ++i )
145  {
146  result += h[i] * getVectorAt( ( *vertexIds )[i] );
147  }
148  }
149 
150  return result;
151 }
152 
154 {
155  boost::shared_ptr< std::vector< size_t > > vertexIds( new std::vector< size_t > );
156  boost::array< double, 8 > h = computePrefactors( pos, m_grid, m_valueSet, success, vertexIds );
157  WVector3d result( 0.0, 0.0, 0.0 );
158 
159  if( *success ) // only if pos was iniside the grid, we proivde a result different to 0.0, 0.0, 0.0
160  {
161  for( size_t i = 0; i < 8; ++i )
162  {
163  double sign = 1.0;
164  if( dot( getVectorAt( ( *vertexIds )[0] ), getVectorAt( ( *vertexIds )[i] ) ) < 0.0 )
165  {
166  sign = -1.0;
167  }
168  result += h[i] * sign * getVectorAt( ( *vertexIds )[i] );
169  }
170  }
171 
172  return result;
173 }
174 
176 {
177  switch( getValueSet()->getDataType() )
178  {
179  case W_DT_UNSIGNED_CHAR:
180  {
181  return boost::shared_dynamic_cast< WValueSet< uint8_t > >( getValueSet() )->getVector3D( index );
182  }
183  case W_DT_INT16:
184  {
185  return boost::shared_dynamic_cast< WValueSet< int16_t > >( getValueSet() )->getVector3D( index );
186  }
187  case W_DT_SIGNED_INT:
188  {
189  return boost::shared_dynamic_cast< WValueSet< int32_t > >( getValueSet() )->getVector3D( index );
190  }
191  case W_DT_FLOAT:
192  {
193  return boost::shared_dynamic_cast< WValueSet< float > >( getValueSet() )->getVector3D( index );
194  }
195  case W_DT_DOUBLE:
196  {
197  return boost::shared_dynamic_cast< WValueSet< double > >( getValueSet() )->getVector3D( index );
198  }
199  default:
200  WAssert( false, "Unknow data type in dataset." );
201  }
202 
203  return WVector3d( 0, 0, 0 );
204 }
205 
207 {
208  return true;
209 }
210