30 #include "WMWriteMesh.h"
31 #include "WMWriteMesh.xpm"
32 #include "core/common/WLimits.h"
33 #include "core/common/WPathHelper.h"
34 #include "core/common/WStringUtils.h"
35 #include "core/common/math/WMath.h"
36 #include "core/kernel/WKernel.h"
41 WMWriteMesh::WMWriteMesh():
46 WMWriteMesh::~WMWriteMesh()
54 return std::shared_ptr< WModule >(
new WMWriteMesh() );
59 return WMWriteMesh_xpm;
70 return "This module writes a triangle mesh to a file. Different file formats are supported.";
75 m_meshInput = std::shared_ptr< WModuleInputData < WTriangleMesh > >(
131 debugLog() <<
"type VTK ASCII file selected";
135 debugLog() <<
"type json file selected";
139 debugLog() <<
"type STL file selected";
143 debugLog() <<
"this shouldn't be reached";
169 const char* c_file =
m_meshFile->get().string().c_str();
170 std::ofstream dataFile( c_file, std::ios_base::binary );
182 dataFile.precision( 16 );
185 dataFile << (
"# vtk DataFile Version 2.0\n" );
186 dataFile << (
"generated using OpenWalnut\n" );
187 dataFile << (
"ASCII\n" );
188 dataFile << (
"DATASET UNSTRUCTURED_GRID\n" );
191 dataFile <<
"POINTS " <<
m_triMesh->vertSize() <<
" float\n";
192 for(
size_t i = 0; i <
m_triMesh->vertSize(); ++i )
200 dataFile << point[0] <<
" " << point[1] <<
" " << point[2] <<
"\n";
203 dataFile <<
"CELLS " <<
m_triMesh->triangleSize() <<
" " <<
m_triMesh->triangleSize() * 4 <<
"\n";
204 for(
size_t i = 0; i <
m_triMesh->triangleSize(); ++i )
206 dataFile <<
"3 " <<
m_triMesh->getTriVertId0( i ) <<
" "
208 <<
m_triMesh->getTriVertId2( i ) <<
"\n";
210 dataFile <<
"CELL_TYPES "<<
m_triMesh->triangleSize() <<
"\n";
211 for(
size_t i = 0; i <
m_triMesh->triangleSize(); ++i )
215 dataFile <<
"POINT_DATA " <<
m_triMesh->vertSize() <<
"\n";
216 dataFile <<
"SCALARS scalars float\n";
217 dataFile <<
"LOOKUP_TABLE default\n";
218 for(
size_t i = 0; i <
m_triMesh->vertSize(); ++i )
246 const char* c_file =
m_meshFile->get().string().c_str();
247 std::ofstream dataFile( c_file, std::ios_base::binary );
260 dataFile << (
"solid OpenWalnut_TriangleMesh\n" );
263 dataFile.precision( 16 );
264 dataFile << std::scientific;
267 for(
size_t i = 0; i <
m_triMesh->triangleSize(); ++i )
269 osg::Vec3f v1 =
m_triMesh->getTriVert( i, 0 );
270 osg::Vec3f v2 =
m_triMesh->getTriVert( i, 1 );
271 osg::Vec3f v3 =
m_triMesh->getTriVert( i, 2 );
274 dataFile <<
"facet normal 0 0 0 " << std::endl;
275 dataFile <<
" outer loop" << std::endl;
276 dataFile <<
" vertex " << v1.x() <<
" " << v1.y() <<
" " << v1.z() << std::endl;
277 dataFile <<
" vertex " << v3.x() <<
" " << v3.y() <<
" " << v3.z() << std::endl;
278 dataFile <<
" vertex " << v2.x() <<
" " << v2.y() <<
" " << v2.z() << std::endl;
279 dataFile <<
" endloop" << std::endl;
280 dataFile <<
"endfacet" << std::endl;
283 dataFile << (
"endsolid OpenWalnut_TriangleMesh\n" );
308 std::vector< std::shared_ptr< WTriangleMesh > >meshes;
311 for(
size_t k = 0; k < meshes.size(); ++k )
314 const char* c_file = fn.c_str();
315 std::ofstream dataFile( c_file, std::ios_base::binary );
327 dataFile.precision( 5 );
331 std::cout << meshes[k]->vertSize() <<
" vertices and " << meshes[k]->triangleSize() <<
" triangles" << std::endl;
332 dataFile << (
"{\n" );
333 dataFile << (
" \"vertices\" : [" );
335 for(
size_t i = 0; i < meshes[k]->vertSize() - 1; ++i )
337 point = meshes[k]->getVertex( i );
343 dataFile << point[0] <<
"," << point[1] <<
"," << point[2] <<
",";
345 point = meshes[k]->getVertex( meshes[k]->vertSize() - 1 );
346 dataFile << point[0] <<
"," << point[1] <<
"," << point[2] <<
"],\n";
348 dataFile << (
" \"normals\" : [" );
350 for(
size_t i = 0; i < meshes[k]->vertSize() - 1; ++i )
352 normal = meshes[k]->getNormal( i );
358 dataFile << normal[0] <<
"," << normal[1] <<
"," << normal[2] <<
",";
360 normal = meshes[k]->getNormal( meshes[k]->vertSize() - 1 );
361 dataFile << normal[0] <<
"," << normal[1] <<
"," << normal[2] <<
"],\n";
363 dataFile << (
" \"indices\" : [" );
364 for(
size_t i = 0; i < meshes[k]->triangleSize() - 1; ++i )
366 dataFile << meshes[k]->getTriVertId0( i ) <<
"," << meshes[k]->getTriVertId1( i ) <<
"," << meshes[k]->getTriVertId2( i ) <<
",";
368 size_t i = meshes[k]->triangleSize() - 1;
369 dataFile << meshes[k]->getTriVertId0( i ) <<
"," << meshes[k]->getTriVertId1( i ) <<
"," << meshes[k]->getTriVertId2( i ) <<
"]";
375 dataFile << (
" \"colors\" : [" );
376 for(
size_t j = 0; j < meshes[k]->vertSize() - 1; ++j )
378 osg::Vec4 color = meshes[k]->getVertColor( j );
380 dataFile << color[0] <<
"," << color[1] <<
"," << color[2] <<
"," << color[3] <<
",";
382 osg::Vec4 color = meshes[k]->getVertColor( meshes[k]->vertSize() - 1 );
383 dataFile << color[0] <<
"," << color[1] <<
"," << color[2] <<
"," << color[3] <<
"]";
393 std::vector< std::shared_ptr< WTriangleMesh > >
WMWriteMesh::splitMesh( std::shared_ptr< WTriangleMesh > triMesh,
size_t targetSize )
395 std::vector< std::shared_ptr< WTriangleMesh > >meshes;
396 if( triMesh->vertSize() <= targetSize )
398 meshes.push_back( triMesh );
401 size_t currentTri( 0 );
402 size_t id0, id1, id2;
404 while( currentTri < triMesh->triangleSize() )
406 std::shared_ptr< WTriangleMesh > newMesh(
new WTriangleMesh( 0, 0 ) );
407 std::vector<int>newIds( triMesh->vertSize(), -1 );
408 while( newMesh->vertSize() < targetSize )
410 id0 = triMesh->getTriVertId0( currentTri );
411 id1 = triMesh->getTriVertId1( currentTri );
412 id2 = triMesh->getTriVertId2( currentTri );
413 if( newIds[id0] == -1 )
415 newIds[id0] = newMesh->vertSize();
416 osg::Vec3 vert = triMesh->getTriVert( currentTri, 0 );
417 newMesh->addVertex( vert.x(), vert.y(), vert.z() );
418 newMesh->setVertexColor( newMesh->vertSize() - 1, triMesh->getVertColor( id0 ) );
420 if( newIds[id1] == -1 )
422 newIds[id1] = newMesh->vertSize();
423 osg::Vec3 vert = triMesh->getTriVert( currentTri, 1 );
424 newMesh->addVertex( vert.x(), vert.y(), vert.z() );
425 newMesh->setVertexColor( newMesh->vertSize() - 1, triMesh->getVertColor( id1 ) );
427 if( newIds[id2] == -1 )
429 newIds[id2] = newMesh->vertSize();
430 osg::Vec3 vert = triMesh->getTriVert( currentTri, 2 );
431 newMesh->addVertex( vert.x(), vert.y(), vert.z() );
432 newMesh->setVertexColor( newMesh->vertSize() - 1, triMesh->getVertColor( id2 ) );
434 newMesh->addTriangle( newIds[id0], newIds[id1], newIds[id2] );
436 if( currentTri == triMesh->triangleSize() )
442 meshes.push_back( newMesh );
virtual void wait() const
Wait for the condition.
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.
A class containing a list of named items.
void addLogMessage(std::string message, std::string source="", LogLevel level=LL_DEBUG)
Appends a log message to the logging queue.
static WLogger * getLogger()
Returns pointer to the currently running logger instance.
This module writes the triangle mesh given at its input connector as a VTK ASCII file or ....
virtual void moduleMain()
Entry point after loading the module.
bool saveSTL()
Write STL format.
WPropSelection m_fileTypeSelection
Selection property for file types.
bool saveVTKASCII() const
Store the mesh in legacy vtk file format.
virtual const std::string getName() const
Gives back the name of this module.
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
bool saveJson()
Store the mesh in a json (javascript object notation) file.
virtual const std::string getDescription() const
Gives back a description of this module.
std::shared_ptr< WItemSelection > m_fileTypeSelectionsList
A list of file type selection types.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
WPropBool m_writeColors
If true, colors get exported too.
WPropFilename m_meshFile
The mesh will be written to this file.
std::shared_ptr< WModuleInputData< WTriangleMesh > > m_meshInput
Input connector for a mesh.
virtual void properties()
Initialize the properties for this module.
std::shared_ptr< WTriangleMesh > m_triMesh
A pointer to the currently processed tri mesh.
WPropTrigger m_saveTriggerProp
This property triggers the actual writing,.
virtual void connectors()
Initialize the connectors this module is using.
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...
std::vector< std::shared_ptr< WTriangleMesh > > splitMesh(std::shared_ptr< WTriangleMesh > triMesh, size_t targetSize)
Splits the mesh in several mesh files (to solve maximum mesh limits in BrainGlL).
WPropGroup m_savePropGroup
Property group containing properties needed for saving the mesh.
Class representing a single module of OpenWalnut.
virtual void properties()
Initialize properties in this function.
wlog::WStreamedLogger debugLog() const
Logger instance for comfortable debug logging.
void addConnector(std::shared_ptr< WModuleInputConnector > con)
Adds the specified connector to the list of inputs.
std::shared_ptr< WProperties > m_properties
The property object for the module.
void ready()
Call this whenever your module is ready and can react on property changes.
WConditionSet m_moduleState
The internal state of the module.
virtual void connectors()
Initialize connectors in this function.
static boost::filesystem::path getAppPath()
The path where the binary file resides in.
This only is a 3d double vector.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
Triangle mesh data structure allowing for convenient access of the elements.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.
std::string toString(const T &value)
Convert a given value to a string.
bool isInf(T value)
Determines if a number is considered as infinity or not.