WMFibersToPoints.cpp 8.77 KB
Newer Older
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
#include <algorithm>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <string>

#include "core/kernel/WKernel.h"
#include "core/common/WPropertyHelper.h"
#include "core/common/math/WMath.h"
#include "core/dataHandler/WDataHandler.h"

#include "WMFibersToPoints.h"

// This line is needed by the module loader to actually find your module. You need to add this to your module too. Do NOT add a ";" here.
W_LOADABLE_MODULE( WMFibersToPoints )

WMFibersToPoints::WMFibersToPoints():
    WModule()
{
}

WMFibersToPoints::~WMFibersToPoints()
{
    // Cleanup!
}

boost::shared_ptr< WModule > WMFibersToPoints::factory() const
{
    return boost::shared_ptr< WModule >( new WMFibersToPoints() );
}

const char** WMFibersToPoints::getXPMIcon() const
{
    return NULL;
}

const std::string WMFibersToPoints::getName() const
{
    return "Fibers To Points";
}

const std::string WMFibersToPoints::getDescription() const
{
65
    return "This module converts fiber data to points by using the fiber vertex points and colors.";
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
}

void WMFibersToPoints::connectors()
{
    // The input fiber dataset
    m_fiberInput = boost::shared_ptr< WModuleInputData < WDataSetFibers > >(
        new WModuleInputData< WDataSetFibers >( shared_from_this(), "fibers", "The fiber dataset" )
    );

    // As properties, every connector needs to be added to the list of connectors.
    addConnector( m_fiberInput );

    // the points
    m_pointsOutput = boost::shared_ptr< WModuleOutputData < WDataSetPoints > >(
        new WModuleOutputData< WDataSetPoints >( shared_from_this(), "out", "The point data." )
    );

    // As above: make it known.
    addConnector( m_pointsOutput );

    // call WModule's initialization
    WModule::connectors();
}

void WMFibersToPoints::properties()
{
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );

94 95 96 97 98 99 100 101 102
    m_filterGroup = m_properties->addPropertyGroup( "Filtering", "Filtering based on fiber parameters." );
    m_paramHint = m_filterGroup->addProperty( "Hint", "If you see this, your data does not contain fiber parameters.",
                                              std::string( "Your data cannot be filtered due to missing parameters." ) );
    m_paramHint->setPurpose( PV_PURPOSE_INFORMATION );

    m_color = m_filterGroup->addProperty( "Color", "The color of the resulting points", defaultColor::WHITE, m_propCondition );
    m_parametersFilterValue = m_filterGroup->addProperty( "Parameter", "Value", 0.0, m_propCondition );
    m_parametersFilterWidth = m_filterGroup->addProperty( "Filter Width", "Width", 0.1, m_propCondition );

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    // call WModule's initialization
    WModule::properties();
}


