OpenWalnut  1.4.0
WRMBranch.h
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 #ifndef WRMBRANCH_H
00026 #define WRMBRANCH_H
00027 
00028 #include <algorithm>
00029 #include <list>
00030 #include <string>
00031 #include <vector>
00032 
00033 #include <boost/enable_shared_from_this.hpp>
00034 
00035 #include "../common/WProperties.h"
00036 
00037 #include "../graphicsEngine/WROI.h"
00038 
00039 class WROIManager;
00040 
00041 /**
00042  * implements a branch in the tree like structure for rois
00043  */
00044 class  WRMBranch : public boost::enable_shared_from_this< WRMBranch >
00045 {
00046 public:
00047     /**
00048      * Convenience type for a shared pointer of this type
00049      */
00050     typedef boost::shared_ptr< WRMBranch > SPtr;
00051 
00052     /**
00053      * Convenience type for a const shared pointer of this type
00054      */
00055     typedef boost::shared_ptr< const WRMBranch > ConstSPtr;
00056 
00057     /**
00058      * construtor
00059      * \param roiManager
00060      */
00061     explicit WRMBranch( boost::shared_ptr< WROIManager > roiManager );
00062 
00063     /**
00064      * destructor
00065      */
00066     ~WRMBranch();
00067 
00068     /**
00069      * Get name property.
00070      *
00071      * \return  name property
00072      */
00073     WPropString nameProperty();
00074 
00075     /**
00076      * Get the "not" property.
00077      *
00078      * \return the property
00079      */
00080     WPropBool invertProperty();
00081 
00082     /**
00083      * The branch color property.
00084      *
00085      * \return the color property
00086      */
00087     WPropColor colorProperty();
00088 
00089     /**
00090      * Get the properties of this branch as group.
00091      *
00092      * \return branch property group
00093      */
00094     WPropertyGroup::SPtr getProperties() const;
00095 
00096     /**
00097      * adds a roi to the branch
00098      *
00099      * \param roi
00100      */
00101     void addRoi( osg::ref_ptr< WROI > roi );
00102 
00103     /**
00104      * removes a roi from the branch
00105      *
00106      * \param roi
00107      */
00108     void removeRoi( osg::ref_ptr< WROI > roi );
00109 
00110     /**
00111      * removes all rois from the branch
00112      *
00113      */
00114     void removeAllRois();
00115 
00116     /**
00117      * getter for dirty flag
00118      *
00119      * \param reset when true the dirty flag will be set to false
00120      * \return the dirty flag
00121      */
00122     bool dirty( bool reset = false );
00123 
00124     /**
00125      * sets dirty flag true and notifies the branch
00126      */
00127     void setDirty();
00128 
00129     /**
00130      * returns whether the branch is empty.
00131      *
00132      * \return true if empty.
00133      */
00134     bool empty();
00135 
00136     /**
00137      * checks wether a roi is in this branch
00138      * \param roi
00139      * \return true if the roi is in the branch, false otherwise
00140      */
00141     bool contains( osg::ref_ptr< WROI > roi );
00142 
00143     /**
00144      * returns a pointer to the first roi in the branch
00145      *
00146      * \return the roi
00147      */
00148     osg::ref_ptr< WROI > getFirstRoi();
00149 
00150     /**
00151      * getter for roi manager pointer
00152      *
00153      * \return the roi manager
00154      */
00155     boost::shared_ptr< WROIManager > getRoiManager();
00156 
00157     /**
00158      * returns the properties object.
00159      *
00160      * \return the properties of this branch
00161      */
00162     boost::shared_ptr< WProperties > getProperties();
00163 
00164     /**
00165      * getter for the NOT flag
00166      * \return flag
00167      */
00168     bool isNot();
00169 
00170     /**
00171      * add all the rois in this branch to a given vector
00172      * \param roiVec the vector to fill
00173      */
00174     void getRois( std::vector< osg::ref_ptr< WROI > >& roiVec ); //NOLINT
00175 
00176     /**
00177      * Create a list of ROIs of the current point in time.
00178      *
00179      * \return the ROIs
00180      */
00181     std::vector< osg::ref_ptr< WROI > > getRois() const;
00182 
00183     /**
00184      * Add a specified notifier to the list of default notifiers which get connected to each branch
00185      *
00186      * \param notifier  the notifier function
00187      */
00188     void addChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier );
00189 
00190     /**
00191      * Remove a specified notifier from the list of default notifiers which get connected to each branch
00192      *
00193      * \param notifier  the notifier function
00194      */
00195     void removeChangeNotifier( boost::shared_ptr< boost::function< void() > > notifier );
00196 
00197     /**
00198      * Resorts the ROIs using the specified comparator from its begin to its end.
00199      *
00200      * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator<().
00201      *
00202      * \param comp the comparator
00203      */
00204     template < typename Comparator >
00205     void sort( Comparator comp );
00206 
00207 protected:
00208     /**
00209      * initializes properties
00210      */
00211     void properties();
00212 
00213     /**
00214      * slot gets called when a property has changed
00215      *
00216      */
00217     void propertyChanged();
00218 private:
00219     boost::shared_ptr< WROIManager > m_roiManager; //!< stores a pointer to the roi manager
00220 
00221     std::vector< osg::ref_ptr< WROI > > m_rois; //!< list of rois in this this branch,
00222                                                   // first in the list is the master roi
00223     /**
00224      * the property object for the module
00225      */
00226     boost::shared_ptr< WProperties > m_properties;
00227 
00228     WPropBool m_dirty; //!< dirty flag to indicate if anything has changed within the branch
00229 
00230     /**
00231      * indicates if the branch is negated
00232      */
00233     WPropBool m_isNot;
00234 
00235     /**
00236      * The color used when in isosurface mode for blending.
00237      */
00238     WPropColor m_bundleColor;
00239 
00240     /**
00241      * Name property.
00242      */
00243     WPropString m_name;
00244 
00245     /**
00246      * The notifiers connected to added rois by default.
00247      */
00248     std::list< boost::shared_ptr< boost::function< void() > > > m_changeNotifiers;
00249 
00250     boost::shared_ptr< boost::function< void() > > m_changeRoiSignal; //!< Signal that can be used to update the ROImanager branch
00251 
00252     /**
00253      * Lock for associated notifiers set.
00254      */
00255     boost::shared_mutex m_associatedNotifiersLock;
00256 };
00257 
00258 inline bool WRMBranch::empty()
00259 {
00260     return m_rois.empty();
00261 }
00262 
00263 inline bool WRMBranch::dirty( bool reset )
00264 {
00265     bool ret = m_dirty->get();
00266     if( reset )
00267     {
00268         m_dirty->set( false );
00269     }
00270     return ret;
00271 }
00272 
00273 inline bool WRMBranch::isNot()
00274 {
00275     return m_isNot->get();
00276 }
00277 
00278 template < typename Comparator >
00279 void WRMBranch::sort( Comparator comp )
00280 {
00281     // NOTE: technically, we need not setDirty here as the order of the ROIs has no influence
00282     return std::sort( m_rois.begin(), m_rois.end(), comp );
00283 }
00284 
00285 #endif  // WRMBRANCH_H