WMFiberCulling.cpp 4.99 KB
Newer Older
Mathias Goldau's avatar
Mathias Goldau 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
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

25 26
#include <iomanip>
#include <iostream>
27
#include <string>
28
#include <vector>
29 30 31 32

#include <osg/Geode>
#include <osg/Geometry>

33
#include "WMFiberCulling.h"
34
#include "../../math/WFiber.h"
35
#include "../../common/WColor.h"
36 37
#include "../../common/WLogger.h"
#include "../../common/WStatusReport.h"
38 39 40 41 42 43
#include "../../dataHandler/WDataHandler.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WDataSetFibers.h"
#include "../../kernel/WKernel.h"
#include "../../utils/WColorUtils.h"

44
WMFiberCulling::WMFiberCulling()
45 46 47 48
    : WModule(),
      m_proximity_t( 0.0 ),
      m_dSt_culling_t( 0.0 ),
      m_saveCulledCurves( false )
49 50 51
{
}

52
WMFiberCulling::~WMFiberCulling()
53 54 55
{
}

56 57 58 59 60
boost::shared_ptr< WModule > WMFiberCulling::factory() const
{
    return boost::shared_ptr< WModule >( new WMFiberCulling() );
}

61
void WMFiberCulling::moduleMain()
62 63
{
    boost::shared_ptr< WDataHandler > dataHandler;
64
    // TODO(math): fix this hack when possible by using an input connector.
65 66 67 68 69 70 71 72 73 74 75 76 77
    while( !WKernel::getRunningKernel() )
    {
        sleep( 1 );
    }
    while( !( dataHandler = WKernel::getRunningKernel()->getDataHandler() ) )
    {
        sleep( 1 );
    }
    while( !dataHandler->getNumberOfSubjects() )
    {
        sleep( 1 );
    }

78 79
    boost::shared_ptr< WDataSetFibers > fiberDS;
    assert( fiberDS = boost::shared_dynamic_cast< WDataSetFibers >( dataHandler->getSubject( 0 )->getDataSet( 0 ) ) );
80 81 82 83 84 85 86 87 88 89 90 91 92 93

//    osg::ref_ptr< osg::Group > group = osg::ref_ptr< osg::Group >( new osg::Group );
//
//    for( size_t i = 0; i < fibers.size(); ++i )
//    {
//        group->addChild( genFiberGeode( fibers[i] ).get() );
//    }
//    group->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
//
//    WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( group.get() );
//

    cullOutFibers( fiberDS );

94 95
    std::cout << "done." << std::endl;

96 97 98 99 100 101 102 103
    // Since the modules run in a separate thread: such loops are possible
    while ( !m_FinishRequested )
    {
        // do fancy stuff
        sleep( 1 );
    }
}

104
void WMFiberCulling::cullOutFibers( boost::shared_ptr< WDataSetFibers > fibers )
105
{
106 107 108 109 110 111 112 113
    size_t numFibers = fibers->size();
    std::cout << "Recoginzed " << numFibers << " fibers" << std::endl;

    fibers->sortDescLength();  // sort the fiber on their length (biggest first)
    std::cout << "Sorted fibers done." << std::endl;

    const double proximity_t_Square = m_proximity_t * m_proximity_t;
    std::vector< bool > unusedFibers( numFibers, false );
114
    WStatusReport st( numFibers );
115 116 117 118 119 120 121 122 123 124 125 126 127
    for( size_t i = 0; i < numFibers; ++i )  // loop over all streamlines
    {
        if( unusedFibers[i] )
        {
            continue;
        }
        const wmath::WFiber& q = (*fibers)[ i ];
        for( size_t j = i + 1;  j < numFibers; ++j )
        {
            if( unusedFibers[j] )
            {
                continue;
            }
128 129 130 131 132
            const wmath::WFiber& r = (*fibers)[j];
            double dst = q.dSt( r, proximity_t_Square );

            if( dst < m_dSt_culling_t )  // cullout small fibs nearby long fibs
            {
133 134
                if( q.size() < r.size() )
                {
135 136 137
                    unusedFibers[i] = true;
                    break;
                }
138 139
                else
                {
140 141 142
                    unusedFibers[j] = true;
                }
            }
143
        }
144 145
        std::cout << "\r" << std::fixed << std::setprecision( 2 );
        std::cout << ( ++st ).progress() << " " << st.stringBar() << std::flush;
146
    }
147
    std::cout << std::endl;
148 149 150 151
//    if( m_saveCulledCurves ) {
//        saveFib( m_savePath, fibers, unusedFibers );
//        std::cout << "Saving culled fibers to " << m_savePath << " done." << std::endl;
//    }
Mathias Goldau's avatar
Mathias Goldau committed
152

153
    // create new DataSet where unused fibers are not contained
154 155 156 157
    fibers->erase( unusedFibers );
    std::cout << "Erasing done." << std::endl;
    std::cout << "Culled out " << numFibers - fibers->size() << " fibers" << std::endl;
    std::cout << "There are " << fibers->size() << " fibers left." << std::endl;
158
}