WROIManagerFibers.cpp 6.3 KB
Newer Older
schurade's avatar
schurade committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include <list>
#include <vector>

schurade's avatar
schurade committed
28
#include "WUpdateThread.h"
schurade's avatar
schurade committed
29 30 31 32
#include "WROIManagerFibers.h"

WROIManagerFibers::WROIManagerFibers()
{
schurade's avatar
schurade committed
33 34
    m_activeBitField = 0;
    m_recalcLock = false;
schurade's avatar
schurade committed
35 36 37 38 39 40
}

WROIManagerFibers::~WROIManagerFibers()
{
}

schurade's avatar
[STYLE]  
schurade committed
41
void WROIManagerFibers::addRoi( boost::shared_ptr< WROI > newRoi )
schurade's avatar
schurade committed
42 43 44 45
{
    // create new branch
    boost::shared_ptr< WRMBranch > newBranch = boost::shared_ptr< WRMBranch >( new WRMBranch( shared_from_this() ) );
    // create roi
schurade's avatar
[STYLE]  
schurade committed
46
    boost::shared_ptr< WRMROIRepresentation > rroi = boost::shared_ptr< WRMROIRepresentation >( new WRMROIRepresentation( newRoi, newBranch ) );
schurade's avatar
schurade committed
47 48 49 50 51
    // add roi to branch
    newBranch->addRoi( rroi );
    // add branch to list
    m_branches.push_back( newBranch );
    // add bit fields
schurade's avatar
schurade committed
52
    newBranch->addBitField( m_fibers.get()->size() );
schurade's avatar
schurade committed
53

schurade's avatar
[STYLE]  
schurade committed
54 55
    for ( std::list< boost::function< void( boost::shared_ptr< WRMROIRepresentation > ) > >::iterator iter = m_notifiers.begin(); iter
            != m_notifiers.end(); ++iter )
schurade's avatar
schurade committed
56 57 58
    {
        ( *iter )( rroi );
    }
schurade's avatar
schurade committed
59 60
}

schurade's avatar
[STYLE]  
schurade committed
61
void WROIManagerFibers::addRoi( boost::shared_ptr< WROI > newRoi, boost::shared_ptr< WROI > parentRoi )
schurade's avatar
schurade committed
62 63
{
    // find branch
schurade's avatar
[STYLE]  
schurade committed
64 65
    boost::shared_ptr< WRMBranch > branch;
    for ( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
schurade's avatar
schurade committed
66
    {
schurade's avatar
[STYLE]  
schurade committed
67
        if ( ( *iter ).get()->getFirstRoi()->getROI() == parentRoi )
schurade's avatar
schurade committed
68 69 70 71 72
        {
            branch = ( *iter );
        }
    }
    // create roi
schurade's avatar
[STYLE]  
schurade committed
73
    boost::shared_ptr< WRMROIRepresentation > rroi = boost::shared_ptr< WRMROIRepresentation >( new WRMROIRepresentation( newRoi, branch ) );
schurade's avatar
schurade committed
74
    // add bit fields
schurade's avatar
schurade committed
75
    rroi->addBitField( m_fibers.get()->size() );
schurade's avatar
schurade committed
76 77
    // add roi to branch
    branch->addRoi( rroi );
schurade's avatar
schurade committed
78

schurade's avatar
[STYLE]  
schurade committed
79 80
    for ( std::list< boost::function< void( boost::shared_ptr< WRMROIRepresentation > ) > >::iterator iter = m_notifiers.begin(); iter
            != m_notifiers.end(); ++iter )
schurade's avatar
schurade committed
81 82 83
    {
        ( *iter )( rroi );
    }
schurade's avatar
schurade committed
84 85
}

schurade's avatar
[STYLE]  
schurade committed
86
void WROIManagerFibers::removeRoi( boost::shared_ptr< WROI > /*roi */ )
schurade's avatar
schurade committed
87 88 89
{
}

Mathias Goldau's avatar
Mathias Goldau committed
90
void WROIManagerFibers::addFiberDataset( boost::shared_ptr< const WDataSetFibers > fibers )
schurade's avatar
schurade committed
91
{
schurade's avatar
schurade committed
92
    m_fibers = fibers;
schurade's avatar
schurade committed
93 94

    boost::shared_ptr< std::vector< float > > verts = fibers->getVertices();
schurade's avatar
schurade committed
95
    m_kdTree = boost::shared_ptr< WKdTree >( new WKdTree( verts->size() / 3, &( ( *verts )[0] ) ) );
schurade's avatar
schurade committed
96 97

    addBitField( fibers->size() );
schurade's avatar
schurade committed
98 99

    setDirty();
schurade's avatar
schurade committed
100 101
}

Mathias Goldau's avatar
Mathias Goldau committed
102
void WROIManagerFibers::removeFiberDataset( boost::shared_ptr< const WDataSetFibers > /*fibers*/ )
schurade's avatar
schurade committed
103
{
104
    assert( 0 && "removeFiberDataset( --- ) not implemented yet." );
schurade's avatar
schurade committed
105 106
}

schurade's avatar
schurade committed
107
boost::shared_ptr< std::vector< bool > > WROIManagerFibers::getBitField()
schurade's avatar
schurade committed
108
{
schurade's avatar
schurade committed
109 110
    m_dirty = false;
    if ( m_activeBitField == 0 )
schurade's avatar
schurade committed
111
    {
schurade's avatar
schurade committed
112
        return m_bitField;
schurade's avatar
schurade committed
113
    }
schurade's avatar
schurade committed
114
    else
schurade's avatar
schurade committed
115
    {
schurade's avatar
schurade committed
116
        return m_bitField2;
schurade's avatar
schurade committed
117 118 119 120 121
    }
}

void WROIManagerFibers::addBitField( size_t size )
{
schurade's avatar
[STYLE]  
schurade committed
122
    boost::shared_ptr< std::vector< bool > > bf = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( size, false ) );
schurade's avatar
schurade committed
123 124 125 126
    m_bitField = bf;
    boost::shared_ptr< std::vector< bool > > bf2 = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( size, false ) );
    m_bitField2 = bf2;

