From d79025c16d83c31c79bb199dccb68c69a27634f4 Mon Sep 17 00:00:00 2001 From: Sebastian Eichelbaum Date: Tue, 20 Aug 2013 16:31:07 +0200 Subject: [PATCH] [ADD] added raw exporter --- src/modules/writeRawData/WMWriteRawData.cpp | 168 ++++++++++++++++++++ src/modules/writeRawData/WMWriteRawData.h | 112 +++++++++++++ src/modules/writeRawData/WMWriteRawData.xpm | 38 +++++ 3 files changed, 318 insertions(+) create mode 100644 src/modules/writeRawData/WMWriteRawData.cpp create mode 100644 src/modules/writeRawData/WMWriteRawData.h create mode 100644 src/modules/writeRawData/WMWriteRawData.xpm diff --git a/src/modules/writeRawData/WMWriteRawData.cpp b/src/modules/writeRawData/WMWriteRawData.cpp new file mode 100644 index 000000000..dfeab1532 --- /dev/null +++ b/src/modules/writeRawData/WMWriteRawData.cpp @@ -0,0 +1,168 @@ +//--------------------------------------------------------------------------- +// +// Project: OpenWalnut ( http://www.openwalnut.org ) +// +// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-CBS +// For more information see http://www.openwalnut.org/copying +// +// This file is part of OpenWalnut. +// +// OpenWalnut is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// OpenWalnut is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with OpenWalnut. If not, see . +// +//--------------------------------------------------------------------------- + +#include +#include + +#include "core/common/WPathHelper.h" +#include "core/kernel/WKernel.h" +#include "WMWriteRawData.xpm" + +#include "WMWriteRawData.h" + +// This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here. +W_LOADABLE_MODULE( WMWriteRawData ) + +WMWriteRawData::WMWriteRawData(): + WModule() +{ +} + +WMWriteRawData::~WMWriteRawData() +{ + // Cleanup! +} + +boost::shared_ptr< WModule > WMWriteRawData::factory() const +{ + return boost::shared_ptr< WModule >( new WMWriteRawData() ); +} + +const char** WMWriteRawData::getXPMIcon() const +{ + return WMWriteRawData_xpm; // Please put a real icon here. +} +const std::string WMWriteRawData::getName() const +{ + return "Write Raw Data"; +} + +const std::string WMWriteRawData::getDescription() const +{ + return "Write scalar data defined on uniform lattices" + "in raw format, i.e., plain three-dimensional arrays of data. Data format is defined by data type of dataset (float, byte, ...)."; +} + +void WMWriteRawData::connectors() +{ + m_input = boost::shared_ptr< WModuleInputData< WDataSetScalar > >( + new WModuleInputData< WDataSetScalar >( shared_from_this(), "Data", "The data to write." ) ); + + addConnector( m_input ); + + WModule::connectors(); +} + +void WMWriteRawData::properties() +{ + m_propCondition = boost::shared_ptr< WCondition >( new WCondition() ); + m_dataFile = m_properties->addProperty( "File", "", WPathHelper::getAppPath(), m_propCondition ); + + WModule::properties(); +} + +void WMWriteRawData::requirements() +{ + // Put the code for your requirements here. See "src/modules/template/" for an extensively documented example. +} + +/** + * Visitor for discriminating the type of the first valueset. + */ +class VisitorVSet: public boost::static_visitor<> +{ +public: + /** + * Create visitor instance and convert it to the given input type + */ + explicit VisitorVSet( boost::filesystem::path fn ): + boost::static_visitor<>(), + m_filename( fn ) + { + } + + /** + * Called by boost::varying during static visiting. Creates new, converted valueset + * + * \tparam T the real integral type of the first value set. + * \param vals the first valueset currently visited. + * + * \return the result from the operation with this and the second value set + */ + template < typename T > + void operator()( const WValueSet< T >* const& vals ) const // NOLINT + { + try + { + std::ofstream file( m_filename.string().c_str(), std::ios::out | std::ofstream::binary ); + file.write( reinterpret_cast( vals->rawData() ) , sizeof( T ) * vals->rawSize() ); + file.close(); + } + catch( int e ) + { + } + } +private: + /** + * Where to write + */ + boost::filesystem::path m_filename; +}; + +void WMWriteRawData::moduleMain() +{ + m_moduleState.add( m_propCondition ); + m_moduleState.add( m_input->getDataChangedCondition() ); + ready(); + + // loop until the module container requests the module to quit + while( !m_shutdownFlag() ) + { + // this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition. + // NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... ) + m_moduleState.wait(); + if( m_shutdownFlag() ) + { + debugLog() << "hallo"; + break; + } + + // acquire data from the input connector + bool dataSetChanged = !( m_dataSet == m_input->getData() ); + m_dataSet = m_input->getData(); + if( m_dataSet && ( dataSetChanged || m_dataFile->changed() ) ) + { + debugLog() << "Writing " << m_dataFile->get().string() << "."; + boost::shared_ptr< WProgress > progress = boost::shared_ptr< WProgress >( new WProgress( "Write File", 2 ) ); + m_progress->addSubProgress( progress ); + + // call visitor for this job + VisitorVSet visitor( m_dataFile->get( true ) ); + m_dataSet->getValueSet()->applyFunction( visitor ); + + progress->finish(); + } + } +} + diff --git a/src/modules/writeRawData/WMWriteRawData.h b/src/modules/writeRawData/WMWriteRawData.h new file mode 100644 index 000000000..48e04d3fe --- /dev/null +++ b/src/modules/writeRawData/WMWriteRawData.h @@ -0,0 +1,112 @@ +//--------------------------------------------------------------------------- +// +// Project: OpenWalnut ( http://www.openwalnut.org ) +// +// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-CBS +// For more information see http://www.openwalnut.org/copying +// +// This file is part of OpenWalnut. +// +// OpenWalnut is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// OpenWalnut is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with OpenWalnut. If not, see . +// +//--------------------------------------------------------------------------- + +#ifndef WMWRITERAWDATA_H +#define WMWRITERAWDATA_H + +#include +#include + +#include + +#include "core/dataHandler/WDataSetScalar.h" +#include "core/kernel/WModule.h" +#include "core/kernel/WModuleInputData.h" +#include "core/kernel/WModuleOutputData.h" + +/** + * A module to read scalar data stored as array of raw data. + * + * \ingroup modules + */ +class WMWriteRawData: public WModule +{ +public: + /** + * Constructor of module. + */ + WMWriteRawData(); + + /** + * Destructor of module. + */ + virtual ~WMWriteRawData(); + + /** + * Gives back the name of this module. + * \return the module's name. + */ + virtual const std::string getName() const; + + /** + * Gives back a description of this module. + * \return description to module. + */ + virtual const std::string getDescription() const; + + /** + * Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it + * should never be initialized or modified in some other way. A simple new instance is required. + * + * \return the prototype used to create every module in OpenWalnut. + */ + virtual boost::shared_ptr< WModule > factory() const; + + /** + * Get the icon for this module in XPM format. + * + * \return The icon. + */ + virtual const char** getXPMIcon() const; + +protected: + /** + * Entry point after loading the module. Runs in separate thread. + */ + virtual void moduleMain(); + + /** + * Initialize the connectors this module is using. + */ + virtual void connectors(); + + /** + * Initialize the properties for this module. + */ + virtual void properties(); + + /** + * Initialize requirements for this module. + */ + virtual void requirements(); + +private: + boost::shared_ptr< WCondition > m_propCondition; //!< A condition used to notify about changes in several properties. + WPropFilename m_dataFile; //!< The data will be read from this file. + + boost::shared_ptr< WDataSetScalar > m_dataSet; //!< This data set is provided as output through the connector. + boost::shared_ptr< WModuleInputData< WDataSetScalar > > m_input; //!< Output connector provided by this module. +}; + +#endif // WMWRITERAWDATA_H diff --git a/src/modules/writeRawData/WMWriteRawData.xpm b/src/modules/writeRawData/WMWriteRawData.xpm new file mode 100644 index 000000000..99f6dd8f3 --- /dev/null +++ b/src/modules/writeRawData/WMWriteRawData.xpm @@ -0,0 +1,38 @@ +/* XPM */ +static const char * WMWriteRawData_xpm[] = { +"32 32 3 1", +" c None", +". c #000000", +"+ c #313196", +"......... ", +".......... ", +".. ... ", +".. ++++++..+++++++++++++++++++++", +".. + +...+ + + + + +", +".......... + + + + + +", +"......... + + + + + +", +".. +++..++++++++++++++++++++++++", +".. + ... + + + + + +", +".. + ... + + + + + +", +".. + +.. + + + + + +", +".. +++++...+++++++++++++++++++++", +".. + + ... + + + + +", +" + + + + + + + +", +" + + + + + + + +", +" +++++++++++++++++++++++++++++", +" + + + + + + + +", +" + + + + + + + +", +" + + + + + + + +", +" +++++++++++++++++++++++++++++", +" + + + + + + + +", +" + + + + + + + +", +" + + + + + + + +", +" +++++++++++++++++++++++++++++", +" + + + + + + + +", +" + + + + + + + +", +" + + + + + + + +", +" +++++++++++++++++++++++++++++", +" + + + + + + + +", +" + + + + + + + +", +" + + + + + + + +", +" +++++++++++++++++++++++++++++"}; -- GitLab