OTB  10.0.0
Orfeo Toolbox
otbWrapperNumericalParameter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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);
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);
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);
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
void FromDouble(double value) override
ParameterType GetType() const override
itk::SmartPointer< const Self > ConstPointer
ParameterType GetType() const override
itk::SmartPointer< const Self > ConstPointer
ParameterType GetType() const override
itk::SmartPointer< const Self > ConstPointer
This class represents a numerical parameter.
NumericalParameter(const Parameter &)=delete
void SetValue(const std::string &valueStr)
void FromString(const std::string &value) override
void operator=(const Parameter &)=delete
itk::SmartPointer< const Self > ConstPointer
This class represent a parameter for the wrapper framework This class is a high level class represent...
ParameterType GetType() const override
itk::SmartPointer< const Self > ConstPointer
itk::SmartPointer< const Self > ConstPointer
ParameterType GetType() const override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
constexpr T const & clamp(T const &v, T const &lo, T const &hi) noexcept
Definition: otbAlgoClamp.h:33