schurade's avatar
[STYLE]  
schurade committed
127
    for ( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
schurade's avatar
schurade committed
128
    {
schurade's avatar
[STYLE]  
schurade committed
129
        ( *iter ).get()->addBitField( size );
schurade's avatar
schurade committed
130 131 132 133 134 135
    }
    setDirty();
}

void WROIManagerFibers::recalculate()
{
schurade's avatar
schurade committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
//    boost::unique_lock<boost::shared_mutex> slock;
//    slock = boost::unique_lock<boost::shared_mutex>( m_updateLock );
    if ( m_recalcLock )
        return;
    m_recalcLock = true;

    boost::shared_ptr< std::vector< bool > > mbf;
    if ( m_activeBitField == 0 )
    {
        mbf = m_bitField2;
    }
    else
    {
        mbf = m_bitField;
    }
151

schurade's avatar
schurade committed
152 153
    int size = mbf->size();
    mbf->clear();
154 155 156 157

    if ( m_branches.empty() )
    {
        mbf->resize( size, true );
schurade's avatar
schurade committed
158 159 160 161 162 163 164 165 166 167
        if ( m_activeBitField == 0 )
        {
            m_activeBitField = 1;
        }
        else
        {
            m_activeBitField = 0;
        }
        m_recalcLock = false;
        //slock.unlock();
168 169 170 171 172 173 174
        return;
    }
    else
    {
        mbf->resize( size, false );
    }

schurade's avatar
[STYLE]  
schurade committed
175
    for ( std::list< boost::shared_ptr< WRMBranch > >::iterator iter = m_branches.begin(); iter != m_branches.end(); ++iter )
schurade's avatar
schurade committed
176
    {
schurade's avatar
schurade committed
177
        boost::shared_ptr< std::vector< bool > > bf = ( *iter )->getBitField();
schurade's avatar
schurade committed
178

schurade's avatar
schurade committed
179
        for ( size_t i = 0; i < mbf->size(); ++i )
schurade's avatar
schurade committed
180 181 182 183
        {
            mbf->at( i ) = mbf->at( i ) | bf->at( i );
        }
    }
schurade's avatar
schurade committed
184 185 186 187 188 189 190 191 192
    if ( m_activeBitField == 0 )
    {
        m_activeBitField = 1;
    }
    else
    {
        m_activeBitField = 0;
    }
    m_dirty = true;
schurade's avatar
schurade committed
193

schurade's avatar
schurade committed
194 195
    //slock.unlock();
    m_recalcLock = false;
schurade's avatar
schurade committed
196 197 198 199
}

void WROIManagerFibers::setDirty()
{
schurade's avatar
schurade committed
200 201
    WUpdateThread* t = new WUpdateThread( shared_from_this() );
    t->run();
schurade's avatar
schurade committed
202 203
}

schurade's avatar
schurade committed
204
boost::shared_ptr< const WDataSetFibers > WROIManagerFibers::getDataSet()
schurade's avatar
schurade committed
205
{
schurade's avatar
schurade committed
206
    return m_fibers;
schurade's avatar
schurade committed
207 208
}

schurade's avatar
schurade committed
209
boost::shared_ptr< WKdTree > WROIManagerFibers::getKdTree()
schurade's avatar
schurade committed
210
{
schurade's avatar
schurade committed
211
    return m_kdTree;
schurade's avatar
schurade committed
212 213 214 215 216 217
}

bool WROIManagerFibers::isDirty()
{
    return m_dirty;
}
schurade's avatar
schurade committed
218

schurade's avatar
[STYLE]  
schurade committed
219
void WROIManagerFibers::addDefaultNotifier( boost::function< void( boost::shared_ptr< WRMROIRepresentation > ) > notifier )
schurade's avatar
schurade committed
220
{
schurade's avatar
[STYLE]  
schurade committed
221 222
    boost::unique_lock< boost::shared_mutex > lock;
    lock = boost::unique_lock< boost::shared_mutex >( m_associatedNotifiersLock );
schurade's avatar
schurade committed
223 224 225
    m_notifiers.push_back( notifier );
    lock.unlock();
}