Commit 0720ff2a authored by Cyril Poupon's avatar Cyril Poupon
Browse files

added dictionary helpers to facilitate the reading of python dictionaries

parent a3343125
......@@ -170,6 +170,7 @@ gkg-processing-container/ContinuousField.h
gkg-processing-container/ContinuousField_i.h
gkg-processing-container/Curve3d.h
gkg-processing-container/Curve3dMap.h
gkg-processing-container/DictionaryHelpers.h
gkg-processing-container/Front.h
gkg-processing-container/Front_i.h
gkg-processing-container/FrontEvolutionRoi.h
......@@ -676,6 +677,7 @@ gkg-processing-colorimetry/RandomColorGenerator.cxx
gkg-processing-container/BoundingBox.cxx
gkg-processing-container/Curve3d.cxx
gkg-processing-container/Curve3dMap.cxx
gkg-processing-container/DictionaryHelpers.cxx
gkg-processing-container/Front.cxx
gkg-processing-container/FrontEvolutionRoi.cxx
gkg-processing-container/LightCurve3d.cxx
......
#include <gkg-processing-container/DictionaryHelpers.h>
#include <gkg-core-object/GenericObject_i.h>
#include <gkg-core-io/StringConverter.h>
#include <gkg-core-exception/Exception.h>
//
// for scalar int32_t / float / double parameters
//
int32_t getIntegerValue( const gkg::Dictionary& dictionary,
const std::string& key,
bool isRequired,
int32_t defaultValue )
{
try
{
int32_t value = defaultValue;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( isRequired )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
value = ( int32_t )( i->second->getScalar() + 0.5 );
}
return value;
}
GKG_CATCH( "int32_t getIntegerParameter( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"bool isRequired, "
"int32_t defaultValue )" );
}
int32_t getFloatValue( const gkg::Dictionary& dictionary,
const std::string& key,
bool isRequired,
float defaultValue )
{
try
{
float value = defaultValue;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( isRequired )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
value = ( float )i->second->getScalar();
}
return value;
}
GKG_CATCH( "float getFloatValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"bool isRequired, "
"float defaultValue )" );
}
int32_t getDoubleValue( const gkg::Dictionary& dictionary,
const std::string& key,
bool isRequired,
double defaultValue )
{
try
{
double value = defaultValue;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( isRequired )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
value = i->second->getScalar();
}
return value;
}
GKG_CATCH( "double getDoubleValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"bool isRequired, "
"double defaultValue )" );
}
//
// for Vector3d< int32_t / float / double >
//
gkg::Vector3d< int32_t >
getVector3dOfIntegerValue( const gkg::Dictionary& dictionary,
const std::string& key,
bool isRequired,
const gkg::Vector3d< int32_t >& defaultValue )
{
try
{
gkg::Vector3d< int32_t > value = defaultValue;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( isRequired )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( gol.size() != 3U )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have 3 sub-items" );
}
value.x = ( int32_t )( gol[ 0 ]->getScalar() + 0.5 );
value.y = ( int32_t )( gol[ 1 ]->getScalar() + 0.5 );
value.z = ( int32_t )( gol[ 2 ]->getScalar() + 0.5 );
}
return value;
}
GKG_CATCH( "gkg::Vector3d< int32_t > getVector3dOfIntegerValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"bool isRequired, "
"const gkg::Vector3d< int32_t >& defaultValue )" );
}
gkg::Vector3d< float >
getVector3dOfFloatValue( const gkg::Dictionary& dictionary,
const std::string& key,
bool isRequired,
const gkg::Vector3d< float >& defaultValue )
{
try
{
gkg::Vector3d< float > value = defaultValue;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( isRequired )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( gol.size() != 3U )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have 3 sub-items" );
}
value.x = ( float )gol[ 0 ]->getScalar();
value.y = ( float )gol[ 1 ]->getScalar();
value.z = ( float )gol[ 2 ]->getScalar();
}
return value;
}
GKG_CATCH( "gkg::Vector3d< float > getVector3dOfFloatValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"bool isRequired, "
"const gkg::Vector3d< float >& defaultValue )" );
}
gkg::Vector3d< double >
getVector3dOfDoubleValue( const gkg::Dictionary& dictionary,
const std::string& key,
bool isRequired,
const gkg::Vector3d< double >& defaultValue )
{
try
{
gkg::Vector3d< double > value = defaultValue;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( isRequired )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( gol.size() != 3U )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have 3 sub-items" );
}
value.x = gol[ 0 ]->getScalar();
value.y = gol[ 1 ]->getScalar();
value.z = gol[ 2 ]->getScalar();
}
return value;
}
GKG_CATCH( "gkg::Vector3d< double > getVector3dOfDoubleValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"const gkg::Vector3d< double >& defaultValue, "
"bool isRequired )" );
}
//
// for std::vector< int32_t / float / double >
//
std::vector< int32_t >
getVectorOfIntegerValue( const gkg::Dictionary& dictionary,
const std::string& key,
int32_t expectedItemCount )
{
try
{
std::vector< int32_t > value;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( expectedItemCount >= 1 )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( ( expectedItemCount >= 1 ) &&
( ( int32_t ) gol.size() != expectedItemCount ) )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have " +
gkg::StringConverter::toString( expectedItemCount ) +
" sub-items" );
}
int32_t itemCount = ( int32_t )gol.size();
value.resize( itemCount );
int32_t index = 0;
for ( index = 0; index < itemCount; index++ )
{
value[ index ] = ( int32_t )( gol[ index ]->getScalar() + 0.5 );
}
}
return value;
}
GKG_CATCH( "std::vector< int32_t > getVectorOfIntegerValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"int32_t expectedItemCount )" );
}
std::vector< float >
getVectorOfFloatValue( const gkg::Dictionary& dictionary,
const std::string& key,
int32_t expectedItemCount )
{
try
{
std::vector< float > value;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( expectedItemCount >= 1 )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( ( expectedItemCount >= 1 ) &&
( ( int32_t ) gol.size() != expectedItemCount ) )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have " +
gkg::StringConverter::toString( expectedItemCount ) +
" sub-items" );
}
int32_t itemCount = ( int32_t )gol.size();
value.resize( itemCount );
int32_t index = 0;
for ( index = 0; index < itemCount; index++ )
{
value[ index ] = ( float )gol[ index ]->getScalar();
}
}
return value;
}
GKG_CATCH( "std::vector< float > getVectorOfFloatValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"int32_t expectedItemCount )" );
}
std::vector< double >
getVectorOfDoubleValue( const gkg::Dictionary& dictionary,
const std::string& key,
int32_t expectedItemCount )
{
try
{
std::vector< double > value;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( expectedItemCount >= 1 )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( ( expectedItemCount >= 1 ) &&
( ( int32_t ) gol.size() != expectedItemCount ) )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have " +
gkg::StringConverter::toString( expectedItemCount ) +
" sub-items" );
}
int32_t itemCount = ( int32_t )gol.size();
value.resize( itemCount );
int32_t index = 0;
for ( index = 0; index < itemCount; index++ )
{
value[ index ] = gol[ index ]->getScalar();
}
}
return value;
}
GKG_CATCH( "std::vector< double > getVectorOfDoubleValue( "
"const gkg::Dictionary& dictonary, "
"const std::string& key, "
"int32_t expectedItemCount )" );
}
//
// for std::vector< Vector3d< int32_t / float / double > >
//
std::vector< gkg::Vector3d< int32_t > >
getVectorOfVector3dOfIntegerValue( const gkg::Dictionary& dictionary,
const std::string& key,
int32_t expectedItemCount )
{
try
{
std::vector< gkg::Vector3d< int32_t > > value;
gkg::Dictionary::const_iterator i = dictionary.find( key );
if ( i == dictionary.end() )
{
if ( expectedItemCount >= 1 )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item is required" );
}
}
else
{
gkg::GenericObjectList
gol = i->second->getValue< gkg::GenericObjectList >();
if ( ( expectedItemCount >= 1 ) &&
( ( int32_t ) gol.size() != expectedItemCount ) )
{
throw std::runtime_error(
std::string( "a '" ) + key + "' item must have " +
gkg::StringConverter::toString( expectedItemCount ) +
" sub-items" );
}
int32_t itemCount = ( int32_t )gol.size();
value.resize( itemCount );
int32_t index = 0;
for ( index = 0; index < itemCount; index++ )
{
gkg::GenericObjectList xyz =
gol[ index ]->getValue< gkg::GenericObjectList >();
if ( xyz.size() != 3U )
{
throw std::runtime_error(
"vector of 3 coordinates x, y, z required" );