WDataCreatorLinearAscent.cpp 3.55 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
//---------------------------------------------------------------------------
//
// 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 <ctime>
#include <cstdlib>
#include <vector>

#include "core/common/WLogger.h"
#include "core/dataHandler/WDataHandlerEnums.h"
#include "core/dataHandler/WValueSet.h"

#include "WDataCreatorLinearAscent.h"

WDataCreatorLinearAscent::WDataCreatorLinearAscent():
    WObjectNDIP< WDataSetSingleCreatorInterface >( "LinearAscent", "Creates a linear increasing values starting from the bottom (x,y,z)." )
{
    m_rangeMin = m_properties->addProperty( "Range Min", "The minimum value in the data.", 0.0 );
    m_rangeMax = m_properties->addProperty( "Range Max", "The maximum value in the data.", 1.0 );
}

WDataCreatorLinearAscent::~WDataCreatorLinearAscent()
{
}

WValueSetBase::SPtr WDataCreatorLinearAscent::operator()( WProgress::SPtr progress,
                                                    WGridRegular3D::ConstSPtr grid, unsigned char order, unsigned char dimension,
                                                    dataType /*type*/ )
{
    std::srand( time( 0 ) );

    // currently, the type is fixed. This will come soon.
    typedef double ValueType;
    typedef WValueSet< ValueType > ValueSetType;

    // create some memory for the data
    boost::shared_ptr< std::vector< ValueType > > data( new std::vector< ValueType > );
    // for scalar data we need only as much space as we have voxels
    size_t valuesPerVoxel = ValueSetType::getRequiredRawSizePerVoxel( order, dimension );
    data->resize( valuesPerVoxel * grid->size() );

    // iterate the data and fill in some LinearAscent values
    for( size_t z = 0; z < grid->getNbCoordsZ(); ++z )
    {
        for( size_t y = 0; y < grid->getNbCoordsY(); ++y )
        {
            for( size_t x = 0; x < grid->getNbCoordsX(); ++x )
            {
                // each voxels might need multiple values
                for( size_t v = 0; v < valuesPerVoxel; ++v )
                {
                    double val = static_cast< double >( grid->getVoxelNum( x, y, z ) ) / grid->size();
                    data->operator[]( ( valuesPerVoxel * grid->getVoxelNum( x, y, z ) ) + v ) =
                        static_cast< ValueType >( m_rangeMin->get() + ( std::abs( m_rangeMin->get() - m_rangeMax->get() ) * val ) );
                }
            }

            // updating progress for each voxel is not needed. It is enough to update each slice
            progress->increment( grid->getNbCoordsX() );
        }
    }

    // finally, create the value set and return it
    // We have scalar data (order = 0 ) in 3D
    return ValueSetType::SPtr( new ValueSetType( order, dimension , data ) );
}