OpenWalnut 1.2.5
WDataSetSphericalHarmonics.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <stdint.h>
00026 
00027 #include <cmath>
00028 #include <string>
00029 #include <vector>
00030 
00031 #include "../common/WAssert.h"
00032 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00033 #include "WDataSetSingle.h"
00034 #include "WDataSetSphericalHarmonics.h"
00035 
00036 // prototype instance as singleton
00037 boost::shared_ptr< WPrototyped > WDataSetSphericalHarmonics::m_prototype = boost::shared_ptr< WPrototyped >();
00038 
00039 WDataSetSphericalHarmonics::WDataSetSphericalHarmonics( boost::shared_ptr< WValueSetBase > newValueSet,
00040                                                         boost::shared_ptr< WGrid > newGrid ) :
00041     WDataSetSingle( newValueSet, newGrid ), m_valueSet( newValueSet )
00042 {
00043     m_gridRegular3D = boost::shared_dynamic_cast< WGridRegular3D >( newGrid );
00044     WAssert( newValueSet, "No value set given." );
00045     WAssert( newGrid, "No grid given." );
00046 }
00047 
00048 WDataSetSphericalHarmonics::WDataSetSphericalHarmonics()
00049     : WDataSetSingle()
00050 {
00051 }
00052 
00053 WDataSetSphericalHarmonics::~WDataSetSphericalHarmonics()
00054 {
00055 }
00056 
00057 WDataSetSingle::SPtr WDataSetSphericalHarmonics::clone( boost::shared_ptr< WValueSetBase > newValueSet ) const
00058 {
00059     return WDataSetSingle::SPtr( new WDataSetSphericalHarmonics( newValueSet, getGrid() ) );
00060 }
00061 
00062 WDataSetSingle::SPtr WDataSetSphericalHarmonics::clone( boost::shared_ptr< WGrid > newGrid ) const
00063 {
00064     return WDataSetSingle::SPtr( new WDataSetSphericalHarmonics( getValueSet(), newGrid ) );
00065 }
00066 
00067 WDataSetSingle::SPtr WDataSetSphericalHarmonics::clone() const
00068 {
00069     return WDataSetSingle::SPtr( new WDataSetSphericalHarmonics( getValueSet(), getGrid() ) );
00070 }
00071 
00072 boost::shared_ptr< WPrototyped > WDataSetSphericalHarmonics::getPrototype()
00073 {
00074     if( !m_prototype )
00075     {
00076         m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetSphericalHarmonics() );
00077     }
00078 
00079     return m_prototype;
00080 }
00081 
00082 WSymmetricSphericalHarmonic WDataSetSphericalHarmonics::interpolate( const WPosition& pos, bool* success ) const
00083 {
00084     *success = m_gridRegular3D->encloses( pos );
00085 
00086     bool isInside = true;
00087     size_t cellId = m_gridRegular3D->getCellId( pos, &isInside );
00088 
00089     if( !isInside )
00090     {
00091         *success = false;
00092         return WSymmetricSphericalHarmonic();
00093     }
00094 
00095     // ids of vertices for interpolation
00096     std::vector< size_t > vertexIds = m_gridRegular3D->getCellVertexIds( cellId );
00097 
00098     WPosition localPos = pos - m_gridRegular3D->getPosition( vertexIds[0] );
00099 
00100     double lambdaX = localPos[0] / m_gridRegular3D->getOffsetX();
00101     double lambdaY = localPos[1] / m_gridRegular3D->getOffsetY();
00102     double lambdaZ = localPos[2] / m_gridRegular3D->getOffsetZ();
00103     WValue< double > h( 8 );
00104 //         lZ     lY
00105 //         |      /
00106 //         | 6___/_7
00107 //         |/:    /|
00108 //         4_:___5 |
00109 //         | :...|.|
00110 //         |.2   | 3
00111 //         |_____|/ ____lX
00112 //        0      1
00113     h[0] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
00114     h[1] = (     lambdaX ) * ( 1 - lambdaY ) * ( 1 - lambdaZ );
00115     h[2] = ( 1 - lambdaX ) * (     lambdaY ) * ( 1 - lambdaZ );
00116     h[3] = (     lambdaX ) * (     lambdaY ) * ( 1 - lambdaZ );
00117     h[4] = ( 1 - lambdaX ) * ( 1 - lambdaY ) * (     lambdaZ );
00118     h[5] = (     lambdaX ) * ( 1 - lambdaY ) * (     lambdaZ );
00119     h[6] = ( 1 - lambdaX ) * (     lambdaY ) * (     lambdaZ );
00120     h[7] = (     lambdaX ) * (     lambdaY ) * (     lambdaZ );
00121 
00122     // take
00123     WValue<double> interpolatedCoefficients( m_valueSet->dimension() );
00124     for( size_t i = 0; i < 8; ++i )
00125     {
00126         interpolatedCoefficients += h[i] * m_valueSet->getWValueDouble( vertexIds[i] );
00127     }
00128 
00129     *success = true;
00130 
00131     return WSymmetricSphericalHarmonic( interpolatedCoefficients );
00132 }
00133 
00134 WSymmetricSphericalHarmonic WDataSetSphericalHarmonics::getSphericalHarmonicAt( size_t index ) const
00135 {
00136     if( index < m_valueSet->size() ) return WSymmetricSphericalHarmonic( m_valueSet->getWValueDouble( index ) );
00137     return WSymmetricSphericalHarmonic();
00138 }
00139 
00140 const std::string WDataSetSphericalHarmonics::getName() const
00141 {
00142     return "WDataSetSphericalHarmonics";
00143 }
00144 
00145 const std::string WDataSetSphericalHarmonics::getDescription() const
00146 {
00147     return "Contains factors for spherical harmonics.";
00148 }
00149 
00150 bool WDataSetSphericalHarmonics::isTexture() const
00151 {
00152     return false;
00153 }
00154 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends