OpenWalnut  1.5.0dev
WMWriteField.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2017 OpenWalnut Community
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 <string>
26 
27 #include "core/kernel/WKernel.h"
28 
29 #include "WMWriteField.h"
30 #include "core/dataHandler/WDataSetScalar.h"
31 #include "core/dataHandler/WGridRegular3D.h"
32 #include "core/common/WPathHelper.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( WMWriteField )
36 
37 WMWriteField::WMWriteField():
38  WModule()
39 {
40 }
41 
42 WMWriteField::~WMWriteField()
43 {
44  // Cleanup!
45 }
46 
47 boost::shared_ptr< WModule > WMWriteField::factory() const
48 {
49  // See "src/modules/template/" for an extensively documented example.
50  return boost::shared_ptr< WModule >( new WMWriteField() );
51 }
52 
53 const std::string WMWriteField::getName() const
54 {
55  // Specify your module name here. This name must be UNIQUE!
56  return "Write Field";
57 }
58 
59 const std::string WMWriteField::getDescription() const
60 {
61  // Specify your module description here. Be detailed. This text is read by the user.
62  // See "src/modules/template/" for an extensively documented example.
63  return "This module writes a scalar field to a file. Different file formats are supported.";
64 }
65 
67 {
68  m_field = boost::shared_ptr<WModuleInputData<WDataSetScalar> >(
69  new WModuleInputData<WDataSetScalar> ( shared_from_this(), "Scalar Field",
70  "The field to be saved." ) );
72 
74 }
75 
77 {
78  m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
79  m_saveTriggerProp = m_properties->addProperty( "Do save", "Press!", WPVBaseTypes::PV_TRIGGER_READY, m_propCondition );
80  m_outputFile = m_properties->addProperty( "File", "", WPathHelper::getAppPath() );
81 
83 }
84 
86 {
87 }
88 
90 {
91  m_moduleState.add( m_field->getDataChangedCondition() );
93 
94  // signal ready state
95  ready();
96 
97  // loop until the module container requests the module to quit
98  while( !m_shutdownFlag() )
99  { // Now, the moduleState variable comes into play. The module can wait for the condition, which gets fired whenever the input receives data
100  // or an property changes. The main loop now waits until something happens.
101  debugLog() << "Waiting ...";
104 
105  // woke up since the module is requested to finish
106  if( m_shutdownFlag() )
107  {
108  break;
109  }
110 
111  saveVTKASCII();
113  }
114 
115 }
116 
118 {
119  const char* c_file = m_outputFile->get().string().c_str();
120  std::ofstream dataFile( c_file, std::ios_base::binary );
121 
122  if( dataFile )
123  {
124  WLogger::getLogger()->addLogMessage( "opening file", "Write Field", LL_DEBUG );
125  }
126  else
127  {
128  WLogger::getLogger()->addLogMessage( "open file failed" + m_outputFile->get().string() , "Write Field", LL_ERROR );
129  return false;
130  }
131 
132  dataFile.precision( 16 );
133 
134  WLogger::getLogger()->addLogMessage( "start writing file", "Write Field", LL_DEBUG );
135  dataFile << ( "# vtk DataFile Version 2.0\n" );
136  dataFile << ( "Generated using OpenWalnut\n" );
137  dataFile << ( "ASCII\n" );
138  dataFile << ( "DATASET STRUCTURED_POINTS\n" );
139 
140 
141  boost::shared_ptr< WDataSetScalar > m_dataSet = m_field->getData();
142  boost::shared_ptr<WGridRegular3D> grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_dataSet->getGrid() );
143  WAssert( grid, "Grid is not of type WGridRegular3D." );
144 
145  size_t x_dim = grid->getNbCoordsX();
146  size_t y_dim = grid->getNbCoordsY();
147  size_t z_dim = grid->getNbCoordsZ();
148  dataFile << "DIMENSIONS " << x_dim << " "
149  << y_dim << " "
150  << z_dim << "\n";
151 
152  size_t x_origin = grid->getOrigin()[0];
153  size_t y_origin = grid->getOrigin()[1];
154  size_t z_origin = grid->getOrigin()[2];
155  dataFile << "ORIGIN " << x_origin << " "
156  << y_origin << " "
157  << z_origin << "\n";
158 
159  size_t x_asp = grid->getOffsetX();
160  size_t y_asp = grid->getOffsetY();
161  size_t z_asp = grid->getOffsetZ();
162  dataFile << "SPACING " << x_asp << " "
163  << y_asp << " "
164  << z_asp << "\n";
165 
166  size_t nbPoints = x_dim * y_dim * z_dim;
167  dataFile << "POINT_DATA " << nbPoints << "\n";
168  dataFile << "SCALARS fieldValues float\n";
169  dataFile << "LOOKUP_TABLE default\n";
170 
171 
172  boost::shared_ptr< WValueSetBase > valueSet( m_dataSet->getValueSet() );
173  for( size_t id = 0; id < nbPoints; ++id )
174  {
175  dataFile << valueSet->getScalarDouble( id ) << " ";
176  }
177 
178  dataFile << "\n";
179 
180  dataFile.close();
181  WLogger::getLogger()->addLogMessage( "Saving done.", "Write Field", LL_DEBUG );
182  return true;
183 }
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
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
Someone should add some documentation here.
Definition: WMWriteField.h:50
virtual void moduleMain()
Entry point after loading the module.
boost::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
Definition: WMWriteField.h:113
virtual void connectors()
Initialize the connectors this module is using.
virtual boost::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
boost::shared_ptr< WModuleInputData< WDataSetScalar > > m_field
Input connector required by this module.
Definition: WMWriteField.h:112
bool saveVTKASCII() const
Store the field in legacy vtk file format.
virtual void requirements()
Initialize requirements for this module.
virtual void properties()
Initialize the properties for this module.
WPropTrigger m_saveTriggerProp
This property triggers the actual writing,.
Definition: WMWriteField.h:114
WPropFilename m_outputFile
The field will be written to this file.
Definition: WMWriteField.h:115
virtual const std::string getName() const
Gives back the name of this module.
virtual const std::string getDescription() const
Gives back a description of this 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
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)