void WMFibersToPoints::moduleMain()
{
    // get notified about data changes
    m_moduleState.setResetable( true, true );
    m_moduleState.add( m_fiberInput->getDataChangedCondition() );
    // Remember the condition provided to some properties in properties()? The condition can now be used with this condition set.
    m_moduleState.add( m_propCondition );

    ready();

    // main loop
    while( !m_shutdownFlag() )
    {
        debugLog() << "Waiting ...";
        m_moduleState.wait();

        // woke up since the module is requested to finish?
        if( m_shutdownFlag() )
        {
            break;
        }

        // To query whether an input was updated, simply ask the input:
        bool dataUpdated = m_fiberInput->handledUpdate();
        boost::shared_ptr< WDataSetFibers > dataSet = m_fiberInput->getData();
133
        bool dataValid = ( dataSet != NULL );
134 135 136
        bool propsChanged = m_parametersFilterValue->changed() ||
                            m_parametersFilterWidth->changed() ||
                            m_color->changed();
137 138 139 140 141 142 143 144 145

        // reset everything if input was disconnected/invalid
        if( !dataValid )
        {
            debugLog() << "Resetting output.";
            m_pointsOutput->reset();
            continue;
        }

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        if( dataValid && dataUpdated )
        {
            if( dataSet->getVertexParameters() )
            {
                m_parameterMin = dataSet->getVertexParameters()->operator[]( 0 );
                m_parameterMax = dataSet->getVertexParameters()->operator[]( 0 );
                for( WDataSetFibers::VertexParemeterArray::element_type::const_iterator it = dataSet->getVertexParameters()->begin();
                     it != dataSet->getVertexParameters()->end(); ++it )
                {
                    m_parameterMax = std::max( *it, m_parameterMax );
                    m_parameterMin = std::min( *it, m_parameterMin );
                }
            }
            else
            {
                m_parameterMin = 0.0;
                m_parameterMax = 1.0;
            }

            m_parametersFilterValue->setRecommendedValue( m_parameterMin );
            m_parametersFilterValue->setMin( m_parameterMin );
            m_parametersFilterValue->setMax( m_parameterMax );
            m_parametersFilterWidth->setRecommendedValue( m_parameterMax - m_parameterMin );
            m_parametersFilterWidth->setMin( 0.0 );
            m_parametersFilterWidth->setMax( m_parameterMax - m_parameterMin );
        }

        if( dataValid && !dataUpdated && !propsChanged )
174 175 176 177 178 179 180 181
        {
            continue;
        }

        // progress indication
        WProgress::SPtr progress = WProgress::SPtr( new WProgress( "Creating Points from Fibers." ) );
        m_progress->addSubProgress( progress );

182
        debugLog() << "Creating point data. Num Points = " << dataSet->getVertices()->size() / 3 << ".";
183 184
        WDataSetFibers::VertexArray fibVerts = dataSet->getVertices();
        WDataSetFibers::ColorArray fibColors = dataSet->getColorScheme()->getColor();
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        WDataSetFibers::VertexParemeterArray fibParams = dataSet->getVertexParameters();

        WDataSetFibers::VertexArray filteredVerts( new WDataSetFibers::VertexArray::element_type() );
        WDataSetFibers::ColorArray filteredColors( new WDataSetFibers::ColorArray::element_type() );
        WBoundingBox filteredBB;

        if( fibParams )
        {
            m_paramHint->setHidden( true );

            WIntervalDouble interval( std::max( m_parametersFilterValue->get( true ) - m_parametersFilterWidth->get( true ), m_parameterMin ),
                                      std::min( m_parametersFilterValue->get( true ) + m_parametersFilterWidth->get( true ), m_parameterMax ) );

            // filter
            for( size_t idx = 0; idx < fibVerts->size() / 3; ++idx )
            {
                if( isInClosed( interval, fibParams->operator[]( idx ) ) )
                {
                    filteredBB.expandBy( fibVerts->operator[]( idx * 3 + 0 ),
                                         fibVerts->operator[]( idx * 3 + 1 ),
                                         fibVerts->operator[]( idx * 3 + 2 ) );

                    filteredVerts->push_back( fibVerts->operator[]( idx * 3 + 0 ) );
                    filteredVerts->push_back( fibVerts->operator[]( idx * 3 + 1 ) );
                    filteredVerts->push_back( fibVerts->operator[]( idx * 3 + 2 ) );

                    filteredColors->push_back( m_color->get().x() );
                    filteredColors->push_back( m_color->get().y() );
                    filteredColors->push_back( m_color->get().z() );
                    filteredColors->push_back( m_color->get().w() );
                }
            }
        }
        else
        {
            m_paramHint->setHidden( false );

            filteredVerts = fibVerts;
            filteredColors = fibColors;
            filteredBB = dataSet->getBoundingBox();
        }


228
        debugLog() << "Done filtering. Result are " << filteredVerts->size() / 3 << " points.";
229
        WDataSetPoints::SPtr result( new WDataSetPoints( filteredVerts, filteredColors, filteredBB ) );
230 231 232 233 234 235 236
        m_pointsOutput->updateData( result );

        progress->finish();
        m_progress->removeSubProgress( progress );
    }
}