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