Commit 0254639f authored by schurade's avatar schurade
Browse files

[ADD] saving of selected fiber bundles

parent b3738926
......@@ -25,6 +25,9 @@
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <boost/filesystem/fstream.hpp>
#include <boost/lexical_cast.hpp>
#include "../common/WColor.h"
#include "../common/WLogger.h"
......@@ -193,3 +196,111 @@ wmath::WPosition WDataSetFibers::getTangent( size_t fiber, size_t vertex ) const
tangent.normalize();
return tangent;
}
void WDataSetFibers::saveSelected( std::string filename, boost::shared_ptr< std::vector< bool > > active ) const
{
std::vector< float > pointsToSave;
std::vector< int > linesToSave;
std::vector< unsigned char > colorsToSave;
int pointIndex = 0;
int countLines = 0;
for ( size_t l = 0; l < m_lineLengths->size(); ++l )
{
if ( ( *active )[l] )
{
unsigned int pc = ( *m_lineStartIndexes )[l] * 3;
linesToSave.push_back( ( *m_lineLengths )[l] );
for ( size_t j = 0; j < ( *m_lineLengths )[l]; ++j )
{
// TODO(schurade): replace this with a permanent solution
pointsToSave.push_back( 160 - ( *m_vertices )[pc] );
colorsToSave.push_back( static_cast<unsigned char> ( ( *m_localColors )[pc] * 255 ) );
++pc;
pointsToSave.push_back( 200 - ( *m_vertices )[pc] );
colorsToSave.push_back( static_cast<unsigned char> ( ( *m_localColors )[pc] * 255 ) );
++pc;
pointsToSave.push_back( ( *m_vertices )[pc] );
colorsToSave.push_back( static_cast<unsigned char> ( ( *m_localColors )[pc] * 255 ) );
++pc;
linesToSave.push_back( pointIndex );
++pointIndex;
}
++countLines;
}
}
converterByteINT32 c;
converterByteFloat f;
std::vector< char > vBuffer;
std::string header1 = "# vtk DataFile Version 3.0\nvtk output\nBINARY\nDATASET POLYDATA\nPOINTS ";
header1 += boost::lexical_cast<std::string>( pointsToSave.size() / 3 );
header1 += " float\n";
for ( unsigned int i = 0; i < header1.size(); ++i )
{
vBuffer.push_back( header1[i] );
}
for ( unsigned int i = 0; i < pointsToSave.size(); ++i )
{
f.f = pointsToSave[i];
vBuffer.push_back( f.b[3] );
vBuffer.push_back( f.b[2] );
vBuffer.push_back( f.b[1] );
vBuffer.push_back( f.b[0] );
}
vBuffer.push_back( '\n' );
std::string header2 = "LINES " + boost::lexical_cast<std::string>( countLines ) + " " +
boost::lexical_cast<std::string>( linesToSave.size() ) + "\n";
for ( unsigned int i = 0; i < header2.size(); ++i )
{
vBuffer.push_back( header2[i] );
}
for ( unsigned int i = 0; i < linesToSave.size(); ++i )
{
c.i = linesToSave[i];
vBuffer.push_back( c.b[3] );
vBuffer.push_back( c.b[2] );
vBuffer.push_back( c.b[1] );
vBuffer.push_back( c.b[0] );
}
vBuffer.push_back( '\n' );
std::string header3 = "POINT_DATA ";
header3 += boost::lexical_cast<std::string>( pointsToSave.size() / 3 );
header3 += " float\n";
header3 += "COLOR_SCALARS scalars 3\n";
for ( unsigned int i = 0; i < header3.size(); ++i )
{
vBuffer.push_back( header3[i] );
}
for ( unsigned int i = 0; i < colorsToSave.size(); ++i )
{
vBuffer.push_back( colorsToSave[i] );
}
vBuffer.push_back( '\n' );
boost::filesystem::path p( filename );
boost::filesystem::ofstream ofs( p );
for ( unsigned int i = 0; i < vBuffer.size(); ++i )
{
ofs << vBuffer[i];
}
}
......@@ -34,6 +34,23 @@
#include "WDataSet.h"
/**
* converts an integer into a byte array and back
*/
union converterByteINT32
{
unsigned char b[4]; //!< the bytes
int i; //!< the int
};
/**
* converts a float into a byte array and back
*/
union converterByteFloat
{
unsigned char b[4]; //!< the bytes
float f; //!< the float
};
/**
* Represents a simple set of WFibers.
......@@ -147,6 +164,13 @@ public:
*/
wmath::WPosition getTangent( size_t fiber, size_t vertex ) const;
/**
* saves the selected fiber bundles to a file
*
*\param filename
* \param active bitfield of the fiber selection
*/
void saveSelected( std::string filename, boost::shared_ptr< std::vector< bool > > active ) const;
protected:
/**
......
......@@ -210,6 +210,8 @@ void WMFiberDisplay::properties()
boost::bind( &WMFiberDisplay::adjustTubes, this ) );
m_tubeThickness->setMin( 0 );
m_tubeThickness->setMax( 1000 );
m_save = m_properties->addProperty( "Save", "saves the selected fiber bundles.", false, boost::bind( &WMFiberDisplay::saveSelected, this ) );
m_saveFileName = m_properties->addProperty( "File Name", "no description yet", WKernel::getAppPathObject() );
}
void WMFiberDisplay::toggleTubes()
......@@ -253,3 +255,9 @@ void WMFiberDisplay::adjustTubes()
m_uniformTubeThickness->set( static_cast<float>( m_tubeThickness->get() ) );
}
}
void WMFiberDisplay::saveSelected()
{
boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField();
m_dataset->saveSelected( m_saveFileName->getAsString(), active );
}
......@@ -117,6 +117,8 @@ private:
WPropBool m_customColoring; //!< Enable/Disable custom colors
WPropBool m_useTubesProp; //!< Property indicating whether to use tubes for the fibers tracts.
WPropDouble m_tubeThickness; //!< Property determining the thickness of tubes .
WPropBool m_save; //!< this should be a button
WPropFilename m_saveFileName; //!< the filename for saving
WBoolFlag m_noData; //!< Flag indicating whether there is data to display.
......@@ -168,6 +170,11 @@ private:
*/
void adjustTubes();
/**
* saves the currently selected (active field from roi manager) fibers to a file
*/
void saveSelected();
/**
* Node callback to handle updates properly
*/
......
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