WMSampleOnFibers.cpp 10.4 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 25 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
//---------------------------------------------------------------------------
//
// 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 <algorithm>
#include <string>

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

#include "WMSampleOnFibers.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( WMSampleOnFibers )

WMSampleOnFibers::WMSampleOnFibers():
    WModule()
{
}

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

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

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

const std::string WMSampleOnFibers::getName() const
{
    return "Sample On Fibers";
}

const std::string WMSampleOnFibers::getDescription() const
{
65
    return "This module allows you to sample points on fibers using the parameters on a fiber.";
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 94 95 96 97 98 99 100 101 102 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
}

void WMSampleOnFibers::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 WMSampleOnFibers::properties()
{
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );

    m_paramHint = m_properties->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_properties->addProperty( "Color", "The color of the resulting points", defaultColor::WHITE, m_propCondition );
    m_parameter = m_properties->addProperty( "Parameter", "Value where to sample", 0.0, m_propCondition );

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

void WMSampleOnFibers::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();
130
        bool dataValid = ( dataSet != NULL );
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 174 175 176 177 178 179 180 181 182 183 184 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        bool propsChanged = m_parameter ||
                            m_color->changed();

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

        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_parameter->setRecommendedValue( m_parameterMin );
            m_parameter->setMin( m_parameterMin );
            m_parameter->setMax( m_parameterMax );
        }

        if( dataValid && !dataUpdated && !propsChanged )
        {
            continue;
        }

        WDataSetFibers::VertexArray fibVerts = dataSet->getVertices();
        WDataSetFibers::ColorArray fibColors = dataSet->getColorScheme()->getColor();
        WDataSetFibers::IndexArray  fibStart = dataSet->getLineStartIndexes();
        WDataSetFibers::LengthArray fibLen   = dataSet->getLineLengths();
        WDataSetFibers::VertexParemeterArray fibParams = dataSet->getVertexParameters();

        debugLog() << "Creating point data.";
        WDataSetFibers::VertexArray filteredVerts( new WDataSetFibers::VertexArray::element_type() );
        WDataSetFibers::ColorArray filteredColors( new WDataSetFibers::ColorArray::element_type() );
        WBoundingBox filteredBB;

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

            double v = m_parameter->get( true );

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

            // for each fiber:
            for( size_t fidx = 0; fidx < fibStart->size() ; ++fidx )
            {
                ++*progress;

                // the start vertex index
                size_t sidx = fibStart->at( fidx ) * 3;

                // the length of the fiber
                size_t len = fibLen->at( fidx );

                // a line needs 2 verts at least, but also check first and only vertex
                if( len < 2 )
                {
                    if( fibParams->at( fibStart->at( fidx ) ) == v )
                    {
                        filteredVerts->push_back( fibVerts->at( sidx + 0 ) );
                        filteredVerts->push_back( fibVerts->at( sidx + 1 ) );
                        filteredVerts->push_back( fibVerts->at( sidx + 2 ) );

                        filteredBB.expandBy(
                            osg::Vec3(
                                fibVerts->at( sidx + 0 ),
                                fibVerts->at( sidx + 1 ),
                                fibVerts->at( sidx + 2 ) )
                        );
                    }
                    continue;
                }

                // keep track of the previous vertex
                osg::Vec3 lastVert = osg::Vec3( fibVerts->at( sidx ),
                                                fibVerts->at( sidx + 1 ),
                                                fibVerts->at( sidx + 2 ) );
                // keep track of the previous value
                double lastParam = fibParams->at( fibStart->at( fidx ) );

                // walk along the fiber
                for( size_t k = 0; k < len; ++k )
                {
                    // get current vertex and value
                    osg::Vec3 vert = osg::Vec3( fibVerts->at( ( 3 * k ) + sidx ),
                                                fibVerts->at( ( 3 * k ) + sidx + 1 ),
                                                fibVerts->at( ( 3 * k ) + sidx + 2 ) );
                    double param = fibParams->at( k + fibStart->at( fidx ) );

                    // is there a sample in between the values?
                    WIntervalDouble interval( param, lastParam );
                    if( isInClosed( interval, v ) )
                    {
                        // linearly interpolate
                        double n = ( v - lastParam ) / ( param - lastParam );
                        osg::Vec3 interpolatedVert( lastVert + ( vert - lastVert ) * n );

                        filteredVerts->push_back( interpolatedVert.x() );
                        filteredVerts->push_back( interpolatedVert.y() );
                        filteredVerts->push_back( interpolatedVert.z() );

                        filteredBB.expandBy( interpolatedVert );
                    }

                    // keep track of the last value/vertex
                    lastParam = param;
                    lastVert = vert;
                }
            }

            // add colors
            for( size_t i = 0; i < filteredVerts->size() / 3; ++i )
            {
                filteredColors->push_back( m_color->get( true ).r() );
                filteredColors->push_back( m_color->get( true ).g() );
                filteredColors->push_back( m_color->get( true ).b() );
                filteredColors->push_back( m_color->get( true ).a() );
            }

            WDataSetPoints::SPtr result( new WDataSetPoints( filteredVerts, filteredColors, filteredBB ) );
            m_pointsOutput->updateData( result );
            progress->finish();
            m_progress->removeSubProgress( progress );
            debugLog() << "Done filtering. Result are " << filteredVerts->size() / 3 << " points.";
        }
        else
        {
            warnLog() << "Data does not contain the values to use for sampling.";
            m_paramHint->setHidden( false );
        }
    }
}