OTB  6.1.0
Orfeo Toolbox
otbVectorDataToDSValidatedVectorDataFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 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 otbVectorDataToDSValidatedVectorDataFilter_txx
22 #define otbVectorDataToDSValidatedVectorDataFilter_txx
23 
25 #include <iostream>
26 
27 namespace otb
28 {
29 
30 // Constructor
31 template <class TVectorData, class TPrecision>
34  m_Bel(0.0),
35  m_Plau(0.0),
36  m_CriterionFormula("((Belief + Plausibility)/2.)"),
37  m_CriterionThreshold(0.5),
38  m_CurrentID(0)
39 {
40  this->SetNumberOfRequiredInputs(1);
41 
42  m_Parser = ParserType::New();
43 }
44 
45 
46 template <class TVectorData, class TPrecision>
47 void
49 ::AddDescriptor(const std::string& key, std::vector<double> model)
50 {
51  bool alreadyExist = false;
52 
53  if (model.size() != 4)
54  {
55  itkExceptionMacro(<< "Wrong model! Size(=" << model.size() << ") > 4" )
56  }
57  else if (model[0]<0 || model[1]<model[0] || model[2]<model[1] || model[2]>1)
58  {
59  itkExceptionMacro(<< "Wrong model! Values have to be 0<=v1<=v2<=v3<=1" )
60  }
61  else if (model[3]<.75 || model[3]>1.0)
62  {
63  itkExceptionMacro(<< "Wrong model! Values have to be 0.75<=v4<=1" )
64  }
65  else
66  {
67  for (unsigned int i=0; i<m_DescriptorModels.size(); ++i)
68  {
69  if (m_DescriptorModels[i].first.compare(key) == 0)
70  {
71  for (unsigned int j=0; j<4; ++j)
72  {
73  m_DescriptorModels[i].second.at(j) = model[j];
74  }
75  alreadyExist = true;
76  }
77  }
78  if (!alreadyExist)
79  {
80  m_DescriptorModels.push_back(std::pair<std::string, std::vector<double> >(key, model));
81  }
82  }
83 
84  this->Modified();
85 }
86 
87 
88 template <class TVectorData, class TPrecision>
89 void
92 {
93  m_DescriptorModels.clear();
94 }
95 
96 template <class TVectorData, class TPrecision>
97 void
100 {
101  for (unsigned int i=0; i<model.size(); ++i)
102  {
103  this->AddDescriptor(model[i].first, model[i].second);
104  }
105 }
106 
107 
108 template <class TVectorData, class TPrecision>
109 void
112 {
113  //Initialize Dempster Shafer tools
114  if( m_DescriptorModels.size() == 0 )
115  {
116  itkExceptionMacro(<< "No defined Descriptor. Please add a descriptor model.");
117  }
118 
119  for (unsigned int i=0; i<m_DescriptorModels.size(); ++i)
120  {
121  typename FuzzyVarType::Pointer fuz = FuzzyVarType::New();
122  std::string fuzName, fuzName_;
123  fuzName = m_DescriptorModels[i].first;
124  fuzName_ = m_DescriptorModels[i].first + "_";
125 
126  fuz->SetMembership(fuzName,
127  0.0,
128  0.0,
129  m_DescriptorModels[i].second[0],
130  m_DescriptorModels[i].second[1],
131  0.0,
132  m_DescriptorModels[i].second[3]);
133  fuz->SetMembership(fuzName_,
134  m_DescriptorModels[i].second[1],
135  m_DescriptorModels[i].second[2],
136  1.0,
137  1.0,
138  0.0,
139  m_DescriptorModels[i].second[3]);
140 
141  m_FuzzyVars.push_back(fuz);
142 
143  m_Universe.insert(fuzName);
144  m_Universe.insert(fuzName_);
145  }
146 
147  //Initialize parser
148  m_Parser->SetExpr(m_CriterionFormula);
149  m_Parser->DefineVar("Belief", &m_Bel);
150  m_Parser->DefineVar("Plausibility", &m_Plau);
151 
152  // Output
153  this->GetOutput(0)->SetMetaDataDictionary(this->GetInput()->GetMetaDataDictionary());
154  // Retrieving root node
155  typename DataNodeType::Pointer root = this->GetOutput(0)->GetDataTree()->GetRoot()->Get();
156  // Create the document node
157  typename DataNodeType::Pointer document = DataNodeType::New();
158  document->SetNodeType(otb::DOCUMENT);
159  // Adding the layer to the data tree
160  this->GetOutput(0)->GetDataTree()->Add(document, root);
161  // Create the folder node
162  typename DataNodeType::Pointer folder = DataNodeType::New();
163  folder->SetNodeType(otb::FOLDER);
164  // Adding the layer to the data tree
165  this->GetOutput(0)->GetDataTree()->Add(folder, document);
166  this->GetOutput(0)->SetProjectionRef(this->GetInput()->GetProjectionRef());
167 
168  TreeIteratorType itVector(this->GetInput()->GetDataTree());
169  itVector.GoToBegin();
170  while (!itVector.IsAtEnd())
171  {
172  if (!itVector.Get()->IsRoot() && !itVector.Get()->IsDocument() && !itVector.Get()->IsFolder())
173  {
174  typename DataNodeType::Pointer currentGeometry = itVector.Get();
175  typename JointMassOfBeliefFilterType::Pointer jointMassFilter = JointMassOfBeliefFilterType::New();
176 
177  for (unsigned int i=0; i<m_DescriptorModels.size(); ++i)
178  {
179  if (currentGeometry->HasField(m_DescriptorModels[i].first))
180  {
181  LabelSetType H, H_;
182  std::string fuzName, fuzName_;
183  fuzName = m_DescriptorModels[i].first;
184  fuzName_ = m_DescriptorModels[i].first + "_";
185 
186  typename MassOfBeliefFunctionType::Pointer mass = MassOfBeliefFunctionType::New();
187  mass->InitializePowerSetMasses(m_Universe);
188 
189  H.insert(fuzName);
190  H_.insert(fuzName_);
191 
192  mass->SetMass(H, m_FuzzyVars[i]->GetMembership(fuzName, currentGeometry->GetFieldAsDouble(fuzName)));
193  mass->SetMass(H_, m_FuzzyVars[i]->GetMembership(fuzName_, currentGeometry->GetFieldAsDouble(fuzName)));
194 
195  mass->EstimateUncertainty();
196 
197  jointMassFilter->PushBackInput(mass);
198  }
199  }
200  jointMassFilter->Update();
201  m_Bel = jointMassFilter->GetOutput()->GetBelief(m_BeliefHypothesis);
202  m_Plau = jointMassFilter->GetOutput()->GetPlausibility(m_PlausibilityHypothesis);
203 
204  if (m_Parser->Eval() >= m_CriterionThreshold)
205  {
206  currentGeometry->SetNodeId(this->GetNextID());
207  currentGeometry->SetFieldAsDouble("Belief", m_Bel);
208  currentGeometry->SetFieldAsDouble("Plausi", m_Plau);
209  this->GetOutput(0)->GetDataTree()->Add(currentGeometry, folder);
210  }
211  }
212  ++itVector;
213  }
214 }
215 
216 
217 // PrintSelf Method
218 template <class TVectorData, class TPrecision>
219 void
221 ::PrintSelf(std::ostream& os, itk::Indent indent) const
222 {
223  Superclass::PrintSelf(os, indent);
224 }
225 
226 } // end namespace otb
227 
228 #endif
FuzzyDescriptorsModelManager::DescriptorsModelType DescriptorModelsType
virtual const ValueType & Get() const
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
bool IsAtEnd(void) const
virtual void AddDescriptor(const std::string &key, std::vector< double > model)