OpenWalnut  1.4.0
WROIArbitrary.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <iostream>
00026 #include <string>
00027 #include <utility>
00028 #include <vector>
00029 
00030 #include <osg/LineWidth>
00031 #include <osg/LightModel>
00032 #include <osg/Geometry>
00033 
00034 #include "core/common/algorithms/WMarchingLegoAlgorithm.h"
00035 
00036 #include "callbacks/WGEFunctorCallback.h"
00037 #include "WGraphicsEngine.h"
00038 
00039 #include "WROIArbitrary.h"
00040 
00041 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
00042                               const WMatrix< double >& mat,
00043                               const std::vector< float >& vals,
00044                               boost::shared_ptr< WTriangleMesh > triMesh,
00045                               float threshold,
00046                               float maxThreshold,
00047                               WColor color ) :
00048     WROI(),
00049     m_nbCoordsVec( 3 ),
00050     m_matrix( mat ),
00051     m_vals( vals ),
00052     m_triMesh( triMesh ),
00053     m_color( color )
00054 {
00055     m_nbCoordsVec[0] = nbCoordsX;
00056     m_nbCoordsVec[1] = nbCoordsY;
00057     m_nbCoordsVec[2] = nbCoordsZ;
00058 
00059     properties();
00060 
00061     m_threshold->set( threshold );
00062     m_threshold->setMax( maxThreshold );
00063 
00064     updateGFX();
00065 
00066     WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
00067     addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
00068 
00069     setDirty();
00070 }
00071 
00072 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
00073         const WMatrix< double >& mat,
00074         const std::vector< float >& vals,
00075         float maxThreshold,
00076         WColor color ) :
00077     WROI(),
00078     m_nbCoordsVec( 3 ),
00079     m_matrix( mat ),
00080     m_vals( vals ),
00081     m_color( color )
00082 {
00083     m_nbCoordsVec[0] = nbCoordsX;
00084     m_nbCoordsVec[1] = nbCoordsY;
00085     m_nbCoordsVec[2] = nbCoordsZ;
00086 
00087     properties();
00088 
00089     m_threshold->set( 0.01 );
00090     m_threshold->setMax( maxThreshold );
00091 
00092     updateGFX();
00093 
00094     WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
00095     addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
00096 
00097     setDirty();
00098 }
00099 
00100 WROIArbitrary::~WROIArbitrary()
00101 {
00102 //    std::cout << "destructor called" << std::endl;
00103 //    std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl;
00104 //
00105 //    WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode );
00106 }
00107 
00108 void WROIArbitrary::properties()
00109 {
00110     m_threshold = m_properties->addProperty( "Threshold", "description", 0. , boost::bind( &WROIArbitrary::propertyChanged, this ) );
00111 }
00112 
00113 void WROIArbitrary::propertyChanged()
00114 {
00115     setDirty();
00116 }
00117 
00118 void WROIArbitrary::setThreshold( double threshold )
00119 {
00120     m_threshold->set( threshold );
00121     setDirty();
00122 }
00123 
00124 double WROIArbitrary::getThreshold()
00125 {
00126     return m_threshold->get();
00127 }
00128 
00129 std::vector< size_t > WROIArbitrary::getCoordDimensions()
00130 {
00131     return m_nbCoordsVec;
00132 }
00133 
00134 std::vector< double > WROIArbitrary::getCoordOffsets()
00135 {
00136     std::vector< double > vec( 3 );
00137     vec[0] = m_matrix( 0, 0 );
00138     vec[1] = m_matrix( 1, 1 );
00139     vec[2] = m_matrix( 2, 2 );
00140     return vec;
00141 }
00142 
00143 float WROIArbitrary::getValue( size_t i )
00144 {
00145     return m_vals[i];
00146 }
00147 
00148 void WROIArbitrary::updateGFX()
00149 {
00150     if( m_dirty->get() )
00151     {
00152         WMarchingLegoAlgorithm mlAlgo;
00153         m_triMesh = mlAlgo.generateSurface( m_nbCoordsVec[0], m_nbCoordsVec[1], m_nbCoordsVec[2],
00154                                             m_matrix,
00155                                             &m_vals,
00156                                             m_threshold->get() );
00157 
00158         osg::Geometry* surfaceGeometry = new osg::Geometry();
00159         setName( "roi" );
00160 
00161         surfaceGeometry->setVertexArray( m_triMesh->getVertexArray() );
00162 
00163         // ------------------------------------------------
00164         // normals
00165         surfaceGeometry->setNormalArray( m_triMesh->getVertexNormalArray() );
00166         surfaceGeometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
00167 
00168         // ------------------------------------------------
00169         // colors
00170         osg::Vec4Array* colors = new osg::Vec4Array;
00171         colors->push_back( m_color );
00172         surfaceGeometry->setColorArray( colors );
00173         surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
00174 
00175         osg::DrawElementsUInt* surfaceElement = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
00176 
00177         std::vector< size_t >tris = m_triMesh->getTriangles();
00178         surfaceElement->reserve( tris.size() );
00179 
00180         for( unsigned int vertId = 0; vertId < tris.size(); ++vertId )
00181         {
00182             surfaceElement->push_back( tris[vertId] );
00183         }
00184         surfaceGeometry->addPrimitiveSet( surfaceElement );
00185         removeDrawables( 0 );
00186         addDrawable( surfaceGeometry );
00187 
00188         osg::StateSet* state = getOrCreateStateSet();
00189         osg::ref_ptr<osg::LightModel> lightModel = new osg::LightModel();
00190         lightModel->setTwoSided( true );
00191         state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
00192 
00193         state->setMode(  GL_BLEND, osg::StateAttribute::ON );
00194 
00195     //    {
00196     //        osg::ref_ptr< osg::Material > material = new osg::Material();
00197     //        material->setDiffuse(   osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
00198     //        material->setSpecular(  osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
00199     //        material->setAmbient(   osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
00200     //        material->setEmission(  osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
00201     //        material->setShininess( osg::Material::FRONT, 25.0 );
00202     //        state->setAttribute( material );
00203     //    }
00204 
00205         m_dirty->set( false );
00206     }
00207 }