Commit 4cc23cfa authored by Cyril Poupon's avatar Cyril Poupon
Browse files

added the possiblity to use itk snap LUT table file to color meshes

parent e8373d35
......@@ -10,6 +10,7 @@
#include <iostream>
#include <set>
#include <map>
#include <fstream>
gkg::MeshSelectorUsingFileSizeCommand::MeshSelectorUsingFileSizeCommand(
......@@ -36,8 +37,10 @@ gkg::MeshSelectorUsingFileSizeCommand::MeshSelectorUsingFileSizeCommand(
gkg::MeshSelectorUsingFileSizeCommand::MeshSelectorUsingFileSizeCommand(
const std::string& inputDirectoryName,
const std::string& inputItkSnapLutName,
const std::string& outputDirectoryName,
const std::list< std::string >& excludedLabelList,
float transparency,
bool verbose )
: gkg::Command()
{
......@@ -45,14 +48,20 @@ gkg::MeshSelectorUsingFileSizeCommand::MeshSelectorUsingFileSizeCommand(
try
{
execute( inputDirectoryName, outputDirectoryName, excludedLabelList,
execute( inputDirectoryName,
inputItkSnapLutName,
outputDirectoryName,
excludedLabelList,
transparency,
verbose );
}
GKG_CATCH( "gkg::MeshSelectorUsingFileSizeCommand::MeshSelectorUsingFileSizeCommand( "
"const std::string& inputDirectoryName, "
"const std::string& inputItkSnapLutName, "
"const std::string& outputDirectoryName, "
"const std::list< std::string >& excludedLabelList, "
"float transparency, "
"bool verbose )" );
}
......@@ -67,13 +76,19 @@ gkg::MeshSelectorUsingFileSizeCommand::MeshSelectorUsingFileSizeCommand(
{
DECLARE_STRING_PARAMETER( parameters, std::string, inputDirectoryName );
DECLARE_STRING_PARAMETER( parameters, std::string, inputItkSnapLutName );
DECLARE_STRING_PARAMETER( parameters, std::string, outputDirectoryName );
DECLARE_LIST_OF_STRINGS_PARAMETER( parameters,
std::list< std::string >,
excludedLabelList );
DECLARE_FLOATING_PARAMETER( parameters, float, transparency );
DECLARE_BOOLEAN_PARAMETER( parameters, bool, verbose );
execute( inputDirectoryName, outputDirectoryName, excludedLabelList,
execute( inputDirectoryName,
inputItkSnapLutName,
outputDirectoryName,
excludedLabelList,
transparency,
verbose );
}
......@@ -110,8 +125,10 @@ void gkg::MeshSelectorUsingFileSizeCommand::parse()
{
std::string inputDirectoryName;
std::string inputItkSnapLutName = "";
std::string outputDirectoryName;
std::list< std::string > excludedLabelList;
float transparency = 1.0f;
bool verbose = false;
gkg::Application application( _argc, _argv,
......@@ -120,12 +137,20 @@ void gkg::MeshSelectorUsingFileSizeCommand::parse()
application.addSingleOption( "-i",
"Input directory name",
inputDirectoryName );
application.addSingleOption( "-l",
"Input ITKSnap LUT name",
inputItkSnapLutName,
true );
application.addSeriesOption( "-excludedLabels",
"List of excluded label name(s)",
excludedLabelList );
application.addSingleOption( "-o",
"Output directory name",
outputDirectoryName );
application.addSingleOption( "-t",
"Mesh transparency",
transparency,
true );
application.addSingleOption( "-verbose",
"Show as much information as possible",
verbose,
......@@ -133,7 +158,11 @@ void gkg::MeshSelectorUsingFileSizeCommand::parse()
application.initialize();
execute( inputDirectoryName, outputDirectoryName, excludedLabelList,
execute( inputDirectoryName,
inputItkSnapLutName,
outputDirectoryName,
excludedLabelList,
transparency,
verbose );
}
......@@ -144,8 +173,10 @@ void gkg::MeshSelectorUsingFileSizeCommand::parse()
void gkg::MeshSelectorUsingFileSizeCommand::execute(
const std::string& inputDirectoryName,
const std::string& inputItkSnapLutName,
const std::string& outputDirectoryName,
const std::list< std::string >& excludedLabelList,
float transparency,
bool verbose )
{
......@@ -233,6 +264,35 @@ void gkg::MeshSelectorUsingFileSizeCommand::execute(
}
////////////////////////////////////////////////////////////////////////////
// analyzing the ITK snap LUT
////////////////////////////////////////////////////////////////////////////
std::map< std::string, gkg::RGBComponent > roiToColorMap;
if ( !inputItkSnapLutName.empty() )
{
if ( verbose )
{
std::cout << "analyzing '"
<< inputItkSnapLutName
<< "' ITK snap LUT file : "
<< std::flush;
}
analyzeITKSnapFile( inputItkSnapLutName, roiToColorMap );
if ( verbose )
{
std::cout << "done" << std::endl;
}
}
////////////////////////////////////////////////////////////////////////////
// creating the output directory
////////////////////////////////////////////////////////////////////////////
......@@ -306,24 +366,35 @@ void gkg::MeshSelectorUsingFileSizeCommand::execute(
}
// copying .mesh.minf file(s)
originFileName = gkg::getCanonicalDirectory( inputDirectoryName ) +
labelRootName + '_' +
gkg::StringConverter::toString( biggestFileIndex ) +
".mesh.minf";
destinationFileName =
// creating .mesh.minf file if itk snap LUT is provided
std::map< std::string, gkg::RGBComponent >::const_iterator
c = roiToColorMap.find( labelRootName.substr(
labelRootName.rfind( "_" ) + 1 ) );
if ( c != roiToColorMap.end() )
{
destinationFileName =
gkg::getCanonicalDirectory( outputDirectoryName ) +
labelRootName + '_' +
gkg::StringConverter::toString( biggestFileIndex ) +
".mesh.minf";
std::ofstream os( destinationFileName.c_str() );
os << "attributes = {"
<< std::endl;
os << " \"material\" : { \"diffuse\" : [ "
<< ( float )c->second.r / 255.0 << ", "
<< ( float )c->second.g / 255.0 << ", "
<< ( float )c->second.b / 255.0 << ", "
<< transparency
<< " ] }"
<< std::endl;
os << "}"
<< std::endl;
os.close();
status = link( originFileName.c_str(), destinationFileName.c_str() );
if ( status < 0 )
{
throw std::runtime_error( "Error linking mesh.minf file" );
}
++ l;
......@@ -340,16 +411,81 @@ void gkg::MeshSelectorUsingFileSizeCommand::execute(
}
GKG_CATCH( "void gkg::MeshSelectorUsingFileSizeCommand::execute( "
"const std::string& inputDirectoryName, "
"const std::string& inputItkSnapLutName, "
"const std::string& outputDirectoryName, "
"const std::list< std::string >& excludedLabelList, "
"float transparency, "
"bool verbose )" );
}
void gkg::MeshSelectorUsingFileSizeCommand::analyzeITKSnapFile(
const std::string& itkSnapFileName,
std::map< std::string, gkg::RGBComponent >&
roiToColorMap ) const
{
try
{
std::ifstream is( itkSnapFileName.c_str() );
std::string line;
// reading the comments (head of the file)
int32_t l = 0;
for ( l = 0; l < 14; l++ )
{
std::getline( is, line );
}
std::string label;
int32_t r = 0;
int32_t g = 0;
int32_t b = 0;
while ( !is.eof() )
{
std::getline( is, line );
std::stringstream ss;
ss << line;
ss >> label;
ss >> r;
ss >> g;
ss >> b;
//std::cout << label << " -> "
// << r << " " << g << " " << b << " " << std::endl;
roiToColorMap[ label ] = gkg::RGBComponent( ( uint8_t )r,
( uint8_t )g,
( uint8_t )b );
}
is.close();
}
GKG_CATCH( "void gkg::MeshSelectorUsingFileSizeCommand::analyzeITKSnapFile( "
" const std::string& itkSnapFileName, "
"std::map< std::string, gkg::RGBComponent >& "
"roiToColorMap ) const" );
}
RegisterCommandCreator(
MeshSelectorUsingFileSizeCommand,
DECLARE_STRING_PARAMETER_HELP( inputDirectoryName ) +
DECLARE_STRING_PARAMETER_HELP( inputItkSnapLutName ) +
DECLARE_STRING_PARAMETER_HELP( outputDirectoryName ) +
DECLARE_LIST_OF_STRINGS_PARAMETER_HELP( excludedLabelList ) +
DECLARE_FLOATING_PARAMETER_HELP( transparency ) +
DECLARE_BOOLEAN_PARAMETER_HELP( verbose ) );
......@@ -4,9 +4,11 @@
#include <gkg-communication-command/Command.h>
#include <gkg-core-pattern/Creator.h>
#include <gkg-processing-colorimetry/RGBComponent.h>
#include <string>
#include <list>
#include <map>
namespace gkg
......@@ -32,8 +34,10 @@ class MeshSelectorUsingFileSizeCommand :
bool removeFirst = true );
MeshSelectorUsingFileSizeCommand(
const std::string& inputDirectoryName,
const std::string& inputItkSnapLutName,
const std::string& outputDirectoryName,
const std::list< std::string >& excludedLabelList,
float transparency,
bool verbose );
MeshSelectorUsingFileSizeCommand( const Dictionary& parameters );
virtual ~MeshSelectorUsingFileSizeCommand();
......@@ -49,10 +53,17 @@ class MeshSelectorUsingFileSizeCommand :
void parse();
void execute( const std::string& inputDirectoryName,
const std::string& inputItkSnapLutName,
const std::string& outputDirectoryName,
const std::list< std::string >& excludedLabelList,
float transparency,
bool verbose );
void analyzeITKSnapFile( const std::string& itkSnapFileName,
std::map< std::string, RGBComponent >&
roiToColorMap ) const;
};
......
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