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