Commit 4511f655 authored by Alexander Wiebel's avatar Alexander Wiebel

[CLEAN #161] made saving meshes a separate module

parent dc7721c8
......@@ -223,13 +223,6 @@ void WMMarchingCubes::properties()
m_surfaceColor = m_properties->addProperty( "Surface color", "Description.", WColor( 0.5, 0.5, 0.5, 1.0 ) );
m_savePropGroup = m_properties->addPropertyGroup( "Save Surface", "" );
m_saveTriggerProp = m_savePropGroup->addProperty( "Do Save", "Press!",
WPVBaseTypes::PV_TRIGGER_READY );
m_saveTriggerProp->getCondition()->subscribeSignal( boost::bind( &WMMarchingCubes::save, this ) );
m_meshFile = m_savePropGroup->addProperty( "Mesh File", "", WPathHelper::getAppPath() );
WModule::properties();
}
......@@ -317,7 +310,7 @@ void WMMarchingCubes::generateSurfacePre( double isoValue )
void WMMarchingCubes::renderMesh()
{
{
// Remove the previous node in a thread save way.
// Remove the previous node in a thread safe way.
boost::unique_lock< boost::shared_mutex > lock;
lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );
......@@ -498,80 +491,6 @@ void WMMarchingCubes::notifyTextureChange()
m_textureChanged = true;
}
bool WMMarchingCubes::save() const
{
m_saveTriggerProp->set( WPVBaseTypes::PV_TRIGGER_READY, false );
if( m_triMesh->vertSize() == 0 )
{
WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 vertices.", "Marching Cubes", LL_ERROR );
return false;
}
if( m_triMesh->triangleSize() == 0 )
{
WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 triangles.", "Marching Cubes", LL_ERROR );
return false;
}
const char* c_file = m_meshFile->get().file_string().c_str();
std::ofstream dataFile( c_file, std::ios_base::binary );
if ( dataFile )
{
WLogger::getLogger()->addLogMessage( "opening file", "Marching Cubes", LL_DEBUG );
}
else
{
WLogger::getLogger()->addLogMessage( "open file failed" + m_meshFile->get().file_string() , "Marching Cubes", LL_ERROR );
return false;
}
dataFile.precision( 16 );
WLogger::getLogger()->addLogMessage( "start writing file", "Marching Cubes", LL_DEBUG );
dataFile << ( "# vtk DataFile Version 2.0\n" );
dataFile << ( "generated using OpenWalnut\n" );
dataFile << ( "ASCII\n" );
dataFile << ( "DATASET UNSTRUCTURED_GRID\n" );
wmath::WPosition point;
dataFile << "POINTS " << m_triMesh->vertSize() << " float\n";
for ( size_t i = 0; i < m_triMesh->vertSize(); ++i )
{
point = m_triMesh->getVertexAsPosition( i );
if( !( wmath::myIsfinite( point[0] ) && wmath::myIsfinite( point[1] ) && wmath::myIsfinite( point[2] ) ) )
{
WLogger::getLogger()->addLogMessage( "Will not write file from data that contains NAN or INF.", "Marching Cubes", LL_ERROR );
return false;
}
dataFile << point[0] << " " << point[1] << " " << point[2] << "\n";
}
dataFile << "CELLS " << m_triMesh->triangleSize() << " " << m_triMesh->triangleSize() * 4 << "\n";
for ( size_t i = 0; i < m_triMesh->triangleSize(); ++i )
{
dataFile << "3 " << m_triMesh->getTriVertId0( i ) << " "
<< m_triMesh->getTriVertId1( i ) << " "
<< m_triMesh->getTriVertId2( i ) << "\n";
}
dataFile << "CELL_TYPES "<< m_triMesh->triangleSize() <<"\n";
for ( size_t i = 0; i < m_triMesh->triangleSize(); ++i )
{
dataFile << "5\n";
}
dataFile << "POINT_DATA " << m_triMesh->vertSize() << "\n";
dataFile << "SCALARS scalars float\n";
dataFile << "LOOKUP_TABLE default\n";
for ( size_t i = 0; i < m_triMesh->vertSize(); ++i )
{
dataFile << "0\n";
}
dataFile.close();
WLogger::getLogger()->addLogMessage( "saving done", "Marching Cubes", LL_DEBUG );
return true;
}
void WMMarchingCubes::updateGraphicsCallback()
{
boost::unique_lock< boost::shared_mutex > lock;
......
......@@ -123,17 +123,6 @@ private:
*/
void renderMesh();
/**
* Store the mesh in legacy vtk file format.
*/
bool save() const;
/**
* Load meshes saved with WMMarchingCubes::save
* \param fileName the mesh will be loaded from this file
*/
WTriangleMesh load( std::string fileName );
/**
* Kind of a convenience function for generate surface.
* It performs the conversions of the value sets of different data types.
......@@ -151,10 +140,6 @@ private:
WPropBool m_useTextureProp; //!< Property indicating whether to use texturing with scalar data sets.
WPropColor m_surfaceColor; //!< Property determining the color for the surface if no textures are displayed
WPropGroup m_savePropGroup; //!< Property group containing properties needed for saving the mesh.
WPropTrigger m_saveTriggerProp; //!< This property triggers the actual writing,
WPropFilename m_meshFile; //!< The mesh will be written to this file.
/**
* True when textures haven changed.
*/
......
ADD_SUBDIRECTORY( exportGeometry )
ADD_SUBDIRECTORY( readMesh )
ADD_SUBDIRECTORY( writeMesh )
ADD_SUBDIRECTORY( writeNIfTI )
#ADD_SUBDIRECTORY( writeTracts )
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 )
IF(MSVC_IDE)
SET_TARGET_PROPERTIES( ${MODULE_NAME} PROPERTIES PREFIX "../")
ENDIF(MSVC_IDE)
# 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 "../../common/math/WMath.h"
#include "../../kernel/WKernel.h"
#include "../emptyIcon.xpm" // Please put a real icon here.
#include "WMWriteMesh.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( WMWriteMesh )
WMWriteMesh::WMWriteMesh():
WModule()
{
}
WMWriteMesh::~WMWriteMesh()
{
// Cleanup!
}
boost::shared_ptr< WModule > WMWriteMesh::factory() const
{
// See "src/modules/template/" for an extensively documented example.
return boost::shared_ptr< WModule >( new WMWriteMesh() );
}
const char** WMWriteMesh::getXPMIcon() const
{
return emptyIcon_xpm; // Please put a real icon here.
}
const std::string WMWriteMesh::getName() const
{
// Specify your module name here. This name must be UNIQUE!
return "WriteMesh";
}
const std::string WMWriteMesh::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 "Someone should add some documentation here. "
"Probably the best person would be the modules's creator, i.e. \"wiebel\"";
}
void WMWriteMesh::connectors()
{
m_meshInput = boost::shared_ptr< WModuleInputData < WTriangleMesh > >(
new WModuleInputData< WTriangleMesh >( shared_from_this(), "mesh", "The mesh to save" )
);
addConnector( m_meshInput );
WModule::connectors();
}
void WMWriteMesh::properties()
{
m_savePropGroup = m_properties->addPropertyGroup( "Save Surface", "" );
m_saveTriggerProp = m_savePropGroup->addProperty( "Do Save", "Press!",
WPVBaseTypes::PV_TRIGGER_READY );
m_saveTriggerProp->getCondition()->subscribeSignal( boost::bind( &WMWriteMesh::save, this ) );
m_meshFile = m_savePropGroup->addProperty( "Mesh File", "", WPathHelper::getAppPath() );
WModule::properties();
}
void WMWriteMesh::moduleMain()
{
// Put the code for your module's main functionality here.
// See "src/modules/template/" for an extensively documented example.
m_moduleState.add( m_meshInput->getDataChangedCondition() );
// signal ready state
ready();
// loop until the module container requests the module to quit
while( !m_shutdownFlag() )
{
if( !m_meshInput->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;
}
m_triMesh = m_meshInput->getData();
// 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();
}
}
bool WMWriteMesh::save() const
{
m_saveTriggerProp->set( WPVBaseTypes::PV_TRIGGER_READY, false );
if( !m_triMesh )
{
return false;
}
if( m_triMesh->vertSize() == 0 )
{
WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 vertices.", "Marching Cubes", LL_ERROR );
return false;
}
if( m_triMesh->triangleSize() == 0 )
{
WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 triangles.", "Marching Cubes", LL_ERROR );
return false;
}
const char* c_file = m_meshFile->get().file_string().c_str();
std::ofstream dataFile( c_file, std::ios_base::binary );
if ( dataFile )
{
WLogger::getLogger()->addLogMessage( "opening file", "Marching Cubes", LL_DEBUG );
}
else
{
WLogger::getLogger()->addLogMessage( "open file failed" + m_meshFile->get().file_string() , "Marching Cubes", LL_ERROR );
return false;
}
dataFile.precision( 16 );
WLogger::getLogger()->addLogMessage( "start writing file", "Marching Cubes", LL_DEBUG );
dataFile << ( "# vtk DataFile Version 2.0\n" );
dataFile << ( "generated using OpenWalnut\n" );
dataFile << ( "ASCII\n" );
dataFile << ( "DATASET UNSTRUCTURED_GRID\n" );
wmath::WPosition point;
dataFile << "POINTS " << m_triMesh->vertSize() << " float\n";
for ( size_t i = 0; i < m_triMesh->vertSize(); ++i )
{
point = m_triMesh->getVertexAsPosition( i );
if( !( wmath::myIsfinite( point[0] ) && wmath::myIsfinite( point[1] ) && wmath::myIsfinite( point[2] ) ) )
{
WLogger::getLogger()->addLogMessage( "Will not write file from data that contains NAN or INF.", "Marching Cubes", LL_ERROR );
return false;
}
dataFile << point[0] << " " << point[1] << " " << point[2] << "\n";
}
dataFile << "CELLS " << m_triMesh->triangleSize() << " " << m_triMesh->triangleSize() * 4 << "\n";
for ( size_t i = 0; i < m_triMesh->triangleSize(); ++i )
{
dataFile << "3 " << m_triMesh->getTriVertId0( i ) << " "
<< m_triMesh->getTriVertId1( i ) << " "
<< m_triMesh->getTriVertId2( i ) << "\n";
}
dataFile << "CELL_TYPES "<< m_triMesh->triangleSize() <<"\n";
for ( size_t i = 0; i < m_triMesh->triangleSize(); ++i )
{
dataFile << "5\n";
}
dataFile << "POINT_DATA " << m_triMesh->vertSize() << "\n";
dataFile << "SCALARS scalars float\n";
dataFile << "LOOKUP_TABLE default\n";
for ( size_t i = 0; i < m_triMesh->vertSize(); ++i )
{
dataFile << "0\n";
}
dataFile.close();
WLogger::getLogger()->addLogMessage( "saving done", "Write Mesh", LL_DEBUG );
return true;
}
//---------------------------------------------------------------------------
//
// 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 WMWRITEMESH_H
#define WMWRITEMESH_H
#include <string>
#include <osg/Geode>
#include "../../graphicsEngine/WTriangleMesh.h"
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../kernel/WModuleOutputData.h"
/**
* 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 WMWriteMesh: public WModule
{
/**
* Only UnitTests may be friends.
*/
friend class WMWriteMeshTest;
public:
/**
*
*/
WMWriteMesh();
/**
*
*/
virtual ~WMWriteMesh();
/**
* 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:
/**
* Store the mesh in legacy vtk file format.
*/
bool save() const;
boost::shared_ptr< WModuleInputData< WTriangleMesh > > m_meshInput; //!< Input connector for a mesh
boost::shared_ptr< WTriangleMesh > m_triMesh; //!< A pointer to the currently processed tri mesh
WPropGroup m_savePropGroup; //!< Property group containing properties needed for saving the mesh.
WPropTrigger m_saveTriggerProp; //!< This property triggers the actual writing,
WPropFilename m_meshFile; //!< The mesh will be written to this file.
};
#endif // WMWRITEMESH_H
......@@ -22,8 +22,8 @@
//
//---------------------------------------------------------------------------
#ifndef WMMARCHINGCUBES_TEST_H
#define WMMARCHINGCUBES_TEST_H
#ifndef WMWRITEMESH_TEST_H
#define WMWRITEMESH_TEST_H
#include <string>
#include <vector>
......@@ -34,15 +34,15 @@
#include "../../../common/WIOTools.h"
#include "../../../common/WLogger.h"
#include "../../../graphicsEngine/WTriangleMesh.h"
#include "../WMMarchingCubes.h"
#include "../WMWriteMesh.h"
static WLogger logger;
static bool loggerInitialized = false;
/**
* Test for WMMarchingCubes
* Test for WMWriteMesh
*/
class WMMarchingCubesTest : public CxxTest::TestSuite
class WMWriteMeshTest : public CxxTest::TestSuite
{
public:
......@@ -68,7 +68,7 @@ public:
*/
void testInstatiation()
{
TS_ASSERT_THROWS_NOTHING( WMMarchingCubes() );
TS_ASSERT_THROWS_NOTHING( WMWriteMesh() );
}
/**
......@@ -76,7 +76,7 @@ public:
*/
void testSaveZero()
{
WMMarchingCubes mc;
WMWriteMesh mc;
boost::shared_ptr< WTriangleMesh > triMesh( new WTriangleMesh( 0, 0 ) );
mc.m_triMesh = triMesh;
std::string fileName = wiotools::tempFileName();
......@@ -95,7 +95,7 @@ public:
*/
void testSaveInfinteNan()
{
WMMarchingCubes mc;
WMWriteMesh mc;
const unsigned int nbPos = 10;
boost::shared_ptr< WTriangleMesh > triMesh( new WTriangleMesh( nbPos, 3 ) );
mc.m_triMesh = triMesh;
......@@ -123,4 +123,4 @@ public:
}
};
#endif // WMMARCHINGCUBES_TEST_H
#endif // WMWRITEMESH_TEST_H
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