30 #include "WMWriteTracts.h"
31 #include "WMWriteTracts.xpm"
32 #include "core/common/WPropertyHelper.h"
33 #include "core/dataHandler/io/WWriterFiberVTK.h"
34 #include "core/kernel/WKernel.h"
54 return WMWriteTracts_xpm;
59 return "Write Tracts";
64 return "Writes tracts either from a cluster or from a WDataSetFibers to a file";
77 m_savePath =
m_properties->addProperty(
"Save Path",
"Where to save the result", boost::filesystem::path(
"/no/such/file" ) );
96 "The tube diameter. Each fibers is represented as a tube with spheres as connections between them",
101 "Enable POVRay's radiosity renderer. Creates more realistic lighting but is very slow.",
104 " rendering is needed.", 1 );
121 debugLog() <<
"Waiting for data ...";
163 debugLog() <<
"this shouldn't be reached";
166 debugLog() <<
"Finished saving fibers.";
174 std::shared_ptr< const WDataSetFibers > ds =
m_tractIC->getData();
181 if( ds->getVertices()->size() == 0 )
187 const char* c_file =
m_savePath->get().string().c_str();
188 std::ofstream dataFile( c_file, std::ios_base::binary );
200 dataFile.setf( std::ios_base::fixed );
201 dataFile.precision( 3 );
205 dataFile << (
"{\n" );
207 dataFile << (
" \"vertices\" : [" );
208 std::shared_ptr<std::vector<float> > verts = ds->getVertices();
210 for(
size_t i = 0; i < ( verts->size() - 1 ) / 3; ++i )
212 fValue = verts->at( i * 3 );
213 dataFile << fValue <<
",";
214 fValue = verts->at( i * 3 + 1 );
215 dataFile << fValue <<
",";
216 fValue = verts->at( i * 3 + 2 );
217 dataFile << fValue <<
",";
219 fValue = verts->at( verts->size() - 3 );
220 dataFile << fValue <<
",";
221 fValue = verts->at( verts->size() - 2 );
222 dataFile << fValue <<
",";
223 fValue = verts->at( verts->size() - 1 );
224 dataFile << fValue <<
"],\n";
226 dataFile << (
" \"normals\" : [" );
227 std::shared_ptr<std::vector<float> > tangents = ds->getTangents();
228 for(
size_t i = 0; i < tangents->size() - 1; ++i )
230 fValue = tangents->at( i );
231 dataFile << fValue <<
",";
233 fValue = tangents->at( tangents->size() - 1 );
234 dataFile << fValue <<
"],\n";
236 dataFile << (
" \"colors\" : [" );
237 std::shared_ptr< std::vector< float > > colors = ds->getColorScheme(
"Global Color" )->getColor();
238 for(
size_t i = 0; i < colors->size() - 3; i += 3 )
240 fValue = colors->at( i );
241 dataFile << fValue <<
",";
242 fValue = colors->at( i + 1 );
243 dataFile << fValue <<
",";
244 fValue = colors->at( i + 2 );
245 dataFile << fValue <<
",1.0,";
247 fValue = colors->at( colors->size() - 3 );
248 dataFile << fValue <<
",";
249 fValue = colors->at( colors->size() - 2 );
250 dataFile << fValue <<
",";
251 fValue = colors->at( colors->size() - 1 );
252 dataFile << fValue <<
",1.0],\n";
255 dataFile << (
" \"indices\" : [" );
256 std::shared_ptr<std::vector<size_t> > lengths = ds->getLineLengths();
257 for(
size_t i = 0; i < lengths->size() - 1; ++i )
259 iValue = lengths->at( i );
260 dataFile << iValue <<
",";
262 iValue = lengths->at( lengths->size() - 1 );
263 dataFile << iValue <<
"]";
274 std::shared_ptr< const WDataSetFibers > ds =
m_tractIC->getData();
281 if( ds->getVertices()->size() == 0 )
287 const char* c_file =
m_savePath->get().string().c_str();
288 std::ofstream dataFile( c_file, std::ios_base::binary );
300 dataFile.setf( std::ios_base::fixed );
301 dataFile.precision( 3 );
306 std::vector<float> nVertices;
307 std::vector<float> nNormals;
308 std::vector<float> nColors;
309 std::vector<size_t> nIndices;
311 std::shared_ptr<std::vector<size_t> > starts = ds->getLineStartIndexes();
312 std::shared_ptr<std::vector<size_t> > lengths = ds->getLineLengths();
315 std::shared_ptr<std::vector<float> > verts = ds->getVertices();
316 std::shared_ptr<std::vector<float> > tangents = ds->getTangents();
317 std::shared_ptr< std::vector< float > > colors = ds->getColorScheme(
"Global Color" )->getColor();
319 for(
size_t k = 0; k < lengths->size(); ++k )
321 size_t newLength = 0;
322 for(
size_t i = starts->at( k ); i < ( starts->at( k ) + lengths->at( k ) ); ++i )
326 nVertices.push_back( verts->at( i * 3 ) );
327 nVertices.push_back( verts->at( i * 3 + 1 ) );
328 nVertices.push_back( verts->at( i * 3 + 2 ) );
330 nNormals.push_back( - tangents->at( i * 3 ) );
331 nNormals.push_back( - tangents->at( i * 3 + 1 ) );
332 nNormals.push_back( tangents->at( i * 3 + 2 ) );
334 nColors.push_back( colors->at( i * 3 ) );
335 nColors.push_back( colors->at( i * 3 + 1 ) );
336 nColors.push_back( colors->at( i * 3 + 2 ) );
337 nColors.push_back( 1.0 );
342 nIndices.push_back( newLength );
349 dataFile << (
"{\n" );
351 dataFile << (
" \"vertices\" : [" );
354 for(
size_t i = 0; i < nVertices.size() - 1 ; ++i )
356 fValue = nVertices[i];
357 dataFile << fValue <<
",";
359 fValue = verts->at( verts->size() - 1 );
360 dataFile << fValue <<
"],\n";
362 dataFile << (
" \"normals\" : [" );
364 for(
size_t i = 0; i < nNormals.size() - 1; ++i )
366 fValue = nNormals[i];
367 dataFile << fValue <<
",";
369 fValue = nNormals[nNormals.size() - 1];
370 dataFile << fValue <<
"],\n";
372 dataFile << (
" \"colors\" : [" );
374 for(
size_t i = 0; i < nColors.size()- 1; ++i )
377 dataFile << fValue <<
",";
379 fValue = nColors[nColors.size() - 1];
380 dataFile << fValue <<
",1.0],\n";
383 dataFile << (
" \"indices\" : [" );
385 for(
size_t i = 0; i < nIndices.size() - 1; ++i )
387 iValue = nIndices[i];
388 dataFile << iValue <<
",";
390 iValue = nIndices[nIndices.size() - 1];
391 dataFile << iValue <<
"]";
403 std::shared_ptr< const WDataSetFibers > ds =
m_tractIC->getData();
410 if( ds->getVertices()->size() == 0 )
416 const char* c_file =
m_savePath->get().string().c_str();
417 std::ofstream dataFile( c_file, std::ios_base::binary );
429 dataFile.precision( 7 );
433 dataFile << (
"{\n" );
435 dataFile << (
" \"vertices\" : [" );
436 std::shared_ptr<std::vector<float> > verts = ds->getVertices();
440 for(
size_t i = 0; i < verts->size() - 3; ++i )
442 fValue0 = verts->at( i );
444 fValue1 = verts->at( i );
446 fValue2 = verts->at( i );
447 dataFile << fValue0 <<
"," << fValue1 <<
"," << fValue2 <<
",";
448 dataFile << fValue0 <<
"," << fValue1 <<
"," << fValue2 <<
",";
450 fValue0 = verts->at( verts->size() - 3 );
451 fValue1 = verts->at( verts->size() - 2 );
452 fValue2 = verts->at( verts->size() - 1 );
453 dataFile << fValue0 <<
"," << fValue1 <<
"," << fValue2 <<
",";
454 dataFile << fValue0 <<
"," << fValue1 <<
"," << fValue2 <<
"],\n";
456 dataFile << (
" \"indices\" : [" );
457 std::shared_ptr<std::vector<size_t> > starts = ds->getLineStartIndexes();
458 std::shared_ptr<std::vector<size_t> > lengths = ds->getLineLengths();
461 for(
size_t i = 0; i < lengths->size(); ++i )
463 for(
size_t k = 0; k < lengths->at( i ); ++k )
465 dataFile << counter <<
"," << counter + 1 <<
"," << counter + 2 <<
",";
466 dataFile << counter + 1 <<
"," << counter + 3 <<
"," << counter + 2 <<
",";
480 boost::filesystem::path meshFile(
m_savePath->get() );
481 std::string fnPath = meshFile.parent_path().string();
482 std::string fnBase = meshFile.stem().string();
483 std::string fnExt = meshFile.extension().string();
487 std::string fnMesh = fnBase +
".mesh" + fnExt;
488 std::string fnScene = fnBase +
".scene" + fnExt;
491 std::string fnMeshAbs = fnPath +
"/" + fnMesh;
492 std::string fnSceneAbs = fnPath +
"/" + fnScene;
494 debugLog() <<
"Opening " << fnMeshAbs <<
" for writing the mesh data.";
495 std::ofstream dataFile( fnMeshAbs.c_str(), std::ios_base::binary );
498 errorLog() <<
"Opening " << fnMeshAbs <<
" failed.";
510 debugLog() <<
"Color mode is " << fibColorMode <<
".";
514 debugLog() <<
"Iterating over all fibers.";
524 size_t currentStart = 0;
527 std::shared_ptr< WProgress > progress1(
new WProgress(
"Converting fibers", fibStart->size() / increment ) );
529 for(
size_t fidx = 0; fidx < fibStart->size(); fidx += increment )
534 size_t sidx = fibStart->at( fidx ) * 3;
535 size_t csidx = fibStart->at( fidx ) * fibColorMode;
538 size_t len = fibLen->at( fidx );
541 WVector3d lastvert( fibVerts->at( sidx ),
542 fibVerts->at( sidx + 1 ),
543 fibVerts->at( sidx + 2 ) );
544 for(
size_t k = 1; k < len; ++k )
547 WVector3d vert( fibVerts->at( ( 3 * k ) + sidx ),
548 fibVerts->at( ( 3 * k ) + sidx + 1 ),
549 fibVerts->at( ( 3 * k ) + sidx + 2 ) );
550 WColor color( fibColors->at( ( fibColorMode * k ) + csidx + ( 0 % fibColorMode ) ),
551 fibColors->at( ( fibColorMode * k ) + csidx + ( 1 % fibColorMode ) ),
552 fibColors->at( ( fibColorMode * k ) + csidx + ( 2 % fibColorMode ) ),
554 fibColors->at( ( fibColorMode * k ) + csidx + ( 3 % fibColorMode ) ) : 1.0 );
556 if( vert.
x() > maxX )
558 if( vert.
y() > maxY )
560 if( vert.
z() > maxZ )
562 if( vert.
x() < minX )
564 if( vert.
y() < minY )
566 if( vert.
z() < minZ )
570 dataFile <<
"cylinder" << std::endl <<
572 " <" << lastvert.
x() <<
"," << lastvert.
y() <<
"," << lastvert.
z() <<
">," <<
573 "<" << vert.
x() <<
"," << vert.
y() <<
"," << vert.
z() <<
">,Diameter" << std::endl <<
574 " pigment{color rgb <" << color.x() <<
"," << color.y() <<
"," << color.z() <<
">}" << std::endl <<
575 " transform MoveToCenter" << std::endl <<
577 dataFile <<
"sphere {" << std::endl <<
578 " <" << vert.
x() <<
"," << vert.
y() <<
"," << vert.
z() <<
">,Diameter" << std::endl <<
579 " pigment{ color rgb <" << color.x() <<
"," << color.y() <<
"," << color.z() <<
">}" << std::endl <<
580 " transform MoveToCenter" << std::endl <<
588 double sizeX = maxX - minX;
589 double sizeY = maxY - minY;
590 double sizeZ = maxZ - minZ;
591 double mX = minX + ( sizeX / 2.0 );
592 double mY = minY + ( sizeY / 2.0 );
593 double mZ = minZ + ( sizeZ / 2.0 );
596 infoLog() <<
"Done. Closing " << fnMesh <<
".";
600 debugLog() <<
"Opening " << fnSceneAbs <<
" for writing.";
601 std::ofstream dataFileScene( fnSceneAbs.c_str(), std::ios_base::binary );
604 errorLog() <<
"Opening " << fnSceneAbs <<
" failed.";
609 dataFileScene <<
"#version 3.6;" << std::endl << std::endl;
611 dataFileScene <<
"// run with povray -w800 -h600 -Q0 fibsLarge.scene.pov " << std::endl <<
612 "// * this creates a fast preview of the scene with a resolution of 800x600." << std::endl <<
613 "// * the Q parameter defines the quality Q0 means plain colors. Q11 is best, including radiosity." << std::endl << std::endl;
617 dataFileScene <<
"global_settings {" << std::endl <<
618 " ambient_light 0" << std::endl << std::endl <<
619 " radiosity {" << std::endl <<
620 " pretrace_start 0.08" << std::endl <<
621 " pretrace_end 0.005" << std::endl <<
622 " count 350" << std::endl <<
623 " error_bound 0.15" << std::endl <<
624 " recursion_limit 2" << std::endl <<
626 "}" << std::endl << std::endl;
629 dataFileScene <<
"// Enable Phong lighting for all the geometry" << std::endl;
630 dataFileScene <<
"#default{" << std::endl <<
631 " finish{" << std::endl <<
632 " ambient 0" << std::endl <<
633 " phong 1" << std::endl <<
634 " // reflection 0.9 " << std::endl <<
636 "}" << std::endl << std::endl;
643 dataFileScene <<
"#declare MoveToCenter = transform{ translate < " << -mX <<
", " << -mY <<
", " << -mZ <<
" > };" << std::endl;
644 dataFileScene <<
"#declare CamPosition = < " << camX <<
", " << camY <<
", " << camZ <<
" >;" << std::endl << std::endl;
645 dataFileScene <<
"// Tube diameter" << std::endl;
646 dataFileScene <<
"#declare Diameter = " <<
m_povrayTubeDiameter->get() <<
";" << std::endl << std::endl;
649 dataFileScene <<
"camera {" << std::endl <<
650 " orthographic angle 45" << std::endl <<
651 " location CamPosition" << std::endl <<
652 " right 1.33*x" << std::endl <<
653 " // use this with 1280x1024" << std::endl <<
654 " // right 1.25*x" << std::endl <<
655 " up y " << std::endl <<
656 " look_at < 0, 0, 0 >" << std::endl <<
657 "}" << std::endl << std::endl;
659 dataFileScene <<
"light_source {" << std::endl <<
660 " CamPosition" << std::endl <<
661 " color rgb <1.0, 1.0, 1.0>" << std::endl <<
662 "}" << std::endl << std::endl;
665 dataFileScene <<
"#include \"" << fnMesh <<
"\"" << std::endl;
669 dataFileScene.close();
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.
ColorMode
different kinds of color arrays can be used in this class.
std::shared_ptr< std::vector< size_t > > IndexArray
Index list indexing fibers in VertexArray in terms of vertex numbers.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< float > > TangentArray
Tangents at each vertex in VertexArray.
std::shared_ptr< std::vector< size_t > > LengthArray
Lengths of fibers in terms of vertices.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
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 tracts of either a fiber cluster or directly out of a WDataSetFibers at its co...
std::shared_ptr< WModuleInputData< const WDataSetFibers > > m_tractIC
Input connector for writing directly tracts to a file.
std::shared_ptr< WModuleInputData< const WFiberCluster > > m_clusterIC
Input connector for writing the tracts out of a WFiberCluster to a file.
WMWriteTracts()
Constructs an instance to write tracts to a file.
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
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...
bool savePOVRay(std::shared_ptr< const WDataSetFibers > fibers) const
Saves the fiber tracts as POVRAY SDL.
virtual void moduleMain()
Entry point after loading the module.
bool saveJson() const
Store the mesh in a json (javascript object notation) file.
std::shared_ptr< WItemSelection > m_fileTypeSelectionsList
A list of file type selection types.
WPropGroup m_povrayOptions
Groups all the options for the povray exporter.
bool saveJson2() const
Store the mesh in a json (javascript object notation) file using only every other vertex.
WPropTrigger m_run
Button to start saving.
virtual void properties()
Initialize the properties for this module.
WPropInt m_povraySaveOnlyNth
Allows thinning of the data.
WPropFilename m_savePath
Path where tracts should be stored.
WPropDouble m_povrayTubeDiameter
The tube diameter in povray export.
virtual void connectors()
Initialize the connectors this module is using.
WPropBool m_povrayRadiosity
Enables radiosity renderer in povray.
virtual const std::string getDescription() const
Gives back a description of this module.
void fileTypeChanged()
Handles updates in filetype property.
virtual const std::string getName() const
Gives back the name of this module.
virtual ~WMWriteTracts()
Destructs this instance.
WPropSelection m_fileTypeSelection
Selection property for file types.
bool saveJsonTriangles() const
Store the mesh in a json (javascript object notation) file.
ValueT & z()
Access z element of vector.
ValueT & y()
Access y element of vector.
ValueT & x()
Access x element of vector.
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.
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.
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
wlog::WStreamedLogger infoLog() const
Logger instance for comfortable info logging.
virtual void connectors()
Initialize connectors in this function.
Class managing progress inside of modules.
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.
void writeFibs(std::shared_ptr< const WDataSetFiberVector > fiberDS) const
Writes a WDataSetFiberVector down to the previousely given file.
@ PV_TRIGGER_TRIGGERED
Trigger property: got triggered.
@ PV_TRIGGER_READY
Trigger property: is ready to be triggered (again)
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.
const double MAX_DOUBLE
Maximum double value.
const double MIN_DOUBLE
Positive minimum double value.