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