OpenWalnut 1.2.5

WSelectorBranch.cpp

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 #include <list>
00026 #include <vector>
00027 
00028 #include "WSelectorBranch.h"
00029 
00030 WSelectorBranch::WSelectorBranch( boost::shared_ptr< const WDataSetFibers > fibers, boost::shared_ptr<WRMBranch> branch ) :
00031     m_fibers( fibers ),
00032     m_size( fibers->size() ),
00033     m_dirty( true ),
00034     m_branch( branch )
00035 {
00036     m_bitField = boost::shared_ptr< std::vector<bool> >( new std::vector<bool>( m_size, false ) );
00037 
00038     m_changeSignal =
00039         boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WSelectorBranch::setDirty, this ) ) );
00040     m_branch->addChangeNotifier( m_changeSignal );
00041 
00042     m_changeRoiSignal =
00043         boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WSelectorBranch::setDirty, this ) ) );
00044 }
00045 
00046 WSelectorBranch::~WSelectorBranch()
00047 {
00048     m_branch->removeChangeNotifier( m_changeSignal );
00049 
00050     // We need the following because not all ROIs are removed per slot below
00051     for( std::list< boost::shared_ptr< WSelectorRoi > >::iterator roiIter = m_rois.begin(); roiIter != m_rois.end(); ++roiIter )
00052     {
00053         ( *roiIter )->getRoi()->removeROIChangeNotifier( m_changeRoiSignal );
00054     }
00055 }
00056 
00057 void WSelectorBranch::addRoi( boost::shared_ptr< WSelectorRoi > roi )
00058 {
00059     m_rois.push_back( roi );
00060     roi->getRoi()->addROIChangeNotifier( m_changeRoiSignal );
00061 }
00062 
00063 std::list< boost::shared_ptr< WSelectorRoi > > WSelectorBranch::getROIs()
00064 {
00065     return m_rois;
00066 }
00067 
00068 void WSelectorBranch::setDirty()
00069 {
00070     m_dirty = true;
00071     if( m_branch->getProperties()->getProperty( "Bundle color" )->toPropColor()->changed() )
00072     {
00073         colorChanged();
00074     }
00075 }
00076 
00077 void WSelectorBranch::removeRoi( osg::ref_ptr< WROI > roi )
00078 {
00079     for( std::list< boost::shared_ptr< WSelectorRoi > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00080     {
00081         if( ( *iter )->getRoi() == roi )
00082         {
00083             ( *iter )->getRoi()->removeROIChangeNotifier( m_changeRoiSignal );
00084             m_rois.erase( iter );
00085             break;
00086         }
00087     }
00088 }
00089 
00090 void WSelectorBranch::recalculate()
00091 {
00092     bool atLeastOneActive = false;
00093 
00094     for( std::list< boost::shared_ptr< WSelectorRoi > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00095     {
00096         if( ( *iter )->getRoi()->active() )
00097         {
00098             atLeastOneActive = true;
00099         }
00100     }
00101 
00102     if( atLeastOneActive )
00103     {
00104         m_workerBitfield = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( m_size, true ) );
00105 
00106         for( std::list< boost::shared_ptr< WSelectorRoi > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
00107         {
00108             if( ( *iter )->getRoi()->active() )
00109             {
00110                 boost::shared_ptr< std::vector<bool> > bf = ( *iter )->getBitField();
00111                 bool isnot = ( *iter )->getRoi()->isNot();
00112                 if( !isnot )
00113                 {
00114                     for( size_t i = 0 ; i < m_size ; ++i )
00115                     {
00116                         ( *m_workerBitfield )[i] = ( *m_workerBitfield )[i] & ( *bf )[i];
00117                     }
00118                 }
00119                 else
00120                 {
00121                     for( size_t i = 0 ; i < m_size ; ++i )
00122                     {
00123                         ( *m_workerBitfield )[i] = ( *m_workerBitfield )[i] & !( *bf )[i];
00124                     }
00125                 }
00126             }
00127         }
00128 
00129         if( m_branch->isNot() )
00130         {
00131            for( size_t i = 0 ; i < m_size ; ++i )
00132             {
00133                 ( *m_workerBitfield )[i] = !( *m_workerBitfield )[i];
00134             }
00135         }
00136     }
00137     else
00138     {
00139         m_workerBitfield = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( m_size, false ) );
00140     }
00141 
00142     m_bitField = m_workerBitfield;
00143 }
00144 
00145 void WSelectorBranch::colorChanged()
00146 {
00147     WColor color = m_branch->getProperties()->getProperty( "Bundle color" )->toPropColor()->get( true );
00148 
00149     boost::shared_ptr<std::vector<float> > array = m_fibers->getColorScheme( "Custom Color" )->getColor();
00150 
00151     boost::shared_ptr< std::vector< size_t > > startIndexes = m_fibers->getLineStartIndexes();
00152     boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_fibers->getLineLengths();
00153 
00154     for( size_t i = 0; i < m_size; ++i )
00155     {
00156         if( ( *m_bitField )[i] )
00157         {
00158             size_t idx = ( *startIndexes )[i] * 3;
00159             for( size_t k = 0; k < ( *pointsPerLine )[i]; ++k )
00160             {
00161                 ( *array )[idx++] = color[0];
00162                 ( *array )[idx++] = color[1];
00163                 ( *array )[idx++] = color[2];
00164             }
00165         }
00166     }
00167 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends