OpenWalnut  1.4.0
WRMBranch.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 <list>
00026 #include <string>
00027 #include <vector>
00028 
00029 #include "../graphicsEngine/WGraphicsEngine.h"
00030 
00031 #include "WROIManager.h"
00032 #include "WRMBranch.h"
00033 
00034 
00035 WRMBranch::WRMBranch( boost::shared_ptr< WROIManager > roiManager ) :
00036     m_roiManager( roiManager )
00037 {
00038     properties();
00039 }
00040 
00041 WRMBranch::~WRMBranch()
00042 {
00043 }
00044 
00045 void WRMBranch::properties()
00046 {
00047     m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "This branch's properties" ) );
00048 
00049     m_dirty = m_properties->addProperty( "Dirty", "", true, boost::bind( &WRMBranch::propertyChanged, this ) );
00050     m_dirty->setHidden( true );
00051     m_name = m_properties->addProperty( "Name", "The name of this branch.", std::string( "Branch" ) );
00052     m_isNot = m_properties->addProperty( "Not", "Negate the effect of this branch.", false, boost::bind( &WRMBranch::propertyChanged, this ) );
00053     m_bundleColor = m_properties->addProperty( "Bundle color", "Color the selected fibers using this color.", WColor( 1.0, 0.0, 0.0, 1.0 ),
00054                                                boost::bind( &WRMBranch::propertyChanged, this ) );
00055 
00056     m_changeRoiSignal = boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WRMBranch::setDirty, this ) ) );
00057 }
00058 
00059 WPropertyGroup::SPtr WRMBranch::getProperties() const
00060 {
00061     return m_properties;
00062 }
00063 
00064 void WRMBranch::propertyChanged()
00065 {
00066     setDirty();
00067 }
00068 
00069 WPropString WRMBranch::nameProperty()
00070 {
00071     return m_name;
00072 }
00073 
00074 WPropBool WRMBranch::invertProperty()
00075 {
00076     return m_isNot;
00077 }
00078 
00079 WPropColor WRMBranch::colorProperty()
00080 {
00081     return m_bundleColor;
00082 }
00083 
00084 void WRMBranch::addRoi( osg::ref_ptr< WROI > roi )
00085 {
00086     m_rois.push_back( roi );
00087     roi->addROIChangeNotifier( m_changeRoiSignal );
00088     setDirty();
00089 }
00090 
00091 bool WRMBranch::contains( osg::ref_ptr< WROI > roi )
00092 {
00093     for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00094     {
00095         if( ( *iter ) == roi )
00096         {
00097             return true;
00098         }
00099     }
00100     return false;
00101 }
00102 
00103 void WRMBranch::removeRoi( osg::ref_ptr< WROI > roi )
00104 {
00105     roi->removeROIChangeNotifier( m_changeRoiSignal );
00106     for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00107     {
00108         if( ( *iter ) == roi )
00109         {
00110             m_rois.erase( iter );
00111             setDirty();
00112             break;
00113         }
00114     }
00115 }
00116 
00117 void WRMBranch::getRois( std::vector< osg::ref_ptr< WROI > >& roiVec ) // NOLINT
00118 {
00119     for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00120     {
00121         roiVec.push_back( ( *iter ) );
00122     }
00123 }
00124 
00125 WROIManager::ROIs WRMBranch::getRois() const
00126 {
00127     WROIManager::ROIs ret;
00128     for( std::vector< osg::ref_ptr< WROI > >::const_iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00129     {
00130         ret.push_back( ( *iter ) );
00131     }
00132     return ret;
00133 }
00134 
00135 void WRMBranch::removeAllRois()
00136 {
00137     for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00138     {
00139         WGraphicsEngine::getGraphicsEngine()->getScene()->remove( ( *iter ) );
00140     }
00141 
00142     m_rois.clear();
00143 }
00144 
00145 void WRMBranch::setDirty()
00146 {
00147     m_dirty->set( true );
00148     m_roiManager->setDirty();
00149 
00150     for( std::list< boost::shared_ptr< boost::function< void() > > >::iterator iter = m_changeNotifiers.begin();
00151                 iter != m_changeNotifiers.end(); ++iter )
00152     {
00153         ( **iter )();
00154     }
00155 }
00156 
00157 osg::ref_ptr< WROI > WRMBranch::getFirstRoi()
00158 {
00159     return m_rois.front();
00160 }
00161 
00162 boost::shared_ptr< WROIManager > WRMBranch::getRoiManager()
00163 {
00164     return m_roiManager;
00165 }
00166 
00167 boost::shared_ptr< WProperties > WRMBranch::getProperties()
00168 {
00169     return m_properties;
00170 }
00171 
00172 void WRMBranch::addChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier )
00173 {
00174     boost::unique_lock< boost::shared_mutex > lock;
00175     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00176     m_changeNotifiers.push_back( notifier );
00177     lock.unlock();
00178 }
00179 
00180 void WRMBranch::removeChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier )
00181 {
00182     boost::unique_lock< boost::shared_mutex > lock;
00183     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00184     std::list<  boost::shared_ptr< boost::function< void() > > >::iterator it;
00185     it = std::find( m_changeNotifiers.begin(), m_changeNotifiers.end(), notifier );
00186     if( it != m_changeNotifiers.end() )
00187     {
00188         m_changeNotifiers.erase( it );
00189     }
00190     lock.unlock();
00191 }