~ubuntu-branches/ubuntu/vivid/openwalnut/vivid-proposed

« back to all changes in this revision

Viewing changes to src/modules/readVIM/WMReadVIM.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2014-03-19 17:46:12 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140319174612-e4mgtr1avbq3f7ph
Tags: 1.4.0~rc1+hg3a3147463ee2-1
* Major functionality and stability improvements.
* Several bug fixes
* Changed ttf-liberation dependency to fonts-liberation (Closes: #722405)
* OpenWalnut now works properly with OpenSceneGraph 3.2 (Closes: #718381)
* See http://www.openwalnut.org/versions/2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <fstream>
 
26
#include <string>
 
27
#include <vector>
 
28
 
 
29
#include "core/kernel/WKernel.h"
 
30
#include "core/common/WPathHelper.h"
 
31
#include "core/common/WStringUtils.h"
 
32
 
 
33
#include "WMReadVIM.h"
 
34
 
 
35
// 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.
 
36
W_LOADABLE_MODULE( WMReadVIM )
 
37
 
 
38
WMReadVIM::WMReadVIM():
 
39
    WModule()
 
40
{
 
41
    // Init
 
42
}
 
43
 
 
44
WMReadVIM::~WMReadVIM()
 
45
{
 
46
    // Cleanup!
 
47
}
 
48
 
 
49
boost::shared_ptr< WModule > WMReadVIM::factory() const
 
50
{
 
51
    return boost::shared_ptr< WModule >( new WMReadVIM() );
 
52
}
 
53
 
 
54
const std::string WMReadVIM::getName() const
 
55
{
 
56
    // Specify your module name here. This name must be UNIQUE!
 
57
    return "Read VIM";
 
58
}
 
59
 
 
60
const std::string WMReadVIM::getDescription() const
 
61
{
 
62
    // Specify your module description here. Be detailed. This text is read by the user.
 
63
    return "This module reads VIM files containing particle data.";
 
64
}
 
65
 
 
66
void WMReadVIM::connectors()
 
67
{
 
68
    m_output = WModuleOutputData < WDataSetPoints  >::createAndAdd( shared_from_this(), "out", "The loaded dataset" );
 
69
 
 
70
    // call WModule's initialization
 
71
    WModule::connectors();
 
72
}
 
73
 
 
74
void WMReadVIM::properties()
 
75
{
 
76
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
 
77
 
 
78
    m_filename = m_properties->addProperty( "VIM file", "The VIM file to load", WPathHelper::getAppPath() );
 
79
    WPropertyHelper::PC_PATHEXISTS::addTo( m_filename );
 
80
 
 
81
    m_aTrigger = m_properties->addProperty( "Read", "Read file.", WPVBaseTypes::PV_TRIGGER_READY,
 
82
                                            m_propCondition );
 
83
 
 
84
    WModule::properties();
 
85
}
 
86
 
 
87
void WMReadVIM::moduleMain()
 
88
{
 
89
    m_moduleState.setResetable( true, true );
 
90
    m_moduleState.add( m_propCondition );
 
91
 
 
92
    // Signal ready state. Now your module can be connected by the container, which owns the module.
 
93
    ready();
 
94
    waitRestored();
 
95
 
 
96
    // main loop
 
97
    while( !m_shutdownFlag() )
 
98
    {
 
99
        m_moduleState.wait();
 
100
 
 
101
        // woke up since the module is requested to finish
 
102
        if( m_shutdownFlag() )
 
103
        {
 
104
            break;
 
105
        }
 
106
 
 
107
        if( m_aTrigger->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
 
108
        {
 
109
            // open file
 
110
            boost::filesystem::path p = m_filename->get();
 
111
 
 
112
            std::ifstream ifs;
 
113
            ifs.open( p.string().c_str(), std::ifstream::in );
 
114
            if( !ifs || ifs.bad() )
 
115
            {
 
116
                errorLog() << "Could not open file \"" << p.string() << "\".";
 
117
                continue;
 
118
            }
 
119
 
 
120
            boost::shared_ptr< WProgress > progress1( new WProgress( "Loading" ) );
 
121
            m_progress->addSubProgress( progress1 );
 
122
 
 
123
            // target memory
 
124
            WDataSetPoints::VertexArray vertices( new WDataSetPoints::VertexArray::element_type() );
 
125
            WDataSetPoints::ColorArray colors( new WDataSetPoints::ColorArray::element_type() );
 
126
            WBoundingBox bb;
 
127
 
 
128
            infoLog() << "Start Loading ...";
 
129
 
 
130
            // interpret file
 
131
            std::string line;
 
132
            std::vector< std::string > tokens;
 
133
            size_t numPoints = 0;
 
134
            while( !ifs.eof() )
 
135
            {
 
136
                std::getline( ifs, line );
 
137
                tokens = string_utils::tokenize( line );
 
138
 
 
139
                if( ( tokens.size() == 9 ) && ( tokens[ 0 ] == "!" ) )    // mathc
 
140
                {
 
141
                    // coordinate:
 
142
                    WVector3f coord(
 
143
                        string_utils::fromString< float >( tokens[2] ),
 
144
                        string_utils::fromString< float >( tokens[3] ),
 
145
                        string_utils::fromString< float >( tokens[4] )
 
146
                    );
 
147
 
 
148
                    // expand bb
 
149
                    bb.expandBy( coord );
 
150
 
 
151
                    // read 3rd to 5th number
 
152
                    vertices->push_back( coord.x() );
 
153
                    vertices->push_back( coord.y() );
 
154
                    vertices->push_back( coord.z() );
 
155
                    colors->push_back( 1.0 );
 
156
                    colors->push_back( 1.0 );
 
157
                    colors->push_back( 1.0 );
 
158
 
 
159
                    numPoints++;
 
160
                }
 
161
            }
 
162
 
 
163
            infoLog() << "Loaded " << numPoints << " points from file. Done.";
 
164
 
 
165
            // finally provide output data
 
166
            boost::shared_ptr< WDataSetPoints> newOutput( new WDataSetPoints( vertices, colors, bb ) );
 
167
            m_output->updateData( newOutput );
 
168
 
 
169
            // done. close file and report finish
 
170
            progress1->finish();
 
171
            ifs.close();
 
172
            m_aTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, false );
 
173
        }
 
174
    }
 
175
}
 
176