Orfeo Toolbox  4.0
otbMaskMuParserFilter.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 __otbMaskMuParserFilter_txx
19 #define __otbMaskMuParserFilter_txx
20 
21 #include "otbMaskMuParserFilter.h"
22 #include <iostream>
23 #include <string>
24 
25 #include "itkImageRegionIterator.h"
27 #include "itkNumericTraits.h"
28 #include "itkProgressReporter.h"
29 
30 namespace otb
31 {
32 
33 // constructor
34 template<class TInputImage, class TOutputImage, class TFunction>
36 {
37  m_UnderflowCount = 0;
38  m_OverflowCount = 0;
39  m_ThreadUnderflow.SetSize(1);
40  m_ThreadOverflow.SetSize(1);
41  m_Expression="";
42 }
43 
44 // Destructor
45 template<class TInputImage, class TOutputImage, class TFunction>
47 {
48 
49 }
50 
51 template<class TInputImage, class TOutputImage, class TFunction>
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) m_Expression = expression;
63  this->Modified();
64 }
65 
66 template<class TInputImage, class TOutputImage, class TFunction>
68 {
69  return m_Expression;
70 }
71 
72 template<class TInputImage, class TOutputImage, class TFunction>
74 {
75  std::vector<std::string> varList;
76  FunctorPointer tempFunctor = FunctorType::New();
77  tempFunctor->SetExpression(m_Expression);
78  FunctorType& functor = *tempFunctor;
79 
80  try
81  {
82  functor(this->GetInput()->GetPixel(this->GetInput()->GetBufferedRegion().GetIndex()));
83  }
84  catch (itk::ExceptionObject& err)
85  {
86  itkDebugMacro(<< err);
87  }
88 
89  const std::map<std::string, double*>& varMap = functor.GetVar();
90  std::map<std::string, double*>::const_iterator it;
91  for (it = varMap.begin(); it != varMap.end(); ++it)
92  {
93  varList.push_back( it->first );
94  }
95  return varList;
96 }
97 
98 
99 template<class TInputImage, class TOutputImage, class TFunction>
101 {
102  FunctorPointer tempFunctor = FunctorType::New();
103  tempFunctor->SetExpression(m_Expression);
104 
105  FunctorType& functor = *tempFunctor;
106 
107  try
108  {
109  functor(this->GetInput()->GetPixel(this->GetInput()->GetBufferedRegion().GetIndex()));
110  }
111  catch (itk::ExceptionObject& err)
112  {
113  itkDebugMacro(<< err);
114  }
115 
116  return functor.GetFunList();
117 }
118 
119 template<class TInputImage, class TOutputImage, class TFunction>
121 {
122  FunctorPointer checkFunctor = FunctorType::New();
123  checkFunctor->SetExpression(m_Expression);
124 
125  FunctorType& functor = *checkFunctor;
126 
127  try
128  {
129  functor(this->GetInput()->GetPixel(this->GetInput()->GetBufferedRegion().GetIndex()));
130  }
131  catch (itk::ExceptionObject& err)
132  {
133  itkDebugMacro(<< err);
134  return false;
135  }
136  return true;
137 }
138 
142 template<class TInputImage, class TOutputImage, class TFunction>
144 {
145 
146  typename std::vector<FunctorPointer>::iterator itFunctor;
147  unsigned int nbThreads = this->GetNumberOfThreads();
148  unsigned int thread_index;
149  std::ostringstream varName;
150 
151  // Allocate and initialize the thread temporaries
152  m_ThreadUnderflow.SetSize(nbThreads);
153  m_ThreadUnderflow.Fill(0);
154  m_ThreadOverflow.SetSize(nbThreads);
155  m_ThreadOverflow.Fill(0);
156  m_VFunctor.resize(nbThreads);
157 
158  for (itFunctor = m_VFunctor.begin(); itFunctor < m_VFunctor.end(); itFunctor++)
159  *itFunctor = FunctorType::New();
160 
161  for (thread_index = 0; thread_index < nbThreads; thread_index++)
162  {
163  m_VFunctor.at(thread_index)->SetExpression(m_Expression);
164  }
165 
166 }
167 
168 template<class TInputImage, class TOutputImage, class TFunction>
170  const OutputImageRegionType &outputRegionForThread,
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

Generated at Sat Mar 8 2014 16:06:54 for Orfeo Toolbox with doxygen 1.8.3.1