Orfeo Toolbox  3.16
otbParser.cxx
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 #include "otbMath.h"
19 
20 #include "otbMacro.h"
21 
22 #include "muParser.h"
23 #include "otbParser.h"
24 
25 namespace otb
26 {
27 
29 {
30 public:
32  typedef ParserImpl Self;
36 
38  itkNewMacro(Self);
39 
41  itkTypeMacro(ParserImpl, itk::LightObject);
42 
44  typedef double ValueType;
45  typedef mu::Parser::exception_type ExceptionType;
46 
48  virtual void InitConst()
49  {
50  m_MuParser.DefineConst( "e", CONST_E );
51  m_MuParser.DefineConst( "log2e", CONST_LOG2E );
52  m_MuParser.DefineConst( "log10e", CONST_LOG10E );
53  m_MuParser.DefineConst( "ln2", CONST_LN2 );
54  m_MuParser.DefineConst( "ln10", CONST_LN10 );
55  m_MuParser.DefineConst( "pi", CONST_PI );
56  m_MuParser.DefineConst( "euler", CONST_EULER );
57  }
58 
60  virtual void InitFun()
61  {
62  m_MuParser.DefineFun("ndvi", NDVI);
63  m_MuParser.DefineFun("NDVI", NDVI);
64  }
65 
67  virtual void SetExpr(const std::string & Expression)
68  {
69  m_MuParser.SetExpr(Expression);
70  }
71 
73  ValueType Eval()
74  {
75  Parser::ValueType result = 0.0;
76  try
77  {
78  result = m_MuParser.Eval();
79  }
80  catch(ExceptionType &e)
81  {
82  ExceptionHandler(e);
83  }
84  return result;
85  }
86 
87 
89  void DefineVar(const std::string &sName, ValueType *fVar)
90  {
91  try
92  {
93  m_MuParser.DefineVar(sName, fVar);
94  }
95  catch(ExceptionType &e)
96  {
97  ExceptionHandler(e);
98  }
99  }
100 
102  void ClearVar()
103  {
104  m_MuParser.ClearVar();
105  }
106 
108  const std::string& GetExpr() const
109  {
110  return m_MuParser.GetExpr();
111  }
112 
114  const std::map<std::string, ValueType*>& GetVar() const
115  {
116  return m_MuParser.GetVar();
117  }
118 
120  bool CheckExpr()
121  {
122 
123  try
124  {
125  m_MuParser.Eval();
126  }
127  catch(ExceptionType &e)
128  {
129  ExceptionHandlerDebug(e);
130  return false;
131  }
132 
133  return true;
134  }
135 
136  // Get the map with the functions
137  Parser::FunctionMapType GetFunList() const
138  {
140  const mu::funmap_type& funmap = m_MuParser.GetFunDef();
141 
142  mu::funmap_type::const_iterator funItem;
143 
144  for (funItem = funmap.begin(); funItem != funmap.end(); ++funItem)
145  {
146  output[funItem->first] = funItem->second.GetArgc();
147  }
148  return output;
149  }
150 
152  virtual void ExceptionHandler(ExceptionType &e)
153  {
154  itkExceptionMacro( << std::endl
155  << "Message: " << e.GetMsg() << std::endl
156  << "Formula: " << e.GetExpr() << std::endl
157  << "Token: " << e.GetToken() << std::endl
158  << "Position: " << e.GetPos() << std::endl
159  << std::endl);
160  // << "Errc: " << e.GetCode() << std::endl);
161  }
162 
164  virtual void ExceptionHandlerDebug(ExceptionType &e)
165  {
166  otbGenericMsgDebugMacro( << std::endl
167  << "Message: " << e.GetMsg() << std::endl
168  << "Formula: " << e.GetExpr() << std::endl
169  << "Token: " << e.GetToken() << std::endl
170  << "Position: " << e.GetPos() << std::endl
171  << std::endl);
172  // << "Errc: " << e.GetCode() << std::endl);
173  }
174 
175 
176 protected:
178  {
179  InitFun();
180  InitConst();
181  }
182 
183  virtual ~ParserImpl()
184  {
185  }
186 
187  virtual void PrintSelf(std::ostream& os, itk::Indent indent) const
188  {
189  Superclass::PrintSelf(os, indent);
190  }
191 
192 
193 private:
194  ParserImpl(const Self &); //purposely not implemented
195  void operator =(const Self &); //purposely not implemented
196 
197  mu::Parser m_MuParser;
198 
199  //---------- User Defined Functions ----------//BEGIN
200  static ValueType NDVI(ValueType r, ValueType niri)
201  {
202  if ( vcl_abs(r + niri) < 1E-6 )
203  {
204  return 0.;
205  }
206  return (niri-r)/(niri+r);
207  }
208 
209  //---------- User Defined Functions ----------//END
210 }; // end class
211 
212 void Parser::PrintSelf(std::ostream& os, itk::Indent indent) const
213 {
214  Superclass::PrintSelf(os, indent);
215 }
216 
217 
219 : m_InternalParser( ParserImpl::New() )
220 {
221 }
222 
224 {
225 }
226 
227 void Parser::SetExpr(const std::string & Expression)
228 {
229  m_InternalParser->SetExpr(Expression);
230 }
231 
233 {
234  return m_InternalParser->Eval();
235 }
236 
237 void Parser::DefineVar(const std::string &sName, Parser::ValueType *fVar)
238 {
239  m_InternalParser->DefineVar(sName, fVar);
240 }
241 
243 {
244  m_InternalParser->ClearVar();
245 }
246 
248 {
249  return m_InternalParser->CheckExpr();
250 }
251 
252 
253 const std::string& Parser::GetExpr() const
254 {
255  return m_InternalParser->GetExpr();
256 }
257 
258 // Get the map with the variables
259 const std::map<std::string, Parser::ValueType*>& Parser::GetVar() const
260 {
261  return m_InternalParser->GetVar();
262 }
263 
264 // Get the map with the functions
266 {
267  return m_InternalParser->GetFunList();
268 }
269 
270 }//end namespace otb

Generated at Sun Feb 3 2013 00:41:13 for Orfeo Toolbox with doxygen 1.8.1.1