WMSubtractDataSetScalar.cpp 4.12 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
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-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 <string>

27
#include "core/kernel/WKernel.h"
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
#include "WMSubtractDataSetScalar.xpm"

#include "WMSubtractDataSetScalar.h"

// This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
W_LOADABLE_MODULE( WMSubtractDataSetScalar )

WMSubtractDataSetScalar::WMSubtractDataSetScalar():
    WModule()
{
}

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

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

const char** WMSubtractDataSetScalar::getXPMIcon() const
{
    return WMSubtractDataSetScalar_xpm;
}

const std::string WMSubtractDataSetScalar::getName() const
{
57
    return "Subtract DataSetScalar";
58 59 60 61 62 63 64 65 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
}

const std::string WMSubtractDataSetScalar::getDescription() const
{
    return "Performs voxel-wise subtraction of values.";
}

void WMSubtractDataSetScalar::connectors()
{
    m_input1 = boost::shared_ptr< WModuleInputData< WDataSetScalar > >(
                            new WModuleInputData< WDataSetScalar >( shared_from_this(),
                                "in1", "The first input dataset." )
            );

    m_input2 = boost::shared_ptr< WModuleInputData< WDataSetScalar > >(
                            new WModuleInputData< WDataSetScalar >( shared_from_this(),
                                "in2", "The second input dataset." )
            );

    m_output = boost::shared_ptr< WModuleOutputData< WDataSetScalar > >(
                            new WModuleOutputData< WDataSetScalar >( shared_from_this(),
                                "out", "The first minus the second dataset." )
            );

    addConnector( m_input1 );
    addConnector( m_input2 );
    addConnector( m_output );

    // call WModules initialization
    WModule::connectors();
}

void WMSubtractDataSetScalar::properties()
{
}

void WMSubtractDataSetScalar::moduleMain()
{
    m_moduleState.setResetable( true, true );
    m_moduleState.add( m_input1->getDataChangedCondition() );
    m_moduleState.add( m_input2->getDataChangedCondition() );

    ready();

102
    while( !m_shutdownFlag() )
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 133 134 135 136 137
    {
        debugLog() << "Waiting.";
        m_moduleState.wait();

        boost::shared_ptr< WDataSetScalar > first = m_input1->getData();
        boost::shared_ptr< WDataSetScalar > second = m_input2->getData();

        bool dataChanged = ( m_first != first ) || ( m_second != second );

        if( dataChanged && first && second )
        {
            m_first = first;
            m_second = second;

            subtract();
            m_output->updateData( m_result );
        }
    }
}

void WMSubtractDataSetScalar::subtract()
{
    if( m_first->getValueSet()->getDataType() != m_second->getValueSet()->getDataType() ||
        m_first->getGrid()->size() != m_second->getGrid()->size() )
    {
        m_result = boost::shared_ptr< WDataSetScalar >();
        return;
    }

    switch( m_first->getValueSet()->getDataType() )
    {
    case W_DT_INT16:
        subtractTyped< int16_t >();
        break;
    default:
138
        throw WException( std::string( "Unknown datatype." ) );
139 140
    }
}