OpenWalnut 1.3.1
|
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 ) 00045 { 00046 // create new branch 00047 boost::shared_ptr< WRMBranch > newBranch = boost::shared_ptr< WRMBranch >( new WRMBranch( shared_from_this() ) ); 00048 // add branch to list 00049 m_branches.push_back( newBranch ); 00050 // add roi to branch 00051 newBranch->addRoi( newRoi ); 00052 00053 for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin(); 00054 iter != m_addNotifiers.end(); ++iter ) 00055 { 00056 ( **iter )( newRoi ); 00057 } 00058 } 00059 00060 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, osg::ref_ptr< WROI > parentRoi ) 00061 { 00062 // find branch 00063 boost::shared_ptr< WRMBranch > branch; 00064 for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter ) 00065 { 00066 if( ( *iter ).get()->contains( parentRoi ) ) 00067 { 00068 branch = ( *iter ); 00069 } 00070 } 00071 // add roi to branch 00072 branch->addRoi( newRoi ); 00073 00074 for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin(); 00075 iter != m_addNotifiers.end(); ++iter ) 00076 { 00077 ( **iter )( newRoi ); 00078 } 00079 } 00080 00081 void WROIManager::removeRoi( osg::ref_ptr< WROI > roi ) 00082 { 00083 WGraphicsEngine::getGraphicsEngine()->getScene()->remove( roi ); 00084 00085 for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter ) 00086 { 00087 ( *iter )->removeRoi( roi ); 00088 00089 if( ( *iter )->empty() ) 00090 { 00091 for( std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator iter2 00092 = m_removeBranchNotifiers.begin(); 00093 iter2 != m_removeBranchNotifiers.end(); 00094 ++iter2 ) 00095 { 00096 ( **iter2 )( *iter ); 00097 } 00098 m_branches.erase( iter ); 00099 break; 00100 } 00101 } 00102 setDirty(); 00103 00104 for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter 00105 = m_removeNotifiers.begin(); 00106 iter != m_removeNotifiers.end(); 00107 ++iter ) 00108 { 00109 ( **iter )( roi ); 00110 } 00111 } 00112 00113 void WROIManager::removeBranch( osg::ref_ptr< WROI > roi ) 00114 { 00115 for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter ) 00116 { 00117 if( roi == ( *iter )->getFirstRoi() ) 00118 { 00119 ( *iter )->removeAllRois(); 00120 } 00121 00122 if( ( *iter )->empty() ) 00123 { 00124 for( std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator iter2 00125 = m_removeBranchNotifiers.begin(); 00126 iter2 != m_removeBranchNotifiers.end(); 00127 ++iter2 ) 00128 { 00129 ( **iter2 )( *iter ); 00130 } 00131 m_branches.erase( iter ); 00132 break; 00133 } 00134 } 00135 setDirty(); 00136 } 00137 00138 boost::shared_ptr< WRMBranch> WROIManager::getBranch( osg::ref_ptr< WROI > roi ) 00139 { 00140 boost::shared_ptr< WRMBranch> branch; 00141 00142 for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter ) 00143 { 00144 if( ( *iter )->contains( roi ) ) 00145 { 00146 branch = ( *iter ); 00147 } 00148 } 00149 return branch; 00150 } 00151 00152 void WROIManager::setDirty() 00153 { 00154 m_dirty->set( true ); 00155 } 00156 00157 void WROIManager::addAddNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier ) 00158 { 00159 boost::unique_lock< boost::shared_mutex > lock; 00160 lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock ); 00161 m_addNotifiers.push_back( notifier ); 00162 lock.unlock(); 00163 } 00164 00165 void WROIManager::removeAddNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier ) 00166 { 00167 boost::unique_lock< boost::shared_mutex > lock; 00168 lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock ); 00169 std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator it; 00170 it = std::find( m_addNotifiers.begin(), m_addNotifiers.end(), notifier ); 00171 if( it != m_addNotifiers.end() ) 00172 { 00173 m_addNotifiers.erase( it ); 00174 } 00175 lock.unlock(); 00176 } 00177 00178 void WROIManager::addRemoveNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier ) 00179 { 00180 boost::unique_lock< boost::shared_mutex > lock; 00181 lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock ); 00182 m_removeNotifiers.push_back( notifier ); 00183 lock.unlock(); 00184 } 00185 00186 void WROIManager::removeRemoveNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier ) 00187 { 00188 boost::unique_lock< boost::shared_mutex > lock; 00189 lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock ); 00190 std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator it; 00191 it = std::find( m_removeNotifiers.begin(), m_removeNotifiers.end(), notifier ); 00192 if( it != m_removeNotifiers.end() ) 00193 { 00194 m_removeNotifiers.erase( it ); 00195 } 00196 lock.unlock(); 00197 } 00198 00199 void WROIManager::addRemoveBranchNotifier( boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > notifier ) 00200 { 00201 boost::unique_lock< boost::shared_mutex > lock; 00202 lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock ); 00203 m_removeBranchNotifiers.push_back( notifier ); 00204 lock.unlock(); 00205 } 00206 00207 void WROIManager::removeRemoveBranchNotifier( boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > notifier ) 00208 { 00209 boost::unique_lock< boost::shared_mutex > lock; 00210 lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock ); 00211 std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator it; 00212 it = std::find( m_removeBranchNotifiers.begin(), m_removeBranchNotifiers.end(), notifier ); 00213 if( it != m_removeBranchNotifiers.end() ) 00214 { 00215 m_removeBranchNotifiers.erase( it ); 00216 } 00217 lock.unlock(); 00218 } 00219 00220 void WROIManager::setSelectedRoi( osg::ref_ptr< WROI > roi ) 00221 { 00222 m_selectedRoi = roi; 00223 } 00224 00225 osg::ref_ptr< WROI > WROIManager::getSelectedRoi() 00226 { 00227 return m_selectedRoi; 00228 } 00229 00230 std::vector< osg::ref_ptr< WROI > > WROIManager::getRois() 00231 { 00232 std::vector< osg::ref_ptr< WROI > > returnVec; 00233 00234 for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter ) 00235 { 00236 ( *iter )->getRois( returnVec ); 00237 } 00238 return returnVec; 00239 }