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