WMAtlasCreator.cpp 6.62 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
//---------------------------------------------------------------------------
//
// 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>
26
#include <vector>
27 28 29 30 31 32 33

#include <boost/filesystem.hpp>

#include <osg/Image>
#include <osg/Texture3D>
#include <osgDB/ReadFile>

34
#include "core/common/WStringUtils.h"
35
#include "core/kernel/WKernel.h"
36

37
#include "WMAtlasCreator.h"
38
#include "WMAtlasCreator.xpm"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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

WMAtlasCreator::WMAtlasCreator():
    WModule()
{
}

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

boost::shared_ptr< WModule > WMAtlasCreator::factory() const
{
    // See "src/modules/template/" for an extensively documented example.
    return boost::shared_ptr< WModule >( new WMAtlasCreator() );
}

const char** WMAtlasCreator::getXPMIcon() const
{
Alexander Wiebel's avatar
Alexander Wiebel committed
61
    return WMAtlasCreator_xpm; // Please put a real icon here.
62 63 64
}
const std::string WMAtlasCreator::getName() const
{
Alexander Wiebel's avatar
Alexander Wiebel committed
65
    return "Atlas Creator";
66 67 68 69 70 71
}

const std::string WMAtlasCreator::getDescription() const
{
    // Specify your module description here. Be detailed. This text is read by the user.
    // See "src/modules/template/" for an extensively documented example.
72
    return "No documentation yet.";
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
}

void WMAtlasCreator::connectors()
{
    m_output = boost::shared_ptr< WModuleOutputData < WDataSetScalar  > >(
        new WModuleOutputData< WDataSetScalar >( shared_from_this(), "out", "The created image." ) );
    addConnector( m_output );

    WModule::connectors();
}

void WMAtlasCreator::properties()
{
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
    // Put the code for your properties here. See "src/modules/template/" for an extensively documented example.

    //m_propMetaFile = m_properties->addProperty( "Tree file", "", WPathHelper::getAppPath() );
    m_propDirectory = m_properties->addProperty( "Directoy", "", boost::filesystem::path( "/SCR/ratte/png8/Amyg034.png" ) );

    m_propReadTrigger = m_properties->addProperty( "Do read",  "Press!", WPVBaseTypes::PV_TRIGGER_READY, m_propCondition );

    WModule::properties();
}

void WMAtlasCreator::moduleMain()
{
    m_moduleState.setResetable( true, true );
    m_moduleState.add( m_propCondition );
    m_moduleState.add( m_active->getUpdateCondition() );
    ready();

    debugLog() << m_volume.size();

106
    while( !m_shutdownFlag() )
107 108 109
    {
        m_moduleState.wait();

110
        if( m_shutdownFlag() )
111 112 113 114
        {
            break;
        }

115
        if( m_propReadTrigger->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        {
            boost::filesystem::path fileName = m_propDirectory->get();
            m_propReadTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, true );

            loadPngs( fileName );

            updateOutDataset();
        }
    }
}

bool WMAtlasCreator::loadPngs( boost::filesystem::path sliceFile )
{
    using namespace boost::filesystem; //NOLINT

131
    if( !exists( sliceFile ) )
132 133 134 135 136
    {
        return false;
    }

    osg::Image* osgImage;
137
    osgImage = osgDB::readImageFile( sliceFile.string().c_str() );
138 139 140 141 142 143 144 145 146

    debugLog() << osgImage->r() << " : " << osgImage->s() << " : " << osgImage->t();

    m_xDim = osgImage->s();
    m_yDim = 60;
    m_zDim = osgImage->t();


    path dirPath( sliceFile );
147
    dirPath.remove_filename().string();
148 149 150 151

    m_volume.resize( m_xDim * m_yDim * m_zDim, 0 );

    directory_iterator end_itr; // default construction yields past-the-end
152
    for( directory_iterator itr( dirPath ); itr != end_itr; ++itr )
153
    {
154
        //debugLog() << itr->path().string().c_str();
155 156
        path p( itr->path() );

157
        if( p.extension() == ".png" )
158 159 160 161 162
        {
            addPngToVolume( p );
        }
    }

163
    for( size_t i = 0; i < m_regions.size(); ++i )
164 165 166 167 168 169 170 171 172 173 174
    {
        debugLog() << i << ": " << m_regions[i];
    }

    return true;
}

void WMAtlasCreator::addPngToVolume( boost::filesystem::path image )
{
    using namespace boost::filesystem; //NOLINT

175
    std::string fn = image.filename().string();
176 177 178 179
    fn.erase( fn.size() - 4, 4 );
    std::string number = fn.substr( fn.size() - 3 );
    fn.erase( fn.size() - 3, 3 );

180
    size_t pos = string_utils::fromString<size_t>( number );
181

182
    if( pos > 119 )
183 184 185 186 187 188 189
    {
        return;
    }


    uint8_t id = 0;
    bool found = false;
190
    for( size_t i = 0; i < m_regions.size(); ++i )
191
    {
192
        if( m_regions[i] == fn )
193 194 195 196 197
        {
            id = static_cast<uint8_t>( i );
            found = true;
        }
    }
198
    if( !found )
199 200 201 202 203 204
    {
        m_regions.push_back( fn );
        id = static_cast<uint8_t>( m_regions.size() - 1 );
    }

    osg::Image* osgImage;
205
    osgImage = osgDB::readImageFile( image.string().c_str() );
206 207 208 209 210
    unsigned char* data = osgImage->data();

    pos /= 2;
    ++id;

211
    for( int i = 0; i < m_zDim; ++i )
212
    {
213
        for( int k = 0; k < m_xDim; ++k )
214
        {
215
            if( data[k * 3 + ( i * m_xDim * 3 )] != 0 )
216 217 218 219 220 221 222 223 224 225
            {
                m_volume[ pos * m_xDim + ( i * m_xDim * m_yDim ) + k ] = id;
            }
        }
    }
}

void WMAtlasCreator::updateOutDataset()
{
    boost::shared_ptr< WValueSet< unsigned char > > vs =
226 227
        boost::shared_ptr< WValueSet< unsigned char > >( new WValueSet< unsigned char >(
        0, 1, boost::shared_ptr< std::vector< uint8_t > >( new std::vector< uint8_t >( m_volume ) ), W_DT_UINT8 ) );
228

229
    WMatrix< double > mat( 4, 4 );
230 231 232 233
    mat.makeIdentity();
    mat( 0, 0 ) = 0.1;
    mat( 2, 2 ) = 0.1;

234
    boost::shared_ptr<WGridRegular3D> grid = boost::shared_ptr<WGridRegular3D>( new WGridRegular3D( m_xDim, m_yDim, m_zDim,
235
                WGridTransformOrtho( mat ) ) );
236 237 238 239

    boost::shared_ptr< WDataSetScalar >outData = boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
    m_output->updateData( outData );
}