OTB  9.0.0
Orfeo Toolbox
otbMassOfBelief.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 otbMassOfBelief_hxx
22 #define otbMassOfBelief_hxx
23 
24 #include "otbMassOfBelief.h"
25 
26 #include <algorithm>
27 #include <iterator>
28 
29 #include "otbMath.h"
30 
31 namespace otb
32 {
33 
34 template <class TLabel, class TMass>
36 {
37  // Insert into the mass map
38  m_MassesMap[labelSet] = mass;
39 
40  // Call modified
41  this->Modified();
42 }
43 
44 template <class TLabel, class TMass>
46 {
47  // Insert into the mass map
48  m_MassesMap.erase(labelSet);
49 
50  // Call modified
51  this->Modified();
52 }
53 
54 
55 template <class TLabel, class TMass>
57 {
58  // Look for mass in the table
59  typename MassMapType::const_iterator it = m_MassesMap.find(labelSet);
60 
61  if (it != m_MassesMap.end())
62  {
63  return it->second;
64  }
65  else
66  {
67  return itk::NumericTraits<MassType>::Zero;
68  }
69 }
70 
71 template <class TLabel, class TMass>
73 {
74  // Build the output
75  LabelSetOfSetType output;
76 
77  // Define an iterator on the mass map
78  typename MassMapType::const_iterator it = m_MassesMap.begin();
79 
80  // Walk the mass map, gathering the element of the power set
81  while (it != m_MassesMap.end())
82  {
83  output.insert(it->first);
84  ++it;
85  }
86 
87  // Return
88  return output;
89 }
90 
91 template <class TLabel, class TMass>
93 {
94  // Build the output
95  LabelSetType output;
96 
97  // Retrieve support set
98  LabelSetOfSetType support = this->GetSupport();
99 
100  // Walk the support and perform union
101  for (typename LabelSetOfSetType::iterator it = support.begin(); it != support.end(); ++it)
102  {
103  // Temporary set
104  LabelSetType tempSet;
105  std::insert_iterator<LabelSetType> tmpIt(tempSet, tempSet.begin());
106 
107  // Perform set union
108  std::set_union(output.begin(), output.end(), it->begin(), it->end(), tmpIt);
109 
110  // swap output and tempSet
111  output.swap(tempSet);
112  }
113 
114  // Return
115  return output;
116 }
117 
118 template <class TLabel, class TMass>
120 {
121  // Sum of masses
122  MassType sum = itk::NumericTraits<MassType>::Zero;
123 
124  // Compute normalization factor
125  for (typename MassMapType::const_iterator it = m_MassesMap.begin(); it != m_MassesMap.end(); ++it)
126  {
127  sum += it->second;
128  }
129 
130  // check if sum is not null (maybe use and 1e-xxx)
131  if (sum > 0.0)
132  {
133  // Apply normalization factor
134  for (typename MassMapType::iterator it = m_MassesMap.begin(); it != m_MassesMap.end(); ++it)
135  {
136  it->second /= sum;
137  }
138  // Call modified
139  this->Modified();
140  }
141 }
142 
143 template <class TLabel, class TMass>
145 {
146  // Retrieve the universe of the mass of belief
147  LabelSetType universe = this->GetUniverse();
148 
149  // Compute the sum of available masses
150  MassType sum = itk::NumericTraits<MassType>::Zero;
151 
152  // Compute normalization factor
153  for (typename MassMapType::const_iterator it = m_MassesMap.begin(); it != m_MassesMap.end(); ++it)
154  {
155  sum += it->second;
156  }
157 
158  // Compute uncertainty mass
159  MassType uncertaintyMass = 1 - sum;
160 
161  // Check if uncertainty mass is not negative
162  if (uncertaintyMass > 0)
163  {
164  // Associate uncertainty mass
165  this->SetMass(universe, uncertaintyMass);
166 
167  // Call modified
168  this->Modified();
169  }
170 }
171 
172 template <class TLabel, class TMass>
174 {
175  m_MassesMap.clear();
176 }
177 
178 template <class TLabel, class TMass>
180 {
181  // Clear any previous mass
182  this->Clear();
183 
184  // Compute number of elements
185  unsigned long nbElements = 1 << universe.size(); // 2^universe.size()
186 
187 
188  // Build each element
189  for (unsigned long elementId = 1; elementId <= nbElements; ++elementId)
190  {
191  // Instantiate a new element
192  LabelSetType newElement;
193 
194  unsigned long residu = elementId;
195 
196  // Walk the universe set
197  for (typename LabelSetType::const_iterator it = universe.begin(); residu > 0 && it != universe.end(); ++it)
198  {
199  // Retrieve the current bit
200  unsigned long bit = residu % 2;
201 
202  // If bit is 1, add the current element from universe to the new set
203  if (bit)
204  {
205  newElement.insert(*it);
206  }
207 
208  // Compute residu
209  residu /= 2;
210  }
211  this->SetMass(newElement, itk::NumericTraits<MassType>::Zero);
212  }
213 }
214 
215 template <class TLabel, class TMass>
217 {
218  // Retrieve support of mass function
219  LabelSetOfSetType support = this->GetSupport();
220 
221  // Define an empty set which will contain contained elements of the power-set
222  LabelSetOfSetType containedSet;
223 
224  // Look for elements in the support which are contained in labelSet
225  for (typename LabelSetOfSetType::const_iterator it = support.begin(); it != support.end(); ++it)
226  {
227  // Temporary set containing intersection
228  LabelSetType intersectionSet;
229  std::insert_iterator<LabelSetType> interIt(intersectionSet, intersectionSet.begin());
230 
231  // Perform set union
232  std::set_intersection(labelSet.begin(), labelSet.end(), it->begin(), it->end(), interIt);
233 
234  // If labelSet inter (*it) == (*it), then (*it) is contained
235  // inside labelSet
236  if (intersectionSet == (*it))
237  {
238  containedSet.insert((*it));
239  }
240  }
241 
242  // Call the generic implementation
243  return this->GetBelief(containedSet);
244 }
245 
246 template <class TLabel, class TMass>
248 {
249  // Retrieve support of mass function
250  LabelSetOfSetType support = this->GetSupport();
251 
252  // Define an empty set which will contain contained elements of the power-set
253  LabelSetOfSetType intersectedSet;
254 
255  // Look for elements in the support which are contained in labelSet
256  for (typename LabelSetOfSetType::const_iterator it = support.begin(); it != support.end(); ++it)
257  {
258  // Temporary set containing intersection
259  LabelSetType intersectionSet;
260  std::insert_iterator<LabelSetType> interIt(intersectionSet, intersectionSet.begin());
261 
262  // Perform set intersection
263  std::set_intersection(labelSet.begin(), labelSet.end(), it->begin(), it->end(), interIt);
264 
265  // If labelSet inter (*it) != {}, then (*it) intersects labelSet
266  if (!intersectionSet.empty())
267  {
268  intersectedSet.insert((*it));
269  }
270  }
271 
272  // Call the generic implementation
273  return this->GetPlausibility(intersectedSet);
274 }
275 
276 template <class TLabel, class TMass>
278 {
279  // Define output
280  MassType belief = itk::NumericTraits<MassType>::Zero;
281 
282  // Sum masses of contained set
283  for (typename LabelSetOfSetType::const_iterator it = containedLabelSet.begin(); it != containedLabelSet.end(); ++it)
284  {
285  belief += this->GetMass((*it));
286  }
287 
288  // return belief
289  return belief;
290 }
291 
292 template <class TLabel, class TMass>
294 {
295  // Define output
296  MassType plausibility = itk::NumericTraits<MassType>::Zero;
297 
298  // Sum masses of contained set
299  for (typename LabelSetOfSetType::const_iterator it = intersectedLabelSet.begin(); it != intersectedLabelSet.end(); ++it)
300  {
301  plausibility += this->GetMass((*it));
302  }
303 
304  // return belief
305  return plausibility;
306 }
307 
308 template <class TLabel, class TMass>
309 void MassOfBelief<TLabel, TMass>::Copy(const Self* massOfBelief)
310 {
311  // Swap content of masses maps
312  m_MassesMap = massOfBelief->m_MassesMap;
313 
314  // Call to Modified()
315  this->Modified();
316 }
317 
318 template <class TLabel, class TMass>
320 {
321  return m_MassesMap.empty();
322 }
323 
324 template <class TLabel, class TMass>
325 void MassOfBelief<TLabel, TMass>::PrintSelf(std::ostream& os, itk::Indent indent) const
326 {
327  // Call superclass implementation
328  Superclass::PrintSelf(os, indent);
329 
330  // Display mass of belief universe
331  os << indent << "Mass of belief universe: ";
332  PrintLabelSet(os, this->GetUniverse());
333  os << std::endl;
334 
335  // Display mass of belief support
336  os << indent << "Mass of belief support: ";
337  PrintLabelSetOfSet(os, this->GetSupport());
338  os << std::endl;
339 
340  // Display individual masses
341  for (typename MassMapType::const_iterator it = m_MassesMap.begin(); it != m_MassesMap.end(); ++it)
342  {
343  os << indent;
344  PrintLabelSet(os, it->first);
345  os << " has mass " << (it->second) << std::endl;
346  }
347  os << indent << "Other masses are null" << std::endl;
348 }
349 
350 template <class TLabel, class TMass>
351 std::ostream& MassOfBelief<TLabel, TMass>::PrintLabelSet(std::ostream& out, const LabelSetType& labelSet)
352 {
353  // Define an iterator on the label set
354  typename LabelSetType::const_iterator it = labelSet.begin();
355 
356  // Open the set
357  out << "{";
358 
359  // Append the set elements
360  while (it != labelSet.end())
361  {
362  out << (*it);
363  ++it;
364  if (it != labelSet.end())
365  out << ", ";
366  }
367 
368  // close the set
369  out << "}";
370 
371  // Return
372  return out;
373 }
374 
375 template <class TLabel, class TMass>
376 std::ostream& MassOfBelief<TLabel, TMass>::PrintLabelSetOfSet(std::ostream& out, const LabelSetOfSetType& labelSet)
377 {
378  // Define an iterator on the label set
379  typename LabelSetOfSetType::const_iterator it = labelSet.begin();
380 
381  // Open the set
382  out << "{";
383 
384  // Append the set elements
385  while (it != labelSet.end())
386  {
387  PrintLabelSet(out, *it);
388  ++it;
389  if (it != labelSet.end())
390  out << ", ";
391  }
392 
393  // close the set
394  out << "}";
395 
396  // Return
397  return out;
398 }
399 
400 
401 } // end namespace otb
402 
404 /*
405 template <class TLabel>
406 std::ostream &
407 operator<<(std::ostream & out,
408  const std::set<TLabel> & labelSet)*/
409 
410 
411 #endif
otb::MassOfBelief::GetPlausibility
MassType GetPlausibility(const LabelSetType &labelSet) const
Definition: otbMassOfBelief.hxx:247
otb::MassOfBelief::GetUniverse
LabelSetType GetUniverse() const
Definition: otbMassOfBelief.hxx:92
otb::MassOfBelief
This class represent a mass of belief function.
Definition: otbMassOfBelief.h:90
otb::MassOfBelief::EstimateUncertainty
void EstimateUncertainty()
Definition: otbMassOfBelief.hxx:144
otbMath.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MassOfBelief::IsEmpty
bool IsEmpty() const
Definition: otbMassOfBelief.hxx:319
otb::MassOfBelief::RemoveMass
void RemoveMass(const LabelSetType &labelSet)
Definition: otbMassOfBelief.hxx:45
otb::MassOfBelief::Copy
void Copy(const Self *massOfBelief)
Definition: otbMassOfBelief.hxx:309
otb::MassOfBelief::MassType
TMass MassType
Definition: otbMassOfBelief.h:106
otb::MassOfBelief::Normalize
void Normalize()
Definition: otbMassOfBelief.hxx:119
otb::MassOfBelief::PrintLabelSet
static std::ostream & PrintLabelSet(std::ostream &out, const LabelSetType &labelSet)
Definition: otbMassOfBelief.hxx:351
otb::MassOfBelief::GetBelief
MassType GetBelief(const LabelSetType &labelSet) const
Definition: otbMassOfBelief.hxx:216
otb::MassOfBelief::InitializePowerSetMasses
void InitializePowerSetMasses(const LabelSetType &universe)
Definition: otbMassOfBelief.hxx:179
otb::MassOfBelief::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbMassOfBelief.hxx:325
otb::MassOfBelief::PrintLabelSetOfSet
static std::ostream & PrintLabelSetOfSet(std::ostream &out, const LabelSetOfSetType &labelSet)
Definition: otbMassOfBelief.hxx:376
otbMassOfBelief.h
otb::MassOfBelief::m_MassesMap
MassMapType m_MassesMap
Definition: otbMassOfBelief.h:195
otb::MassOfBelief::GetSupport
LabelSetOfSetType GetSupport() const
Definition: otbMassOfBelief.hxx:72
otb::MassOfBelief::SetMass
void SetMass(const LabelSetType &labelSet, const MassType &mass)
Definition: otbMassOfBelief.hxx:35
otb::MassOfBelief::GetMass
MassType GetMass(const LabelSetType &labelSet) const
Definition: otbMassOfBelief.hxx:56
otb::MassOfBelief::LabelSetType
std::set< LabelType > LabelSetType
Definition: otbMassOfBelief.h:107
otb::MassOfBelief::LabelSetOfSetType
std::set< LabelSetType > LabelSetOfSetType
Definition: otbMassOfBelief.h:109
otb::MassOfBelief::Clear
void Clear()
Definition: otbMassOfBelief.hxx:173