OTB  9.0.0
Orfeo Toolbox
otbJointMassOfBeliefFilter.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 otbJointMassOfBeliefFilter_hxx
22 #define otbJointMassOfBeliefFilter_hxx
23 
25 
26 namespace otb
27 {
28 template <class TMassFunction>
30 {
31  // Set the number of outputs
32  this->SetNumberOfRequiredOutputs(1);
33 
34  // Build the output
35  typename MassFunctionType::Pointer outputPtr = MassFunctionType::New();
36  this->SetNthOutput(0, outputPtr.GetPointer());
37 }
38 
39 template <class TMassFunction>
41 {
42  this->itk::ProcessObject::PushBackInput(input);
43 }
44 
45 template <class TMassFunction>
47 {
48  this->itk::ProcessObject::PushFrontInput(input);
49 }
50 
51 template <class TMassFunction>
53 {
54  this->itk::ProcessObject::PopBackInput();
55 }
56 
57 template <class TMassFunction>
59 {
60  this->itk::ProcessObject::PopFrontInput();
61 }
62 
63 template <class TMassFunction>
65 {
66  return static_cast<const MassFunctionType*>(this->itk::ProcessObject::GetInput(idx));
67 }
68 
69 template <class TMassFunction>
71 {
72  if (this->GetNumberOfOutputs() < 1)
73  {
74  return nullptr;
75  }
76  return static_cast<MassFunctionType*>(this->itk::ProcessObject::GetOutput(0));
77 }
78 
79 template <class TMassFunction>
81 {
82  // Retrieve the output pointer
83  typename MassFunctionType::Pointer outputPtr = this->GetOutput();
84 
85  // Walk the inputs
86  for (unsigned int i = 0; i < this->GetNumberOfInputs(); ++i)
87  {
88  // Retrieve the ith input mass function
89  typename MassFunctionType::ConstPointer inputPtr = this->GetInput(i);
90 
91  // Combine it with the current joint mass
92  this->CombineMasses(inputPtr, outputPtr);
93  }
94 }
95 
96 template <class TMassFunction>
97 void JointMassOfBeliefFilter<TMassFunction>::PrintSelf(std::ostream& os, itk::Indent indent) const
98 {
99  // Call superclass implementation
100  Superclass::PrintSelf(os, indent);
101 }
102 
103 template <class TMassFunction>
105 {
106  // Handle case where output is empty
107  if (output->IsEmpty())
108  {
109  // In this case, copy the input mass function
110  output->Copy(input);
111 
112  // And return
113  return;
114  }
115 
116  // Define a temporary mass function
117  typename MassFunctionType::Pointer newJointMass = MassFunctionType::New();
118 
119  // Conflict sum will be used to wheight the final masses
120  MassType conflict = itk::NumericTraits<MassType>::Zero;
121 
122  // Store joint mass support set so as to avoid computing it again
123  // when normalizing by conflict
124  LabelSetOfSetType jointSupport;
125 
126  // First, retrieve the support set of input and currentJointMap
127  LabelSetOfSetType inputSupportSet = input->GetSupport();
128  LabelSetOfSetType currentSupportSet = output->GetSupport();
129 
130  // Then, Walk the two sets
131  for (typename LabelSetOfSetType::const_iterator inputIt = inputSupportSet.begin(); inputIt != inputSupportSet.end(); ++inputIt)
132  {
133  for (typename LabelSetOfSetType::const_iterator currentIt = currentSupportSet.begin(); currentIt != currentSupportSet.end(); ++currentIt)
134  {
135  // Compute intersection
136  LabelSetType intersectionSet;
137  std::insert_iterator<LabelSetType> interIt(intersectionSet, intersectionSet.begin());
138 
139  // Perform set intersection
140  std::set_intersection(inputIt->begin(), inputIt->end(), currentIt->begin(), currentIt->end(), interIt);
141 
142  // Compute mass product
143  MassType massProduct = input->GetMass((*inputIt)) * output->GetMass((*currentIt));
144 
145  // If intersection is null, update conflict
146  if (intersectionSet.empty())
147  {
148  conflict += massProduct;
149  }
150  // Else, update output mass
151  else
152  {
153  // Retrieve current mass for this intersection set
154  MassType intersectionMass = newJointMass->GetMass(intersectionSet);
155 
156  // Cumulate with product of masses
157  intersectionMass += massProduct;
158 
159  // Update new joint mass
160  newJointMass->SetMass(intersectionSet, intersectionMass);
161 
162  // Store in joint support
163  jointSupport.insert(intersectionSet);
164  }
165  }
166  }
167  // Normalize new joint mass with conflict
168  MassType conflictCoefficient = 1 / (1 - conflict);
169 
170  // Retrieve support of joint mass
171  for (typename LabelSetOfSetType::const_iterator it = jointSupport.begin(); it != jointSupport.end(); ++it)
172  {
173  // Retrieve joint mass
174  MassType jointMass = newJointMass->GetMass((*it));
175 
176  // Normalize by conflict
177  jointMass *= conflictCoefficient;
178 
179  // Update joint mass
180  newJointMass->SetMass((*it), jointMass);
181  }
182 
183  // Finally, swap output and newJointMass function
184  output->Copy(newJointMass);
185 }
186 
187 } // end namespace otb
188 
189 #ifndef OTB_MANUAL_INSTANTIATION
191 #endif
192 
193 #endif
otb::JointMassOfBeliefFilter::GenerateData
void GenerateData() override
Definition: otbJointMassOfBeliefFilter.hxx:80
otb::JointMassOfBeliefFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbJointMassOfBeliefFilter.hxx:97
otb::JointMassOfBeliefFilter::PopBackInput
void PopBackInput() override
Definition: otbJointMassOfBeliefFilter.hxx:52
otb::JointMassOfBeliefFilter::MassFunctionType
TMassFunction MassFunctionType
Definition: otbJointMassOfBeliefFilter.h:54
otbJointMassOfBeliefFilter.hxx
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbJointMassOfBeliefFilter.h
otb::JointMassOfBeliefFilter::PushBackInput
virtual void PushBackInput(const MassFunctionType *input)
Definition: otbJointMassOfBeliefFilter.hxx:40
otb::JointMassOfBeliefFilter::CombineMasses
void CombineMasses(const MassFunctionType *input, MassFunctionType *output)
Definition: otbJointMassOfBeliefFilter.hxx:104
otb::JointMassOfBeliefFilter::MassType
MassFunctionType::MassType MassType
Definition: otbJointMassOfBeliefFilter.h:60
otb::JointMassOfBeliefFilter::GetInput
const MassFunctionType * GetInput(unsigned int idx)
Definition: otbJointMassOfBeliefFilter.hxx:64
otb::JointMassOfBeliefFilter::JointMassOfBeliefFilter
JointMassOfBeliefFilter()
Definition: otbJointMassOfBeliefFilter.hxx:29
otb::JointMassOfBeliefFilter::PopFrontInput
void PopFrontInput() override
Definition: otbJointMassOfBeliefFilter.hxx:58
otb::JointMassOfBeliefFilter::LabelSetOfSetType
MassFunctionType::LabelSetOfSetType LabelSetOfSetType
Definition: otbJointMassOfBeliefFilter.h:62
otb::JointMassOfBeliefFilter::GetOutput
MassFunctionType * GetOutput()
Definition: otbJointMassOfBeliefFilter.hxx:70
otb::JointMassOfBeliefFilter::PushFrontInput
virtual void PushFrontInput(const MassFunctionType *input)
Definition: otbJointMassOfBeliefFilter.hxx:46
otb::JointMassOfBeliefFilter::LabelSetType
MassFunctionType::LabelSetType LabelSetType
Definition: otbJointMassOfBeliefFilter.h:61