OpenWalnut  1.5.0dev
WMReadRawData.cpp
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 <memory>
27 #include <string>
28 
29 #include "WMReadRawData.h"
30 #include "WMReadRawData.xpm"
31 #include "core/common/WPathHelper.h"
32 #include "core/kernel/WKernel.h"
33 
34 // This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
35 W_LOADABLE_MODULE( WMReadRawData )
36 
38  WModule()
39 {
40 }
41 
43 {
44  // Cleanup!
45 }
46 
47 std::shared_ptr< WModule > WMReadRawData::factory() const
48 {
49  return std::shared_ptr< WModule >( new WMReadRawData() );
50 }
51 
52 const char** WMReadRawData::getXPMIcon() const
53 {
54  return WMReadRawData_xpm; // Please put a real icon here.
55 }
56 const std::string WMReadRawData::getName() const
57 {
58  return "Read Raw Data";
59 }
60 
61 const std::string WMReadRawData::getDescription() const
62 {
63  return "Read scalar data defined on uniform lattices"
64  "in raw format, i.e., plain three-dimensional arrays of data.";
65 }
66 
68 {
69  m_output = std::shared_ptr< WModuleOutputData< WDataSetScalar > >(
70  new WModuleOutputData< WDataSetScalar >( shared_from_this(), "Data", "The loaded data." ) );
71 
73 
75 }
76 
78 {
79  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
80  m_dataFile = m_properties->addProperty( "File", "", WPathHelper::getAppPath(), m_propCondition );
82 
83  m_dataTypeSelectionsList = std::shared_ptr< WItemSelection >( new WItemSelection() );
84  m_dataTypeSelectionsList->addItem( "UINT8", "" );
85  m_dataTypeSelectionsList->addItem( "UINT16", "" );
86  m_dataTypeSelectionsList->addItem( "UINT32", "" );
87  m_dataTypeSelectionsList->addItem( "UINT64", "" );
88  m_dataTypeSelectionsList->addItem( "INT8", "" );
89  m_dataTypeSelectionsList->addItem( "INT16", "" );
90  m_dataTypeSelectionsList->addItem( "INT32", "" );
91  m_dataTypeSelectionsList->addItem( "INT64", "" );
92  m_dataTypeSelectionsList->addItem( "FLOAT", "" );
93  m_dataTypeSelectionsList->addItem( "DOUBLE", "" );
94  m_dataTypeSelectionsList->addItem( "FLOAT128", "" );
95 
96  m_dataTypeSelection = m_properties->addProperty( "Data type", "Data type.", m_dataTypeSelectionsList->getSelectorFirst(), m_propCondition );
98 
99  m_X = m_properties->addProperty( "X", "Data sample in X direction.", 256, m_propCondition );
100  m_X->setMin( 0 );
101  m_X->setMax( 4096 );
102  m_Y = m_properties->addProperty( "Y", "Data sample in Y direction.", 256, m_propCondition );
103  m_Y->setMin( 0 );
104  m_Y->setMax( 4096 );
105  m_Z = m_properties->addProperty( "Z", "Data sample in Z direction.", 256, m_propCondition );
106  m_Z->setMin( 0 );
107  m_Z->setMax( 4096 );
108 
109  m_xScale = m_properties->addProperty( "X Scale", "Data scaling in X direction.", 1.0, m_propCondition );
110  m_xScale->setMin( 0 );
111  m_yScale = m_properties->addProperty( "Y Scale", "Data scaling in Y direction.", 1.0, m_propCondition );
112  m_yScale->setMin( 0 );
113  m_zScale = m_properties->addProperty( "Z Scale", "Data scaling in Z direction.", 1.0, m_propCondition );
114  m_zScale->setMin( 0 );
115 
117 }
118 
120 {
121  // Put the code for your requirements here. See "src/modules/template/" for an extensively documented example.
122 }
123 
125 {
127  ready();
128  while( !m_shutdownFlag() )
129  {
131 
132  if( m_shutdownFlag() )
133  {
134  break;
135  }
136 
137  debugLog() << "Loading " << m_dataFile->get().string() << ".";
138  std::shared_ptr< WProgress > progress( new WProgress( "Read Raw Data", 2 ) );
139  ++*progress;
140  m_dataSet = readData( m_dataFile->get().string() );
141  ++*progress;
142  m_output->updateData( m_dataSet );
143  progress->finish();
144  }
145 }
146 
147 
148 std::shared_ptr< WDataSetScalar > WMReadRawData::readData( std::string fileName )
149 {
150  std::string dataTypeName = m_dataTypeSelection->get().at( 0 )->getName();
151  std::shared_ptr< WValueSetBase > newValueSet;
152  if( dataTypeName == "UINT8" )
153  {
154  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< uint8_t >( 0, 1, readDataTyped< uint8_t >( fileName ), W_DT_UINT8 ) );
155  }
156  else if( dataTypeName == "UINT16" )
157  {
158  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< uint16_t >( 0, 1, readDataTyped< uint16_t >( fileName ), W_DT_UINT16 ) );
159  }
160  else if( dataTypeName == "UINT32" )
161  {
162  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< uint32_t >( 0, 1, readDataTyped< uint32_t >( fileName ), W_DT_UINT32 ) );
163  }
164  else if( dataTypeName == "UINT64" )
165  {
166  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< uint64_t >( 0, 1, readDataTyped< uint64_t >( fileName ), W_DT_UINT64 ) );
167  }
168  else if( dataTypeName == "INT8" )
169  {
170  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< int8_t >( 0, 1, readDataTyped< int8_t >( fileName ), W_DT_INT8 ) );
171  }
172  else if( dataTypeName == "INT16" )
173  {
174  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< int16_t >( 0, 1, readDataTyped< int16_t >( fileName ), W_DT_INT16 ) );
175  }
176  else if( dataTypeName == "INT32" )
177  {
178  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< int32_t >( 0, 1, readDataTyped< int32_t >( fileName ), W_DT_SIGNED_INT ) );
179  }
180  else if( dataTypeName == "INT64" )
181  {
182  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< int64_t >( 0, 1, readDataTyped< int64_t >( fileName ), W_DT_INT64 ) );
183  }
184  else if( dataTypeName == "FLOAT" )
185  {
186  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< float >( 0, 1, readDataTyped< float >( fileName ), W_DT_FLOAT ) );
187  }
188  else if( dataTypeName == "DOUBLE" )
189  {
190  newValueSet = std::shared_ptr< WValueSetBase >( new WValueSet< double >( 0, 1, readDataTyped< double >( fileName ), W_DT_DOUBLE ) );
191  }
192  else if( dataTypeName == "FLOAT128" )
193  {
194  newValueSet =
195  std::shared_ptr< WValueSetBase >( new WValueSet< long double >( 0, 1, readDataTyped< long double >( fileName ), W_DT_FLOAT128 ) );
196  }
197  else
198  {
199  throw WException( "Not supported data type while reading raw data." );
200  }
201 
202  std::shared_ptr< WGridRegular3D > newGrid;
203  size_t numX = m_X->get();
204  size_t numY = m_Y->get();
205  size_t numZ = m_Z->get();
206  newGrid = std::shared_ptr< WGridRegular3D >( new WGridRegular3D( numX, numY, numZ, m_xScale->get(), m_yScale->get(), m_zScale->get() ) );
207 
208  std::shared_ptr< WDataSetScalar > newDataSet;
209  newDataSet = std::shared_ptr< WDataSetScalar >( new WDataSetScalar( newValueSet, newGrid ) );
210 
211  return newDataSet;
212 }
virtual void wait() const
Wait for the condition.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
This data set type contains scalars as values.
Basic exception handler.
Definition: WException.h:39
A grid that has parallelepiped cells which all have the same proportion.
A class containing a list of named items.
A module to read scalar data stored as array of raw data.
Definition: WMReadRawData.h:45
WPropSelection m_dataTypeSelection
Selection property for file types.
WPropDouble m_yScale
Scaling in Y direction.
WMReadRawData()
Constructor of module.
virtual const std::string getDescription() const
Gives back a description of this module.
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
WPropDouble m_zScale
Scaling in Z direction.
WPropFilename m_dataFile
The data will be read from this file.
WPropInt m_Y
Samples in Y direction.
virtual void connectors()
Initialize the connectors this module is using.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
WPropDouble m_xScale
Scaling in X direction.
std::shared_ptr< WItemSelection > m_dataTypeSelectionsList
A list of file type selection types.
std::shared_ptr< WDataSetScalar > readData(std::string fileName)
Perform the reading from file an interpretation.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
std::shared_ptr< WDataSetScalar > m_dataSet
This data set is provided as output through the connector.
WPropInt m_Z
Samples in Z direction.
virtual void requirements()
Initialize requirements for this module.
virtual ~WMReadRawData()
Destructor of module.
WPropInt m_X
Samples in X direction.
std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output
Output connector provided by this module.
virtual const std::string getName() const
Gives back the name of this module.
virtual void properties()
Initialize the properties for this module.
virtual void moduleMain()
Entry point after loading the module.
Class offering an instantiate-able data connection between modules.
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
Definition: WModule.cpp:575
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
Definition: WModule.cpp:108
std::shared_ptr< WProperties > m_properties
The property object for the module.
Definition: WModule.h:640
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
Definition: WPathHelper.cpp:93
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base Class for all value set types.
Definition: WValueSet.h:47
void addTo(WPropFilename prop)
Add the PC_PATHEXISTS constraint to the property.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.