Commit 62b2e5f3 authored by schurade's avatar schurade
Browse files

[ADD] initial work on coordinate systems

parent 8d41a6b5
......@@ -54,7 +54,8 @@ WGEViewer::WGEViewer( osg::ref_ptr<WindowData> wdata, int x, int y, int width, i
m_View = osg::ref_ptr<osgViewer::Viewer>( new osgViewer::Viewer() );
m_View->getCamera()->setGraphicsContext( m_GraphicsContext );
m_View->getCamera()->setProjectionMatrixAsPerspective( 30.0f, 1.333, 1.0, 1000.0 );
// m_View->getCamera()->setProjectionMatrixAsPerspective( 30.0f, 1.333, 1.0, 1000.0 );
m_View->getCamera()->setProjectionMatrixAsOrtho( -120, 120, -120, 120, -1000, +1000 );
m_View->getCamera()->setViewport( new osg::Viewport( 0, 0, 10, 10 ) );
// add the stats handler
......@@ -127,7 +128,8 @@ void WGEViewer::resize( int width, int height )
WGEGraphicsWindow::resize( width, height );
// also update the camera
m_View->getCamera()->setProjectionMatrixAsPerspective( 30.0f, 1.333, 1.0, 1000.0 );
// m_View->getCamera()->setProjectionMatrixAsPerspective( 30.0f, 1.333, 1.0, 1000.0 );
m_View->getCamera()->setProjectionMatrixAsOrtho( -120, 120, -120, 120, -1000, +1000 );
m_View->getCamera()->setViewport( new osg::Viewport( 0, 0, width, height ) );
}
......
......@@ -36,6 +36,7 @@
#include "WModule.h"
#include "../modules/data/WDataModule.hpp"
#include "../modules/navigationSlices/WNavigationSliceModule.h"
#include "../modules/coordinateSystem/WCoordinateSystem.h"
#include "../modules/FiberDisplay/WFiberDisplay.h"
#include "../common/WException.h"
......@@ -185,11 +186,15 @@ void WKernel::loadModules()
m_modules.clear();
using boost::shared_ptr;
shared_ptr< WModule > m = shared_ptr< WModule >( new WNavigationSliceModule() );
shared_ptr< WModule > m1 = shared_ptr< WModule >( new WNavigationSliceModule() );
// shared_ptr< WModule > m = shared_ptr< WModule >( new WFiberDisplay() );
WLogger::getLogger()->addLogMessage( "Loading module: " + m->getName(), "Kernel", LL_DEBUG );
WLogger::getLogger()->addLogMessage( "Loading module: " + m1->getName(), "Kernel", LL_DEBUG );
m_modules.push_back( m );
m_modules.push_back( m1 );
shared_ptr< WModule > m2 = shared_ptr< WModule >( new WCoordinateSystem() );
WLogger::getLogger()->addLogMessage( "Loading module: " + m2->getName(), "Kernel", LL_DEBUG );
m_modules.push_back( m2 );
}
void WKernel::init()
......
//---------------------------------------------------------------------------
//
// 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 <string>
#include <vector>
#include <osg/Geode>
#include <osg/Geometry>
#include "../../kernel/WKernel.h"
#include "WCoordinateSystem.h"
WCoordinateSystem::WCoordinateSystem() :
WModule()
{
properties();
}
WCoordinateSystem::~WCoordinateSystem()
{
}
void WCoordinateSystem::threadMain()
{
createGeometry();
// Since the modules run in a separate thread: such loops are possible
while ( !m_FinishRequested )
{
// do fancy stuff
sleep( 1 );
}
// clean up stuff
}
const std::string WCoordinateSystem::getName() const
{
return "Coordinate System Module";
}
const std::string WCoordinateSystem::getDescription() const
{
return "This module displays coordinate systems as overlay withn the main 3D view.";
}
void WCoordinateSystem::connectToGui()
{
WKernel::getRunningKernel()->getGui()->connectProperties( m_properties );
}
void WCoordinateSystem::properties()
{
m_properties->addBool( "textureChanged", false );
m_properties->addInt( "type", 1 );
// initialize the properties with a certain standard set
// those properties will be updatet as soon as the first dataset is looaded
m_properties->addFloat( "zeroX", 80.0 );
m_properties->addFloat( "zeroY", 100.0 );
m_properties->addFloat( "zeroZ", 80.0 );
m_properties->addFloat( "fltX", 0.0 );
m_properties->addFloat( "fltY", 0.0 );
m_properties->addFloat( "fltZ", 0.0 );
m_properties->addFloat( "brbX", 160.0 );
m_properties->addFloat( "brbY", 200.0 );
m_properties->addFloat( "brbZ", 160.0 );
}
void WCoordinateSystem::createGeometry()
{
float zeroX = m_properties->getValue<float>( "zeroX" );
float zeroY = m_properties->getValue<float>( "zeroY" );
float zeroZ = m_properties->getValue<float>( "zeroZ" );
float fltX = m_properties->getValue<float>( "fltX" );
float fltY = m_properties->getValue<float>( "fltY" );
float fltZ = m_properties->getValue<float>( "fltZ" );
float brbX = m_properties->getValue<float>( "brbX" );
float brbY = m_properties->getValue<float>( "brbY" );
float brbZ = m_properties->getValue<float>( "brbZ" );
m_rootNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
osg::ref_ptr<osg::Geometry> geometry = osg::ref_ptr<osg::Geometry>( new osg::Geometry() );
m_rootNode->addDrawable( geometry );
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3( zeroX, zeroY, zeroZ ) );
vertices->push_back( osg::Vec3( fltX, fltY, fltZ ) );
vertices->push_back( osg::Vec3( fltX, brbY, fltZ ) );
vertices->push_back( osg::Vec3( fltX, brbY, brbZ ) );
vertices->push_back( osg::Vec3( fltX, fltY, brbZ ) );
vertices->push_back( osg::Vec3( brbX, fltY, fltZ ) );
vertices->push_back( osg::Vec3( brbX, brbY, fltZ ) );
vertices->push_back( osg::Vec3( brbX, brbY, brbZ ) );
vertices->push_back( osg::Vec3( brbX, fltY, brbZ ) );
vertices->push_back( osg::Vec3( zeroX, zeroY, fltZ ) );
vertices->push_back( osg::Vec3( zeroX, zeroY, brbZ ) );
vertices->push_back( osg::Vec3( zeroX, fltY, zeroZ ) );
vertices->push_back( osg::Vec3( zeroX, brbY, zeroZ ) );
vertices->push_back( osg::Vec3( fltX, zeroY, zeroZ ) );
vertices->push_back( osg::Vec3( brbX, zeroY, zeroZ ) );
geometry->setVertexArray( vertices );
osg::DrawElementsUInt* lines = new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
lines->push_back( 1 );
lines->push_back( 2 );
lines->push_back( 2 );
lines->push_back( 3 );
lines->push_back( 3 );
lines->push_back( 4 );
lines->push_back( 4 );
lines->push_back( 1 );
lines->push_back( 5 );
lines->push_back( 6 );
lines->push_back( 6 );
lines->push_back( 7 );
lines->push_back( 7 );
lines->push_back( 8 );
lines->push_back( 8 );
lines->push_back( 5 );
lines->push_back( 1 );
lines->push_back( 5 );
lines->push_back( 2 );
lines->push_back( 6 );
lines->push_back( 3 );
lines->push_back( 7 );
lines->push_back( 4 );
lines->push_back( 8 );
lines->push_back( 9 );
lines->push_back( 10 );
lines->push_back( 11 );
lines->push_back( 12 );
lines->push_back( 13 );
lines->push_back( 14 );
geometry->addPrimitiveSet( lines );
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( m_rootNode );
// osg::StateSet* rootState = m_rootNode->getOrCreateStateSet();
m_rootNode->setUpdateCallback( new coordinateNodeCallback( boost::shared_dynamic_cast<WCoordinateSystem>( shared_from_this() ) ) );
}
void WCoordinateSystem::updateGeometry()
{
boost::shared_lock< boost::shared_mutex > slock;
slock = boost::shared_lock< boost::shared_mutex >( m_updateLock );
// *******************************************************************************************************
if ( !m_properties->getValue< bool > ( "textureChanged" ) )
{
return;
}
if ( m_properties->getValue< bool > ( "textureChanged" ) && WKernel::getRunningKernel()->getGui()->isInitalized() )
{
m_properties->setValue( "textureChanged", false );
std::vector< boost::shared_ptr< WModule > > datasetList = WKernel::getRunningKernel()->getGui()->getDataSetList( 0 );
if ( datasetList.size() > 0 )
{
boost::shared_ptr< WDataModule< int > > module = boost::shared_dynamic_cast< WDataModule< int > >( datasetList[0] );
boost::shared_ptr< WDataSetSingle > ds = boost::shared_dynamic_cast< WDataSetSingle >( module->getDataSet() );
if ( ds->getValueSet()->getDataType() != 2 )
{
return;
}
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( ds->getGrid() );
for ( size_t x = 0; x < grid->getNbCoordsX(); ++x )
{
int count = 0;
for ( size_t y = 0; y < grid->getNbCoordsY(); ++y )
{
for ( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
{
unsigned char v = ds->getValueAt< unsigned char > ( x, y, z );
if ( v > 0 )
{
++count;
}
}
}
if ( count > 5 )
{
m_properties->setValue( "fltX", static_cast<float>( x ) );
break;
}
}
for ( int x = grid->getNbCoordsX() - 1; x > -1; --x )
{
int count = 0;
for ( size_t y = 0; y < grid->getNbCoordsY(); ++y )
{
for ( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
{
unsigned char v = ds->getValueAt< unsigned char > ( x, y, z );
if ( v > 0 )
{
++count;
}
}
}
if ( count > 5 )
{
m_properties->setValue( "brbX", static_cast<float>( x ) );
break;
}
}
for ( size_t y = 0; y < grid->getNbCoordsY(); ++y )
{
int count = 0;
for ( size_t x = 0; x < grid->getNbCoordsX(); ++x )
{
for ( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
{
unsigned char v = ds->getValueAt< unsigned char > ( x, y, z );
if ( v > 0 )
{
++count;
}
}
}
if ( count > 5 )
{
m_properties->setValue( "fltY", static_cast<float>( y ) );
break;
}
}
for ( int y = grid->getNbCoordsY() - 1; y > -1; --y )
{
int count = 0;
for ( size_t x = 0; x < grid->getNbCoordsX(); ++x )
{
for ( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
{
unsigned char v = ds->getValueAt< unsigned char > ( x, y, z );
if ( v > 0 )
{
++count;
}
}
}
if ( count > 5 )
{
m_properties->setValue( "brbY", static_cast<float>( y ) );
break;
}
}
for ( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
{
int count = 0;
for ( size_t x = 0; x < grid->getNbCoordsX(); ++x )
{
for ( size_t y = 0; y < grid->getNbCoordsY(); ++y )
{
unsigned char v = ds->getValueAt< unsigned char > ( x, y, z );
if ( v > 0 )
{
++count;
}
}
}
if ( count > 5 )
{
m_properties->setValue( "fltZ", static_cast<float>( z ) );
break;
}
}
for ( int z = grid->getNbCoordsZ() - 1; z > -1; --z )
{
int count = 0;
for ( size_t x = 0; x < grid->getNbCoordsX(); ++x )
{
for ( size_t y = 0; y < grid->getNbCoordsY(); ++y )
{
unsigned char v = ds->getValueAt< unsigned char > ( x, y, z );
if ( v > 0 )
{
++count;
}
}
}
if ( count > 5 )
{
m_properties->setValue( "brbZ", static_cast<float>( z ) );
break;
}
}
m_properties->setValue( "textureChanged", false );
}
}
float zeroX = m_properties->getValue<float>( "zeroX" );
float zeroY = m_properties->getValue<float>( "zeroY" );
float zeroZ = m_properties->getValue<float>( "zeroZ" );
float fltX = m_properties->getValue<float>( "fltX" );
float fltY = m_properties->getValue<float>( "fltY" );
float fltZ = m_properties->getValue<float>( "fltZ" );
float brbX = m_properties->getValue<float>( "brbX" );
float brbY = m_properties->getValue<float>( "brbY" );
float brbZ = m_properties->getValue<float>( "brbZ" );
osg::ref_ptr<osg::Geometry> geometry = osg::ref_ptr<osg::Geometry>( new osg::Geometry() );
osg::Vec3Array* vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3( zeroX, zeroY, zeroZ ) );
vertices->push_back( osg::Vec3( fltX, fltY, fltZ ) );
vertices->push_back( osg::Vec3( fltX, brbY, fltZ ) );
vertices->push_back( osg::Vec3( fltX, brbY, brbZ ) );
vertices->push_back( osg::Vec3( fltX, fltY, brbZ ) );
vertices->push_back( osg::Vec3( brbX, fltY, fltZ ) );
vertices->push_back( osg::Vec3( brbX, brbY, fltZ ) );
vertices->push_back( osg::Vec3( brbX, brbY, brbZ ) );
vertices->push_back( osg::Vec3( brbX, fltY, brbZ ) );
vertices->push_back( osg::Vec3( zeroX, zeroY, fltZ ) );
vertices->push_back( osg::Vec3( zeroX, zeroY, brbZ ) );
vertices->push_back( osg::Vec3( zeroX, fltY, zeroZ ) );
vertices->push_back( osg::Vec3( zeroX, brbY, zeroZ ) );
vertices->push_back( osg::Vec3( fltX, zeroY, zeroZ ) );
vertices->push_back( osg::Vec3( brbX, zeroY, zeroZ ) );
geometry->setVertexArray( vertices );
osg::DrawElementsUInt* lines = new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
lines->push_back( 1 );
lines->push_back( 2 );
lines->push_back( 2 );
lines->push_back( 3 );
lines->push_back( 3 );
lines->push_back( 4 );
lines->push_back( 4 );
lines->push_back( 1 );
lines->push_back( 5 );
lines->push_back( 6 );
lines->push_back( 6 );
lines->push_back( 7 );
lines->push_back( 7 );
lines->push_back( 8 );
lines->push_back( 8 );
lines->push_back( 5 );
lines->push_back( 1 );
lines->push_back( 5 );
lines->push_back( 2 );
lines->push_back( 6 );
lines->push_back( 3 );
lines->push_back( 7 );
lines->push_back( 4 );
lines->push_back( 8 );
lines->push_back( 9 );
lines->push_back( 10 );
lines->push_back( 11 );
lines->push_back( 12 );
lines->push_back( 13 );
lines->push_back( 14 );
geometry->addPrimitiveSet( lines );
osg::ref_ptr<osg::Drawable> old = osg::ref_ptr<osg::Drawable>( m_rootNode->getDrawable( 0 ) );
m_rootNode->replaceDrawable( old, geometry );
// *******************************************************************************************************
slock.unlock();
}
//---------------------------------------------------------------------------
//
// 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 WCOORDINATESYSTEM_H
#define WCOORDINATESYSTEM_H
#include <string>
#include <osg/Geode>
#include "../../dataHandler/WDataSet.h"
#include "../data/WDataModule.hpp"
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleConnector.h"
#include "../../kernel/WModuleInputData.hpp"
/**
* class that implements the various coordinate systems as overlays within the 3D view
*/
class WCoordinateSystem : public WModule, public osg::Referenced
{
public:
/**
* standard constructor
*/
WCoordinateSystem();
/**
* destructor
*/
virtual ~WCoordinateSystem();
/**
* callback for updating the geometry
*/
void updateGeometry();
/**
* \par Description
* Gives back the name of this module.
* \return the module's name.
*/
virtual const std::string getName() const;
/**
* \par Description
* Gives back a description of this module.
* \return description to module.
*/
virtual const std::string getDescription() const;
/**
* Connect the listener function of the module to the gui signals
* this has to be called after full initialization fo the gui
*/
void connectToGui();
protected:
/**
* \par Description
* Entry point after loading the module. Runs in separate thread.
*/
virtual void threadMain();
private:
/**
* initialize the properties for this module
*/
void properties();
/**
* initial creation function for the slice geometry
*/
void createGeometry();
/**
* the root node for this module
*/
osg::ref_ptr<osg::Geode> m_rootNode;
/**
* the shader object for this module
*/
// boost::shared_ptr< WShader >m_shader;
/**
* lock to prevent concurrent threads trying to update the osg node
*/
boost::shared_mutex m_updateLock;
};
class coordinateNodeCallback : public osg::NodeCallback
{
public:
explicit coordinateNodeCallback( boost::shared_ptr< WCoordinateSystem > module )
{
m_module = module;
}
virtual void operator()( osg::Node* node, osg::NodeVisitor* nv )
{
if ( m_module )
{
m_module->updateGeometry();
}
traverse( node, nv );
}
private:
boost::shared_ptr< WCoordinateSystem > m_module;
};