OTB  9.0.0
Orfeo Toolbox
otbWrapperNumericalParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbWrapperNumericalParameter_h
22 #define otbWrapperNumericalParameter_h
23 
24 #include "otbWrapperParameter.h"
25 #include "otbAlgoClamp.h"
26 #include "itkNumericTraits.h"
27 #include <boost/optional.hpp>
28 
29 namespace otb
30 {
31 namespace Wrapper
32 {
38 template <class T>
39 class ITK_ABI_EXPORT NumericalParameter : public Parameter
40 {
41 public:
45  typedef itk::SmartPointer<Self> Pointer;
46  typedef itk::SmartPointer<const Self> ConstPointer;
47 
49  typedef T ScalarType;
50 
52  void Reset() override
53  {
54  m_Value = m_DefaultValue;
55  }
56 
58  void SetValue(ScalarType value)
59  {
60  m_Value = otb::clamp(value, m_MinimumValue, m_MaximumValue);
61 
62  // Set Active only if the parameter is not automatically set
63  if (!GetAutomaticValue())
64  {
65  SetActive(true);
66  }
67  }
68 
69  void SetValue(const std::string& valueStr)
70  {
71  ScalarType value = static_cast<ScalarType>(atof(valueStr.c_str()));
72  SetValue(value);
73  }
74 
76  {
77  if (!HasValue())
78  {
79  itkGenericExceptionMacro(<< "Parameter " << this->GetKey() << " has no value yet.");
80  }
81  return static_cast<ScalarType>(*m_Value);
82  }
83 
84  bool HasValue() const override
85  {
86  return m_Value != boost::none;
87  }
88 
89  void ClearValue() override
90  {
91  m_Value.reset();
92  }
93 
95  itkSetMacro(DefaultValue, ScalarType);
96 
98  itkGetMacro(DefaultValue, ScalarType);
99 
101  itkSetMacro(MinimumValue, ScalarType);
102 
104  itkGetMacro(MinimumValue, ScalarType);
105 
107  itkSetMacro(MaximumValue, ScalarType);
108 
110  itkGetMacro(MaximumValue, ScalarType);
111 
112  // TODO move to hxx
113  int ToInt() const override
114  {
115  if (!HasValue())
116  {
117  itkExceptionMacro("Cannot convert parameter " << GetKey() << " to int (no value).");
118  }
119  return static_cast<int>(*m_Value);
120  }
121 
122  float ToFloat() const override
123  {
124  if (!HasValue())
125  {
126  itkExceptionMacro("Cannot convert parameter " << GetKey() << " to float (no value).");
127  }
128  return static_cast<float>(*m_Value);
129  }
130 
131  double ToDouble() const override
132  {
133  if (!HasValue())
134  {
135  itkExceptionMacro("Cannot convert parameter " << GetKey() << " to double (no value).");
136  }
137  return static_cast<double>(*m_Value);
138  }
139 
140  void FromInt(int value) override
141  {
142  SetValue(value);
143  }
144 
145  void FromString(const std::string& value) override
146  {
147  SetValue(value);
148  }
149 
150  std::string ToString() const override
151  {
152  std::ostringstream oss;
153  oss << this->GetValue();
154  return oss.str();
155  }
156 
157 protected:
160  : m_DefaultValue(itk::NumericTraits<T>::Zero), m_MinimumValue(itk::NumericTraits<T>::NonpositiveMin()), m_MaximumValue(itk::NumericTraits<T>::max())
161  {
162  }
163 
166  {
167  }
168 
170  boost::optional<T> m_Value;
171 
174 
177 
180 
181 private:
182  NumericalParameter(const Parameter&) = delete;
183  void operator=(const Parameter&) = delete;
184 
185 }; // End class Numerical Parameter
186 
187 class OTBApplicationEngine_EXPORT FloatParameter : public NumericalParameter<float>
188 {
189 public:
192  typedef itk::SmartPointer<Self> Pointer;
193  typedef itk::SmartPointer<const Self> ConstPointer;
194 
195  itkNewMacro(Self);
196  itkTypeMacro(NumericalParameter, Parameter);
197 
198  ParameterType GetType() const override
199  {
200  return ParameterType_Float;
201  }
202 
203  void FromFloat(float value) override
204  {
205  SetValue(value);
206  }
207 };
208 
209 class OTBApplicationEngine_EXPORT DoubleParameter : public NumericalParameter<double>
210 {
211 public:
214  typedef itk::SmartPointer<Self> Pointer;
215  typedef itk::SmartPointer<const Self> ConstPointer;
216 
217  itkNewMacro(Self);
218  itkTypeMacro(NumericalParameter, Parameter);
219 
220  ParameterType GetType() const override
221  {
222  return ParameterType_Double;
223  }
224 
225  void FromDouble(double value) override
226  {
227  SetValue(value);
228  }
229 };
230 
231 class OTBApplicationEngine_EXPORT IntParameter : public NumericalParameter<int>
232 {
233 public:
236  typedef itk::SmartPointer<Self> Pointer;
237  typedef itk::SmartPointer<const Self> ConstPointer;
238 
239  itkNewMacro(Self);
240  itkTypeMacro(NumericalParameter, Parameter);
241 
242  ParameterType GetType() const override
243  {
244  return ParameterType_Int;
245  }
246 };
247 
248 class OTBApplicationEngine_EXPORT RAMParameter : public NumericalParameter<unsigned int>
249 {
250 public:
254  typedef itk::SmartPointer<Self> Pointer;
255  typedef itk::SmartPointer<const Self> ConstPointer;
256 
258  itkNewMacro(Self);
259 
261  itkTypeMacro(RAMParameter, Parameter);
262 
263  ParameterType GetType() const override
264  {
265  return ParameterType_RAM;
266  }
267 
269  RAMParameter() : NumericalParameter<unsigned int>()
270  {
271  this->SetName("RAM");
272  this->SetDescription("Set the maximum of available memory for the pipeline execution in mega bytes (optional, 256 by default).");
273  this->SetKey("ram");
275 
276  // 0 RAM is not allowed, make the minimum to 1 by default
277  this->SetMinimumValue(1);
278  }
279 };
280 
281 class OTBApplicationEngine_EXPORT RadiusParameter : public IntParameter
282 {
283 public:
285  typedef itk::SmartPointer<Self> Pointer;
286  typedef itk::SmartPointer<const Self> ConstPointer;
287 
288  itkNewMacro(Self);
289  itkTypeMacro(RadiusParameter, Parameter);
290 
291  bool HasValue() const override
292  {
293  return true;
294  }
295 
296  ParameterType GetType() const override
297  {
298  return ParameterType_Radius;
299  }
300 
301 protected:
303  {
304  this->SetName("Radius");
305  this->SetKey("r");
306  this->SetDescription("Radius in pixels");
307  }
308 };
309 
310 } // End namespace Wrapper
311 } // End namespace otb
312 
313 #endif
otb::Wrapper::RAMParameter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbWrapperNumericalParameter.h:255
otb::Wrapper::IntParameter::GetType
ParameterType GetType() const override
Definition: otbWrapperNumericalParameter.h:242
otb::Wrapper::Parameter
This class represent a parameter for the wrapper framework This class is a high level class represent...
Definition: otbWrapperParameter.h:49
otb::Wrapper::DoubleParameter
Definition: otbWrapperNumericalParameter.h:209
otb::Wrapper::FloatParameter
Definition: otbWrapperNumericalParameter.h:187
otb::Wrapper::RadiusParameter
Definition: otbWrapperNumericalParameter.h:281
otb::Wrapper::FloatParameter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbWrapperNumericalParameter.h:192
otb::Wrapper::ParameterType_RAM
@ ParameterType_RAM
Definition: otbWrapperTypes.h:57
otb::Wrapper::NumericalParameter::NumericalParameter
NumericalParameter()
Definition: otbWrapperNumericalParameter.h:159
otb::Wrapper::RadiusParameter::RadiusParameter
RadiusParameter()
Definition: otbWrapperNumericalParameter.h:302
otb::Wrapper::DoubleParameter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbWrapperNumericalParameter.h:214
otb::Wrapper::NumericalParameter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbWrapperNumericalParameter.h:45
otb::Wrapper::IntParameter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbWrapperNumericalParameter.h:236
otb::Wrapper::NumericalParameter::SetValue
void SetValue(const std::string &valueStr)
Definition: otbWrapperNumericalParameter.h:69
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Wrapper::DoubleParameter::FromDouble
void FromDouble(double value) override
Definition: otbWrapperNumericalParameter.h:225
otb::Wrapper::NumericalParameter::m_Value
boost::optional< T > m_Value
Definition: otbWrapperNumericalParameter.h:170
otb::Wrapper::IntParameter::Self
IntParameter Self
Definition: otbWrapperNumericalParameter.h:235
otb::Wrapper::FloatParameter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbWrapperNumericalParameter.h:193
otb::Wrapper::RadiusParameter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbWrapperNumericalParameter.h:286
otb::Wrapper::RadiusParameter::HasValue
bool HasValue() const override
Definition: otbWrapperNumericalParameter.h:291
otb::Wrapper::NumericalParameter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbWrapperNumericalParameter.h:46
otb::Wrapper::FloatParameter::FromFloat
void FromFloat(float value) override
Definition: otbWrapperNumericalParameter.h:203
otb::Wrapper::RadiusParameter::Self
RadiusParameter Self
Definition: otbWrapperNumericalParameter.h:284
otb::Wrapper::FloatParameter::GetType
ParameterType GetType() const override
Definition: otbWrapperNumericalParameter.h:198
otb::Wrapper::NumericalParameter::Reset
void Reset() override
Definition: otbWrapperNumericalParameter.h:52
otb::Wrapper::ParameterType_Int
@ ParameterType_Int
Definition: otbWrapperTypes.h:38
otb::Wrapper::NumericalParameter::HasValue
bool HasValue() const override
Definition: otbWrapperNumericalParameter.h:84
otb::Wrapper::NumericalParameter::Self
NumericalParameter Self
Definition: otbWrapperNumericalParameter.h:43
otb::Wrapper::NumericalParameter::ScalarType
T ScalarType
Definition: otbWrapperNumericalParameter.h:49
otb::Wrapper::ParameterType_Double
@ ParameterType_Double
Definition: otbWrapperTypes.h:40
otb::Wrapper::IntParameter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbWrapperNumericalParameter.h:237
otb::Wrapper::NumericalParameter::ToInt
int ToInt() const override
Definition: otbWrapperNumericalParameter.h:113
otb::Wrapper::NumericalParameter::SetValue
void SetValue(ScalarType value)
Definition: otbWrapperNumericalParameter.h:58
otb::Wrapper::ParameterType_Float
@ ParameterType_Float
Definition: otbWrapperTypes.h:39
otb::Wrapper::RAMParameter
Definition: otbWrapperNumericalParameter.h:248
otb::Wrapper::IntParameter
Definition: otbWrapperNumericalParameter.h:231
otb::Wrapper::NumericalParameter::ToDouble
double ToDouble() const override
Definition: otbWrapperNumericalParameter.h:131
otb::Wrapper::RAMParameter::Self
RAMParameter Self
Definition: otbWrapperNumericalParameter.h:252
otb::Wrapper::RAMParameter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbWrapperNumericalParameter.h:254
otb::Wrapper::RAMParameter::GetType
ParameterType GetType() const override
Definition: otbWrapperNumericalParameter.h:263
otb::Wrapper::NumericalParameter
This class represents a numerical parameter.
Definition: otbWrapperNumericalParameter.h:39
otb::Wrapper::NumericalParameter::FromString
void FromString(const std::string &value) override
Definition: otbWrapperNumericalParameter.h:145
itk
Definition: otbNoDataHelper.h:31
otb::Wrapper::NumericalParameter::m_DefaultValue
ScalarType m_DefaultValue
Definition: otbWrapperNumericalParameter.h:173
otb::Wrapper::NumericalParameter::m_MaximumValue
ScalarType m_MaximumValue
Definition: otbWrapperNumericalParameter.h:179
otb::Wrapper::RAMParameter::Superclass
Parameter Superclass
Definition: otbWrapperNumericalParameter.h:253
otb::clamp
constexpr T const & clamp(T const &v, T const &lo, T const &hi) noexcept
Definition: otbAlgoClamp.h:33
otbAlgoClamp.h
otb::Wrapper::NumericalParameter::ToFloat
float ToFloat() const override
Definition: otbWrapperNumericalParameter.h:122
otb::Wrapper::RAMParameter::RAMParameter
RAMParameter()
Definition: otbWrapperNumericalParameter.h:269
otb::Wrapper::ParameterType_Radius
@ ParameterType_Radius
Definition: otbWrapperTypes.h:54
otb::Wrapper::NumericalParameter::FromInt
void FromInt(int value) override
Definition: otbWrapperNumericalParameter.h:140
otb::Wrapper::NumericalParameter::m_MinimumValue
ScalarType m_MinimumValue
Definition: otbWrapperNumericalParameter.h:176
otb::Wrapper::FloatParameter::Self
FloatParameter Self
Definition: otbWrapperNumericalParameter.h:191
otbWrapperParameter.h
otb::Wrapper::DoubleParameter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbWrapperNumericalParameter.h:215
otb::Wrapper::NumericalParameter::ToString
std::string ToString() const override
Definition: otbWrapperNumericalParameter.h:150
otb::Wrapper::RadiusParameter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbWrapperNumericalParameter.h:285
otb::Wrapper::NumericalParameter::~NumericalParameter
~NumericalParameter() override
Definition: otbWrapperNumericalParameter.h:165
otb::Wrapper::RadiusParameter::GetType
ParameterType GetType() const override
Definition: otbWrapperNumericalParameter.h:296
otb::Wrapper::NumericalParameter::Superclass
Parameter Superclass
Definition: otbWrapperNumericalParameter.h:44
otb::Wrapper::DoubleParameter::GetType
ParameterType GetType() const override
Definition: otbWrapperNumericalParameter.h:220
otb::Wrapper::ParameterType
ParameterType
Definition: otbWrapperTypes.h:37
otb::Wrapper::NumericalParameter::GetValue
ScalarType GetValue() const
Definition: otbWrapperNumericalParameter.h:75
otb::Wrapper::NumericalParameter::ClearValue
void ClearValue() override
Definition: otbWrapperNumericalParameter.h:89
otb::Wrapper::DoubleParameter::Self
DoubleParameter Self
Definition: otbWrapperNumericalParameter.h:213