Commit 28788373 authored by guillaume pernot's avatar guillaume pernot

Merge branch '1971-double-precision-parameters' into 'develop'

Added 'double' application parameter type.

Closes #1971

See merge request !628
parents 023b7f64 6bc00f6f
Pipeline #3157 passed with stages
in 9 minutes and 56 seconds
......@@ -219,6 +219,7 @@ public:
* \li ParameterType_Int
* \li ParameterType_Bool
* \li ParameterType_Float
* \li ParameterType_Double
* \li ParameterType_Radius
* \li ParameterType_Choice
*/
......@@ -228,9 +229,18 @@ public:
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
void SetParameterFloat(std::string const& parameter, float value, bool hasUserValueFlag = true);
/* Set a double precision floating value
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
void SetParameterDouble(std::string const& parameter, double value, bool hasUserValueFlag = true);
/* Set a string value
*
* Can be called for types :
......@@ -253,6 +263,7 @@ public:
* \li ParameterType_Directory
* \li ParameterType_Choice
* \li ParameterType_Float
* \li ParameterType_Double
* \li ParameterType_Int
* \li ParameterType_Radius
* \li ParameterType_InputImageParameter
......@@ -285,6 +296,7 @@ public:
* Can be called for types :
* \li ParameterType_Int
* \li ParameterType_Float
* \li ParameterType_Double
* \li ParameterType_Radius
* \li ParameterType_Choice
*/
......@@ -295,6 +307,7 @@ public:
* Can be called for types :
* \li ParameterType_Int
* \li ParameterType_Float
* \li ParameterType_Double
* \li ParameterType_Radius
* \li ParameterType_Choice
*/
......@@ -306,6 +319,7 @@ public:
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
void SetDefaultParameterFloat(std::string const& parameter, float value);
......@@ -313,9 +327,28 @@ public:
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
float GetDefaultParameterFloat(std::string const& parameter);
/* Set a default double precision floating value, must be used in the
* DoInit when setting a value by default
* for the parameter
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
void SetDefaultParameterDouble(std::string const& parameter, double value);
/* Get the default double precision floating value of a parameter
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
double GetDefaultParameterDouble(std::string const& parameter);
/** Set a default pixel type for an output image parameter
*
* \param[in] parameter Name of the output image parameter
......@@ -348,7 +381,7 @@ public:
*/
void SetMaximumParameterIntValue(std::string const& parameter, int value);
/* Set a minimum int value, must used in the
/* Set a minimum float value, must used in the
* DoInit when setting a value by default
* for the parameter
*
......@@ -357,7 +390,7 @@ public:
*/
void SetMinimumParameterFloatValue(std::string const& parameter, float value);
/* Set a maximum int value, must used in the
/* Set a maximum float value, must used in the
* DoInit when setting a value by default
* for the parameter
*
......@@ -366,6 +399,26 @@ public:
*/
void SetMaximumParameterFloatValue(std::string const& parameter, float value);
/* Set a minimum double precision float value, must used in the
* DoInit when setting a value by default
* for the parameter
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
void SetMinimumParameterDoubleValue(std::string const& parameter, double value);
/* Set a maximum double precision value, must used in the
* DoInit when setting a value by default
* for the parameter
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
void SetMaximumParameterDoubleValue(std::string const& parameter, double value);
/**
* Enable single selection mode for list view if status is true
......@@ -411,6 +464,7 @@ public:
* \li ParameterType_Int
* \li ParameterType_Bool
* \li ParameterType_Float
* \li ParameterType_Double
* \li ParameterType_Radius
* \li ParameterType_Choice
*/
......@@ -420,9 +474,18 @@ public:
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
float GetParameterFloat(std::string const& parameter) const;
/* Get a double precision floating parameter value
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
*/
double GetParameterDouble(std::string const& parameter) const;
/* Get a string parameter value
*
* Can be called for types :
......@@ -609,6 +672,7 @@ public:
*
* Can be called for types :
* \li ParameterType_Float
* \li ParameterType_Double
* \li ParameterType_Int
* \li ParameterType_Choice
* \li ParameterType_Radius
......
......@@ -127,6 +127,15 @@ public:
return static_cast<float>(*m_Value);
}
double ToDouble() const override
{
if (!HasValue())
{
itkExceptionMacro("Cannot convert parameter " << GetKey() << " to double (no value).");
}
return static_cast<double>(*m_Value);
}
void FromInt(int value) override
{
SetValue(value);
......@@ -185,7 +194,7 @@ public:
itkNewMacro(Self);
itkTypeMacro(NumericalParameter, Parameter);
virtual ParameterType GetType() const override
ParameterType GetType() const override
{
return ParameterType_Float;
}
......@@ -196,6 +205,28 @@ public:
}
};
class OTBApplicationEngine_EXPORT DoubleParameter : public NumericalParameter<double>
{
public:
/** Standard class typedef */
typedef DoubleParameter Self;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
itkNewMacro(Self);
itkTypeMacro(NumericalParameter, Parameter);
ParameterType GetType() const override
{
return ParameterType_Double;
}
void FromDouble(double value) override
{
SetValue(value);
}
};
class OTBApplicationEngine_EXPORT IntParameter : public NumericalParameter<int>
{
public:
......@@ -207,7 +238,7 @@ public:
itkNewMacro(Self);
itkTypeMacro(NumericalParameter, Parameter);
virtual ParameterType GetType() const override
ParameterType GetType() const override
{
return ParameterType_Int;
}
......@@ -228,7 +259,7 @@ public:
/** RTTI support */
itkTypeMacro(RAMParameter, Parameter);
virtual ParameterType GetType() const override
ParameterType GetType() const override
{
return ParameterType_RAM;
}
......@@ -260,7 +291,7 @@ public:
return true;
}
virtual ParameterType GetType() const override
ParameterType GetType() const override
{
return ParameterType_Radius;
}
......
......@@ -147,11 +147,13 @@ public:
*/
virtual int ToInt() const;
virtual float ToFloat() const;
virtual double ToDouble() const;
virtual std::string ToString() const;
virtual std::vector<std::string> ToStringList() const;
virtual void FromInt(int);
virtual void FromFloat(float);
virtual void FromDouble(double);
virtual void FromString(const std::string&);
virtual void FromStringList(const std::vector<std::string>&);
......
......@@ -37,6 +37,7 @@ namespace Wrapper
typedef enum {
ParameterType_Int,
ParameterType_Float,
ParameterType_Double,
ParameterType_String,
ParameterType_StringList,
ParameterType_InputFilename,
......@@ -64,6 +65,7 @@ namespace
{
constexpr char const* parameterTypesStrings[] = {"Int",
"Float",
"Double",
"String",
"StringList",
"InputFilename",
......
......@@ -411,6 +411,12 @@ void Application::SetParameterFloat(std::string const& key, float value, bool ha
this->SetParameterUserValue(key, hasUserValueFlag);
}
void Application::SetParameterDouble(std::string const& key, double value, bool hasUserValueFlag)
{
GetParameterByKey(key)->FromDouble(value);
this->SetParameterUserValue(key, hasUserValueFlag);
}
void Application::SetParameterString(std::string const& parameter, std::string value, bool hasUserValueFlag)
{
GetParameterByKey(parameter)->FromString(value);
......@@ -1097,6 +1103,13 @@ void Application::SetDefaultParameterInt(std::string const& parameter, int value
if (!hasUserValue)
paramFloat->SetValue(static_cast<float>(value));
}
else if (dynamic_cast<DoubleParameter*>(param))
{
DoubleParameter* paramDouble = dynamic_cast<DoubleParameter*>(param);
paramDouble->SetDefaultValue(static_cast<double>(value));
if (!hasUserValue)
paramDouble->SetValue(static_cast<double>(value));
}
else if (dynamic_cast<RAMParameter*>(param))
{
RAMParameter* paramRAM = dynamic_cast<RAMParameter*>(param);
......@@ -1125,6 +1138,11 @@ int Application::GetDefaultParameterInt(std::string const& parameter)
FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
ret = paramFloat->GetDefaultValue();
}
else if (dynamic_cast<DoubleParameter*>(param))
{
DoubleParameter* paramDouble = dynamic_cast<DoubleParameter*>(param);
ret = paramDouble->GetDefaultValue();
}
else if (dynamic_cast<RAMParameter*>(param))
{
RAMParameter* paramRAM = dynamic_cast<RAMParameter*>(param);
......@@ -1154,6 +1172,23 @@ float Application::GetDefaultParameterFloat(std::string const& key)
return param->GetDefaultValue();
}
void Application::SetDefaultParameterDouble(std::string const& key, double value)
{
auto param = downcast_check<DoubleParameter>(GetParameterByKey(key));
param->SetDefaultValue(value);
if (!param->HasUserValue())
{
param->SetValue(value);
}
}
double Application::GetDefaultParameterDouble(std::string const& key)
{
auto param = downcast_check<DoubleParameter>(GetParameterByKey(key));
return param->GetDefaultValue();
}
void Application::SetDefaultOutputPixelType(std::string const& key, ImagePixelType type)
{
auto param = downcast_check<OutputImageParameter>(GetParameterByKey(key));
......@@ -1185,6 +1220,18 @@ void Application::SetMaximumParameterFloatValue(std::string const& key, float va
param->SetMaximumValue(value);
}
void Application::SetMinimumParameterDoubleValue(std::string const& key, double value)
{
auto param = downcast_check<DoubleParameter>(GetParameterByKey(key));
param->SetMinimumValue(value);
}
void Application::SetMaximumParameterDoubleValue(std::string const& key, double value)
{
auto param = downcast_check<DoubleParameter>(GetParameterByKey(key));
param->SetMaximumValue(value);
}
void Application::SetListViewSingleSelectionMode(std::string const& key, bool status)
{
auto param = downcast_check<ListViewParameter>(GetParameterByKey(key));
......@@ -1246,6 +1293,11 @@ float Application::GetParameterFloat(std::string const& key) const
return GetParameterByKey(key)->ToFloat();
}
double Application::GetParameterDouble(std::string const& key) const
{
return GetParameterByKey(key)->ToDouble();
}
std::string Application::GetParameterString(std::string const& key) const
{
return GetParameterByKey(key)->ToString();
......@@ -1413,6 +1465,13 @@ std::vector<std::pair<std::string, std::string>> Application::GetOutputParameter
oss << GetParameterFloat(*it);
keyVal.second = oss.str();
}
else if (type == ParameterType_Double)
{
std::ostringstream oss;
oss << std::setprecision(19);
oss << GetParameterDouble(*it);
keyVal.second = oss.str();
}
else
{
keyVal.second = GetParameterAsString(*it);
......
......@@ -269,6 +269,12 @@ int Read(const std::string& filename, Application::Pointer this_)
std::stringstream(value) >> floatValue;
this_->SetParameterFloat(key, floatValue);
}
else if (type == ParameterType_Double)
{
double doubleValue;
std::stringstream(value) >> doubleValue;
this_->SetParameterDouble(key, doubleValue);
}
else if (type == ParameterType_StringList || type == ParameterType_ListView)
{
this_->SetParameterStringList(key, values);
......
......@@ -250,6 +250,13 @@ void ParseGroup(Application::Pointer app, TiXmlElement* n_App, const std::string
oss << app->GetParameterFloat(key);
value = oss.str();
}
else if (type == ParameterType_Double)
{
std::ostringstream oss;
oss << std::setprecision(std::numeric_limits<double>::digits10 + 1);
oss << app->GetParameterDouble(key);
value = oss.str();
}
else if (type == ParameterType_String || type == ParameterType_InputFilename || type == ParameterType_Directory || type == ParameterType_InputImage ||
type == ParameterType_InputVectorData || type == ParameterType_Choice || type == ParameterType_OutputVectorData ||
type == ParameterType_OutputFilename || type == ParameterType_Bool)
......
......@@ -229,6 +229,11 @@ float Parameter::ToFloat() const
TypeError("float");
}
double Parameter::ToDouble() const
{
TypeError("double");
}
std::string Parameter::ToString() const
{
TypeError("std::string");
......@@ -249,6 +254,11 @@ void Parameter::FromFloat(float)
TypeError("float");
}
void Parameter::FromDouble(double)
{
TypeError("double");
}
void Parameter::FromString(const std::string&)
{
TypeError("std::string");
......
......@@ -231,6 +231,11 @@ void ParameterGroup::AddParameter(ParameterType type, std::string paramKey, std:
newParam = FloatParameter::New();
}
break;
case ParameterType_Double:
{
newParam = DoubleParameter::New();
}
break;
case ParameterType_String:
{
newParam = StringParameter::New();
......
......@@ -56,6 +56,10 @@ otb_add_test(NAME owTvFloatParameter COMMAND otbApplicationEngineTestDriver
otbWrapperFloatParameterTest
)
otb_add_test(NAME owTvDoubleParameter COMMAND otbApplicationEngineTestDriver
otbWrapperDoubleParameterTest
)
otb_add_test(NAME owTvIntParameter COMMAND otbApplicationEngineTestDriver
otbWrapperIntParameterTest
)
......
......@@ -24,6 +24,7 @@ void RegisterTests()
{
REGISTER_TEST(otbWrapperInputImageParameterTest);
REGISTER_TEST(otbWrapperFloatParameterTest);
REGISTER_TEST(otbWrapperDoubleParameterTest);
REGISTER_TEST(otbWrapperIntParameterTest);
REGISTER_TEST(otbWrapperRAMParameterTest);
REGISTER_TEST(otbWrapperStringParameterTest1);
......
......@@ -23,6 +23,7 @@
#endif
#include "otbWrapperNumericalParameter.h"
#include <typeinfo>
using namespace otb::Wrapper;
......@@ -35,6 +36,15 @@ void assert_equal(const T& a, const T& b)
}
}
template <typename T>
void assert_close(const T& a, const T& b)
{
if (std::abs(a-b) > 1e-7)
{
itkGenericExceptionMacro("assert_close failed");
}
}
int otbWrapperFloatParameterTest(int, char* [])
{
auto param = FloatParameter::New();
......@@ -50,6 +60,7 @@ int otbWrapperFloatParameterTest(int, char* [])
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), int(val));
assert_equal(param->ToFloat(), val);
assert_equal(param->ToDouble(), (double)val);
assert_equal(param->ToString(), std::string("42.005"));
}
......@@ -59,6 +70,7 @@ int otbWrapperFloatParameterTest(int, char* [])
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), int(val));
assert_equal(param->ToFloat(), val);
assert_equal(param->ToDouble(), (double)val);
assert_equal(param->ToString(), std::string("-6.5"));
}
......@@ -69,6 +81,50 @@ int otbWrapperFloatParameterTest(int, char* [])
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), int(val));
assert_equal(param->ToFloat(), val);
assert_equal(param->ToDouble(), (double)val);
assert_equal(param->ToString(), std::string("-100.01"));
}
return EXIT_SUCCESS;
}
int otbWrapperDoubleParameterTest(int, char* [])
{
auto param = DoubleParameter::New();
param->SetKey("mykey");
param->SetDescription("My description.");
assert_equal(param->GetType(), ParameterType_Double);
{ // SetValue
const double val = 42.005;
param->SetValue(val);
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), int(val));
assert_close(param->ToFloat(), (float)val);
assert_close(param->ToDouble(), val);
assert_equal(param->ToString(), std::string("42.005"));
}
{ // FromDouble
const double val = -6.5;
param->FromDouble(val);
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), int(val));
assert_close(param->ToFloat(), (float)val);
assert_close(param->ToDouble(), val);
assert_equal(param->ToString(), std::string("-6.5"));
}
{ // FromString
const std::string str = "-100.01";
const double val = -100.01;
param->FromString(str);
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), int(val));
assert_close(param->ToFloat(), (float)val);
assert_close(param->ToDouble(), val);
assert_equal(param->ToString(), std::string("-100.01"));
}
......@@ -90,6 +146,7 @@ int otbWrapperIntParameterTest(int, char* [])
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), val);
assert_equal(param->ToFloat(), float(val));
assert_equal(param->ToDouble(), double(val));
assert_equal(param->ToString(), std::string("42"));
}
......@@ -100,6 +157,7 @@ int otbWrapperIntParameterTest(int, char* [])
assert_equal(param->GetValue(), val);
assert_equal(param->ToInt(), val);
assert_equal(param->ToFloat(), float(val));
assert_equal(param->ToDouble(), double(val));
assert_equal(param->ToString(), std::string("-100"));
}
......
......@@ -431,7 +431,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters()
// Multiple values parameters
m_Application->SetParameterStringList(paramKey, values);
}
else if (type == ParameterType_Choice || type == ParameterType_Float || type == ParameterType_Int || type == ParameterType_Radius ||
else if (type == ParameterType_Choice || type == ParameterType_Float || type == ParameterType_Double || type == ParameterType_Int || type == ParameterType_Radius ||
type == ParameterType_Directory || type == ParameterType_String || type == ParameterType_InputFilename ||
type == ParameterType_OutputFilename || type == ParameterType_InputImage || type == ParameterType_OutputImage ||
type == ParameterType_InputVectorData || type == ParameterType_OutputVectorData || type == ParameterType_RAM || type == ParameterType_Bool)
......@@ -708,6 +708,10 @@ std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer&
{
oss << "<float> ";
}
else if (type == ParameterType_Double)
{
oss << "<double> ";
}
else if (type == ParameterType_InputFilename || type == ParameterType_OutputFilename || type == ParameterType_Directory || type == ParameterType_InputImage ||
type == ParameterType_InputVectorData || type == ParameterType_OutputVectorData || type == ParameterType_String || type == ParameterType_Choice ||
(type == ParameterType_ListView && singleSelectionForListView))
......
......@@ -57,6 +57,7 @@ int main(int argc, char* argv[])
parameterTypeToString[ParameterType_Bool] = "QgsProcessingParameterBoolean";
parameterTypeToString[ParameterType_Int] = "QgsProcessingParameterNumber";
parameterTypeToString[ParameterType_Float] = "QgsProcessingParameterNumber";
parameterTypeToString[ParameterType_Double] = "QgsProcessingParameterNumber";
parameterTypeToString[ParameterType_RAM] = "QgsProcessingParameterNumber";
parameterTypeToString[ParameterType_Radius] = "QgsProcessingParameterNumber";
parameterTypeToString[ParameterType_Choice] = "OTBParameterChoice";
......@@ -201,7 +202,7 @@ int main(int argc, char* argv[])
default_value = param->HasValue() ? appli->GetParameterAsString(name) : "0";
}
}
else if (type == ParameterType_Float)
else if (type == ParameterType_Float || type == ParameterType_Double)
{
dFile << "|QgsProcessingParameterNumber.Double";
default_value = param->HasValue() ? appli->GetParameterAsString(name) : "0";
......
......@@ -78,6 +78,7 @@ namespace Wrapper
{
ParameterType_Int,
ParameterType_Float,
ParameterType_Double,
ParameterType_String,
ParameterType_StringList,
ParameterType_InputFilename,
......@@ -217,6 +218,10 @@ public:
self.UpdateParameters()
%}
%pythonappend Application::SetParameterDouble %{
self.UpdateParameters()
%}
%pythonappend Application::SetParameterString %{
self.UpdateParameters()
%}
......@@ -307,6 +312,7 @@ public:
void SetParameterInt(std::string parameter, int value, bool hasUserValueFlag = true);
void SetParameterFloat(std::string parameter, float value, bool hasUserValueFlag = true);
void SetParameterDouble(std::string parameter, double value, bool hasUserValueFlag = true);
void SetParameterString(std::string parameter, std::string value, bool hasUserValueFlag = true);
void SetParameterStringList(std::string parameter, std::vector<std::string> values, bool hasUserValueFlag = true);
......@@ -316,6 +322,7 @@ public:
int GetParameterInt(std::string parameter);
float GetParameterFloat(std::string parameter);
double GetParameterDouble(std::string parameter);
std::string GetParameterString(std::string parameter);
std::vector<std::string> GetParameterStringList(std::string parameter);
std::string GetParameterAsString(std::string paramKey);
......@@ -625,6 +632,7 @@ class ApplicationProxy(object):
ParameterType_Int : 'ParameterType_Int',
ParameterType_Radius : 'ParameterType_Radius',
ParameterType_RAM : 'ParameterType_RAM',
ParameterType_Double : 'ParameterType_Double',
ParameterType_Float : 'ParameterType_Float',
ParameterType_Choice : 'ParameterType_Choice',
ParameterType_Group : 'ParameterType_Group',
......@@ -658,6 +666,8 @@ class ApplicationProxy(object):
return self.SetParameterInt(paramKey, value)
elif paramType in [ParameterType_Float]:
return self.SetParameterFloat(paramKey, value)
elif paramType in [ParameterType_Double]:
return self.SetParameterDouble(paramKey, value)
elif paramType in [ParameterType_Bool]:
return self.SetParameterString(paramKey, str(value) )
elif paramType in [ParameterType_Group]:
......@@ -692,6 +702,8 @@ class ApplicationProxy(object):
return self.GetParameterInt(paramKey)
elif paramType in [ParameterType_Float]:
return self.GetParameterFloat(paramKey)
elif paramType in [ParameterType_Double]:
return self.GetParameterDouble(paramKey)
elif paramType in [ParameterType_Bool]:
return bool(self.GetParameterInt(paramKey))
elif paramType in [ParameterType_Group, ParameterType_Choice]:
......
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