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