OpenWalnut  1.5.0dev
WMDataTypeConversion.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 <cmath>
26 #include <fstream>
27 #include <iostream>
28 #include <memory>
29 #include <stdint.h>
30 #include <string>
31 #include <vector>
32 
33 #include "WMDataTypeConversion.h"
34 #include "WMDataTypeConversion.xpm"
35 #include "core/common/WAssert.h"
36 #include "core/common/WProgress.h"
37 #include "core/common/WStringUtils.h"
38 #include "core/dataHandler/WDataHandlerEnums.h"
39 #include "core/dataHandler/WDataSetScalar.h"
40 #include "core/dataHandler/WGridRegular3D.h"
41 #include "core/kernel/WKernel.h"
42 
43 // This line is needed by the module loader to actually find your module.
44 W_LOADABLE_MODULE( WMDataTypeConversion )
45 
47  WModule()
48 {
49  // WARNING: initializing connectors inside the constructor will lead to an exception.
50  // Implement WModule::initializeConnectors instead.
51 }
52 
54 {
55  // cleanup
57 }
58 
59 std::shared_ptr< WModule > WMDataTypeConversion::factory() const
60 {
61  return std::shared_ptr< WModule >( new WMDataTypeConversion() );
62 }
63 
65 {
66  return datatypeconversion_xpm;
67 }
68 
69 const std::string WMDataTypeConversion::getName() const
70 {
71  return "Data Type Conversion";
72 }
73 
74 const std::string WMDataTypeConversion::getDescription() const
75 {
76  return "This module is intended for development use only. Modules for general use should not depend on it.<br><br> "
77  "Allows one to change the data type of the provided WDataSetSingle to another type. "
78  "E.g., double to float.";
79 }
80 
81 /**
82  * Visitor for discriminating the type of the first valueset.
83  */
84 template< class TargetType >
85 class VisitorVSet: public boost::static_visitor< std::shared_ptr< WValueSetBase > >
86 {
87 public:
88  /**
89  * Create visitor instance and convert it to the given input type
90  */
92  boost::static_visitor< result_type >()
93  {
94  }
95 
96  /**
97  * Called by boost::varying during static visiting. Creates new, converted valueset
98  *
99  * \tparam T the real integral type of the first value set.
100  * \param vals the first valueset currently visited.
101  *
102  * \return the result from the operation with this and the second value set
103  */
104  template < typename T >
105  result_type operator()( const WValueSet< T >* const& vals ) const // NOLINT
106  {
107  std::shared_ptr< std::vector< TargetType > > newVals( new std::vector< TargetType >( vals->size() ) );
108  for( size_t i = 0; i < newVals->size(); ++i )
109  {
110  ( *newVals )[i] = static_cast< TargetType >( vals->getScalar( i ) );
111  }
112 
113  std::shared_ptr< WValueSet< TargetType > > valueSet(
115  );
116 
117  return valueSet;
118  }
119 };
120 
122 {
123  // use the m_input "data changed" flag
124  m_moduleState.add( m_input->getDataChangedCondition() );
126 
127  // signal ready state
128  ready();
129 
130  // loop until the module container requests the module to quit
131  while( !m_shutdownFlag() )
132  {
133  // this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
134  // NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
136 
137  // acquire data from the input connector
138  m_dataSet = m_input->getData();
139  if( !m_dataSet )
140  {
141  // ok, the output has not yet sent data
142  // NOTE: see comment at the end of this while loop for m_moduleState
143  debugLog() << "Waiting for data ...";
144  continue;
145  }
146 
147  std::string dataTypeName = m_dataTypeSelection->get().at( 0 )->getName();
148  std::shared_ptr< WValueSetBase > valueSet;
149 
150  // different types
151  if( dataTypeName == "UINT8" )
152  {
153  VisitorVSet< uint8_t > visitor;
154  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
155  }
156  else if( dataTypeName == "UINT16" )
157  {
158  VisitorVSet< uint16_t > visitor;
159  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
160  }
161  else if( dataTypeName == "UINT32" )
162  {
163  VisitorVSet< uint32_t > visitor;
164  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
165  }
166  else if( dataTypeName == "UINT64" )
167  {
168  VisitorVSet< uint64_t > visitor;
169  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
170  }
171  else if( dataTypeName == "INT8" )
172  {
173  VisitorVSet< int8_t > visitor;
174  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
175  }
176  else if( dataTypeName == "INT16" )
177  {
178  VisitorVSet< int16_t > visitor;
179  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
180  }
181  else if( dataTypeName == "INT32" )
182  {
183  VisitorVSet< int32_t > visitor;
184  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
185  }
186  else if( dataTypeName == "INT64" )
187  {
188  VisitorVSet< int64_t > visitor;
189  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
190  }
191  else if( dataTypeName == "FLOAT" )
192  {
193  VisitorVSet< float > visitor;
194  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
195  }
196  else if( dataTypeName == "DOUBLE" )
197  {
198  VisitorVSet< double > visitor;
199  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
200  }
201  else if( dataTypeName == "FLOAT128" )
202  {
204  valueSet = m_dataSet->getValueSet()->applyFunction( visitor );
205  }
206  else
207  {
208  throw WException( "Not supported data type while reading raw data." );
209  }
210 
211  // we have the valueset -> create dataset
212  m_dataSet = std::shared_ptr<WDataSetScalar>( new WDataSetScalar( valueSet, m_dataSet->getGrid() ) );
213  m_output->updateData( m_dataSet );
214  }
215 }
216 
218 {
219  // initialize connectors
220  m_input = std::shared_ptr<WModuleInputData<WDataSetSingle> >(
221  new WModuleInputData<WDataSetSingle> ( shared_from_this(), "in",
222  "The dataset whose values' type should be converted." ) );
223 
224  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
226 
227  // initialize connectors
228  m_output = std::shared_ptr<WModuleOutputData<WDataSetSingle> >(
229  new WModuleOutputData<WDataSetSingle> ( shared_from_this(), "out",
230  "The converted dataset." ) );
231 
232  // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
234 
235  // call WModules initialization
237 }
238 
240 {
241  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
242 
243  m_dataTypeSelectionsList = std::shared_ptr< WItemSelection >( new WItemSelection() );
244  m_dataTypeSelectionsList->addItem( "DOUBLE", "" );
245  m_dataTypeSelectionsList->addItem( "FLOAT128", "" );
246  m_dataTypeSelectionsList->addItem( "FLOAT", "" );
247  m_dataTypeSelectionsList->addItem( "UINT8", "" );
248  m_dataTypeSelectionsList->addItem( "UINT16", "" );
249  m_dataTypeSelectionsList->addItem( "UINT32", "" );
250  m_dataTypeSelectionsList->addItem( "UINT64", "" );
251  m_dataTypeSelectionsList->addItem( "INT8", "" );
252  m_dataTypeSelectionsList->addItem( "INT16", "" );
253  m_dataTypeSelectionsList->addItem( "INT32", "" );
254  m_dataTypeSelectionsList->addItem( "INT64", "" );
255 
256  m_dataTypeSelection = m_properties->addProperty( "Data type", "Data type.", m_dataTypeSelectionsList->getSelectorFirst(), m_propCondition );
258 
260 }
261 
Visitor for discriminating the type of the first valueset.
VisitorVSet()
Create visitor instance and convert it to the given input type.
result_type operator()(const WValueSet< T > *const &vals) const
Called by boost::varying during static visiting.
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 class containing a list of named items.
Provides a new field with the approximately the same data, but with another data type.
WPropSelection m_dataTypeSelection
Selection.
std::shared_ptr< WModuleOutputData< WDataSetSingle > > m_output
The only output of this filter module.
std::shared_ptr< WItemSelection > m_dataTypeSelectionsList
A list of file type selection types.
virtual const std::string getName() const
Gives back the name of this module.
virtual void moduleMain()
Entry point after loading the module.
virtual void properties()
Initialize the properties for this module.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WMDataTypeConversion()
Standard constructor.
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...
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WDataSetSingle > m_dataSet
Pointer providing access to the treated data set in the whole module.
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.
std::shared_ptr< WModuleInputData< WDataSetSingle > > m_input
Input connector required by this module.
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 removeConnectors()
Removes all connectors properly.
Definition: WModule.cpp:194
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
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Base Class for all value set types.
Definition: WValueSet.h:47
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.
An object that knows an appropriate dataType flag for the typename T.