OpenWalnut  1.5.0dev
WMWriteTransferFunction.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 // C headers
26 // ...
27 
28 // C++ headers
29 #include <fstream>
30 #include <iostream>
31 #include <memory>
32 #include <string>
33 
34 
35 #include "WMWriteTransferFunction.h"
36 #include "WMWriteTransferFunction.xpm"
37 #include "core/common/WAssert.h"
38 #include "core/common/WPathHelper.h"
39 #include "core/common/WPropertyHelper.h"
40 #include "core/common/WStringUtils.h"
41 
42 W_LOADABLE_MODULE( WMWriteTransferFunction )
43 
45  WModule()
46 {
47 }
48 
50 {
51  // cleanup
52 }
53 
54 std::shared_ptr< WModule > WMWriteTransferFunction::factory() const
55 {
56  // See "src/modules/template/" for an extensively documented example.
57  return std::shared_ptr< WModule >( new WMWriteTransferFunction() );
58 }
59 
61 {
62  return WMWriteTransferFunction_xpm;
63 }
64 
65 const std::string WMWriteTransferFunction::getName() const
66 {
67  return "Write Transfer Function";
68 }
69 
70 const std::string WMWriteTransferFunction::getDescription() const
71 {
72  // Specify your module description here. Be detailed. This text is read by the user.
73 return "Allows one to export transfer functions in several formats.";
74 }
75 
77 {
78  // an output connector for the transfer function created
79  m_input = WModuleInputData < WDataSetSingle >::createAndAdd( shared_from_this(), "transferFunction1D", "The transfer function" );
80 
81  // call WModule's initialization
83 }
84 
86 {
87  m_propCondition = std::shared_ptr< WCondition >( new WCondition() );
88 
89  m_savePath = m_properties->addProperty( "Save Location", "Set the path to the file.", WPathHelper::getAppPath() / "tf.txt" );
90  m_saveAsRaw = m_properties->addProperty( "Save binary", "Save file in RAW binary format.", false, m_propCondition );
91  m_saveTrigger = m_properties->addProperty( "Save", "Save to file.", WPVBaseTypes::PV_TRIGGER_READY, m_propCondition );
92 
93  // Call parent method
95 }
96 
98 {
99  m_moduleState.setResetable( true, true );
102 
103  ready();
104 
105  // lets go
106  while( !m_shutdownFlag() )
107  {
110 
111  if( m_shutdownFlag() )
112  {
113  break;
114  }
115 
117  {
119  // valid data?
120  if( !d )
121  {
122  warnLog() << "No data available to save.";
123  continue;
124  }
125 
126  // get value set and write it
127  std::shared_ptr< WValueSetBase > vsb = d->getValueSet();
128  if( ( vsb->order() != 1 ) || ( vsb->dimension() != 4 ) || ( vsb->getDataType() != W_DT_UNSIGNED_CHAR ) )
129  {
130  errorLog() << "This is not a proper TF. Abort.";
131  continue;
132  }
133 
134  // we ensured that the valuesetbase is this valueset:
135  std::shared_ptr< WValueSet< unsigned char > > vs = std::dynamic_pointer_cast< WValueSet< unsigned char > >( vsb );
136 
137  // valid path?
138  boost::filesystem::path p = m_savePath->get( true );
139 
140  debugLog() << "Save TF to \"" + p.string() + "\".";
141 
142  if( !m_saveAsRaw->get( true ) )
143  {
144  std::ofstream f;
145  f.open( p.string().c_str() );
146  if( !f.good() )
147  {
148  errorLog() << "Failed to open file. Abort.";
149  continue;
150  }
151 
152  f << "# Exported using OpenWalnut. http://www.openwalnut.org" << std::endl;
153  f << "# TF export format:" << std::endl <<
154  "# Comments begin with #" << std::endl <<
155  "# 1st line: width height" << std::endl <<
156  "# Then, RGBA quadruples; line-wise; values space separated; in x direction first." << std::endl;
157 
158  // later, we might support 2d TFs. Now, write 1 as second dimension
159  f << string_utils::toString( vs->size() ) << " " << "1" << std::endl;
160  // go through each RGBA vector
161  for( size_t i = 0; i < vs->size(); ++i )
162  {
163  f << string_utils::toString( vs->getScalar( i * 4 + 0 ) ) << " "
164  << string_utils::toString( vs->getScalar( i * 4 + 1 ) ) << " "
165  << string_utils::toString( vs->getScalar( i * 4 + 2 ) ) << " "
166  << string_utils::toString( vs->getScalar( i * 4 + 3 ) ) << std::endl;
167  }
168 
169  // done.
170  f.close();
171  }
172  else
173  {
174  // write header file separately
175  std::ofstream fhead;
176  fhead.open( ( p.string() + ".header" ).c_str() );
177  if( !fhead.good() )
178  {
179  errorLog() << "Failed to write header file " << ( p.string() + ".header" ).c_str() << ". Abort.";
180  continue;
181  }
182 
183  fhead << "# Exported using OpenWalnut. http://www.openwalnut.org" << std::endl;
184  fhead << "# TF export format:" << std::endl <<
185  "# Comments begin with #" << std::endl <<
186  "# 1st line: width height" << std::endl <<
187  "# 2nd line: filename to binary file, relative to header" << std::endl <<
188  "# The RGBA quadruples are stored in binary format as four unsigned char values per quadruples." << std::endl;
189 
190  // later, we might support 2d TFs. Now, write 1 as second dimension
191  fhead << string_utils::toString( vs->size() ) << " " << "1" << std::endl;
192  fhead << p.filename().string().c_str() << std::endl;
193  // go through each RGBA vector
194 
195  fhead.close();
196 
197  // write data file.
198  std::ofstream f;
199  f.open( p.string().c_str(), std::ios::out | std::ios::binary );
200  if( !f.good() )
201  {
202  errorLog() << "Failed to open file. Abort.";
203  continue;
204  }
205 
206  f.write( reinterpret_cast< const char*>( vs->rawData() ), vs->rawSize() );
207 
208  // done.
209  f.close();
210  }
211  // done
213  }
214  }
215 }
216 
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
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
std::shared_ptr< WDataSetSingle > SPtr
Convenience typedef for a std::shared_ptr.
Module to export transferfunctions.
WPropTrigger m_saveTrigger
DO the save operation.
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.
WPropFilename m_savePath
Where to save the file to.
WModuleInputData< WDataSetSingle >::SPtr m_input
The connector used to get the TF.
virtual ~WMWriteTransferFunction()
Destructor.
virtual void moduleMain()
Entry point after loading the module.
virtual const std::string getName() const
Gives back the name of this module.
const char ** getXPMIcon() const
Get the icon for this module in XPM format.
virtual void properties()
Initialize the properties for this module.
WPropBool m_saveAsRaw
Allows saving as raw data file instead of ascii.
virtual void connectors()
Initialize the connectors this module is using.
std::shared_ptr< WCondition > m_propCondition
Condition used throughout the module to notify the thread if some changes happened (like properties h...
std::shared_ptr< WCondition > getDataChangedCondition()
Gets the condition variable that gets fired whenever new data has been sent.
Class offering an instantiate-able data connection between modules.
const std::shared_ptr< T > getData(bool reset=true)
Gives the currently set data and resets the update flag.
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
wlog::WStreamedLogger warnLog() const
Logger instance for comfortable warning- logs.
Definition: WModule.cpp:580
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
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
Definition: WModule.cpp:570
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_TRIGGERED
Trigger property: got triggered.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)
std::string toString(const T &value)
Convert a given value to a string.
Definition: WStringUtils.h:120