Commit 14a8f1ca authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD #284] saving of isosurface possible

parent 82161b79
......@@ -205,7 +205,14 @@ void WMMarchingCubes::properties()
m_useTextureProp = m_properties->addProperty( "Use Texture", "Use texturing of the surface?", false );
m_surfaceColor = m_properties->addProperty( "Surface Color", "Description.", WColor( 0.3, 0.3, 0.3, 1.0 ) );
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", "", WKernel::getAppPathObject() );
}
void WMMarchingCubes::generateSurfacePre( double isoValue )
......@@ -456,22 +463,23 @@ void WMMarchingCubes::notifyTextureChange()
m_textureChanged = true;
}
// TODO(wiebel): MC move this to a separate module in the future
bool WMMarchingCubes::save( std::string fileName, const WTriangleMesh2& triMesh ) const
bool WMMarchingCubes::save() const
{
if( triMesh.vertSize() == 0 )
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( triMesh.triangleSize() == 0 )
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 = fileName.c_str();
const char* c_file = m_meshFile->get().file_string().c_str();
std::ofstream dataFile( c_file );
if ( dataFile )
......@@ -480,7 +488,7 @@ bool WMMarchingCubes::save( std::string fileName, const WTriangleMesh2& triMesh
}
else
{
WLogger::getLogger()->addLogMessage( "open file failed" + fileName , "Marching Cubes", LL_ERROR );
WLogger::getLogger()->addLogMessage( "open file failed" + m_meshFile->get().file_string() , "Marching Cubes", LL_ERROR );
return false;
}
......@@ -493,10 +501,10 @@ bool WMMarchingCubes::save( std::string fileName, const WTriangleMesh2& triMesh
dataFile << ( "DATASET UNSTRUCTURED_GRID\n" );
wmath::WPosition point;
dataFile << "POINTS " << triMesh.vertSize() << " float\n";
for ( size_t i = 0; i < triMesh.vertSize(); ++i )
dataFile << "POINTS " << m_triMesh->vertSize() << " float\n";
for ( size_t i = 0; i < m_triMesh->vertSize(); ++i )
{
point = triMesh.getVertexAsPosition( i );
point = m_triMesh->getVertexAsPosition( i );
if( !( myIsfinite( point[0] ) && myIsfinite( point[1] ) && myIsfinite( point[2] ) ) )
{
WLogger::getLogger()->addLogMessage( "Will not write file from data that contains NAN or INF.", "Marching Cubes", LL_ERROR );
......@@ -505,22 +513,22 @@ bool WMMarchingCubes::save( std::string fileName, const WTriangleMesh2& triMesh
dataFile << point[0] << " " << point[1] << " " << point[2] << "\n";
}
dataFile << "CELLS " << triMesh.triangleSize() << " " << triMesh.triangleSize() * 4 << "\n";
for ( size_t i = 0; i < triMesh.triangleSize(); ++i )
dataFile << "CELLS " << m_triMesh->triangleSize() << " " << m_triMesh->triangleSize() * 4 << "\n";
for ( size_t i = 0; i < m_triMesh->triangleSize(); ++i )
{
dataFile << "3 " << triMesh.getTriVertId0( i ) << " "
<< triMesh.getTriVertId1( i ) << " "
<< triMesh.getTriVertId2( i ) << "\n";
dataFile << "3 " << m_triMesh->getTriVertId0( i ) << " "
<< m_triMesh->getTriVertId1( i ) << " "
<< m_triMesh->getTriVertId2( i ) << "\n";
}
dataFile << "CELL_TYPES "<< triMesh.triangleSize() <<"\n";
for ( size_t i = 0; i < triMesh.triangleSize(); ++i )
dataFile << "CELL_TYPES "<< m_triMesh->triangleSize() <<"\n";
for ( size_t i = 0; i < m_triMesh->triangleSize(); ++i )
{
dataFile << "5\n";
}
dataFile << "POINT_DATA " << triMesh.vertSize() << "\n";
dataFile << "POINT_DATA " << m_triMesh->vertSize() << "\n";
dataFile << "SCALARS scalars float\n";
dataFile << "LOOKUP_TABLE default\n";
for ( size_t i = 0; i < triMesh.vertSize(); ++i )
for ( size_t i = 0; i < m_triMesh->vertSize(); ++i )
{
dataFile << "0\n";
}
......
......@@ -132,10 +132,8 @@ private:
/**
* Store the mesh in legacy vtk file format.
* \param fileName the file where the triMesh will be written to
* \param triMesh this mesh will be stored.
*/
bool save( std::string fileName, const WTriangleMesh2& triMesh ) const;
bool save() const;
/**
* Load meshes saved with WMMarchingCubes::save
......@@ -156,6 +154,10 @@ 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.
*/
......
......@@ -93,10 +93,15 @@ public:
void testSaveZero()
{
WMMarchingCubes mc;
WTriangleMesh2 triMesh( 0, 0 );
boost::shared_ptr< WTriangleMesh2 > triMesh( new WTriangleMesh2( 0, 0 ) );
mc.m_triMesh = triMesh;
std::string fileName = wiotools::tempFileName();
mc.m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "Module's properties" ) );
mc.m_savePropGroup = mc.m_properties->addPropertyGroup( "Save Surface", "" );
mc.m_meshFile = mc.m_savePropGroup->addProperty( "Mesh File", "", boost::filesystem::path( fileName.c_str() ) );
mc.m_saveTriggerProp = mc.m_savePropGroup->addProperty( "Do Save", "Press!", WPVBaseTypes::PV_TRIGGER_READY );
bool result = mc.save( fileName, triMesh );
bool result = mc.save();
TS_ASSERT_EQUALS( result, false ); // should return false as we did not have any vertices or triangles.
TS_ASSERT( !wiotools::fileExists( fileName ) );
}
......@@ -108,7 +113,8 @@ public:
{
WMMarchingCubes mc;
const unsigned int nbPos = 10;
WTriangleMesh2 triMesh( nbPos, 3 );
boost::shared_ptr< WTriangleMesh2 > triMesh( new WTriangleMesh2( nbPos, 3 ) );
mc.m_triMesh = triMesh;
std::vector< wmath::WPosition > vertices( 0 );
for( unsigned int posId = 0; posId < nbPos; ++posId )
......@@ -116,17 +122,22 @@ public:
double x = posId * posId + 3.4;
double y = posId + 1;
double z = 3. / static_cast< double >( posId ); // provide nan values by dividing with zero
triMesh.addVertex( x, y, z );
triMesh->addVertex( x, y, z );
}
std::string fileName = wiotools::tempFileName();
mc.m_properties = boost::shared_ptr< WProperties >( new WProperties( "Properties", "Module's properties" ) );
mc.m_savePropGroup = mc.m_properties->addPropertyGroup( "Save Surface", "" );
mc.m_meshFile = mc.m_savePropGroup->addProperty( "Mesh File", "", boost::filesystem::path( fileName.c_str() ) );
mc.m_saveTriggerProp = mc.m_savePropGroup->addProperty( "Do Save", "Press!", WPVBaseTypes::PV_TRIGGER_READY );
mc.save( fileName, triMesh );
mc.save();
bool result = mc.save( fileName, triMesh );
bool result = mc.save();
TS_ASSERT_EQUALS( result, false ); // should return false as we did not have all coordinates values finite.
TS_ASSERT( !wiotools::fileExists( fileName ) );
}
// TODO(wiebel): reactivate these when schurade has reactivated loading
// /**
// * Test reading of surfaces
......
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