OpenWalnut  1.4.0
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,
86  boost::shared_ptr< const WGrid > i_grid,
87  boost::shared_ptr< const WValueSetBase > i_valueSet,
88  bool *success,
89  boost::shared_ptr< WGridRegular3D::CellVertexArray > vertexIds )
90  {
91  boost::shared_ptr< const WGridRegular3D > grid = boost::dynamic_pointer_cast< const WGridRegular3D >( i_grid );
92 
93  WAssert( grid, "This data set has a grid whose type is not yet supported for interpolation." );
94  WAssert( ( i_valueSet->order() == 1 && i_valueSet->dimension() == 3 ),
95  "Only implemented for 3D Vectors so far." );
96  boost::array< double, 8 > h;
97 
98  bool isInside = true;
99  size_t cellId = grid->getCellId( pos, &isInside );
100 
101  if( !isInside )
102  {
103  *success = false;
104  return h;
105  }
106  *success = true; // set it here, before the real work is done, because this cannot fail anymore.
107 
108  *vertexIds = grid->getCellVertexIds( cellId );
109 
110  WPosition localPos = grid->getTransform().positionToGridSpace( pos - grid->getPosition( ( *vertexIds )[0] ) );
111 
112  double lambdaX = localPos[0];
113  double lambdaY = localPos[1];
114  double lambdaZ = localPos[2];
115 
116  // lZ lY
117  // | /
118  // | 6___/_7
119  // |/: /|
120  // 4_:___5 |
121  // | :...|.|
122  // |.2 | 3
123  // |_____|/ ____lX
124  // 0 1
125  h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
126  h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
127  h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
128  h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
129  h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
130  h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
131  h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
132  h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
133 
134  return h;
135  }
136 }
137 
138 WVector3d WDataSetVector::interpolate( const WPosition& pos, bool *success ) const
139 {
140  boost::shared_ptr< WGridRegular3D::CellVertexArray > vertexIds( new WGridRegular3D::CellVertexArray );
141  boost::array< double, 8 > h = computePrefactors( pos, m_grid, m_valueSet, success, vertexIds );
142  WVector3d result( 0.0, 0.0, 0.0 );
143 
144  if( *success ) // only if pos was iniside the grid, we proivde a result different to 0.0, 0.0, 0.0
145  {
146  for( size_t i = 0; i < 8; ++i )
147  {
148  result += h[i] * getVectorAt( ( *vertexIds )[i] );
149  }
150  }
151 
152  return result;
153 }
154 
156 {
157  boost::shared_ptr< WGridRegular3D::CellVertexArray > vertexIds( new WGridRegular3D::CellVertexArray );
158  boost::array< double, 8 > h = computePrefactors( pos, m_grid, m_valueSet, success, vertexIds );
159  WVector3d result( 0.0, 0.0, 0.0 );
160 
161  if( *success ) // only if pos was iniside the grid, we proivde a result different to 0.0, 0.0, 0.0
162  {
163  for( size_t i = 0; i < 8; ++i )
164  {
165  double sign = 1.0;
166  if( dot( getVectorAt( ( *vertexIds )[0] ), getVectorAt( ( *vertexIds )[i] ) ) < 0.0 )
167  {
168  sign = -1.0;
169  }
170  result += h[i] * sign * getVectorAt( ( *vertexIds )[i] );
171  }
172  }
173 
174  return result;
175 }
176 
178 {
179  switch( getValueSet()->getDataType() )
180  {
181  case W_DT_UNSIGNED_CHAR:
182  {
183  return boost::dynamic_pointer_cast< WValueSet< uint8_t > >( getValueSet() )->getVector3D( index );
184  }
185  case W_DT_INT16:
186  {
187  return boost::dynamic_pointer_cast< WValueSet< int16_t > >( getValueSet() )->getVector3D( index );
188  }
189  case W_DT_SIGNED_INT:
190  {
191  return boost::dynamic_pointer_cast< WValueSet< int32_t > >( getValueSet() )->getVector3D( index );
192  }
193  case W_DT_FLOAT:
194  {
195  return boost::dynamic_pointer_cast< WValueSet< float > >( getValueSet() )->getVector3D( index );
196  }
197  case W_DT_DOUBLE:
198  {
199  return boost::dynamic_pointer_cast< WValueSet< double > >( getValueSet() )->getVector3D( index );
200  }
201  default:
202  WAssert( false, "Unknow data type in dataset." );
203  }
204 
205  return WVector3d( 0, 0, 0 );
206 }
207 
209 {
210  return true;
211 }
212 
boost::shared_ptr< WGrid > m_grid
Stores the reference of the WGrid of this DataSetSingle instance.
virtual ~WDataSetVector()
Destroys this DataSet instance.
A grid that has parallelepiped cells which all have the same proportion.
boost::array< size_t, 8 > CellVertexArray
Convenience typedef for a boost::array< size_t, 8 >.
WVector3d interpolate(const WPosition &pos, bool *success) const
Interpolates the vector field at the given position.
boost::shared_ptr< WValueSetBase > getValueSet() const
WVector3d getVectorAt(size_t index) const
Get the vector on the given position in value set.
WDataSetVector()
Construct an empty and unusable instance.
This only is a 3d double vector.
boost::shared_ptr< WGrid > getGrid() const
virtual bool isTexture() const
Determines whether this dataset can be used as a texture.
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.
WVector3d eigenVectorInterpolate(const WPosition &pos, bool *success) const
Interpolates the very same way as interpolate but it assures that all vecs are aligned to point into ...
static boost::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
boost::shared_ptr< WDataSetSingle > SPtr
Convenience typedef for a boost::shared_ptr.
Base Class for all value set types.
Definition: WValueSet.h:46
static boost::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
virtual WDataSetSingle::SPtr clone() const
Creates a copy (clone) of this instance.