OpenWalnut  1.3.1
WRMBranch.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 "../graphicsEngine/WGraphicsEngine.h"
29 
30 #include "WROIManager.h"
31 #include "WRMBranch.h"
32 
33 
34 WRMBranch::WRMBranch( boost::shared_ptr< WROIManager > roiManager ) :
35  m_roiManager( roiManager )
36 {
37  properties();
38 }
39 
41 {
42 }
43 
45 {
46  m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "This branch's properties" ) );
47 
48  m_dirty = m_properties->addProperty( "Dirty", "", true, boost::bind( &WRMBranch::propertyChanged, this ) );
49  m_dirty->setHidden( true );
50  m_isNot = m_properties->addProperty( "Not", "Negate the effect of this branch.", false, boost::bind( &WRMBranch::propertyChanged, this ) );
51  m_bundleColor = m_properties->addProperty( "Bundle color", "", WColor( 1.0, 0.0, 0.0, 1.0 ),
52  boost::bind( &WRMBranch::propertyChanged, this ) );
53  m_changeRoiSignal = boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WRMBranch::setDirty, this ) ) );
54 }
55 
57 {
58  setDirty();
59 }
60 
61 
62 void WRMBranch::addRoi( osg::ref_ptr< WROI > roi )
63 {
64  m_rois.push_back( roi );
65  roi->addROIChangeNotifier( m_changeRoiSignal );
66 
67  setDirty();
68 }
69 
70 bool WRMBranch::contains( osg::ref_ptr< WROI > roi )
71 {
72  for( std::list< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
73  {
74  if( ( *iter ) == roi )
75  {
76  return true;
77  }
78  }
79  return false;
80 }
81 
82 void WRMBranch::removeRoi( osg::ref_ptr< WROI > roi )
83 {
84  roi->removeROIChangeNotifier( m_changeRoiSignal );
85  for( std::list< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
86  {
87  if( ( *iter ) == roi )
88  {
89  m_rois.erase( iter );
90  setDirty();
91  break;
92  }
93  }
94 }
95 
96 void WRMBranch::getRois( std::vector< osg::ref_ptr< WROI > >& roiVec ) // NOLINT
97 {
98  for( std::list< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
99  {
100  roiVec.push_back( ( *iter ) );
101  }
102 }
103 
105 {
106  for( std::list< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
107  {
108  WGraphicsEngine::getGraphicsEngine()->getScene()->remove( ( *iter ) );
109  }
110 
111  m_rois.clear();
112 }
113 
115 {
116  m_dirty->set( true );
117  m_roiManager->setDirty();
118 
119  for( std::list< boost::shared_ptr< boost::function< void() > > >::iterator iter = m_changeNotifiers.begin();
120  iter != m_changeNotifiers.end(); ++iter )
121  {
122  ( **iter )();
123  }
124 }
125 
126 osg::ref_ptr< WROI > WRMBranch::getFirstRoi()
127 {
128  return m_rois.front();
129 }
130 
131 boost::shared_ptr< WROIManager > WRMBranch::getRoiManager()
132 {
133  return m_roiManager;
134 }
135 
136 boost::shared_ptr< WProperties > WRMBranch::getProperties()
137 {
138  return m_properties;
139 }
140 
141 void WRMBranch::addChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier )
142 {
143  boost::unique_lock< boost::shared_mutex > lock;
144  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
145  m_changeNotifiers.push_back( notifier );
146  lock.unlock();
147 }
148 
149 void WRMBranch::removeChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier )
150 {
151  boost::unique_lock< boost::shared_mutex > lock;
152  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
153  std::list< boost::shared_ptr< boost::function< void() > > >::iterator it;
154  it = std::find( m_changeNotifiers.begin(), m_changeNotifiers.end(), notifier );
155  if( it != m_changeNotifiers.end() )
156  {
157  m_changeNotifiers.erase( it );
158  }
159  lock.unlock();
160 }