OTB  10.0.0
Orfeo Toolbox
otbMaskMuParserFilter.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 #ifndef otbMaskMuParserFilter_hxx
22 #define otbMaskMuParserFilter_hxx
23 
24 #include "otbMaskMuParserFilter.h"
25 #include <iostream>
26 #include <string>
27 
28 #include "itkImageRegionIterator.h"
29 #include "itkNumericTraits.h"
30 
31 namespace otb
32 {
33 
34 // constructor
35 template <class TInputImage, class TOutputImage, class TFunction>
37 {
38  this->DynamicMultiThreadingOff();
39  m_UnderflowCount = 0;
40  m_OverflowCount = 0;
41  m_ThreadUnderflow.SetSize(1);
42  m_ThreadOverflow.SetSize(1);
43  m_Expression = "";
44 }
45 
46 // Destructor
47 template <class TInputImage, class TOutputImage, class TFunction>
49 {
50 }
51 
52 template <class TInputImage, class TOutputImage, class TFunction>
53 void MaskMuParserFilter<TInputImage, TOutputImage, TFunction>::PrintSelf(std::ostream& os, itk::Indent indent) const
54 {
55  Superclass::PrintSelf(os, indent);
56 
57  os << indent << "Expression: " << m_Expression << std::endl;
58 }
59 
60 template <class TInputImage, class TOutputImage, class TFunction>
62 {
63  if (m_Expression != expression)
64  m_Expression = expression;
65  this->Modified();
66 }
67 
68 template <class TInputImage, class TOutputImage, class TFunction>
70 {
71  return m_Expression;
72 }
73 
74 template <class TInputImage, class TOutputImage, class TFunction>
76 {
77  std::vector<std::string> varList;
78  FunctorPointer tempFunctor = FunctorType::New();
79  tempFunctor->SetExpression(m_Expression);
80  FunctorType& functor = *tempFunctor;
81 
82  try
83  {
84  functor(this->GetInput()->GetPixel(this->GetInput()->GetBufferedRegion().GetIndex()));
85  }
86  catch (itk::ExceptionObject& err)
87  {
88  itkDebugMacro(<< err);
89  }
90 
91  const std::map<std::string, double*>& varMap = functor.GetVar();
92  std::map<std::string, double*>::const_iterator it;
93  for (it = varMap.begin(); it != varMap.end(); ++it)
94  {
95  varList.push_back(it->first);
96  }
97  return varList;
98 }
99 
100 
101 template <class TInputImage, class TOutputImage, class TFunction>
103 {
104  FunctorPointer tempFunctor = FunctorType::New();
105  tempFunctor->SetExpression(m_Expression);
106 
107  FunctorType& functor = *tempFunctor;
108 
109  try
110  {
111  functor(this->GetInput()->GetPixel(this->GetInput()->GetBufferedRegion().GetIndex()));
112  }
113  catch (itk::ExceptionObject& err)
114  {
115  itkDebugMacro(<< err);
116  }
117 
118  return functor.GetFunList();
119 }
120 
121 template <class TInputImage, class TOutputImage, class TFunction>
123 {
124  FunctorPointer checkFunctor = FunctorType::New();
125  checkFunctor->SetExpression(m_Expression);
126 
127  FunctorType& functor = *checkFunctor;
128 
129  try
130  {
131  functor(this->GetInput()->GetPixel(this->GetInput()->GetBufferedRegion().GetIndex()));
132  }
133  catch (itk::ExceptionObject& err)
134  {
135  itkDebugMacro(<< err);
136  return false;
137  }
138  return true;
139 }
140 
144 template <class TInputImage, class TOutputImage, class TFunction>
146 {
147 
148  typename std::vector<FunctorPointer>::iterator itFunctor;
149  unsigned int nbThreads = this->GetNumberOfWorkUnits();
150  unsigned int thread_index;
151  std::ostringstream varName;
152 
153  // Allocate and initialize the thread temporaries
154  m_ThreadUnderflow.SetSize(nbThreads);
155  m_ThreadUnderflow.Fill(0);
156  m_ThreadOverflow.SetSize(nbThreads);
157  m_ThreadOverflow.Fill(0);
158  m_VFunctor.resize(nbThreads);
159 
160  for (itFunctor = m_VFunctor.begin(); itFunctor < m_VFunctor.end(); itFunctor++)
161  *itFunctor = FunctorType::New();
162 
163  for (thread_index = 0; thread_index < nbThreads; thread_index++)
164  {
165  m_VFunctor.at(thread_index)->SetExpression(m_Expression);
166  }
167 }
168 
169 template <class TInputImage, class TOutputImage, class TFunction>
171  itk::ThreadIdType threadId)
172 {
173 
174  InputImageConstPointer inputPtr = this->GetInput();
175  OutputImagePointer outputPtr = this->GetOutput(0);
176 
177  // Define the portion of the input to walk for this thread, using
178  // the CallCopyOutputRegionToInputRegion method allows for the input
179  // and output images to be different dimensions
180  InputImageRegionType inputRegionForThread;
181  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
182 
183  // Define the iterators
184  itk::ImageRegionConstIterator<TInputImage> inputIt(inputPtr, inputRegionForThread);
185  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
186 
187  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
188 
189  inputIt.GoToBegin();
190  outputIt.GoToBegin();
191 
192  FunctorPointer functorP = m_VFunctor.at(threadId);
193  FunctorType& functor = *functorP;
194  while (!inputIt.IsAtEnd())
195  {
196  outputIt.Set(functor(inputIt.Get()));
197  ++inputIt;
198  ++outputIt;
199  progress.CompletedPixel(); // potential exception thrown here
200  }
201 }
202 
203 template <class TInputImage, class TOutputImage, class TFunction>
205 {
206 }
207 
208 } // end namespace otb
209 
210 #endif
void SetExpression(const std::string expression)
void BeforeThreadedGenerateData() override
OutputImageType::RegionType OutputImageRegionType
Parser::FunctionMapType GetFunList()
InputImageType::ConstPointer InputImageConstPointer
FunctorType::Pointer FunctorPointer
std::string GetExpression() const
OutputImageType::Pointer OutputImagePointer
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
std::vector< std::string > GetVar()
void AfterThreadedGenerateData() override
InputImageType::RegionType InputImageRegionType
std::map< std::string, int > FunctionMapType
Definition: otbParser.h:65
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.