summaryrefslogtreecommitdiff
blob: 3ee0dee0bff7ba47ea697b8b5b593353588f5821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
Contains revisions 6407:917d117ba6b0 6597:5b768c308932 6688:dde84e96b755 6689:e237d6d1d873 of ParseExp.cpp/h

diff -r 917d117ba6b0 -r e237d6d1d873 src/tools/ParseExp.cpp
--- a/src/tools/ParseExp.cpp	Sun Dec 15 18:20:14 2013 +0100
+++ b/src/tools/ParseExp.cpp	Sat Sep 13 11:22:39 2014 +0200
@@ -1,262 +1,246 @@
-// -*- c-basic-offset: 4 -*-
-
-/** @file ParseExp.cpp
- *
- *  @brief functions to parse expressions from strings
- *
- *  @author T. Modes
- *
- */
-
-/*  This program is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU General Public
- *  License as published by the Free Software Foundation; either
- *  version 2 of the License, or (at your option) any later version.
- *
- *  This software is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *  General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public
- *  License along with this software; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- */
-
-// implementation is based on blog at
-// http://agentzlerich.blogspot.de/2011/06/using-boost-spirit-21-to-evaluate.html
-// modified to Hugins need
-// added if statement
-
-#include "ParseExp.h"
-
-#include <limits>
-#include <iterator>
-
-#include <boost/spirit/version.hpp>
-#if !defined(SPIRIT_VERSION) || SPIRIT_VERSION < 0x2010
-#error "At least Spirit version 2.1 required"
-#endif
-#include <boost/math/constants/constants.hpp>
-#include <boost/spirit/include/phoenix.hpp>
-#include <boost/spirit/include/qi.hpp>
-
-namespace Parser
-{
-
-// helper classes to implement operators
-
-//power function
-struct lazy_pow_
-{
-    template <typename X, typename Y>
-    struct result { typedef X type; };
-
-    template <typename X, typename Y>
-    X operator()(X x, Y y) const
-    {
-        return std::pow(x, y);
-    }
-};
-
-// modulus for double values
-struct lazy_mod_
-{
-    template <typename X, typename Y>
-    struct result { typedef X type; };
-
-    template <typename X, typename Y>
-    X operator()(X x, Y y) const
-    {
-        return std::fmod(x,y);
-    }
-};
-
-// if statement
-struct lazy_if_
-{
-    template <typename X, typename Y, typename Z>
-    struct result { typedef Y type; };
-
-    template <typename X, typename Y, typename Z>
-    X operator()(X x, Y y, Z z) const
-    {
-        return x ? y : z;
-    }
-};
-
-// wrapper for unary function
-struct lazy_ufunc_
-{
-    template <typename F, typename A1>
-    struct result { typedef A1 type; };
-
-    template <typename F, typename A1>
-    A1 operator()(F f, A1 a1) const
-    {
-        return f(a1);
-    }
-};
-
-// convert rad into deg
-double deg(const double d)
-{
-    return d*180.0/boost::math::constants::pi<double>();
-};
-
-// convert deg into rad
-double rad(const double d)
-{
-    return d*boost::math::constants::pi<double>()/180;
-};
-
-// the main grammar class
-struct grammar:boost::spirit::qi::grammar<std::string::const_iterator, double(), boost::spirit::ascii::space_type>
-{
-
-    // symbol table for constants like "pi", e.g. image number and value
-    struct constant_ : boost::spirit::qi::symbols<char, double>
-    {
-        constant_(const ConstantMap constMap)
-        {
-            this->add("pi", boost::math::constants::pi<double>());
-            if(constMap.size()>0)
-            {
-                for(ConstantMap::const_iterator it=constMap.begin(); it!=constMap.end(); it++)
-                {
-                    this->add(it->first, it->second); 
-                };
-            };
-        };
-    };
-
-    // symbol table for unary functions like "abs"
-    struct ufunc_  : boost::spirit::qi::symbols<char, double(*)(double) >
-    {
-        ufunc_()
-        {
-            this->add
-                ("abs"   , (double (*)(double)) std::abs  )
-                ("acos"  , (double (*)(double)) std::acos )
-                ("asin"  , (double (*)(double)) std::asin )
-                ("atan"  , (double (*)(double)) std::atan )
-                ("ceil"  , (double (*)(double)) std::ceil )
-                ("sin"   , (double (*)(double)) std::sin  )
-                ("cos"   , (double (*)(double)) std::cos  )
-                ("tan"   , (double (*)(double)) std::tan  )
-                ("exp"   , (double (*)(double)) std::exp  )
-                ("floor" , (double (*)(double)) std::floor)
-                ("sqrt"  , (double (*)(double)) std::sqrt )
-                ("deg"   , (double (*)(double)) deg  )
-                ("rad"   , (double (*)(double)) rad  )
-            ;
-        }
-    } ufunc;
-
-    boost::spirit::qi::rule<std::string::const_iterator, double(), boost::spirit::ascii::space_type> expression, term, factor, primary, compExpression, compTerm, numExpression;
-
-    grammar(const ConstantMap constMap) : grammar::base_type(expression)
-    {
-        using boost::spirit::qi::real_parser;
-        using boost::spirit::qi::real_policies;
-        real_parser<double,real_policies<double> > real;
-
-        using boost::spirit::qi::_1;
-        using boost::spirit::qi::_2;
-        using boost::spirit::qi::_3;
-        using boost::spirit::qi::no_case;
-        using boost::spirit::qi::_val;
-        struct constant_ constant(constMap);
-
-        boost::phoenix::function<lazy_pow_>   lazy_pow;
-        boost::phoenix::function<lazy_mod_>   lazy_mod;
-        boost::phoenix::function<lazy_if_>    lazy_if;
-        boost::phoenix::function<lazy_ufunc_> lazy_ufunc;
-
-        expression = 
-            (compExpression >> '\?' >> compExpression >> ':' >> compExpression) [_val = lazy_if(_1, _2, _3)]
-            | compExpression [_val=_1]
-            ;
-        
-        compExpression=
-            compTerm  [_val=_1]
-            >> * ( ("&&" >> compTerm [_val = _val && _1] )
-                  |("||" >> compTerm [_val = _val || _1] )
-                 )
-            ;
-
-        compTerm =
-            numExpression                [_val = _1        ]
-            >>*( ( '<'  >> numExpression [_val = _val <  _1])
-                |( '>'  >> numExpression [_val = _val >  _1])
-                |( "<=" >> numExpression [_val = _val <= _1])
-                |( ">=" >> numExpression [_val = _val >= _1])
-                |( "==" >> numExpression [_val = _val == _1])
-                |( "!=" >> numExpression [_val = _val != _1])
-               )
-            ;
-
-        numExpression =
-            term                   [_val =  _1]
-            >> *(  ('+' >> term    [_val += _1])
-                |  ('-' >> term    [_val -= _1])
-                )
-            ;
-
-        term =
-            factor                 [_val =  _1]
-            >> *(  ('*' >> factor  [_val *= _1])
-                |  ('/' >> factor  [_val /= _1])
-                |  ('%' >> factor  [_val = lazy_mod(_val, _1)])
-                )
-            ;
-
-        factor =
-            primary                [_val =  _1]
-            >> *(  ('^' >> factor [_val = lazy_pow(_val, _1)]) )
-            ;
-
-        primary =
-            real                   [_val =  _1]
-            |  '(' >> expression   [_val =  _1] >> ')'
-            |  ('-' >> primary     [_val = -_1])
-            |  ('+' >> primary     [_val =  _1])
-            |  no_case[constant]   [_val =  _1]
-            |  (no_case[ufunc] >> '(' >> expression >> ')') [_val = lazy_ufunc(_1, _2) ]
-            ;
-
-    };
-};
-
-//template <typename ParserType, typename Iterator>
-bool parse(std::string::const_iterator &iter,
-           std::string::const_iterator end,
-           const grammar &g,
-           double& result)
-{
-    if(!boost::spirit::qi::phrase_parse(iter, end, g, boost::spirit::ascii::space, result))
-    {
-        return false;
-    };
-    // we check if the full string could parsed
-    return iter==end;
-}
-
-// the function which exposes the interface to external
-// version without pre-defined constants 
-bool ParseExpression(const std::string& expression, double& result)
-{
-    ConstantMap constants;
-    return ParseExpression(expression, result, constants);
-};
-    
-// version with pre-defined constants
-bool ParseExpression(const std::string& expression, double& result, const ConstantMap& constants)
-{
-    grammar g(constants);
-    std::string::const_iterator it=expression.begin();
-    return parse(it, expression.end(), g, result);
-};
-
-} // namespace
+// -*- c-basic-offset: 4 -*-
+
+/** @file ParseExp.cpp
+ *
+ *  @brief functions to parse expressions from strings
+ *
+ *  @author T. Modes
+ *
+ */
+
+/*  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This software is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public
+ *  License along with this software; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+// implementation is based on blog at
+// http://agentzlerich.blogspot.de/2011/06/using-boost-spirit-21-to-evaluate.html
+// modified to Hugins need
+// added if statement
+
+#include "ParseExp.h"
+
+#include <limits>
+#include <iterator>
+
+#define BOOST_SPIRIT_USE_PHOENIX_V3 1
+#include <boost/spirit/version.hpp>
+#if !defined(SPIRIT_VERSION) || SPIRIT_VERSION < 0x2010
+#error "At least Spirit version 2.1 required"
+#endif
+#include <boost/math/constants/constants.hpp>
+#include <boost/spirit/include/phoenix.hpp>
+#include <boost/spirit/include/qi.hpp>
+
+namespace Parser
+{
+
+// helper classes to implement operators
+
+//power function
+struct lazy_pow_
+{
+    typedef double result_type;
+
+    double operator()(double x, double y) const
+    {
+        return std::pow(x, y);
+    }
+};
+
+// modulus for double values
+struct lazy_mod_
+{
+    typedef double result_type;
+
+    double operator()(double x, double y) const
+    {
+        return std::fmod(x,y);
+    }
+};
+
+// if statement
+struct lazy_if_
+{
+    typedef double result_type;
+
+    double operator()(double x, double y, double z) const
+    {
+        return (std::fabs(x)>1e-5) ? y : z;
+    }
+};
+
+// wrapper for unary function
+struct lazy_ufunc_
+{
+    typedef double result_type;
+
+    double operator()(double (*f)(double), double a1) const
+    {
+        return f(a1);
+    }
+};
+
+// convert rad into deg
+const double deg(const double d)
+{
+    return d*180.0/boost::math::constants::pi<double>();
+};
+
+// convert deg into rad
+const double rad(const double d)
+{
+    return d*boost::math::constants::pi<double>()/180;
+};
+
+// the main grammar class
+struct grammar:boost::spirit::qi::grammar<std::string::const_iterator, double(), boost::spirit::ascii::space_type>
+{
+
+    // symbol table for constants like "pi", e.g. image number and value
+    struct constant_ : boost::spirit::qi::symbols<char, double>
+    {
+        constant_(const ConstantMap constMap)
+        {
+            this->add("pi", boost::math::constants::pi<double>());
+            if (constMap.size()>0)
+            {
+                for (ConstantMap::const_iterator it = constMap.begin(); it != constMap.end(); it++)
+                {
+                    this->add(it->first, it->second);
+                };
+            };
+        };
+    };
+
+    // symbol table for unary functions like "abs"
+    struct ufunc_ : boost::spirit::qi::symbols<char, double(*)(double) >
+    {
+        ufunc_()
+        {
+            this->add
+                ("abs", (double(*)(double)) std::abs)
+                ("acos", (double(*)(double)) std::acos)
+                ("asin", (double(*)(double)) std::asin)
+                ("atan", (double(*)(double)) std::atan)
+                ("ceil", (double(*)(double)) std::ceil)
+                ("sin", (double(*)(double)) std::sin)
+                ("cos", (double(*)(double)) std::cos)
+                ("tan", (double(*)(double)) std::tan)
+                ("exp", (double(*)(double)) std::exp)
+                ("floor", (double(*)(double)) std::floor)
+                ("sqrt", (double(*)(double)) std::sqrt)
+                ("deg", (double(*)(double)) deg)
+                ("rad", (double(*)(double)) rad)
+                ;
+        }
+    } ufunc;
+
+    boost::spirit::qi::rule<std::string::const_iterator, double(), boost::spirit::ascii::space_type> expression, term, factor, primary, compExpression, compTerm, numExpression;
+
+    grammar(const ConstantMap constMap) : grammar::base_type(expression)
+    {
+        using boost::spirit::qi::real_parser;
+        using boost::spirit::qi::real_policies;
+        real_parser<double, real_policies<double> > real;
+
+        using boost::spirit::qi::_1;
+        using boost::spirit::qi::_2;
+        using boost::spirit::qi::_3;
+        using boost::spirit::qi::no_case;
+        using boost::spirit::qi::_val;
+        struct constant_ constant(constMap);
+
+        boost::phoenix::function<lazy_pow_>   lazy_pow;
+        boost::phoenix::function<lazy_mod_>   lazy_mod;
+        boost::phoenix::function<lazy_if_>    lazy_if;
+        boost::phoenix::function<lazy_ufunc_> lazy_ufunc;
+
+        expression =
+            (compExpression >> '\?' >> compExpression >> ':' >> compExpression)[_val = lazy_if(_1, _2, _3)]
+            | compExpression[_val = _1]
+            ;
+
+        compExpression =
+            compTerm[_val = _1]
+            >> *(("&&" >> compTerm[_val = _val && _1])
+            | ("||" >> compTerm[_val = _val || _1])
+            )
+            ;
+
+        compTerm =
+            numExpression[_val = _1]
+            >> *(('<' >> numExpression[_val = _val <  _1])
+            | ('>' >> numExpression[_val = _val >  _1])
+            | ("<=" >> numExpression[_val = _val <= _1])
+            | (">=" >> numExpression[_val = _val >= _1])
+            | ("==" >> numExpression[_val = _val == _1])
+            | ("!=" >> numExpression[_val = _val != _1])
+            )
+            ;
+
+        numExpression =
+            term[_val = _1]
+            >> *(('+' >> term[_val += _1])
+            | ('-' >> term[_val -= _1])
+            )
+            ;
+
+        term =
+            factor[_val = _1]
+            >> *(('*' >> factor[_val *= _1])
+            | ('/' >> factor[_val /= _1])
+            | ('%' >> factor[_val = lazy_mod(_val, _1)])
+            )
+            ;
+
+        factor =
+            primary[_val = _1]
+            >> *(('^' >> factor[_val = lazy_pow(_val, _1)]))
+            ;
+
+        primary =
+            real[_val = _1]
+            | '(' >> expression[_val = _1] >> ')'
+            | ('-' >> primary[_val = -_1])
+            | ('+' >> primary[_val = _1])
+            | no_case[constant][_val = _1]
+            | (no_case[ufunc] >> '(' >> expression >> ')')[_val = lazy_ufunc(_1, _2)]
+            ;
+
+    };
+};
+
+bool parse(std::string::const_iterator& iter,
+           std::string::const_iterator end,
+           const grammar& g,
+           double& result)
+{
+    if(!boost::spirit::qi::phrase_parse(iter, end, g, boost::spirit::ascii::space, result))
+    {
+        return false;
+    };
+    // we check if the full string could parsed
+    return iter==end;
+}
+
+// version with pre-defined constants
+bool ParseExpression(const std::string& expression, double& result, const ConstantMap& constants)
+{
+    grammar g(constants);
+    std::string::const_iterator it=expression.begin();
+    return parse(it, expression.end(), g, result);
+};
+
+} // namespace
diff -r 917d117ba6b0 -r e237d6d1d873 src/tools/ParseExp.h
--- a/src/tools/ParseExp.h	Sun Dec 15 18:20:14 2013 +0100
+++ b/src/tools/ParseExp.h	Sat Sep 13 11:22:39 2014 +0200
@@ -33,8 +33,7 @@
 {
 typedef std::map<const char*, double> ConstantMap;
 
-bool ParseExpression(const std::string& expression, double& result);
-bool ParseExpression(const std::string& expression, double& result, const ConstantMap& constants);
+bool ParseExpression(const std::string& expression, double& result, const ConstantMap& constants=ConstantMap());
 
 };