Commit 4083309f authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[ADD] interpolate for WDataSetVector3D

parent 9efe1773
......@@ -112,10 +112,10 @@ double WDataSetScalar::interpolate( const wmath::WPosition& pos, bool* success )
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." ) );
// }
// 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 ) )
{
......
......@@ -22,9 +22,11 @@
//
//---------------------------------------------------------------------------
#include <string>
#include <vector>
#include "../common/WAssert.h"
#include "WDataSetSingle.h"
#include "WDataSetVector.h"
// prototype instance as singleton
......@@ -59,3 +61,96 @@ boost::shared_ptr< WPrototyped > WDataSetVector::getPrototype()
return m_prototype;
}
wmath::WVector3D WDataSetVector::interpolate( const wmath::WPosition& pos, bool *success )
{
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() == 1 && m_valueSet->dimension() == 3 ) )
{
throw WException( std::string( "Only implemented for 3D Vectors so far." ) );
}
*success = grid->encloses( pos );
if( !*success )
{
return wmath::WVector3D( 0, 0, 0 );
}
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 );
wmath::WVector3D result( 0, 0, 0 );
for( size_t i = 0; i < 8; ++i )
{
result += h[i] * getVectorAt( vertexIds[i] );
}
*success = true;
return result;
}
wmath::WVector3D WDataSetVector::getVectorAt( size_t index ) const
{
switch( getValueSet()->getDataType() )
{
case W_DT_UNSIGNED_CHAR:
{
return boost::shared_dynamic_cast< WValueSet< unsigned char > >( getValueSet() )->getVector3D( index );
}
case W_DT_INT16:
{
return boost::shared_dynamic_cast< WValueSet< int16_t > >( getValueSet() )->getVector3D( index );
}
case W_DT_SIGNED_INT:
{
return boost::shared_dynamic_cast< WValueSet< int32_t > >( getValueSet() )->getVector3D( index );
}
case W_DT_FLOAT:
{
return boost::shared_dynamic_cast< WValueSet< float > >( getValueSet() )->getVector3D( index );
}
case W_DT_DOUBLE:
{
return boost::shared_dynamic_cast< WValueSet< double > >( getValueSet() )->getVector3D( index );
}
default:
assert( false && "Unknow data type in dataset." );
}
return wmath::WVector3D( 0, 0, 0 );
}
......@@ -61,6 +61,26 @@ public:
*/
static boost::shared_ptr< WPrototyped > getPrototype();
/**
* Interpolates the vector field at the given position
*
* \param pos position to interpolate
* \param success if the position was inside the grid
*
* \return Vector beeing the interpolate.
*/
wmath::WVector3D interpolate( const wmath::WPosition &pos, bool *success );
/**
* Get the vector on the given position in value set.
* \note currently only implmented for WVector3D
*
* \param index the position where to get the vector from
*
* \return the vector
*/
wmath::WVector3D getVectorAt( size_t index ) const;
protected:
/**
......
......@@ -29,9 +29,11 @@
#include <cstddef>
#include <vector>
#include "WValueSetBase.h"
#include "WDataHandlerEnums.h"
#include "../common/math/WValue.h"
#include "../common/math/WVector3D.h"
#include "../common/WAssert.h"
#include "WDataHandlerEnums.h"
#include "WValueSetBase.h"
/**
* Base Class for all value set types.
......@@ -107,6 +109,15 @@ public:
return static_cast< double >( m_data[i] );
}
/**
* Get the i'th vector
*
* \param index the index number of the vector
*
* \return the vector
*/
wmath::WVector3D getVector3D( size_t index ) const;
/**
* Sometimes we need raw access to the data array, for e.g. OpenGL.
*/
......@@ -133,4 +144,11 @@ private:
const std::vector< T > m_data; // WARNING: don't remove constness since &m_data[0] won't work anymore!
};
template< typename T > wmath::WVector3D WValueSet< T >::getVector3D( size_t index ) const
{
WAssert( m_order == 1 && m_dimension == 3, "WValueSet<T>::getVector3D only implemented for order==1, dim==3 value sets" );
size_t offset = index * 3;
return wmath::WVector3D( m_data[ offset ], m_data[ offset + 1 ], m_data[ offset + 2 ] );
}
#endif // WVALUESET_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WDATASETVECTOR_TEST_H
#define WDATASETVECTOR_TEST_H
#include <vector>
#include <cxxtest/TestSuite.h>
#include "../WDataSetVector.h"
/**
* TODO(lmath): Document this!
*/
class WDataSetVectorTest : public CxxTest::TestSuite
{
public:
/**
* An interpolate of an vector is as if every components were interpolated
*/
void testInterpolate( void )
{
boost::shared_ptr< WGrid > grid = boost::shared_ptr< WGrid >( new WGridRegular3D( 5, 3, 3, 1, 1, 1 ) );
std::vector< double > data( grid->size() * 3 );
for( size_t i = 0; i < grid->size() * 3; ++i )
{
data[i] = i;
}
boost::shared_ptr< WValueSet< double > > valueSet( new WValueSet< double >( 1, 3, data, W_DT_DOUBLE ) );
WDataSetVector ds( valueSet, grid );
bool success = false;
TS_ASSERT_EQUALS( ds.interpolate( wmath::WPosition(), &success )[0], data[0] );
TS_ASSERT_EQUALS( ds.interpolate( wmath::WPosition(), &success )[1], data[1] );
TS_ASSERT_EQUALS( ds.interpolate( wmath::WPosition(), &success )[2], data[2] );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 0 ), &success )[0], data[3], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 0 ), &success )[1], data[4], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 0 ), &success )[2], data[5], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 0 ), &success )[0], data[15], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 0 ), &success )[1], data[16], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 0 ), &success )[2], data[17], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 0 ), &success )[0], data[18], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 0 ), &success )[1], data[19], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 0 ), &success )[2], data[20], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 0, 1 ), &success )[0], data[45], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 0, 1 ), &success )[1], data[46], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 0, 1 ), &success )[2], data[47], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 1 ), &success )[0], data[48], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 1 ), &success )[1], data[49], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 0, 1 ), &success )[2], data[50], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 1 ), &success )[0], data[60], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 1 ), &success )[1], data[61], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0, 1, 1 ), &success )[2], data[62], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 1 ), &success )[0], data[63], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 1 ), &success )[1], data[64], 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 1, 1, 1 ), &success )[2], data[65], 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.3, 0.4, 0.5 ), &success )[0], 29.4, 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.3, 0.4, 0.5 ), &success )[1], 30.4, 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.3, 0.4, 0.5 ), &success )[2], 31.4, 1e-9 );
TS_ASSERT( success );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.5, 0.5, 0.5 ), &success )[0], 31.5, 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.5, 0.5, 0.5 ), &success )[1], 32.5, 1e-9 );
TS_ASSERT_DELTA( ds.interpolate( wmath::WPosition( 0.5, 0.5, 0.5 ), &success )[2], 33.5, 1e-9 );
TS_ASSERT( success );
}
};
#endif // WDATASETVECTOR_TEST_H
......@@ -39,7 +39,7 @@ WRoiProjectFileIO::~WRoiProjectFileIO()
// cleanup
}
bool WRoiProjectFileIO::parse( std::string line, unsigned int lineNumber )
bool WRoiProjectFileIO::parse( std::string /* line */, unsigned int /* lineNumber */ )
{
// read something
return false;
......
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