OTB  10.0.0
Orfeo Toolbox
otbPathListToHistogramGenerator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 
22 #ifndef otbPathListToHistogramGenerator_hxx
23 #define otbPathListToHistogramGenerator_hxx
24 
26 
27 namespace otb
28 {
29 
30 template <class TPath, class TFunction>
32 {
33  m_HistogramGenerator = GeneratorType::New();
34 
35  this->SetNumberOfRequiredInputs(1);
36  this->SetNumberOfRequiredOutputs(1);
37 
38  this->itk::ProcessObject::SetNthOutput(0, this->MakeOutput(0).GetPointer());
39 }
40 
41 template <class TPath, class TFunction>
43 {
44  // Process object is not const-correct so the const_cast is required here
45  this->itk::ProcessObject::SetNthInput(0, const_cast<PathListType*>(path));
46 }
47 
48 template <class TPath, class TFunction>
50 {
51  if (this->GetNumberOfInputs() < 1)
52  {
53  return nullptr;
54  }
55  return static_cast<const PathListType*>(this->itk::ProcessObject::GetInput(0));
56 }
57 
61 template <class TPath, class TFunction>
63 {
64  this->GraftNthOutput(0, graft);
65 }
66 
71 template <class TPath, class TFunction>
72 void PathListToHistogramGenerator<TPath, TFunction>::GraftNthOutput(unsigned int idx, itk::DataObject* graft)
73 {
74  if (idx >= this->GetNumberOfOutputs())
75  {
76  itkExceptionMacro(<< "Requested to graft output " << idx << " but this filter only has " << this->GetNumberOfOutputs() << " Outputs.");
77  }
78 
79  if (!graft)
80  {
81  itkExceptionMacro(<< "Requested to graft output that is a NULL pointer");
82  }
83 
84  itk::DataObject* output = const_cast<HistogramType*>(this->GetOutput());
85 
86  // Call GraftImage to copy meta-information, regions, and the pixel container
87  output->Graft(graft);
88 }
89 
90 template <class TPath, class TFunction>
93 {
94  DataObjectPointer output;
95  output = static_cast<itk::DataObject*>(HistogramType::New().GetPointer());
96  return output;
97 }
98 
99 
100 template <class TPath, class TFunction>
102 {
103  return dynamic_cast<const HistogramType*>(this->itk::ProcessObject::GetOutput(0));
104 }
105 
106 template <class TPath, class TFunction>
108 {
109  // Get the input pathlist
110  typename PathListType::ConstPointer pathList = this->GetInput();
111 
112  // Path pointer
113  PathPointer vertexList;
114 
115  ListSamplePointer listSample = ListSampleType::New();
116  int nbElementPathList = pathList->Size();
117 
118  ListSampleVectorType ResultFunction;
119 
120  typename TFunction::Pointer function = TFunction::New();
121 
122  for (int noPathList = 0; noPathList < nbElementPathList; noPathList++)
123  {
124  vertexList = pathList->GetNthElement(noPathList); //(*pathList)[noPathList];
125  function->SetInputPath(vertexList);
126 
127  ResultFunction[0] = static_cast<MeasurementType>(function->Evaluate());
128  // Set the ListSample MeasurementVectorSize once before the PushBack
129  if (noPathList == 0)
130  listSample->SetMeasurementVectorSize(ResultFunction.Size());
131 
132  listSample->PushBack(ResultFunction);
133  }
134 
135  m_HistogramGenerator->SetInput(listSample);
136  m_HistogramGenerator->GraftOutput(const_cast<HistogramType*>(this->GetOutput()));
137  m_HistogramGenerator->Update();
138  this->GraftOutput(const_cast<HistogramType*>(m_HistogramGenerator->GetOutput()));
139 }
140 
141 template <class TPath, class TFunction>
143 {
144  m_HistogramGenerator->SetHistogramSize(size);
145 }
146 
147 template <class TPath, class TFunction>
149 {
150  m_HistogramGenerator->SetMarginalScale(marginalScale);
151 }
152 
153 template <class TPath, class TFunction>
155 {
156  m_HistogramGenerator->SetHistogramMin(histogramMin);
157 }
158 
159 template <class TPath, class TFunction>
161 {
162  m_HistogramGenerator->SetHistogramMax(histogramMax);
163 }
164 
165 template <class TPath, class TFunction>
167 {
168  m_HistogramGenerator->SetAutoMinMax(autoMinMax);
169 }
170 
171 template <class TPath, class TFunction>
172 void PathListToHistogramGenerator<TPath, TFunction>::PrintSelf(std::ostream& os, itk::Indent indent) const
173 {
174  Superclass::PrintSelf(os, indent);
175  os << "PathList = " << this->GetInput() << std::endl;
176  os << "HistogramGenerator = " << m_HistogramGenerator << std::endl;
177 }
178 
179 } // end of namespace otb
180 
181 #endif
This class is a generic all-purpose wrapping around an std::vector<itk::SmartPointer<ObjectType> >.
Definition: otbObjectList.h:41
itk::SmartPointer< const Self > ConstPointer
Definition: otbObjectList.h:47
void GraftNthOutput(unsigned int idx, itk::DataObject *graft)
void SetHistogramMax(const MeasurementVectorType &histogramMax)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
HistogramType::MeasurementVectorType MeasurementVectorType
itk::Vector< MeasurementType, 1 > ListSampleVectorType
itk::Statistics::Histogram< MeasurementType, FrequencyContainerType > HistogramType
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType) override
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
void SetHistogramMin(const MeasurementVectorType &histogramMin)
Superclass::DataObjectPointer DataObjectPointer
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.