OTB  9.0.0
Orfeo Toolbox
otbFuzzyVariable.hxx
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 otbFuzzyVariable_hxx
22 #define otbFuzzyVariable_hxx
23 
24 
25 #include "otbFuzzyVariable.h"
26 #include "otbMacro.h"
27 
28 namespace otb
29 {
30 
31 template <class TLabel, class TPrecision>
33 {
34 }
35 
36 template <class TLabel, class TPrecision>
38  const PrecisionType& v4)
39 {
40  // Check if values are ordered correctly
41  if (v1 > v2 || v2 > v3 || v3 > v4)
42  itkExceptionMacro(<< "Values have to be v1<=v2<=v3<=v4");
43 
44  // Build the membership parameters
45  ParametersType parameters;
46  parameters[0] = v1;
47  parameters[1] = v2;
48  parameters[2] = v3;
49  parameters[3] = v4;
50  parameters[4] = static_cast<TPrecision>(0);
51  parameters[5] = static_cast<TPrecision>(1);
52 
53  // Insert it in the parameters map
54  m_MembershipFunctions[var] = parameters;
55 
56  // Call modified
57  this->Modified();
58 }
59 
60 template <class TLabel, class TPrecision>
62  const PrecisionType& v4, const PrecisionType& min, const PrecisionType& max)
63 {
64  // Check if values are ordered correctly
65  if (v1 > v2 || v2 > v3 || v3 > v4)
66  itkExceptionMacro(<< "Values have to be v1<=v2<=v3<=v4");
67  if (min >= max)
68  itkExceptionMacro(<< "Values have to be min<max");
69 
70  // Build the membership parameters
71  ParametersType parameters;
72  parameters[0] = v1;
73  parameters[1] = v2;
74  parameters[2] = v3;
75  parameters[3] = v4;
76  parameters[4] = min;
77  parameters[5] = max;
78 
79  // Insert it in the parameters map
80  m_MembershipFunctions[var] = parameters;
81 
82  // Call modified
83  this->Modified();
84 }
85 
86 template <class TLabel, class TPrecision>
88 {
89  // Erase one parameter
90  m_MembershipFunctions.erase(var);
91 
92  // Call Modified()
93  this->Modified();
94 }
95 
96 template <class TLabel, class TPrecision>
98 {
99  // Clear the membership parameters map
100  m_MembershipFunctions.clear();
101 
102  // Call Modified()
103  this->Modified();
104 }
105 
106 
107 template <class TLabel, class TPrecision>
109  const PrecisionType& value) const
110 {
111  // Declare output
112  PrecisionType output = itk::NumericTraits<PrecisionType>::Zero;
113 
114  // Retrieve parameters for the given membership function
115  typename ParametersMapType::const_iterator mapIt = m_MembershipFunctions.find(var);
116 
117  // If var exists in the parameters map
118  if (mapIt != m_MembershipFunctions.end())
119  {
120  // Retrieve parameters
121  ParametersType parameters = mapIt->second;
122 
123  // Remaining of the code is trapezoidal function
124  if (value < parameters[0] || value >= parameters[3])
125  {
126  output = parameters[4];
127  }
128  else if (value >= parameters[0] && value < parameters[1])
129  {
130  if (parameters[1] > parameters[0])
131  {
132  output = static_cast<TPrecision>(parameters[4] + (value - parameters[0]) / (parameters[1] - parameters[0]) * (parameters[5] - parameters[4]));
133  }
134  else
135  {
136  output = parameters[5];
137  }
138  }
139 
140  if (value >= parameters[1] && value < parameters[2])
141  {
142  output = parameters[5];
143  }
144 
145  if (value >= parameters[2] && value < parameters[3])
146  {
147  if (parameters[3] > parameters[2])
148  {
149  output = static_cast<TPrecision>(parameters[4] + (parameters[3] - value) / (parameters[3] - parameters[2]) * (parameters[5] - parameters[4]));
150  }
151  else
152  {
153  output = parameters[5];
154  }
155  }
156  }
157 
158  // Return the membership value
159  return output;
160 }
161 
162 template <class TLabel, class TPrecision>
164 {
165  // Build the output membership map
166  MembershipValueType output;
167 
168  // Walk the membership parameters map
169  for (typename ParametersMapType::const_iterator mapIt = m_MembershipFunctions.begin(); mapIt != m_MembershipFunctions.end(); ++mapIt)
170  {
171  // Compute the membership
172  output[mapIt->first] = this->GetMembership(mapIt->first, value);
173  }
174 
175  // Return output
176  return output;
177 }
178 
179 template <class TLabel, class TPrecision>
181 {
182  // If parameters map is empty, throw an exception
183  if (m_MembershipFunctions.empty())
184  {
185  itkExceptionMacro(<< "Membership parameters map is empty");
186  }
187 
188  // First retrieve all membership values
189  MembershipValueType memberships = this->GetMembership(value);
190 
191  // Define an iterator on the membership parameters map
192  typename MembershipValueType::const_iterator mapIt = memberships.begin();
193 
194  // Look for the higher value
195  LabelType higherVar = mapIt->first;
196  PrecisionType higherVal = mapIt->second;
197  ++mapIt;
198 
199  while (mapIt != memberships.end())
200  {
201  if (mapIt->second > higherVal)
202  {
203  higherVal = mapIt->second;
204  higherVar = mapIt->first;
205  }
206  ++mapIt;
207  }
208 
209  // Return the higher var
210  return higherVar;
211 }
212 
213 template <class TLabel, class TPrecision>
214 void FuzzyVariable<TLabel, TPrecision>::PrintSelf(std::ostream& os, itk::Indent indent) const
215 {
216  Superclass::PrintSelf(os, indent);
217 }
218 
219 template <class TLabel, class TPrecision>
221 {
222  // Define an iterator on the label set
223  typedef std::map<TLabel, TPrecision> LabelMapType;
224  typename LabelMapType::const_iterator it = labelMap.begin();
225 
226  // Open the set
227  out << "{";
228 
229  // Append the set elements
230  while (it != labelMap.end())
231  {
232  out << it->first << " : " << it->second;
233  ++it;
234  if (it != labelMap.end())
235  out << ", ";
236  }
237 
238  // close the set
239  out << "}";
240 
241  // Return
242  return out;
243 }
244 
245 } // end namespace otb
246 
247 
248 #endif
otb::FuzzyVariable::SetMembership
void SetMembership(const LabelType &var, const PrecisionType &v1, const PrecisionType &v2, const PrecisionType &v3, const PrecisionType &v4)
Definition: otbFuzzyVariable.hxx:37
otb::FuzzyVariable::PrintMembershipValueType
static std::ostream & PrintMembershipValueType(std::ostream &out, const MembershipValueType &membership)
Definition: otbFuzzyVariable.hxx:220
otb::FuzzyVariable::LabelType
TLabel LabelType
Definition: otbFuzzyVariable.h:60
otb::var
Definition: otbParserXPlugins.h:282
otbFuzzyVariable.h
otb::FuzzyVariable::PrecisionType
TPrecision PrecisionType
Definition: otbFuzzyVariable.h:66
otb::FuzzyVariable::Clear
void Clear()
Definition: otbFuzzyVariable.hxx:97
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::FuzzyVariable::GetMaxVar
LabelType GetMaxVar(const PrecisionType &value) const
Definition: otbFuzzyVariable.hxx:180
otbMacro.h
otb::FuzzyVariable::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbFuzzyVariable.hxx:214
otb::FuzzyVariable::MembershipValueType
std::map< LabelType, PrecisionType > MembershipValueType
Definition: otbFuzzyVariable.h:69
otb::FuzzyVariable::ParametersType
itk::FixedArray< PrecisionType, 6 > ParametersType
Definition: otbFuzzyVariable.h:70
otb::FuzzyVariable::GetMembership
PrecisionType GetMembership(const LabelType &var, const PrecisionType &value) const
Definition: otbFuzzyVariable.hxx:108
otb::FuzzyVariable::FuzzyVariable
FuzzyVariable()
Definition: otbFuzzyVariable.hxx:32
otb::FuzzyVariable::RemoveMembership
void RemoveMembership(const LabelType &var)
Definition: otbFuzzyVariable.hxx:87