OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WDataTexture3D.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
26 
27 #include "WValueSet.h"
28 
29 #include "../graphicsEngine/WGETextureUtils.h"
30 
31 #include "WDataTexture3D.h"
32 
33 WDataTexture3D::WDataTexture3D( boost::shared_ptr< WValueSetBase > valueSet, boost::shared_ptr< WGridRegular3D > grid ):
34  WGETexture3D( static_cast< float >( valueSet->getMaximumValue() - valueSet->getMinimumValue() ),
35  static_cast< float >( valueSet->getMinimumValue() ) ),
36  m_valueSet( valueSet ),
37  m_boundingBox( grid->getBoundingBox() )
38 {
39  // initialize members
40  setTextureSize( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
41 
42  // data textures do not repeat or something
43  setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
44  setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
45  setWrap( osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER );
46 
47  threshold()->setMin( valueSet->getMinimumValue() );
48  threshold()->setMax( valueSet->getMaximumValue() );
49  threshold()->set( valueSet->getMinimumValue() );
50 
51  // Scale according to bbox. This scaling is NOT included in the grid's transform, so we need to add it here
53  scale( 0, 0 ) = 1.0 / grid->getNbCoordsX();
54  scale( 1, 1 ) = 1.0 / grid->getNbCoordsY();
55  scale( 2, 2 ) = 1.0 / grid->getNbCoordsZ();
56  scale( 3, 3 ) = 1.0;
57 
58  // Move to voxel center. This scaling is NOT included in the grid's transform, so we need to add it here
59  WMatrix4d offset = WMatrix4d::identity();
60  offset( 0, 3 ) = 0.5 / grid->getNbCoordsX();
61  offset( 1, 3 ) = 0.5 / grid->getNbCoordsY();
62  offset( 2, 3 ) = 0.5 / grid->getNbCoordsZ();
63 
64  transformation()->set( invert( static_cast< WMatrix4d >( grid->getTransform() ) ) * scale * offset );
65 
66  // set the size
67  WGETexture3D::initTextureSize( this, grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
68 }
69 
71 {
72  // cleanup
73 }
74 
76 {
77  osg::ref_ptr< osg::Image > ima;
78 
79  if( m_valueSet->getDataType() == W_DT_UINT8 )
80  {
81  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT8";
82  boost::shared_ptr< WValueSet< uint8_t > > vs = boost::shared_dynamic_cast< WValueSet< uint8_t > >( m_valueSet );
83  uint8_t* source = const_cast< uint8_t* > ( vs->rawData() );
84  ima = createTexture( source, m_valueSet->dimension() );
85  }
86  else if( m_valueSet->getDataType() == W_DT_INT8 )
87  {
88  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT8";
89  boost::shared_ptr< WValueSet< int8_t > > vs = boost::shared_dynamic_cast< WValueSet< int8_t > >( m_valueSet );
90  int8_t* source = const_cast< int8_t* > ( vs->rawData() );
91  ima = createTexture( source, m_valueSet->dimension() );
92  }
93  else if( m_valueSet->getDataType() == W_DT_INT16 )
94  {
95  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT16";
96  boost::shared_ptr< WValueSet< int16_t > > vs = boost::shared_dynamic_cast< WValueSet< int16_t > >( m_valueSet );
97  int16_t* source = const_cast< int16_t* > ( vs->rawData() );
98  ima = createTexture( source, m_valueSet->dimension() );
99  }
100  else if( m_valueSet->getDataType() == W_DT_UINT16 )
101  {
102  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT16";
103  boost::shared_ptr< WValueSet< uint16_t > > vs = boost::shared_dynamic_cast< WValueSet< uint16_t > >( m_valueSet );
104  uint16_t* source = const_cast< uint16_t* > ( vs->rawData() );
105  ima = createTexture( source, m_valueSet->dimension() );
106  }
107  else if( m_valueSet->getDataType() == W_DT_SIGNED_INT )
108  {
109  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_SIGNED_INT";
110  boost::shared_ptr< WValueSet< int32_t > > vs = boost::shared_dynamic_cast< WValueSet< int32_t > >( m_valueSet );
111  int* source = const_cast< int* > ( vs->rawData() );
112  ima = createTexture( source, m_valueSet->dimension() );
113  }
114  else if( m_valueSet->getDataType() == W_DT_FLOAT )
115  {
116  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT";
117  boost::shared_ptr< WValueSet< float > > vs = boost::shared_dynamic_cast< WValueSet< float > >( m_valueSet );
118  float* source = const_cast< float* > ( vs->rawData() );
119  ima = createTexture( source, m_valueSet->dimension() );
120  }
121  else if( m_valueSet->getDataType() == W_DT_DOUBLE )
122  {
123  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_DOUBLE";
124  boost::shared_ptr< WValueSet< double > > vs = boost::shared_dynamic_cast< WValueSet< double > >( m_valueSet );
125  double* source = const_cast< double* > ( vs->rawData() );
126  ima = createTexture( source, m_valueSet->dimension() );
127  }
128  else
129  {
130  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type" << m_valueSet->getDataType();
131  wlog::error( "WDataTexture3D" ) << "Conversion of this data type to texture not supported yet.";
132  }
133 
134  // remove our link to the value set here. It can be free'd now if no one else uses it anymore
135  m_valueSet.reset();
136 
137  setImage( ima );
138  dirtyTextureObject();
139 }
140 
142 {
143  return m_boundingBox;
144 }
145 
146 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit, std::string prefix )
147 {
148  wge::bindTexture( node, osg::ref_ptr< WGETexture3D >( texture ), unit, prefix );
149 }
150