OpenWalnut  1.4.0
WROIManager.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <list>
26 #include <vector>
27 
28 #include "../common/WAssert.h"
29 
30 #include "../graphicsEngine/WGraphicsEngine.h"
31 
32 #include "WROIManager.h"
33 
35 {
36  m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "Module's properties" ) );
37  m_dirty = m_properties->addProperty( "dirty", "dirty flag", true );
38 }
39 
41 {
42 }
43 
44 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, boost::shared_ptr< WRMBranch > toBranch )
45 {
46  // add roi to branch
47  toBranch->addRoi( newRoi );
48 
49  for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin();
50  iter != m_addNotifiers.end(); ++iter )
51  {
52  ( **iter )( newRoi );
53  }
54 }
55 
56 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi )
57 {
58  addRoi( newRoi, addBranch() );
59 }
60 
61 boost::shared_ptr< WRMBranch > WROIManager::addBranch()
62 {
63  // create new branch
64  boost::shared_ptr< WRMBranch > newBranch( new WRMBranch( shared_from_this() ) );
65  // add branch to list
66  m_branches.push_back( newBranch );
67 
68  // return
69  return newBranch;
70 }
71 
72 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, osg::ref_ptr< WROI > parentRoi )
73 {
74  // find branch
75  boost::shared_ptr< WRMBranch > branch;
76  for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
77  {
78  if( ( *iter ).get()->contains( parentRoi ) )
79  {
80  branch = ( *iter );
81  }
82  }
83  // add roi to branch
84  branch->addRoi( newRoi );
85 
86  for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin();
87  iter != m_addNotifiers.end(); ++iter )
88  {
89  ( **iter )( newRoi );
90  }
91 }
92 
93 void WROIManager::removeRoi( osg::ref_ptr< WROI > roi )
94 {
95  WGraphicsEngine::getGraphicsEngine()->getScene()->remove( roi );
96 
97  for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
98  {
99  ( *iter )->removeRoi( roi );
100 
101  if( ( *iter )->empty() )
102  {
103  for( std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator iter2
104  = m_removeBranchNotifiers.begin();
105  iter2 != m_removeBranchNotifiers.end();
106  ++iter2 )
107  {
108  ( **iter2 )( *iter );
109  }
110  m_branches.erase( iter );
111  break;
112  }
113  }
114  setDirty();
115 
116  for( std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter
117  = m_removeNotifiers.begin();
118  iter != m_removeNotifiers.end();
119  ++iter )
120  {
121  ( **iter )( roi );
122  }
123 }
124 
125 void WROIManager::removeBranch( osg::ref_ptr< WROI > roi )
126 {
127  for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
128  {
129  if( roi == ( *iter )->getFirstRoi() )
130  {
131  ( *iter )->removeAllRois();
132  }
133 
134  if( ( *iter )->empty() )
135  {
136  for( std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator iter2
137  = m_removeBranchNotifiers.begin();
138  iter2 != m_removeBranchNotifiers.end();
139  ++iter2 )
140  {
141  ( **iter2 )( *iter );
142  }
143  m_branches.erase( iter );
144  break;
145  }
146  }
147  setDirty();
148 }
149 
150 boost::shared_ptr< WRMBranch> WROIManager::getBranch( osg::ref_ptr< WROI > roi )
151 {
152  boost::shared_ptr< WRMBranch> branch;
153 
154  for( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
155  {
156  if( ( *iter )->contains( roi ) )
157  {
158  branch = ( *iter );
159  }
160  }
161  return branch;
162 }
163 
165 {
166  m_dirty->set( true );
167 }
168 
169 void WROIManager::addAddNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
170 {
171  boost::unique_lock< boost::shared_mutex > lock;
172  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
173  m_addNotifiers.push_back( notifier );
174  lock.unlock();
175 }
176 
177 void WROIManager::removeAddNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
178 {
179  boost::unique_lock< boost::shared_mutex > lock;
180  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
181  std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator it;
182  it = std::find( m_addNotifiers.begin(), m_addNotifiers.end(), notifier );
183  if( it != m_addNotifiers.end() )
184  {
185  m_addNotifiers.erase( it );
186  }
187  lock.unlock();
188 }
189 
190 void WROIManager::addRemoveNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
191 {
192  boost::unique_lock< boost::shared_mutex > lock;
193  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
194  m_removeNotifiers.push_back( notifier );
195  lock.unlock();
196 }
197 
198 void WROIManager::removeRemoveNotifier( boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
199 {
200  boost::unique_lock< boost::shared_mutex > lock;
201  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
202  std::list< boost::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator it;
203  it = std::find( m_removeNotifiers.begin(), m_removeNotifiers.end(), notifier );
204  if( it != m_removeNotifiers.end() )
205  {
206  m_removeNotifiers.erase( it );
207  }
208  lock.unlock();
209 }
210 
211 void WROIManager::addRemoveBranchNotifier( boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > notifier )
212 {
213  boost::unique_lock< boost::shared_mutex > lock;
214  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
215  m_removeBranchNotifiers.push_back( notifier );
216  lock.unlock();
217 }
218 
219 void WROIManager::removeRemoveBranchNotifier( boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > notifier )
220 {
221  boost::unique_lock< boost::shared_mutex > lock;
222  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
223  std::list< boost::shared_ptr< boost::function< void( boost::shared_ptr< WRMBranch > ) > > >::iterator it;
224  it = std::find( m_removeBranchNotifiers.begin(), m_removeBranchNotifiers.end(), notifier );
225  if( it != m_removeBranchNotifiers.end() )
226  {
227  m_removeBranchNotifiers.erase( it );
228  }
229  lock.unlock();
230 }
231 
232 void WROIManager::setSelectedRoi( osg::ref_ptr< WROI > roi )
233 {
234  m_selectedRoi = roi;
235 }
236 
237 osg::ref_ptr< WROI > WROIManager::getSelectedRoi()
238 {
239  return m_selectedRoi;
240 }
241 
243 {
244  ROIs returnVec;
245 
246  for( std::list< boost::shared_ptr< WRMBranch > >::const_iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
247  {
248  ( *iter )->getRois( returnVec );
249  }
250  return returnVec;
251 }
252 
254 {
255  // copy to this vec
256  Branches returnVec;
257 
258  // copy
259  for( std::list< boost::shared_ptr< WRMBranch > >::const_iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
260  {
261  returnVec.push_back( *iter );
262  }
263  return returnVec;
264 }
265