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