Commit 51bd98af authored by Alexander Wiebel's avatar Alexander Wiebel

[ADD #367] new module that (so far) allows to save triangle meshes into

VRML files.
parent 36d496be
FILE( GLOB_RECURSE MODULES_SRC "*.cpp" "*.h" )
# Grab module name and setup target directories
GET_FILENAME_COMPONENT( MODULE_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME )
SET( MODULE_TARGET_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/modules/${MODULE_NAME} )
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MODULE_TARGET_DIR} )
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MODULE_TARGET_DIR} )
SET( MODULE_DIRNAME ${MODULE_NAME} )
SET( MODULE_NAME "OWmodule_${MODULE_NAME}" ) # prefix all module names with "OWmodule_" to separate them from other libs
# Build module lib
ADD_LIBRARY( ${MODULE_NAME} SHARED ${MODULES_SRC} )
TARGET_LINK_LIBRARIES( ${MODULE_NAME} OWkernel )
# Copy local shaders to module target directory
IF( OW_COPY_SHADERS AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/shaders ) # copy shaders only if the user wants it
ADD_CUSTOM_TARGET( ${MODULE_NAME}_CopyShaders
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders ${MODULE_TARGET_DIR}/shaders/
COMMENT "Copy shaders of ${MODULE_NAME}"
)
ADD_DEPENDENCIES( ${MODULE_NAME} ${MODULE_NAME}_CopyShaders )
ENDIF()
# Build unit tests
IF( OW_COMPILE_TESTS )
# This ensures that the test is copied to the module directory
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MODULE_TARGET_DIR} )
CXXTEST_ADD_TESTS_FROM_LIST( "${MODULES_SRC}"
"OWkernel;${MODULE_NAME}"
)
# Copy fixtures if they exist
IF( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/fixtures )
ADD_CUSTOM_TARGET( ${MODULE_NAME}_CopyFixtures
# as the "test" target runs in CMakes temporary build dir, the fixtures need to be placed there too.
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test/fixtures ${CMAKE_BINARY_DIR}/modules/${MODULE_DIRNAME}/fixtures/
COMMENT "Copy fixtures of ${MODULE_NAME}"
)
ADD_DEPENDENCIES( ${MODULE_NAME} ${MODULE_NAME}_CopyFixtures )
ENDIF( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/fixtures )
ENDIF( OW_COMPILE_TESTS )
//---------------------------------------------------------------------------
//
// 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 <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include <sstream>
#include "../../common/WPropertyHelper.h"
#include "../../graphicsEngine/WTriangleMesh2.h"
#include "../../kernel/WKernel.h"
#include "../emptyIcon.xpm" // Please put a real icon here.
#include "WMExportGeometry.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( WMExportGeometry )
WMExportGeometry::WMExportGeometry():
WModule()
{
}
WMExportGeometry::~WMExportGeometry()
{
// Cleanup!
}
boost::shared_ptr< WModule > WMExportGeometry::factory() const
{
// See "src/modules/template/" for an extensively documented example.
return boost::shared_ptr< WModule >( new WMExportGeometry() );
}
const char** WMExportGeometry::getXPMIcon() const
{
return emptyIcon_xpm;
}
const std::string WMExportGeometry::getName() const
{
// Specify your module name here. This name must be UNIQUE!
return "Export Geometry";
}
const std::string WMExportGeometry::getDescription() const
{
// Specify your module description here. Be detailed. This text is read by the user.
// See "src/modules/template/" for an extensively documented example.
return "Exports triangle meshes to certain file formats. VRML implemented so far.";
}
void WMExportGeometry::connectors()
{
m_input = boost::shared_ptr< WModuleInputData < WTriangleMesh2 > >(
new WModuleInputData< WTriangleMesh2 >( shared_from_this(), "Triangle Mesh", "The mesh that will be stored." )
);
addConnector( m_input );
// call WModules initialization
WModule::connectors();
}
void WMExportGeometry::properties()
{
m_savePath = m_properties->addProperty( "Save Path", "Where to save the result", boost::filesystem::path( "/tmp/test.wrl" ) );
WPropertyHelper::PC_NOTEMPTY::addTo( m_savePath );
}
void WMExportGeometry::moduleMain()
{
m_moduleState.add( m_input->getDataChangedCondition() );
// signal ready state
ready();
// loop until the module container requests the module to quit
while( !m_shutdownFlag() )
{
if( !m_input->getData() )
{
// ok, the output has not yet sent data
// NOTE: see comment at the end of this while loop for m_moduleState
debugLog() << "Waiting for data ...";
m_moduleState.wait();
continue;
}
writeFile();
// 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();
}
}
void WMExportGeometry::writeFile()
{
boost::shared_ptr< WTriangleMesh2 > mesh = m_input->getData();
using std::fstream;
fstream out( m_savePath->get().file_string().c_str(), fstream::out | fstream::in | fstream::trunc );
if( !out || out.bad() )
{
throw WException( "Invalid file, or permission: " + m_savePath->get().file_string() );
}
std::string appearance = ""
"appearance Appearance {\n"
"material Material {\n"
"diffuseColor 0.0 1.0 1.0\n"
"}\n"
"}\n";
std::stringstream points;
for( size_t i = 0; i < mesh->vertSize(); ++i )
{
wmath::WPosition pos = mesh->getVertexAsPosition( i );
points << "" << pos[0] << " " << pos[1] << " " << pos[2];
if( i < mesh->vertSize() - 1 )
{
points << ",\n ";
}
}
std::stringstream indices;
for( size_t i = 0; i < mesh->triangleSize(); ++i )
{
indices << "" << mesh->getTriVertId0( i ) << ", ";
indices << "" << mesh->getTriVertId1( i ) << ", ";
indices << "" << mesh->getTriVertId2( i );
if( i < mesh->triangleSize() - 1 )
{
indices << ", -1,\n ";
}
}
out << "#VRML V2.0 utf8" << std::endl;
out << "# Written with OpenWalnut ( http://www.openwalnut.org )." << std::endl;
out << "Shape {" << std::endl;
out << appearance << std::endl;
out << "geometry IndexedFaceSet {" << std::endl;
out << "coord DEF SurfaceCoords Coordinate {" << std::endl;
out << "point [" << std::endl;
out << points.str() << std::endl;
out << "]" << std::endl;
out << "}" << std::endl;
out << "coordIndex [" << std::endl;
out << indices.str() << std::endl;
out << "]" << std::endl;
out << "}" << std::endl;
out << "}" << std::endl;
out.close();
debugLog() << "Data written to file.";
}
//---------------------------------------------------------------------------
//
// 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 <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMEXPORTGEOMETRY_H
#define WMEXPORTGEOMETRY_H
#include <string>
#include <osg/Geode>
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../kernel/WModuleOutputData.h"
class WTriangleMesh2;
/**
* Someone should add some documentation here.
* Probably the best person would be the module's
* creator, i.e. "wiebel".
*
* This is only an empty template for a new module. For
* an example module containing many interesting concepts
* and extensive documentation have a look at "src/modules/template"
*
* \ingroup modules
*/
class WMExportGeometry: public WModule
{
public:
/**
*
*/
WMExportGeometry();
/**
*
*/
virtual ~WMExportGeometry();
/**
* 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.
*/
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();
private:
/**
* Routine that acutally writes the file.
*/
void writeFile();
/**
* An input connector used to get mehses from other modules.
*/
boost::shared_ptr< WModuleInputData< WTriangleMesh2 > > m_input;
WPropFilename m_savePath; //!< Path where geometry should be stored
};
#endif // WMEXPORTGEOMETRY_H
ADD_SUBDIRECTORY( meshReader )
ADD_SUBDIRECTORY( writeNIfTI )
ADD_SUBDIRECTORY( writeTracts )
ADD_SUBDIRECTORY( exportGeometry )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment