Orfeo ToolBox  4.2
Orfeo ToolBox is not a black box
otbFuzzyVariable.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbFuzzyVariable_txx
19 #define __otbFuzzyVariable_txx
20 
21 
22 #include "otbFuzzyVariable.h"
23 #include "otbMacro.h"
24 
25 namespace otb
26 {
27 
28 template <class TLabel, class TPrecision>
30 {}
31 
32 template <class TLabel, class TPrecision>
33 void
35  const PrecisionType & v1,
36  const PrecisionType & v2,
37  const PrecisionType & v3,
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>
61 void
63  const PrecisionType & v1,
64  const PrecisionType & v2,
65  const PrecisionType & v3,
66  const PrecisionType & v4,
67  const PrecisionType & min,
68  const PrecisionType & max)
69 {
70  // Check if values are ordered correctly
71  if( v1>v2 || v2>v3 || v3>v4)
72  itkExceptionMacro(<< "Values have to be v1<=v2<=v3<=v4");
73  if( min>=max)
74  itkExceptionMacro(<< "Values have to be min<max");
75 
76  // Build the membership parameters
77  ParametersType parameters;
78  parameters[0] = v1;
79  parameters[1] = v2;
80  parameters[2] = v3;
81  parameters[3] = v4;
82  parameters[4] = min;
83  parameters[5] = max;
84 
85  // Insert it in the parameters map
86  m_MembershipFunctions[var]=parameters;
87 
88  // Call modified
89  this->Modified();
90 }
91 
92 template <class TLabel, class TPrecision>
93 void
96 {
97  // Erase one parameter
98  m_MembershipFunctions.erase(var);
99 
100  // Call Modified()
101  this->Modified();
102 }
103 
104 template <class TLabel, class TPrecision>
105 void
108 {
109  // Clear the membership parameters map
110  m_MembershipFunctions.clear();
111 
112  // Call Modified()
113  this->Modified();
114 }
115 
116 
117 template <class TLabel, class TPrecision>
119 ::PrecisionType
121 ::GetMembership(const LabelType & var, const PrecisionType & value) const
122 {
123  // Declare output
125 
126  // Retrieve parameters for the given membership function
127  typename ParametersMapType::const_iterator mapIt = m_MembershipFunctions.find(var);
128 
129  // If var exists in the parameters map
130  if(mapIt!=m_MembershipFunctions.end())
131  {
132  // Retrieve parameters
133  ParametersType parameters = mapIt->second;
134 
135  // Remaining of the code is trapezoidal function
136  if( value < parameters[0] || value >= parameters[3] )
137  {
138  output = parameters[4];
139  }
140  else if( value >= parameters[0]
141  && value < parameters[1] )
142  {
143  if(parameters[1]>parameters[0])
144  {
145  output = static_cast<TPrecision>(parameters[4] + (value - parameters[0])
146  /(parameters[1] - parameters[0])
147  *(parameters[5] - parameters[4]));
148  }
149  else
150  {
151  output = parameters[5];
152  }
153  }
154 
155  if( value >= parameters[1]
156  && value < parameters[2] )
157  {
158  output = parameters[5];
159  }
160 
161  if( value >= parameters[2]
162  && value < parameters[3] )
163  {
164  if(parameters[3]>parameters[2])
165  {
166  output = static_cast<TPrecision>(parameters[4] + (parameters[3] - value)
167  /(parameters[3] - parameters[2])
168  *(parameters[5] - parameters[4]));
169  }
170  else
171  {
172  output = parameters[5];
173  }
174  }
175 
176  }
177 
178  // Return the membership value
179  return output;
180 }
181 
182 template <class TLabel, class TPrecision>
184 ::MembershipValueType
186 ::GetMembership(const PrecisionType & value) const
187 {
188  // Build the output membership map
189  MembershipValueType output;
190 
191  // Walk the membership parameters map
192  for(typename ParametersMapType::const_iterator mapIt = m_MembershipFunctions.begin();
193  mapIt!=m_MembershipFunctions.end(); ++mapIt)
194  {
195  // Compute the membership
196  output[mapIt->first] = this->GetMembership(mapIt->first, value);
197  }
198 
199  // Return output
200  return output;
201 }
202 
203 template <class TLabel, class TPrecision>
205 ::LabelType
207 ::GetMaxVar(const PrecisionType & value) const
208 {
209  // If parameters map is empty, throw an exception
210  if(m_MembershipFunctions.empty())
211  {
212  itkExceptionMacro(<<"Membership parameters map is empty");
213  }
214 
215  // First retrieve all membership values
216  MembershipValueType memberships = this->GetMembership(value);
217 
218  // Define an iterator on the membership parameters map
219  typename MembershipValueType::const_iterator mapIt = memberships.begin();
220 
221  // Look for the higher value
222  LabelType higherVar = mapIt->first;
223  PrecisionType higherVal = mapIt->second;
224  ++mapIt;
225 
226  while(mapIt!=memberships.end())
227  {
228  if(mapIt->second > higherVal)
229  {
230  higherVal = mapIt->second;
231  higherVar = mapIt->first;
232  }
233  ++mapIt;
234  }
235 
236  // Return the higher var
237  return higherVar;
238 }
239 
240 template <class TLabel, class TPrecision>
241 void
243 ::PrintSelf(std::ostream& os, itk::Indent indent) const
244 {
245  Superclass::PrintSelf(os, indent);
246 }
247 
248 template <class TLabel, class TPrecision>
249 std::ostream &
251 ::PrintMembershipValueType(std::ostream & out,
252  const MembershipValueType & labelMap)
253 {
254  // Define an iterator on the label set
255  typedef std::map<TLabel, TPrecision> LabelMapType;
256  typename LabelMapType::const_iterator it = labelMap.begin();
257 
258  // Open the set
259  out<<"{";
260 
261  // Append the set elements
262  while(it != labelMap.end())
263  {
264  out<<it->first<<" : "<<it->second;
265  ++it;
266  if(it != labelMap.end())
267  out<<", ";
268  }
269 
270  // close the set
271  out<<"}";
272 
273  // Return
274  return out;
275 }
276 
277 } // end namespace otb
278 
279 
280 #endif
PrecisionType GetMembership(const LabelType &var, const PrecisionType &value) const
std::map< LabelType, PrecisionType > MembershipValueType
LabelType GetMaxVar(const PrecisionType &value) const
void SetMembership(const LabelType &var, const PrecisionType &v1, const PrecisionType &v2, const PrecisionType &v3, const PrecisionType &v4)
TPrecision PrecisionType
void PrintSelf(std::ostream &os, itk::Indent indent) const
Class to represent a fuzzy N-valued variable.
void RemoveMembership(const LabelType &var)
static std::ostream & PrintMembershipValueType(std::ostream &out, const MembershipValueType &membership)