Orfeo Toolbox  4.0
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 
28 class ITK_EXPORT ParserImpl : public itk::LightObject
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", Self::NDVI);
63  m_MuParser.DefineFun("NDVI", Self::NDVI);
64 
65 #ifdef OTB_MUPARSER_HAS_CXX_LOGICAL_OPERATORS
66  /* Starting with muParser 2.0.0, logical operators have been
67  renamed to "&&" and "||", instead of "and" and "or".
68  Reintroducing pre-2.0.0 operators, so formulas can be
69  defined with "and" and "or" even after 2.0.0
70  */
71  m_MuParser.DefineOprt("and", Self::AND);
72  m_MuParser.DefineOprt("or", Self::OR);
73 #endif
74  }
75 
77  virtual void SetExpr(const std::string & Expression)
78  {
79  m_MuParser.SetExpr(Expression);
80  }
81 
83  ValueType Eval()
84  {
85  Parser::ValueType result = 0.0;
86  try
87  {
88  result = m_MuParser.Eval();
89  }
90  catch(ExceptionType &e)
91  {
92  ExceptionHandler(e);
93  }
94  return result;
95  }
96 
97 
99  void DefineVar(const std::string &sName, ValueType *fVar)
100  {
101  try
102  {
103  m_MuParser.DefineVar(sName, fVar);
104  }
105  catch(ExceptionType &e)
106  {
107  ExceptionHandler(e);
108  }
109  }
110 
112  void ClearVar()
113  {
114  m_MuParser.ClearVar();
115  }
116 
118  const std::string& GetExpr() const
119  {
120  return m_MuParser.GetExpr();
121  }
122 
124  const std::map<std::string, ValueType*>& GetVar() const
125  {
126  return m_MuParser.GetVar();
127  }
128 
130  bool CheckExpr()
131  {
132  try
133  {
134  m_MuParser.Eval();
135  }
136  catch(ExceptionType &e)
137  {
138  ExceptionHandlerDebug(e);
139  return false;
140  }
141 
142  return true;
143  }
144 
145  // Get the map with the functions
146  Parser::FunctionMapType GetFunList() const
147  {
149  const mu::funmap_type& funmap = m_MuParser.GetFunDef();
150 
151  mu::funmap_type::const_iterator funItem;
152 
153  for (funItem = funmap.begin(); funItem != funmap.end(); ++funItem)
154  {
155  output[funItem->first] = funItem->second.GetArgc();
156  }
157  return output;
158  }
159 
161  virtual void ExceptionHandler(ExceptionType &e)
162  {
163  itkExceptionMacro( << std::endl
164  << "Message: " << e.GetMsg() << std::endl
165  << "Formula: " << e.GetExpr() << std::endl
166  << "Token: " << e.GetToken() << std::endl
167  << "Position: " << e.GetPos() << std::endl
168  << std::endl);
169  // << "Errc: " << e.GetCode() << std::endl);
170  }
171 
173  virtual void ExceptionHandlerDebug(ExceptionType &e)
174  {
175  otbGenericMsgDebugMacro( << std::endl
176  << "Message: " << e.GetMsg() << std::endl
177  << "Formula: " << e.GetExpr() << std::endl
178  << "Token: " << e.GetToken() << std::endl
179  << "Position: " << e.GetPos() << std::endl
180  << std::endl);
181  // << "Errc: " << e.GetCode() << std::endl);
182  }
183 
184 
185 protected:
187  {
188  InitFun();
189  InitConst();
190  }
191 
192  virtual ~ParserImpl()
193  {
194  }
195 
196  virtual void PrintSelf(std::ostream& os, itk::Indent indent) const
197  {
198  Superclass::PrintSelf(os, indent);
199  }
200 
201 
202 private:
203  ParserImpl(const Self &); //purposely not implemented
204  void operator =(const Self &); //purposely not implemented
205 
206  mu::Parser m_MuParser;
207 
208  //---------- User Defined Functions ----------//BEGIN
209  static ValueType NDVI(ValueType r, ValueType niri)
210  {
211  if ( vcl_abs(r + niri) < 1E-6 )
212  {
213  return 0.;
214  }
215  return (niri-r)/(niri+r);
216  }
217 
218 #ifdef OTB_MUPARSER_HAS_CXX_LOGICAL_OPERATORS
219  static ValueType AND(ValueType left, ValueType right)
220  {
221  return static_cast<int>(left) && static_cast<int>(right);
222  }
223 
224  static ValueType OR(ValueType left, ValueType right)
225  {
226  return static_cast<int>(left) || static_cast<int>(right);
227  }
228 #endif
229  //---------- User Defined Functions ----------//END
230 }; // end class
231 
232 void Parser::PrintSelf(std::ostream& os, itk::Indent indent) const
233 {
234  Superclass::PrintSelf(os, indent);
235 }
236 
237 
239 : m_InternalParser( ParserImpl::New() )
240 {
241 }
242 
244 {
245 }
246 
247 void Parser::SetExpr(const std::string & Expression)
248 {
249  m_InternalParser->SetExpr(Expression);
250 }
251 
253 {
254  return m_InternalParser->Eval();
255 }
256 
257 void Parser::DefineVar(const std::string &sName, Parser::ValueType *fVar)
258 {
259  m_InternalParser->DefineVar(sName, fVar);
260 }
261 
263 {
264  m_InternalParser->ClearVar();
265 }
266 
268 {
269  return m_InternalParser->CheckExpr();
270 }
271 
272 
273 const std::string& Parser::GetExpr() const
274 {
275  return m_InternalParser->GetExpr();
276 }
277 
278 // Get the map with the variables
279 const std::map<std::string, Parser::ValueType*>& Parser::GetVar() const
280 {
281  return m_InternalParser->GetVar();
282 }
283 
284 // Get the map with the functions
286 {
287  return m_InternalParser->GetFunList();
288 }
289 
290 }//end namespace otb

Generated at Sat Mar 8 2014 16:12:26 for Orfeo Toolbox with doxygen 1.8.3.1