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