OpenWalnut  1.5.0dev
WMReadRawData.h
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 #ifndef WMREADRAWDATA_H
26 #define WMREADRAWDATA_H
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <osg/Geode>
33 
34 #include "core/dataHandler/WDataSetScalar.h"
35 #include "core/kernel/WModule.h"
36 #include "core/kernel/WModuleInputData.h"
37 #include "core/kernel/WModuleOutputData.h"
38 
39 /**
40  * A module to read scalar data stored as array of raw data.
41  *
42  * \ingroup modules
43  */
44 class WMReadRawData: public WModule
45 {
46 public:
47  /**
48  * Constructor of module.
49  */
50  WMReadRawData();
51 
52  /**
53  * Destructor of module.
54  */
55  virtual ~WMReadRawData();
56 
57  /**
58  * Gives back the name of this module.
59  * \return the module's name.
60  */
61  virtual const std::string getName() const;
62 
63  /**
64  * Gives back a description of this module.
65  * \return description to module.
66  */
67  virtual const std::string getDescription() const;
68 
69  /**
70  * Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
71  * should never be initialized or modified in some other way. A simple new instance is required.
72  *
73  * \return the prototype used to create every module in OpenWalnut.
74  */
75  virtual std::shared_ptr< WModule > factory() const;
76 
77  /**
78  * Get the icon for this module in XPM format.
79  *
80  * \return The icon.
81  */
82  virtual const char** getXPMIcon() const;
83 
84 protected:
85  /**
86  * Entry point after loading the module. Runs in separate thread.
87  */
88  virtual void moduleMain();
89 
90  /**
91  * Initialize the connectors this module is using.
92  */
93  virtual void connectors();
94 
95  /**
96  * Initialize the properties for this module.
97  */
98  virtual void properties();
99 
100  /**
101  * Initialize requirements for this module.
102  */
103  virtual void requirements();
104 
105 
106 private:
107  /**
108  * Perform the reading from file an interpretation
109  *
110  * \param fileName Location of the data file.
111  *
112  * \return The read data as scalar data set.
113  */
114  std::shared_ptr< WDataSetScalar > readData( std::string fileName );
115 
116  /**
117  * Helper function to read data of the given data type.
118  *
119  * \param fileName Location of the data file.
120  *
121  * \return A vector containing the scalar data values.
122  */
123  template< class T > std::shared_ptr< std::vector< T > > readDataTyped( std::string fileName );
124 
125  std::shared_ptr< WCondition > m_propCondition; //!< A condition used to notify about changes in several properties.
126  WPropFilename m_dataFile; //!< The data will be read from this file.
127  WPropInt m_X; //!< Samples in X direction
128  WPropInt m_Y; //!< Samples in Y direction
129  WPropInt m_Z; //!< Samples in Z direction
130 
131  WPropDouble m_xScale; //!< Scaling in X direction
132  WPropDouble m_yScale; //!< Scaling in Y direction
133  WPropDouble m_zScale; //!< Scaling in Z direction
134 
135  std::shared_ptr< WItemSelection > m_dataTypeSelectionsList; //!< A list of file type selection types
136  WPropSelection m_dataTypeSelection; //!< Selection property for file types
137 
138  std::shared_ptr< WDataSetScalar > m_dataSet; //!< This data set is provided as output through the connector.
139  std::shared_ptr< WModuleOutputData< WDataSetScalar > > m_output; //!< Output connector provided by this module.
140 };
141 
142 
143 
144 template< class T >
145 std::shared_ptr< std::vector< T > > WMReadRawData::readDataTyped( std::string fileName )
146 {
147  size_t numX = m_X->get();
148  size_t numY = m_Y->get();
149  size_t numZ = m_Z->get();
150  size_t numVoxels = numX * numY * numZ;
151  std::shared_ptr< std::vector< T > > values;
152  values = std::shared_ptr< std::vector< T > >( new std::vector< T >( numVoxels ) );
153 
154  std::ifstream ifs;
155  ifs.open( fileName.c_str(), std::ifstream::in|std::ios::binary );
156  if( !ifs || ifs.bad() )
157  {
158  WLogger::getLogger()->addLogMessage( "Try load broken file '" + fileName + "'", "Read Raw Data", LL_ERROR );
159  throw std::runtime_error( "Problem during reading file. Probably file not found." );
160  }
161 
162  T *data = new T[ numVoxels ];
163  ifs.read( reinterpret_cast< char* >( data ), sizeof( T ) * numVoxels );
164  ifs.close();
165 
166  for( size_t voxelId = 0; voxelId < numVoxels; ++voxelId )
167  {
168  (*values)[voxelId] = data[voxelId];
169  }
170 
171  delete[] data;
172 
173  return values;
174 }
175 
176 #endif // WMREADRAWDATA_H
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
Definition: WLogger.cpp:84
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
Definition: WLogger.cpp:64
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.
std::shared_ptr< std::vector< T > > readDataTyped(std::string fileName)
Helper function to read data of the given data type.
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 representing a single module of OpenWalnut.
Definition: WModule.h:72