Commit 5f2f9f8d authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[ADD] - new way to keep textures in datasets

parent ab6ee71a
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <vector>
#include "WDataSetSingle.h"
#include "WDataTexture3D.h"
WDataTexture3D::WDataTexture3D( boost::shared_ptr<WValueSetBase> valueSet, boost::shared_ptr<WGrid> grid ):
m_alpha( 1.0 ),
m_threshold( 0.0 ),
m_texture( osg::ref_ptr< osg::Texture3D >() ),
m_valueSet( valueSet ),
m_grid( grid )
{
// initialize members
}
WDataTexture3D::~WDataTexture3D()
{
// cleanup
}
float WDataTexture3D::getAlpha() const
{
return m_alpha;
}
void WDataTexture3D::setAlpha( float alpha )
{
if ( ( alpha > 1.0 ) || ( alpha < 0.0 ) )
{
return;
}
m_alpha = alpha;
}
float WDataTexture3D::getThreshold() const
{
return m_threshold;
}
void WDataTexture3D::setThreshold( float threshold )
{
m_threshold = threshold;
}
osg::ref_ptr< osg::Texture3D > WDataTexture3D::getTexture()
{
if ( !m_texture )
{
createTexture();
}
return m_texture;
}
void WDataTexture3D::createTexture3D( unsigned char* source, int components )
{
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
if ( components == 1 )
{
osg::ref_ptr< osg::Image > ima = new osg::Image;
ima->allocateImage( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ(), GL_LUMINANCE, GL_UNSIGNED_BYTE );
unsigned char* data = ima->data();
for ( unsigned int i = 0; i < grid->getNbCoordsX() * grid->getNbCoordsY() * grid->getNbCoordsZ(); ++i )
{
data[i] = source[i];
}
m_texture = osg::ref_ptr<osg::Texture3D>( new osg::Texture3D );
m_texture->setFilter( osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR );
m_texture->setFilter( osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR );
m_texture->setWrap( osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT );
m_texture->setImage( ima );
m_texture->setResizeNonPowerOfTwoHint( false );
}
else if ( components == 3 )
{
osg::ref_ptr< osg::Image > ima = new osg::Image;
ima->allocateImage( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ(), GL_RGB, GL_UNSIGNED_BYTE );
unsigned char* data = ima->data();
for ( unsigned int i = 0; i < grid->getNbCoordsX() * grid->getNbCoordsY() * grid->getNbCoordsZ() * 3; ++i )
{
data[i] = source[i];
}
m_texture = osg::ref_ptr<osg::Texture3D>( new osg::Texture3D );
m_texture->setFilter( osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR );
m_texture->setFilter( osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR );
m_texture->setWrap( osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT );
m_texture->setImage( ima );
m_texture->setResizeNonPowerOfTwoHint( false );
}
// TODO(seralph): throw exception if components!=1 or 3
}
void WDataTexture3D::createTexture3D( int16_t* source, int components )
{
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
// TODO(seralph): throw exception if components!=1 or 3
if ( components == 1)
{
int nSize = grid->getNbCoordsX() * grid->getNbCoordsY() * grid->getNbCoordsZ();
std::vector<int16_t> tempSource( nSize );
for ( int i = 0; i < nSize; ++i )
{
tempSource[i] = static_cast<int16_t>( source[i] );
}
int max = 0;
std::vector< int > histo( 65536, 0 );
for ( int i = 0; i < nSize; ++i )
{
if ( max < tempSource[i])
{
max = tempSource[i];
}
++histo[tempSource[i]];
}
int fivepercent = static_cast<int>( nSize * 0.001 );
int newMax = 65535;
int adder = 0;
for ( int i = 65535; i > 0; --i )
{
adder += histo[i];
newMax = i;
if ( adder > fivepercent )
break;
}
for ( int i = 0; i < nSize; ++i )
{
if ( tempSource[i] > newMax )
{
tempSource[i] = newMax;
}
}
int mult = 65535 / newMax;
for ( int i = 0; i < nSize; ++i )
{
tempSource[i] *= mult;
}
osg::ref_ptr< osg::Image > ima = new osg::Image;
ima->allocateImage( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ(), GL_LUMINANCE, GL_UNSIGNED_SHORT );
unsigned char* data = ima->data();
unsigned char* charSource = ( unsigned char* )&tempSource[0];
for ( unsigned int i = 0; i < grid->getNbCoordsX() * grid->getNbCoordsY() * grid->getNbCoordsZ() * 2 ; ++i )
{
data[i] = charSource[i];
}
m_texture = osg::ref_ptr<osg::Texture3D>( new osg::Texture3D );
m_texture->setFilter( osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR );
m_texture->setFilter( osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR );
m_texture->setWrap( osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT );
m_texture->setImage( ima );
m_texture->setResizeNonPowerOfTwoHint( false );
}
}
void WDataTexture3D::createTexture3D( float* source, int components )
{
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_grid );
// TODO(seralph): throw exception if texture generation failed
if ( components == 1)
{
osg::ref_ptr< osg::Image > ima = new osg::Image;
ima->allocateImage( grid->getNbCoordsX(), grid->getNbCoordsY(), grid->getNbCoordsZ(), GL_LUMINANCE, GL_FLOAT );
unsigned char* data = ima->data();
unsigned char* charSource = ( unsigned char* )source;
for ( unsigned int i = 0; i < grid->getNbCoordsX() * grid->getNbCoordsY() * grid->getNbCoordsZ() * 4 ; ++i )
{
data[i] = charSource[i];
}
m_texture = osg::ref_ptr<osg::Texture3D>( new osg::Texture3D );
m_texture->setFilter( osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR );
m_texture->setFilter( osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR );
m_texture->setWrap( osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT );
m_texture->setImage( ima );
m_texture->setResizeNonPowerOfTwoHint( false );
}
}
void WDataTexture3D::createTexture()
{
if ( !m_texture )
{
if ( m_valueSet->getDataType() == 2 )
{
boost::shared_ptr< WValueSet< unsigned char > > vs = boost::shared_dynamic_cast< WValueSet< unsigned char > >( m_valueSet );
unsigned char* source = const_cast< unsigned char* > ( vs->rawData() );
createTexture3D( source, m_valueSet->dimension() );
}
else if ( m_valueSet->getDataType() == 4 )
{
boost::shared_ptr< WValueSet< int16_t > > vs = boost::shared_dynamic_cast< WValueSet< int16_t > >( m_valueSet );
int16_t* source = const_cast< int16_t* > ( vs->rawData() );
createTexture3D( source, m_valueSet->dimension() );
}
else if ( m_valueSet->getDataType() == 16 )
{
boost::shared_ptr< WValueSet< float > > vs = boost::shared_dynamic_cast< WValueSet< float > >( m_valueSet );
float* source = const_cast< float* > ( vs->rawData() );
createTexture3D( source, m_valueSet->dimension() );
}
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WDATATEXTURE3D_H
#define WDATATEXTURE3D_H
#include <boost/shared_ptr.hpp>
#include <osg/Texture3D>
#include "WValueSetBase.h"
#include "WGrid.h"
/**
* Class encapsulating a 3D texture. It is able to use a value set and grid to create an OpenSceneGraph texture, that can be used
* directly by modules.
*/
class WDataTexture3D
{
public:
/**
* Constructor. Creates the texture. Just run it after graphics engine was initialized.
*
* \param valueSet the value set to use
* \param grid the grid to use
*/
explicit WDataTexture3D( boost::shared_ptr<WValueSetBase> valueSet, boost::shared_ptr<WGrid> grid );
/**
* Destructor.
*/
virtual ~WDataTexture3D();
/**
* Gives the alpha value for this texture.
*
* \return the alpha value.
*/
float getAlpha() const;
/**
* Sets the alpha value. The value must be in [0,1]. Otherwise nothing will happen.
*
* \param alpha the alpha value.
*/
void setAlpha( float alpha );
/**
* Returns the currently set threshold.
*
* \return the threshold.
*/
float getThreshold() const;
/**
* Sets the threshold to use.
*
* \param threshold the threshold.
*/
void setThreshold( float threshold );
osg::ref_ptr< osg::Texture3D > getTexture();
protected:
/**
* Creates a 3d texture from a dataset. This function will be overloaded for the
* various data types. A template function is not recommended due to the different commands
* in the image creation.
*
* TODO(schurade): create other functions once dataset meta data is available again
*
* \param source Pointer to the raw data of a dataset
* \param components Number of values used in a Voxel, usually 1, 3 or 4
*/
void createTexture3D( unsigned char* source, int components = 1 );
void createTexture3D( int16_t* source, int components = 1 );
void createTexture3D( float* source, int components = 1 );
/**
* Creates a 3D texture for the data set.
*/
void createTexture();
/**
* Alpha value. Used for blending in/out textures.
*/
float m_alpha;
/**
* Threshold used for to exclude values.
*/
float m_threshold;
/**
* The actual texture.
*/
osg::ref_ptr< osg::Texture3D > m_texture;
/**
* The value set from which the texture gets created.
*/
boost::shared_ptr<WValueSetBase> m_valueSet;
/**
* The grid used to set up the texture.
*/
boost::shared_ptr<WGrid> m_grid;
private:
};
#endif // WDATATEXTURE3D_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <osg/ShapeDrawable>
#include <osg/Group>
#include <osg/Geode>
#include "WMTextureList.h"
#include "../../kernel/WKernel.h"
WMTextureList::WMTextureList():
WModule()
{
// initialize members
}
WMTextureList::~WMTextureList()
{
// cleanup
}
boost::shared_ptr< WModule > WMTextureList::factory() const
{
return boost::shared_ptr< WModule >( new WMTextureList() );
}
const std::string WMTextureList::getName() const
{
return "Texture List";
}
const std::string WMTextureList::getDescription() const
{
return "Module for managing lists of loaded textures.";
}
void WMTextureList::moduleMain()
{
// TODO(seralph): write me
// load the sample scene.
osg::ref_ptr<osg::Geode> sceneDataGeode = new osg::Geode();
// 20 units into the screen
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Box( osg::Vec3( -6, 5, -20 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( -3, 5, -20 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cone( osg::Vec3( 0, 5, -20 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cylinder( osg::Vec3( 3, 5, -20 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Capsule( osg::Vec3( 6, 5, -20 ), 1.0, 1.0 ) ) );
// 25 units into the screen
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Box( osg::Vec3( -6, 0, -25 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( -3, 0, -25 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cone( osg::Vec3( 0, 0, -25 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cylinder( osg::Vec3( 3, 0, -25 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Capsule( osg::Vec3( 6, 0, -25 ), 1.0, 1.0 ) ) );
// 30 units into the screen
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Box( osg::Vec3( -6, -5, -30 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( -3, -5, -30 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cone( osg::Vec3( 0, -5, -30 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cylinder( osg::Vec3( 3, -5, -30 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Capsule( osg::Vec3( 6, -5, -30 ), 1.0, 1.0 ) ) );
// 35 units into the screen
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Box( osg::Vec3( -6, -10, -35 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Sphere( osg::Vec3( -3, -10, -35 ), 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cone( osg::Vec3( 0, -10, -35 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Cylinder( osg::Vec3( 3, -10, -35 ), 1.0, 1.0 ) ) );
sceneDataGeode->addDrawable(
new osg::ShapeDrawable( new osg::Capsule( osg::Vec3( 6, -10, -35 ), 1.0, 1.0 ) ) );
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( sceneDataGeode );
// Since the modules run in a separate thread: such loops are possible
while ( !m_FinishRequested )
{
// do fancy stuff
sleep( 1 );
}
// clean up stuff
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMTEXTURELIST_H
#define WMTEXTURELIST_H
#include <string>
#include "../../kernel/WModule.h"
/**
* Module representing the available textures. It is currently just a skeleton.
* \ingroup kernel
*/
class WMTextureList: public WModule
{
public:
/**
* Default constructor.
*/
WMTextureList();
/**
* Destructor.
*/
virtual ~WMTextureList();
/**
* Gives back the name of this module.
* \return the module's name.
*/
virtual const std::string getName() const;
/**
* Gives back a description of this module.
* \return description to module.
*/
virtual const std::string getDescription() const;
/**
* Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
* should never be initialized or modified in some other way. A simple new instance is required.
*
* \return the prototype used to create every module in OpenWalnut.
*/
virtual boost::shared_ptr< WModule > factory() const;
protected:
/**
* Entry point after loading the module. Runs in separate thread.
*/
virtual void moduleMain();
private:
};
#endif // WMTEXTURELIST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment