OpenWalnut 1.3.1
WDataTexture3D.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 <string>
00026 
00027 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00028 #include "../graphicsEngine/WGETextureUtils.h"
00029 #include "WDataTexture3D.h"
00030 #include "WValueSet.h"
00031 
00032 WDataTexture3D::WDataTexture3D( boost::shared_ptr< WValueSetBase > valueSet, boost::shared_ptr< WGridRegular3D > grid ):
00033     WGETexture3D( static_cast< float >( valueSet->getMaximumValue() - valueSet->getMinimumValue() ),
00034                   static_cast< float >( valueSet->getMinimumValue() ) ),
00035     m_valueSet( valueSet ),
00036     m_boundingBox( grid->getBoundingBox() )
00037 {
00038     // initialize members
00039     setTextureSize( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
00040 
00041     // data textures do not repeat or something
00042     setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
00043     setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
00044     setWrap( osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER );
00045 
00046     threshold()->setMin( valueSet->getMinimumValue() );
00047     threshold()->setMax( valueSet->getMaximumValue() );
00048     threshold()->set( valueSet->getMinimumValue() );
00049 
00050     // Scale according to bbox. This scaling is NOT included in the grid's transform, so we need to add it here
00051     WMatrix4d scale = WMatrix4d::zero();
00052     scale( 0, 0 ) = 1.0 / grid->getNbCoordsX();
00053     scale( 1, 1 ) = 1.0 / grid->getNbCoordsY();
00054     scale( 2, 2 ) = 1.0 / grid->getNbCoordsZ();
00055     scale( 3, 3 ) = 1.0;
00056 
00057     // Move to voxel center. This scaling is NOT included in the grid's transform, so we need to add it here
00058     WMatrix4d offset = WMatrix4d::identity();
00059     offset( 0, 3 ) = 0.5 / grid->getNbCoordsX();
00060     offset( 1, 3 ) = 0.5 / grid->getNbCoordsY();
00061     offset( 2, 3 ) = 0.5 / grid->getNbCoordsZ();
00062 
00063     transformation()->set( invert( static_cast< WMatrix4d >( grid->getTransform() ) ) * scale * offset );
00064 
00065     // set the size
00066     WGETexture3D::initTextureSize( this, grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
00067 }
00068 
00069 WDataTexture3D::~WDataTexture3D()
00070 {
00071     // cleanup
00072 }
00073 
00074 void WDataTexture3D::create()
00075 {
00076     osg::ref_ptr< osg::Image > ima;
00077 
00078     if( m_valueSet->getDataType() == W_DT_UINT8 )
00079     {
00080         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT8";
00081         boost::shared_ptr< WValueSet< uint8_t > > vs = boost::shared_dynamic_cast< WValueSet< uint8_t > >( m_valueSet );
00082         uint8_t* source = const_cast< uint8_t* > ( vs->rawData() );
00083         ima = createTexture( source, m_valueSet->dimension() );
00084     }
00085     else if( m_valueSet->getDataType() == W_DT_INT8 )
00086     {
00087         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT8";
00088         boost::shared_ptr< WValueSet< int8_t > > vs = boost::shared_dynamic_cast< WValueSet< int8_t > >( m_valueSet );
00089         int8_t* source = const_cast< int8_t* > ( vs->rawData() );
00090         ima = createTexture( source, m_valueSet->dimension() );
00091     }
00092     else if( m_valueSet->getDataType() == W_DT_INT16 )
00093     {
00094         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT16";
00095         boost::shared_ptr< WValueSet< int16_t > > vs = boost::shared_dynamic_cast< WValueSet< int16_t > >( m_valueSet );
00096         int16_t* source = const_cast< int16_t* > ( vs->rawData() );
00097         ima = createTexture( source, m_valueSet->dimension() );
00098     }
00099     else if( m_valueSet->getDataType() == W_DT_UINT16 )
00100     {
00101         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT16";
00102         boost::shared_ptr< WValueSet< uint16_t > > vs = boost::shared_dynamic_cast< WValueSet< uint16_t > >( m_valueSet );
00103         uint16_t* source = const_cast< uint16_t* > ( vs->rawData() );
00104         ima = createTexture( source, m_valueSet->dimension() );
00105     }
00106     else if( m_valueSet->getDataType() == W_DT_UINT32 )
00107     {
00108         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT32";
00109         boost::shared_ptr< WValueSet< uint32_t > > vs = boost::shared_dynamic_cast< WValueSet< uint32_t > >( m_valueSet );
00110         uint32_t* source = const_cast< uint32_t* > ( vs->rawData() );
00111         ima = createTexture( source, m_valueSet->dimension() );
00112     }
00113     else if( m_valueSet->getDataType() == W_DT_INT64 )
00114     {
00115         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT64";
00116         boost::shared_ptr< WValueSet< int64_t > > vs = boost::shared_dynamic_cast< WValueSet< int64_t > >( m_valueSet );
00117         int64_t* source = const_cast< int64_t* > ( vs->rawData() );
00118         ima = createTexture( source, m_valueSet->dimension() );
00119     }
00120     else if( m_valueSet->getDataType() == W_DT_UINT64 )
00121     {
00122         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT64";
00123         boost::shared_ptr< WValueSet< uint64_t > > vs = boost::shared_dynamic_cast< WValueSet< uint64_t > >( m_valueSet );
00124         uint64_t* source = const_cast< uint64_t* > ( vs->rawData() );
00125         ima = createTexture( source, m_valueSet->dimension() );
00126     }
00127     else if( m_valueSet->getDataType() == W_DT_SIGNED_INT )
00128     {
00129         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_SIGNED_INT";
00130         boost::shared_ptr< WValueSet< int32_t > > vs = boost::shared_dynamic_cast< WValueSet< int32_t > >( m_valueSet );
00131         int* source = const_cast< int* > ( vs->rawData() );
00132         ima = createTexture( source, m_valueSet->dimension() );
00133     }
00134     else if( m_valueSet->getDataType() == W_DT_FLOAT )
00135     {
00136         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT";
00137         boost::shared_ptr< WValueSet< float > > vs = boost::shared_dynamic_cast< WValueSet< float > >( m_valueSet );
00138         float* source = const_cast< float* > ( vs->rawData() );
00139         ima = createTexture( source, m_valueSet->dimension() );
00140     }
00141     else if( m_valueSet->getDataType() == W_DT_DOUBLE )
00142     {
00143         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_DOUBLE";
00144         boost::shared_ptr< WValueSet< double > > vs = boost::shared_dynamic_cast< WValueSet< double > >( m_valueSet );
00145         double* source = const_cast< double* > ( vs->rawData() );
00146         ima = createTexture( source, m_valueSet->dimension() );
00147     }
00148     else if( m_valueSet->getDataType() == W_DT_FLOAT128 )
00149     {
00150         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT128";
00151         boost::shared_ptr< WValueSet< long double > > vs = boost::shared_dynamic_cast< WValueSet< long double > >( m_valueSet );
00152         long double* source = const_cast< long double* > ( vs->rawData() );
00153         ima = createTexture( source, m_valueSet->dimension() );
00154     }
00155     else
00156     {
00157         wlog::debug( "WDataTexture3D" ) << "Creating Texture of type " << m_valueSet->getDataType();
00158         wlog::error( "WDataTexture3D" ) << "Conversion of this data type to texture not supported yet.";
00159     }
00160 
00161     // remove our link to the value set here. It can be free'd now if no one else uses it anymore
00162     m_valueSet.reset();
00163 
00164     setImage( ima );
00165     dirtyTextureObject();
00166 }
00167 
00168 WBoundingBox WDataTexture3D::getBoundingBox() const
00169 {
00170     return m_boundingBox;
00171 }
00172 
00173 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit, std::string prefix )
00174 {
00175     wge::bindTexture( node, osg::ref_ptr< WGETexture3D >( texture ), unit, prefix );
00176 }
00177