OpenWalnut  1.4.0
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 "../graphicsEngine/WGETextureUtils.h"
28 #include "WDataTexture3D.h"
29 #include "WValueSet.h"
30 
31 WDataTexture3D::WDataTexture3D( boost::shared_ptr< WValueSetBase > valueSet, boost::shared_ptr< WGridRegular3D > grid ):
32  WGETexture3D( static_cast< float >( valueSet->getMaximumValue() - valueSet->getMinimumValue() ),
33  static_cast< float >( valueSet->getMinimumValue() ) ),
34  m_valueSet( valueSet ),
35  m_boundingBox( grid->getBoundingBoxIncludingBorder() )
36 {
37  // initialize members
38  setTextureSize( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
39 
40  // data textures do not repeat or something
41  setWrap( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER );
42  setWrap( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER );
43  setWrap( osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_BORDER );
44 
45  thresholdLower()->setMin( valueSet->getMinimumValue() );
46  thresholdLower()->setMax( valueSet->getMaximumValue() );
47  thresholdLower()->set( valueSet->getMinimumValue() );
48  thresholdUpper()->setMin( valueSet->getMinimumValue() );
49  thresholdUpper()->setMax( valueSet->getMaximumValue() );
50  thresholdUpper()->set( valueSet->getMaximumValue() );
51 
52  window()->set( make_interval( valueSet->getMinimumValue(),
53  valueSet->getMaximumValue() ) );
54 
55  // Scale according to bbox. This scaling is NOT included in the grid's transform, so we need to add it here
57  scale( 0, 0 ) = 1.0 / grid->getNbCoordsX();
58  scale( 1, 1 ) = 1.0 / grid->getNbCoordsY();
59  scale( 2, 2 ) = 1.0 / grid->getNbCoordsZ();
60  scale( 3, 3 ) = 1.0;
61 
62  // Move to voxel center. This scaling is NOT included in the grid's transform, so we need to add it here
63  WMatrix4d offset = WMatrix4d::identity();
64  offset( 0, 3 ) = 0.5 / grid->getNbCoordsX();
65  offset( 1, 3 ) = 0.5 / grid->getNbCoordsY();
66  offset( 2, 3 ) = 0.5 / grid->getNbCoordsZ();
67 
68  transformation()->set( invert( static_cast< WMatrix4d >( grid->getTransform() ) ) * scale * offset );
69 
70  // set the size
71  WGETexture3D::initTextureSize( this, grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ() );
72 }
73 
75 {
76  // cleanup
77 }
78 
80 {
81  osg::ref_ptr< osg::Image > ima;
82 
83  if( m_valueSet->getDataType() == W_DT_UINT8 )
84  {
85  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT8";
86  boost::shared_ptr< WValueSet< uint8_t > > vs = boost::dynamic_pointer_cast< WValueSet< uint8_t > >( m_valueSet );
87  uint8_t* source = const_cast< uint8_t* > ( vs->rawData() );
88  ima = createTexture( source, m_valueSet->dimension() );
89  }
90  else if( m_valueSet->getDataType() == W_DT_INT8 )
91  {
92  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT8";
93  boost::shared_ptr< WValueSet< int8_t > > vs = boost::dynamic_pointer_cast< WValueSet< int8_t > >( m_valueSet );
94  int8_t* source = const_cast< int8_t* > ( vs->rawData() );
95  ima = createTexture( source, m_valueSet->dimension() );
96  }
97  else if( m_valueSet->getDataType() == W_DT_INT16 )
98  {
99  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT16";
100  boost::shared_ptr< WValueSet< int16_t > > vs = boost::dynamic_pointer_cast< WValueSet< int16_t > >( m_valueSet );
101  int16_t* source = const_cast< int16_t* > ( vs->rawData() );
102  ima = createTexture( source, m_valueSet->dimension() );
103  }
104  else if( m_valueSet->getDataType() == W_DT_UINT16 )
105  {
106  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT16";
107  boost::shared_ptr< WValueSet< uint16_t > > vs = boost::dynamic_pointer_cast< WValueSet< uint16_t > >( m_valueSet );
108  uint16_t* source = const_cast< uint16_t* > ( vs->rawData() );
109  ima = createTexture( source, m_valueSet->dimension() );
110  }
111  else if( m_valueSet->getDataType() == W_DT_UINT32 )
112  {
113  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT32";
114  boost::shared_ptr< WValueSet< uint32_t > > vs = boost::dynamic_pointer_cast< WValueSet< uint32_t > >( m_valueSet );
115  uint32_t* source = const_cast< uint32_t* > ( vs->rawData() );
116  ima = createTexture( source, m_valueSet->dimension() );
117  }
118  else if( m_valueSet->getDataType() == W_DT_INT64 )
119  {
120  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_INT64";
121  boost::shared_ptr< WValueSet< int64_t > > vs = boost::dynamic_pointer_cast< WValueSet< int64_t > >( m_valueSet );
122  int64_t* source = const_cast< int64_t* > ( vs->rawData() );
123  ima = createTexture( source, m_valueSet->dimension() );
124  }
125  else if( m_valueSet->getDataType() == W_DT_UINT64 )
126  {
127  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_UINT64";
128  boost::shared_ptr< WValueSet< uint64_t > > vs = boost::dynamic_pointer_cast< WValueSet< uint64_t > >( m_valueSet );
129  uint64_t* source = const_cast< uint64_t* > ( vs->rawData() );
130  ima = createTexture( source, m_valueSet->dimension() );
131  }
132  else if( m_valueSet->getDataType() == W_DT_SIGNED_INT )
133  {
134  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_SIGNED_INT";
135  boost::shared_ptr< WValueSet< int32_t > > vs = boost::dynamic_pointer_cast< WValueSet< int32_t > >( m_valueSet );
136  int* source = const_cast< int* > ( vs->rawData() );
137  ima = createTexture( source, m_valueSet->dimension() );
138  }
139  else if( m_valueSet->getDataType() == W_DT_FLOAT )
140  {
141  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT";
142  boost::shared_ptr< WValueSet< float > > vs = boost::dynamic_pointer_cast< WValueSet< float > >( m_valueSet );
143  float* source = const_cast< float* > ( vs->rawData() );
144  ima = createTexture( source, m_valueSet->dimension() );
145  }
146  else if( m_valueSet->getDataType() == W_DT_DOUBLE )
147  {
148  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_DOUBLE";
149  boost::shared_ptr< WValueSet< double > > vs = boost::dynamic_pointer_cast< WValueSet< double > >( m_valueSet );
150  double* source = const_cast< double* > ( vs->rawData() );
151  ima = createTexture( source, m_valueSet->dimension() );
152  }
153  else if( m_valueSet->getDataType() == W_DT_FLOAT128 )
154  {
155  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type W_DT_FLOAT128";
156  boost::shared_ptr< WValueSet< long double > > vs = boost::dynamic_pointer_cast< WValueSet< long double > >( m_valueSet );
157  long double* source = const_cast< long double* > ( vs->rawData() );
158  ima = createTexture( source, m_valueSet->dimension() );
159  }
160  else
161  {
162  wlog::debug( "WDataTexture3D" ) << "Creating Texture of type " << m_valueSet->getDataType();
163  wlog::error( "WDataTexture3D" ) << "Conversion of this data type to texture not supported yet.";
164  }
165 
166  // remove our link to the value set here. It can be free'd now if no one else uses it anymore
167  m_valueSet.reset();
168 
169  setImage( ima );
170  dirtyTextureObject();
171 }
172 
174 {
175  return m_boundingBox;
176 }
177 
178 void wge::bindTexture( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit, std::string prefix )
179 {
180  wge::bindTexture( node, osg::ref_ptr< WGETexture3D >( texture ), unit, prefix );
181 }
182 
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:302
boost::shared_ptr< WValueSetBase > m_valueSet
The value set from which the texture gets created.
WPropInterval window() const
Returns the window level definition for the colormap.
Definition: WGETexture.h:651
osg::ref_ptr< osg::Image > createTexture(T *source, int components=1)
Creates a properly sized osg::Image from the specified source data.
virtual ~WDataTexture3D()
Destructor.
virtual WBoundingBox getBoundingBox() const
Returns the texture's bounding box.
WPropDouble thresholdUpper() const
Returns the threshold property.
Definition: WGETexture.h:615
static MatrixType zero()
Returns a zero-initialized matrix.
Definition: WMatrixFixed.h:329
This calls serves a simple purpose: have a texture and its scaling information together which allows ...
Definition: WGETexture.h:55
WBoundingBox m_boundingBox
The bounding box of the underlying grid.
virtual void create()
Creates the texture data.
WPropDouble thresholdLower() const
Returns the threshold property.
Definition: WGETexture.h:609
WDataTexture3D(boost::shared_ptr< WValueSetBase > valueSet, boost::shared_ptr< WGridRegular3D > grid)
Constructor.
static MatrixType identity()
Returns an identity matrix.
Definition: WMatrixFixed.h:314
Base Class for all value set types.
Definition: WValueSet.h:48
WPropDouble scale() const
Get the scaling factor for de-scaling the texture.
Definition: WGETexture.h:591
void bindTexture(osg::ref_ptr< osg::Node > node, osg::ref_ptr< WDataTexture3D > texture, size_t unit=0, std::string prefix="")
Binds the specified texture to the specified unit.
static void initTextureSize(osg::Texture1D *texture, int width, int height, int depth)
Initialize the size of the texture properly according to real texture type (1D,2D,3D).
Definition: WGETexture.h:729
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:335
WPropMatrix4X4 transformation() const
Returns the texture transformation matrix.
Definition: WGETexture.h:657