Commit 9128b74d authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[CHANGE] moved interpolate to WDataSetScalar

parent 1c046ac2
......@@ -22,6 +22,9 @@
//
//---------------------------------------------------------------------------
#include <string>
#include <vector>
#include "../common/WAssert.h"
#include "../common/WLimits.h"
#include "WDataSetSingle.h"
......@@ -99,3 +102,56 @@ boost::shared_ptr< WPrototyped > WDataSetScalar::getPrototype()
return m_prototype;
}
double WDataSetScalar::interpolate( wmath::WPosition pos )
{
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
// TODO(wiebel): change this to eassert.
if( !grid )
{
throw WException( std::string( "This data set has a grid whose type is not yet supported for interpolation." ) );
}
// TODO(wiebel): change this to eassert.
if( grid->getTransformationMatrix() != wmath::WMatrix<double>( 4, 4 ).makeIdentity() )
{
throw WException( std::string( "Only feasible for untranslated grid so far." ) );
}
// TODO(wiebel): change this to eassert.
if( !( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ) )
{
throw WException( std::string( "Only implemented for scalar values so far." ) );
}
std::vector< size_t > vertexIds = grid->getCellVertexIds( grid->getCellId( pos ) );
wmath::WPosition localPos = pos - grid->getPosition( vertexIds[0] );
double lambdaX = localPos[0] / grid->getOffsetX();
double lambdaY = localPos[1] / grid->getOffsetY();
double lambdaZ = localPos[2] / grid->getOffsetZ();
std::vector< double > h( 8 );
// lZ lY
// | /
// | 6___/_7
// |/: /|
// 4_:___5 |
// | :...|.|
// |.2 | 3
// |_____|/ ____lX
// 0 1
h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
double result = 0;
for( size_t i = 0; i < 8; ++i )
{
result += h[i] * getValueAt( vertexIds[i] );
}
return result;
}
......@@ -79,6 +79,16 @@ public:
*/
double getMin() const;
/**
* Interpolate the value fo the valueset at the given position
*
* \param pos The position for wich we would like to get a value.
*
* \return Scalar value for that given position
*/
double interpolate( wmath::WPosition pos );
/**
* Returns a prototype instantiated with the true type of the deriving class.
*
......
......@@ -142,57 +142,3 @@ double WDataSetSingle::getValueAt( int x, int y, int z )
return getValueAt( id );
}
double WDataSetSingle::interpolate( wmath::WPosition pos )
{
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
// TODO(wiebel): change this to eassert.
if( !grid )
{
throw WException( std::string( "This data set has a grid whose type is not yet supported for interpolation." ) );
}
// TODO(wiebel): change this to eassert.
if( grid->getTransformationMatrix() != wmath::WMatrix<double>( 4, 4 ).makeIdentity() )
{
throw WException( std::string( "Only feasible for untranslated grid so far." ) );
}
// TODO(wiebel): change this to eassert.
if( !( m_valueSet->order() == 0 && m_valueSet->dimension() == 1 ) )
{
throw WException( std::string( "Only implemented for scalar values so far." ) );
}
std::vector< size_t > vertexIds = grid->getCellVertexIds( grid->getCellId( pos ) );
wmath::WPosition localPos = pos - grid->getPosition( vertexIds[0] );
double lambdaX = localPos[0] / grid->getOffsetX();
double lambdaY = localPos[1] / grid->getOffsetY();
double lambdaZ = localPos[2] / grid->getOffsetZ();
std::vector< double > h( 8 );
// lZ lY
// | /
// | 6___/_7
// |/: /|
// 4_:___5 |
// | :...|.|
// |.2 | 3
// |_____|/ ____lX
// 0 1
h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
h[1] = ( lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
h[2] = ( 1 - lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
h[3] = ( lambdaX ) * ( lambdaY ) * ( 1 - lambdaZ );
h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
h[5] = ( lambdaX ) * ( 1 - lambdaY ) * ( lambdaZ );
h[6] = ( 1 - lambdaX ) * ( lambdaY ) * ( lambdaZ );
h[7] = ( lambdaX ) * ( lambdaY ) * ( lambdaZ );
double result = 0;
for( size_t i = 0; i < 8; ++i )
{
result += h[i] * getValueAt( vertexIds[i] );
}
return result;
}
......@@ -101,15 +101,6 @@ public:
*/
template< typename T > T getValueAt( size_t id );
/**
* Interpolate the value fo the valueset at the given position
*
* \param pos The position for wich we would like to get a value.
*
* \return Scalar value for that given position
*/
double interpolate( wmath::WPosition pos );
/**
* Get the value stored at a certain grid position of the data set in type double.
*
......@@ -161,17 +152,17 @@ protected:
*/
static boost::shared_ptr< WPrototyped > m_prototype;
private:
/**
* Stores the reference of the WValueSet of this DataSetSingle instance.
* Stores the reference of the WGrid of this DataSetSingle instance.
*/
boost::shared_ptr< WValueSetBase > m_valueSet;
boost::shared_ptr< WGrid > m_grid;
/**
* Stores the reference of the WGrid of this DataSetSingle instance.
* Stores the reference of the WValueSet of this DataSetSingle instance.
*/
boost::shared_ptr< WGrid > m_grid;
boost::shared_ptr< WValueSetBase > m_valueSet;
private:
/**
* The 3D texture representing this dataset.
*/
......
......@@ -87,34 +87,5 @@ public:
TS_ASSERT_EQUALS( dataSetSingle.getGrid(), gridDummy );
TS_ASSERT_DIFFERS( dataSetSingle.getGrid(), other );
}
/**
* Test if the interpolate function works reasonable.
*/
void testInterpolate( void )
{
// create dummies, since they are needed in almost every test
boost::shared_ptr< WGrid > grid = boost::shared_ptr< WGrid >( new WGridRegular3D( 5, 3, 3, 1, 1, 1 ) );
std::vector< double > data( grid->size() );
for( size_t i = 0; i < grid->size(); ++i )
{
data[i] = i;
}
boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 0, 1, data, W_DT_DOUBLE ) );
WDataSetSingle ds( valueSet, grid );
TS_ASSERT_EQUALS( ds.interpolate( wmath::WPosition() ), data[0] );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 0 ) ), data[1], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 0 ) ), data[5], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 0 ) ), data[6], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 0, 1 ) ), data[15], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 1 ) ), data[16], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 1 ) ), data[20], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 1 ) ), data[21], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.3, 0.4, 0.5 ) ), 9.8, 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.5, 0.5, 0.5 ) ), 10.5, 1e-9 );
}
};
#endif // WDATASETSINGLE_TEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment