OpenWalnut  1.4.0
WROIManager.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 <vector>
00027 
00028 #include "../common/WAssert.h"
00029 
00030 #include "../graphicsEngine/WGraphicsEngine.h"
00031 
00032 #include "WROIManager.h"
00033 
00034 WROIManager::WROIManager()
00035 {
00036     m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "Module's properties" ) );
00037     m_dirty = m_properties->addProperty( "dirty", "dirty flag", true );
00038 }
00039 
00040 WROIManager::~WROIManager()
00041 {
00042 }
00043 
00044 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, boost::shared_ptr< WRMBranch > toBranch )
00045 {
00046     // add roi to branch
00047     toBranch->addRoi( newRoi );
00048 
00049     for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin();
00050             iter != m_addNotifiers.end(); ++iter )
00051     {
00052         ( **iter )( newRoi );
00053     }
00054 }
00055 
00056 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi )
00057 {
00058     addRoi( newRoi, addBranch() );
00059 }
00060 
00061 boost::shared_ptr< WRMBranch > WROIManager::addBranch()
00062 {
00063     // create new branch
00064     boost::shared_ptr< WRMBranch > newBranch( new WRMBranch( shared_from_this() ) );
00065     // add branch to list
00066     m_branches.push_back( newBranch );
00067 
00068     // return
00069     return newBranch;
00070 }
00071 
00072 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, osg::ref_ptr< WROI > parentRoi )
00073 {
00074     // find branch
00075     boost::shared_ptr< WRMBranch > branch;
00076     for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
00077     {
00078         if( ( *iter ).get()->contains( parentRoi ) )
00079         {
00080             branch = ( *iter );
00081         }
00082     }
00083     // add roi to branch
00084     branch->addRoi( newRoi );
00085 
00086     for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin();
00087             iter != m_addNotifiers.end(); ++iter )
00088     {
00089         ( **iter )( newRoi );
00090     }
00091 }
00092 
00093 void WROIManager::removeRoi( osg::ref_ptr< WROI > roi )
00094 {
00095     WGraphicsEngine::getGraphicsEngine()->getScene()->remove( roi );
00096 
00097     for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
00098     {
00099         ( *iter )->removeRoi( roi );
00100 
00101         if( ( *iter )->empty() )
00102         {
00103             for( std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator iter2
00104                       = m_removeBranchNotifiers.begin();
00105                   iter2 != m_removeBranchNotifiers.end();
00106                   ++iter2 )
00107             {
00108                 ( **iter2 )( *iter );
00109             }
00110             m_branches.erase( iter );
00111             break;
00112         }
00113     }
00114     setDirty();
00115 
00116     for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter
00117               = m_removeNotifiers.begin();
00118           iter != m_removeNotifiers.end();
00119           ++iter )
00120     {
00121         ( **iter )( roi );
00122     }
00123 }
00124 
00125 void WROIManager::removeBranch( osg::ref_ptr< WROI > roi )
00126 {
00127     for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
00128     {
00129         if( roi == ( *iter )->getFirstRoi() )
00130         {
00131             ( *iter )->removeAllRois();
00132         }
00133 
00134         if( ( *iter )->empty() )
00135         {
00136             for( std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator iter2
00137                       = m_removeBranchNotifiers.begin();
00138                   iter2 != m_removeBranchNotifiers.end();
00139                   ++iter2 )
00140             {
00141                 ( **iter2 )( *iter );
00142             }
00143             m_branches.erase( iter );
00144             break;
00145         }
00146     }
00147     setDirty();
00148 }
00149 
00150 boost::shared_ptr< WRMBranch> WROIManager::getBranch( osg::ref_ptr< WROI > roi )
00151 {
00152     boost::shared_ptr< WRMBranch> branch;
00153 
00154     for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
00155     {
00156         if( ( *iter )->contains( roi ) )
00157         {
00158             branch = ( *iter );
00159         }
00160     }
00161     return branch;
00162 }
00163 
00164 void WROIManager::setDirty()
00165 {
00166     m_dirty->set( true );
00167 }
00168 
00169 void WROIManager::addAddNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
00170 {
00171     boost::unique_lock< boost::shared_mutex > lock;
00172     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00173     m_addNotifiers.push_back( notifier );
00174     lock.unlock();
00175 }
00176 
00177 void WROIManager::removeAddNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
00178 {
00179     boost::unique_lock< boost::shared_mutex > lock;
00180     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00181     std::list<  boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator it;
00182     it = std::find( m_addNotifiers.begin(), m_addNotifiers.end(), notifier );
00183     if( it != m_addNotifiers.end() )
00184     {
00185         m_addNotifiers.erase( it );
00186     }
00187     lock.unlock();
00188 }
00189 
00190 void WROIManager::addRemoveNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
00191 {
00192     boost::unique_lock< boost::shared_mutex > lock;
00193     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00194     m_removeNotifiers.push_back( notifier );
00195     lock.unlock();
00196 }
00197 
00198 void WROIManager::removeRemoveNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
00199 {
00200     boost::unique_lock< boost::shared_mutex > lock;
00201     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00202     std::list<  boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator it;
00203     it = std::find( m_removeNotifiers.begin(), m_removeNotifiers.end(), notifier );
00204     if( it != m_removeNotifiers.end() )
00205     {
00206         m_removeNotifiers.erase( it );
00207     }
00208     lock.unlock();
00209 }
00210 
00211 void WROIManager::addRemoveBranchNotifier(  boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > notifier )
00212 {
00213     boost::unique_lock< boost::shared_mutex > lock;
00214     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00215     m_removeBranchNotifiers.push_back( notifier );
00216     lock.unlock();
00217 }
00218 
00219 void WROIManager::removeRemoveBranchNotifier(  boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > notifier )
00220 {
00221     boost::unique_lock< boost::shared_mutex > lock;
00222     lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
00223     std::list<  boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator it;
00224     it = std::find( m_removeBranchNotifiers.begin(), m_removeBranchNotifiers.end(), notifier );
00225     if( it != m_removeBranchNotifiers.end() )
00226     {
00227         m_removeBranchNotifiers.erase( it );
00228     }
00229     lock.unlock();
00230 }
00231 
00232 void WROIManager::setSelectedRoi( osg::ref_ptr< WROI > roi )
00233 {
00234     m_selectedRoi = roi;
00235 }
00236 
00237 osg::ref_ptr< WROI > WROIManager::getSelectedRoi()
00238 {
00239     return m_selectedRoi;
00240 }
00241 
00242 WROIManager::ROIs WROIManager::getRois() const
00243 {
00244     ROIs returnVec;
00245 
00246     for( std::list< boost::shared_ptr< WRMBranch > >::const_iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
00247     {
00248         ( *iter )->getRois( returnVec );
00249     }
00250     return returnVec;
00251 }
00252 
00253 WROIManager::Branches WROIManager::getBranches() const
00254 {
00255     // copy to this vec
00256     Branches returnVec;
00257 
00258     // copy
00259     for( std::list< boost::shared_ptr< WRMBranch > >::const_iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
00260     {
00261         returnVec.push_back( *iter );
00262     }
00263     return returnVec;
00264 }
00265