OpenWalnut  1.5.0dev
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 <memory>
27 #include <vector>
28 
29 #include "../common/WAssert.h"
30 #include "../graphicsEngine/WGraphicsEngine.h"
31 #include "WROIManager.h"
32 
34 {
35  m_properties = std::shared_ptr< WProperties >( new WProperties( "Properties", "Module's properties" ) );
36  m_dirty = m_properties->addProperty( "dirty", "dirty flag", true );
37 }
38 
40 {
41 }
42 
43 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, std::shared_ptr< WRMBranch > toBranch )
44 {
45  // add roi to branch
46  toBranch->addRoi( newRoi );
47 
48  for( std::list< std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin();
49  iter != m_addNotifiers.end(); ++iter )
50  {
51  ( **iter )( newRoi );
52  }
53 }
54 
55 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi )
56 {
57  addRoi( newRoi, addBranch() );
58 }
59 
60 std::shared_ptr< WRMBranch > WROIManager::addBranch()
61 {
62  // create new branch
63  std::shared_ptr< WRMBranch > newBranch( new WRMBranch( shared_from_this() ) );
64  // add branch to list
65  m_branches.push_back( newBranch );
66 
67  // return
68  return newBranch;
69 }
70 
71 void WROIManager::addRoi( osg::ref_ptr< WROI > newRoi, osg::ref_ptr< WROI > parentRoi )
72 {
73  // find branch
74  std::shared_ptr< WRMBranch > branch;
75  for( std::list< std::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
76  {
77  if( ( *iter ).get()->contains( parentRoi ) )
78  {
79  branch = ( *iter );
80  }
81  }
82  // add roi to branch
83  branch->addRoi( newRoi );
84 
85  for( std::list< std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter = m_addNotifiers.begin();
86  iter != m_addNotifiers.end(); ++iter )
87  {
88  ( **iter )( newRoi );
89  }
90 }
91 
92 void WROIManager::removeRoi( osg::ref_ptr< WROI > roi )
93 {
94  WGraphicsEngine::getGraphicsEngine()->getScene()->remove( roi );
95 
96  for( std::list< std::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
97  {
98  ( *iter )->removeRoi( roi );
99 
100  if( ( *iter )->empty() )
101  {
102  for( std::list< std::shared_ptr< boost::function< void( std::shared_ptr< WRMBranch > ) > > >::iterator iter2
103  = m_removeBranchNotifiers.begin();
104  iter2 != m_removeBranchNotifiers.end();
105  ++iter2 )
106  {
107  ( **iter2 )( *iter );
108  }
109  m_branches.erase( iter );
110  break;
111  }
112  }
113  setDirty();
114 
115  for( std::list< std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > >::iterator iter
116  = m_removeNotifiers.begin();
117  iter != m_removeNotifiers.end();
118  ++iter )
119  {
120  ( **iter )( roi );
121  }
122 }
123 
124 void WROIManager::removeBranch( osg::ref_ptr< WROI > roi )
125 {
126  for( std::list< std::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
127  {
128  if( roi == ( *iter )->getFirstRoi() )
129  {
130  ( *iter )->removeAllRois();
131  }
132 
133  if( ( *iter )->empty() )
134  {
135  for( std::list< std::shared_ptr< boost::function< void( std::shared_ptr< WRMBranch > ) > > >::iterator iter2
136  = m_removeBranchNotifiers.begin();
137  iter2 != m_removeBranchNotifiers.end();
138  ++iter2 )
139  {
140  ( **iter2 )( *iter );
141  }
142  m_branches.erase( iter );
143  break;
144  }
145  }
146  setDirty();
147 }
148 
149 std::shared_ptr< WRMBranch> WROIManager::getBranch( osg::ref_ptr< WROI > roi )
150 {
151  std::shared_ptr< WRMBranch> branch;
152 
153  for( std::list< std::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
154  {
155  if( ( *iter )->contains( roi ) )
156  {
157  branch = ( *iter );
158  }
159  }
160  return branch;
161 }
162 
164 {
165  m_dirty->set( true );
166 }
167 
168 /// @cond Supress doxygen because it produces warning here becuase it does not correctly understand the C++ code.
169 void WROIManager::addAddNotifier( std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
170 {
171  std::unique_lock< std::shared_mutex > lock;
172  lock = std::unique_lock< std::shared_mutex >( m_associatedNotifiersLock );
173  m_addNotifiers.push_back( notifier );
174  lock.unlock();
175 }
176 
177 void WROIManager::removeAddNotifier( std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
178 {
179  std::unique_lock< std::shared_mutex > lock;
180  lock = std::unique_lock< std::shared_mutex >( m_associatedNotifiersLock );
181  std::list< std::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( std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
191 {
192  std::unique_lock< std::shared_mutex > lock;
193  lock = std::unique_lock< std::shared_mutex >( m_associatedNotifiersLock );
194  m_removeNotifiers.push_back( notifier );
195  lock.unlock();
196 }
197 
198 void WROIManager::removeRemoveNotifier( std::shared_ptr< boost::function< void( osg::ref_ptr< WROI > ) > > notifier )
199 {
200  std::unique_lock< std::shared_mutex > lock;
201  lock = std::unique_lock< std::shared_mutex >( m_associatedNotifiersLock );
202  std::list< std::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( std::shared_ptr< boost::function< void( std::shared_ptr< WRMBranch > ) > > notifier )
212 {
213  std::unique_lock< std::shared_mutex > lock;
214  lock = std::unique_lock< std::shared_mutex >( m_associatedNotifiersLock );
215  m_removeBranchNotifiers.push_back( notifier );
216  lock.unlock();
217 }
218 
219 void WROIManager::removeRemoveBranchNotifier( std::shared_ptr< boost::function< void( std::shared_ptr< WRMBranch > ) > > notifier )
220 {
221  std::unique_lock< std::shared_mutex > lock;
222  lock = std::unique_lock< std::shared_mutex >( m_associatedNotifiersLock );
223  std::list< std::shared_ptr< boost::function< void( std::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 /// @endcond
232 
233 void WROIManager::setSelectedRoi( osg::ref_ptr< WROI > roi )
234 {
235  m_selectedRoi = roi;
236 }
237 
238 osg::ref_ptr< WROI > WROIManager::getSelectedRoi()
239 {
240  return m_selectedRoi;
241 }
242 
244 {
245  ROIs returnVec;
246 
247  for( std::list< std::shared_ptr< WRMBranch > >::const_iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
248  {
249  ( *iter )->getRois( returnVec );
250  }
251  return returnVec;
252 }
253 
255 {
256  // copy to this vec
257  Branches returnVec;
258 
259  // copy
260  for( std::list< std::shared_ptr< WRMBranch > >::const_iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
261  {
262  returnVec.push_back( *iter );
263  }
264  return returnVec;
265 }
static std::shared_ptr< WGraphicsEngine > getGraphicsEngine()
Returns instance of the graphics engine.
Class to manage properties of an object and to provide convenience methods for easy access and manipu...
implements a branch in the tree like structure for rois
Definition: WRMBranch.h:44
std::list< std::shared_ptr< WRMBranch > > m_branches
list of branches in the logical tree structure
Definition: WROIManager.h:205
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WROIManager.h:233
std::list< std::shared_ptr< boost::function< void(osg::ref_ptr< WROI >) > > > m_removeNotifiers
The notifiers connected to removed rois by default.
Definition: WROIManager.h:220
void setSelectedRoi(osg::ref_ptr< WROI > roi)
setter
std::shared_ptr< WRMBranch > getBranch(osg::ref_ptr< WROI > roi)
getter returns the branch item the roi is in
std::list< std::shared_ptr< boost::function< void(std::shared_ptr< WRMBranch >) > > > m_removeBranchNotifiers
The notifiers connected to removed rois by default.
Definition: WROIManager.h:225
std::vector< std::shared_ptr< WRMBranch > > Branches
Branches list.
Definition: WROIManager.h:194
void setDirty()
sets the dirty flag which will cause recalculation of the bit field
~WROIManager()
destructor
Definition: WROIManager.cpp:39
WPropBool m_dirty
dirty flag
Definition: WROIManager.h:238
std::vector< osg::ref_ptr< WROI > > ROIs
ROI list.
Definition: WROIManager.h:183
WROIManager()
standard constructor
Definition: WROIManager.cpp:33
std::shared_ptr< WRMBranch > addBranch()
Add a new branch.
Definition: WROIManager.cpp:60
void removeBranch(osg::ref_ptr< WROI > roi)
removes a branch
ROIs getRois() const
getter
Branches getBranches() const
Get a copy of the current branch list.
void removeRoi(osg::ref_ptr< WROI > roi)
removes a roi
Definition: WROIManager.cpp:92
osg::ref_ptr< WROI > getSelectedRoi()
getter
osg::ref_ptr< WROI > m_selectedRoi
stores a pointer to the currently selected roi
Definition: WROIManager.h:228
std::list< std::shared_ptr< boost::function< void(osg::ref_ptr< WROI >) > > > m_addNotifiers
The notifiers connected to added rois by default.
Definition: WROIManager.h:215
void addRoi(osg::ref_ptr< WROI > newRoi)
Adds a new main ROI.
Definition: WROIManager.cpp:55
std::shared_mutex m_associatedNotifiersLock
Lock for associated notifiers set.
Definition: WROIManager.h:210