OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 
00033 #include "algorithms/WMarchingLegoAlgorithm.h"
00034 
00035 #include "callbacks/WGEFunctorCallback.h"
00036 #include "WGraphicsEngine.h"
00037 
00038 #include "WROIArbitrary.h"
00039 
00040 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
00041                               const WMatrix< double >& mat,
00042                               const std::vector< float >& vals,
00043                               boost::shared_ptr< WTriangleMesh > triMesh,
00044                               float threshold,
00045                               float maxThreshold,
00046                               WColor color ) :
00047     WROI(),
00048     m_nbCoordsVec( 3 ),
00049     m_matrix( mat ),
00050     m_vals( vals ),
00051     m_triMesh( triMesh ),
00052     m_color( color )
00053 {
00054     m_nbCoordsVec[0] = nbCoordsX;
00055     m_nbCoordsVec[1] = nbCoordsY;
00056     m_nbCoordsVec[2] = nbCoordsZ;
00057 
00058     properties();
00059 
00060     m_threshold->set( threshold );
00061     m_threshold->setMax( maxThreshold );
00062 
00063     updateGFX();
00064 
00065     WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
00066     addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
00067 
00068     setDirty();
00069 }
00070 
00071 WROIArbitrary::WROIArbitrary( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
00072         const WMatrix< double >& mat,
00073         const std::vector< float >& vals,
00074         float maxThreshold,
00075         WColor color ) :
00076     WROI(),
00077     m_nbCoordsVec( 3 ),
00078     m_matrix( mat ),
00079     m_vals( vals ),
00080     m_color( color )
00081 {
00082     m_nbCoordsVec[0] = nbCoordsX;
00083     m_nbCoordsVec[1] = nbCoordsY;
00084     m_nbCoordsVec[2] = nbCoordsZ;
00085 
00086     properties();
00087 
00088     m_threshold->set( 0.01 );
00089     m_threshold->setMax( maxThreshold );
00090 
00091     updateGFX();
00092 
00093     WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
00094     addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WROIArbitrary::updateGFX, this ) ) );
00095 
00096     setDirty();
00097 }
00098 
00099 WROIArbitrary::~WROIArbitrary()
00100 {
00101 //    std::cout << "destructor called" << std::endl;
00102 //    std::cout << "ref count geode: " << m_geode->referenceCount() << std::endl;
00103 //
00104 //    WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_geode );
00105 }
00106 
00107 void WROIArbitrary::properties()
00108 {
00109     m_threshold = m_properties->addProperty( "Threshold", "description", 0. , boost::bind( &WROIArbitrary::propertyChanged, this ) );
00110 }
00111 
00112 void WROIArbitrary::propertyChanged()
00113 {
00114     setDirty();
00115 }
00116 
00117 void WROIArbitrary::setThreshold( double threshold )
00118 {
00119     m_threshold->set( threshold );
00120     setDirty();
00121 }
00122 
00123 double WROIArbitrary::getThreshold()
00124 {
00125     return m_threshold->get();
00126 }
00127 
00128 std::vector< size_t > WROIArbitrary::getCoordDimensions()
00129 {
00130     return m_nbCoordsVec;
00131 }
00132 
00133 std::vector< double > WROIArbitrary::getCoordOffsets()
00134 {
00135     std::vector< double > vec( 3 );
00136     vec[0] = m_matrix( 0, 0 );
00137     vec[1] = m_matrix( 1, 1 );
00138     vec[2] = m_matrix( 2, 2 );
00139     return vec;
00140 }
00141 
00142 float WROIArbitrary::getValue( size_t i )
00143 {
00144     return m_vals[i];
00145 }
00146 
00147 void WROIArbitrary::updateGFX()
00148 {
00149     if( m_dirty->get() )
00150     {
00151         WMarchingLegoAlgorithm mlAlgo;
00152         m_triMesh = mlAlgo.generateSurface( m_nbCoordsVec[0], m_nbCoordsVec[1], m_nbCoordsVec[2],
00153                                             m_matrix,
00154                                             &m_vals,
00155                                             m_threshold->get() );
00156 
00157         osg::Geometry* surfaceGeometry = new osg::Geometry();
00158         setName( "roi" );
00159 
00160         surfaceGeometry->setVertexArray( m_triMesh->getVertexArray() );
00161 
00162         // ------------------------------------------------
00163         // normals
00164         surfaceGeometry->setNormalArray( m_triMesh->getTriangleNormalArray() );
00165         surfaceGeometry->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
00166 
00167         // ------------------------------------------------
00168         // colors
00169         osg::Vec4Array* colors = new osg::Vec4Array;
00170         colors->push_back( m_color );
00171         surfaceGeometry->setColorArray( colors );
00172         surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
00173 
00174         osg::DrawElementsUInt* surfaceElement = new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
00175 
00176         std::vector< size_t >tris = m_triMesh->getTriangles();
00177         surfaceElement->reserve( tris.size() );
00178 
00179         for( unsigned int vertId = 0; vertId < tris.size(); ++vertId )
00180         {
00181             surfaceElement->push_back( tris[vertId] );
00182         }
00183         surfaceGeometry->addPrimitiveSet( surfaceElement );
00184         removeDrawables( 0 );
00185         addDrawable( surfaceGeometry );
00186 
00187         osg::StateSet* state = getOrCreateStateSet();
00188         osg::ref_ptr<osg::LightModel> lightModel = new osg::LightModel();
00189         lightModel->setTwoSided( true );
00190         state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
00191 
00192         state->setMode(  GL_BLEND, osg::StateAttribute::ON );
00193 
00194     //    {
00195     //        osg::ref_ptr< osg::Material > material = new osg::Material();
00196     //        material->setDiffuse(   osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
00197     //        material->setSpecular(  osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
00198     //        material->setAmbient(   osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
00199     //        material->setEmission(  osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
00200     //        material->setShininess( osg::Material::FRONT, 25.0 );
00201     //        state->setAttribute( material );
00202     //    }
00203 
00204         m_dirty->set( false );
00205     }
00206 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends