OpenWalnut  1.4.0
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 <string>
27 #include <vector>
28 
29 #include "../graphicsEngine/WGraphicsEngine.h"
30 
31 #include "WROIManager.h"
32 #include "WRMBranch.h"
33 
34 
35 WRMBranch::WRMBranch( boost::shared_ptr< WROIManager > roiManager ) :
36  m_roiManager( roiManager )
37 {
38  properties();
39 }
40 
42 {
43 }
44 
46 {
47  m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "This branch's properties" ) );
48 
49  m_dirty = m_properties->addProperty( "Dirty", "", true, boost::bind( &WRMBranch::propertyChanged, this ) );
50  m_dirty->setHidden( true );
51  m_name = m_properties->addProperty( "Name", "The name of this branch.", std::string( "Branch" ) );
52  m_isNot = m_properties->addProperty( "Not", "Negate the effect of this branch.", false, boost::bind( &WRMBranch::propertyChanged, this ) );
53  m_bundleColor = m_properties->addProperty( "Bundle color", "Color the selected fibers using this color.", WColor( 1.0, 0.0, 0.0, 1.0 ),
54  boost::bind( &WRMBranch::propertyChanged, this ) );
55 
56  m_changeRoiSignal = boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WRMBranch::setDirty, this ) ) );
57 }
58 
60 {
61  return m_properties;
62 }
63 
65 {
66  setDirty();
67 }
68 
70 {
71  return m_name;
72 }
73 
75 {
76  return m_isNot;
77 }
78 
80 {
81  return m_bundleColor;
82 }
83 
84 void WRMBranch::addRoi( osg::ref_ptr< WROI > roi )
85 {
86  m_rois.push_back( roi );
87  roi->addROIChangeNotifier( m_changeRoiSignal );
88  setDirty();
89 }
90 
91 bool WRMBranch::contains( osg::ref_ptr< WROI > roi )
92 {
93  for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
94  {
95  if( ( *iter ) == roi )
96  {
97  return true;
98  }
99  }
100  return false;
101 }
102 
103 void WRMBranch::removeRoi( osg::ref_ptr< WROI > roi )
104 {
105  roi->removeROIChangeNotifier( m_changeRoiSignal );
106  for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
107  {
108  if( ( *iter ) == roi )
109  {
110  m_rois.erase( iter );
111  setDirty();
112  break;
113  }
114  }
115 }
116 
117 void WRMBranch::getRois( std::vector< osg::ref_ptr< WROI > >& roiVec ) // NOLINT
118 {
119  for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
120  {
121  roiVec.push_back( ( *iter ) );
122  }
123 }
124 
126 {
127  WROIManager::ROIs ret;
128  for( std::vector< osg::ref_ptr< WROI > >::const_iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
129  {
130  ret.push_back( ( *iter ) );
131  }
132  return ret;
133 }
134 
136 {
137  for( std::vector< osg::ref_ptr< WROI > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
138  {
139  WGraphicsEngine::getGraphicsEngine()->getScene()->remove( ( *iter ) );
140  }
141 
142  m_rois.clear();
143 }
144 
146 {
147  m_dirty->set( true );
148  m_roiManager->setDirty();
149 
150  for( std::list< boost::shared_ptr< boost::function< void() > > >::iterator iter = m_changeNotifiers.begin();
151  iter != m_changeNotifiers.end(); ++iter )
152  {
153  ( **iter )();
154  }
155 }
156 
157 osg::ref_ptr< WROI > WRMBranch::getFirstRoi()
158 {
159  return m_rois.front();
160 }
161 
162 boost::shared_ptr< WROIManager > WRMBranch::getRoiManager()
163 {
164  return m_roiManager;
165 }
166 
167 boost::shared_ptr< WProperties > WRMBranch::getProperties()
168 {
169  return m_properties;
170 }
171 
172 void WRMBranch::addChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier )
173 {
174  boost::unique_lock< boost::shared_mutex > lock;
175  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
176  m_changeNotifiers.push_back( notifier );
177  lock.unlock();
178 }
179 
180 void WRMBranch::removeChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier )
181 {
182  boost::unique_lock< boost::shared_mutex > lock;
183  lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
184  std::list< boost::shared_ptr< boost::function< void() > > >::iterator it;
185  it = std::find( m_changeNotifiers.begin(), m_changeNotifiers.end(), notifier );
186  if( it != m_changeNotifiers.end() )
187  {
188  m_changeNotifiers.erase( it );
189  }
190  lock.unlock();
191 }