OpenWalnut  1.3.1
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 <string>
26 
27 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
28 #include "../graphicsEngine/WGETextureUtils.h"
29 #include "WDataTexture3D.h"
30 #include "WValueSet.h"
31 
32 WDataTexture3D::WDataTexture3D( boost::shared_ptr< WValueSetBase > valueSet, boost::shared_ptr< WGridRegular3D > grid ):
33  WGETexture3D( static_cast< float >( valueSet->getMaximumValue() - valueSet->getMinimumValue() ),
34  static_cast< float >( valueSet->getMinimumValue() ) ),
35  m_valueSet( valueSet ),
36  m_boundingBox( grid->getBoundingBox() )
37 {
38  // initialize members
39  setTextureSize( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
40 
41  // data textures do not repeat or something
42  setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
43  setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
44  setWrap( osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER );
45 
46  threshold()->setMin( valueSet->getMinimumValue() );
47  threshold()->setMax( valueSet->getMaximumValue() );
48  threshold()->set( valueSet->getMinimumValue() );
49 
50  // Scale according to bbox. This scaling is NOT included in the grid's transform, so we need to add it here
52  scale( 0, 0 ) = 1.0 / grid->getNbCoordsX();
53  scale( 1, 1 ) = 1.0 / grid->getNbCoordsY();
54  scale( 2, 2 ) = 1.0 / grid->getNbCoordsZ();
55  scale( 3, 3 ) = 1.0;
56 
57  // Move to voxel center. This scaling is NOT included in the grid's transform, so we need to add it here
58  WMatrix4d offset = WMatrix4d::identity();
59  offset( 0, 3 ) = 0.5 / grid->getNbCoordsX();
60  offset( 1, 3 ) = 0.5 / grid->getNbCoordsY();
61  offset( 2, 3 ) = 0.5 / grid->getNbCoordsZ();
62 
63  transformation()->set( invert( static_cast< WMatrix4d >( grid->getTransform() ) ) * scale * offset );
64 
65  // set the size
66  WGETexture3D::initTextureSize( this, grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
67 }
68 
70 {
71  // cleanup
72 }
73 
75 {
76  osg::ref_ptr< osg::Image > ima;
77 
78  if( m_valueSet->getDataType() == W_DT_UINT8 )
79  {
80  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT8";
81  boost::shared_ptr< WValueSet< uint8_t > > vs = boost::shared_dynamic_cast< WValueSet< uint8_t > >( m_valueSet );
82  uint8_t* source = const_cast< uint8_t* > ( vs->rawData() );
83  ima = createTexture( source, m_valueSet->dimension() );
84  }
85  else if( m_valueSet->getDataType() == W_DT_INT8 )
86  {
87  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT8";
88  boost::shared_ptr< WValueSet< int8_t > > vs = boost::shared_dynamic_cast< WValueSet< int8_t > >( m_valueSet );
89  int8_t* source = const_cast< int8_t* > ( vs->rawData() );
90  ima = createTexture( source, m_valueSet->dimension() );
91  }
92  else if( m_valueSet->getDataType() == W_DT_INT16 )
93  {
94  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT16";
95  boost::shared_ptr< WValueSet< int16_t > > vs = boost::shared_dynamic_cast< WValueSet< int16_t > >( m_valueSet );
96  int16_t* source = const_cast< int16_t* > ( vs->rawData() );
97  ima = createTexture( source, m_valueSet->dimension() );
98  }
99  else if( m_valueSet->getDataType() == W_DT_UINT16 )
100  {
101  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT16";
102  boost::shared_ptr< WValueSet< uint16_t > > vs = boost::shared_dynamic_cast< WValueSet< uint16_t > >( m_valueSet );
103  uint16_t* source = const_cast< uint16_t* > ( vs->rawData() );
104  ima = createTexture( source, m_valueSet->dimension() );
105  }
106  else if( m_valueSet->getDataType() == W_DT_UINT32 )
107  {
108  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT32";
109  boost::shared_ptr< WValueSet< uint32_t > > vs = boost::shared_dynamic_cast< WValueSet< uint32_t > >( m_valueSet );
110  uint32_t* source = const_cast< uint32_t* > ( vs->rawData() );
111  ima = createTexture( source, m_valueSet->dimension() );
112  }
113  else if( m_valueSet->getDataType() == W_DT_INT64 )
114  {
115  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT64";
116  boost::shared_ptr< WValueSet< int64_t > > vs = boost::shared_dynamic_cast< WValueSet< int64_t > >( m_valueSet );
117  int64_t* source = const_cast< int64_t* > ( vs->rawData() );
118  ima = createTexture( source, m_valueSet->dimension() );
119  }
120  else if( m_valueSet->getDataType() == W_DT_UINT64 )
121  {
122  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT64";
123  boost::shared_ptr< WValueSet< uint64_t > > vs = boost::shared_dynamic_cast< WValueSet< uint64_t > >( m_valueSet );
124  uint64_t* source = const_cast< uint64_t* > ( vs->rawData() );
125  ima = createTexture( source, m_valueSet->dimension() );
126  }
127  else if( m_valueSet->getDataType() == W_DT_SIGNED_INT )
128  {
129  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_SIGNED_INT";
130  boost::shared_ptr< WValueSet< int32_t > > vs = boost::shared_dynamic_cast< WValueSet< int32_t > >( m_valueSet );
131  int* source = const_cast< int* > ( vs->rawData() );
132  ima = createTexture( source, m_valueSet->dimension() );
133  }
134  else if( m_valueSet->getDataType() == W_DT_FLOAT )
135  {
136  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT";
137  boost::shared_ptr< WValueSet< float > > vs = boost::shared_dynamic_cast< WValueSet< float > >( m_valueSet );
138  float* source = const_cast< float* > ( vs->rawData() );
139  ima = createTexture( source, m_valueSet->dimension() );
140  }
141  else if( m_valueSet->getDataType() == W_DT_DOUBLE )
142  {
143  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_DOUBLE";
144  boost::shared_ptr< WValueSet< double > > vs = boost::shared_dynamic_cast< WValueSet< double > >( m_valueSet );
145  double* source = const_cast< double* > ( vs->rawData() );
146  ima = createTexture( source, m_valueSet->dimension() );
147  }
148  else if( m_valueSet->getDataType() == W_DT_FLOAT128 )
149  {
150  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT128";
151  boost::shared_ptr< WValueSet< long double > > vs = boost::shared_dynamic_cast< WValueSet< long double > >( m_valueSet );
152  long double* source = const_cast< long double* > ( vs->rawData() );
153  ima = createTexture( source, m_valueSet->dimension() );
154  }
155  else
156  {
157  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type " << m_valueSet->getDataType();
158  wlog::error( "WDataTexture3D" ) << "Conversion of this data type to texture not supported yet.";
159  }
160 
161  // remove our link to the value set here. It can be free'd now if no one else uses it anymore
162  m_valueSet.reset();
163 
164  setImage( ima );
165  dirtyTextureObject();
166 }
167 
169 {
170  return m_boundingBox;
171 }
172 
173 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit, std::string prefix )
174 {
175  wge::bindTexture( node, osg::ref_ptr< WGETexture3D >( texture ), unit, prefix );
176 }
177