aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/llcalc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/llmath/llcalc.cpp')
-rw-r--r--linden/indra/llmath/llcalc.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/linden/indra/llmath/llcalc.cpp b/linden/indra/llmath/llcalc.cpp
new file mode 100644
index 0000000..74f9544
--- /dev/null
+++ b/linden/indra/llmath/llcalc.cpp
@@ -0,0 +1,161 @@
1/*
2 * LLCalc.cpp
3 * SecondLife
4 *
5 * Created by Aimee Walton on 28/09/2008.
6 * Copyright 2008 Aimee Walton.
7 *
8 */
9
10#include "linden_common.h"
11
12#include "llcalc.h"
13
14#include <boost/version.hpp>
15#if BOOST_VERSION >= 103600
16#include <boost/spirit/include/classic_core.hpp>
17#include <boost/spirit/include/classic_error_handling.hpp>
18#else
19#include <boost/spirit/core.hpp>
20#include <boost/spirit/error_handling.hpp>
21#endif
22
23#include "llcalcparser.h"
24#include "llmath.h"
25
26
27// Variable names for use in the build floater
28const char* LLCalc::X_POS = "PX";
29const char* LLCalc::Y_POS = "PY";
30const char* LLCalc::Z_POS = "PZ";
31const char* LLCalc::X_SCALE = "SX";
32const char* LLCalc::Y_SCALE = "SY";
33const char* LLCalc::Z_SCALE = "SZ";
34const char* LLCalc::X_ROT = "RX";
35const char* LLCalc::Y_ROT = "RY";
36const char* LLCalc::Z_ROT = "RZ";
37const char* LLCalc::HOLLOW = "HLW";
38const char* LLCalc::CUT_BEGIN = "CB";
39const char* LLCalc::CUT_END = "CE";
40const char* LLCalc::PATH_BEGIN = "PB";
41const char* LLCalc::PATH_END = "PE";
42const char* LLCalc::TWIST_BEGIN = "TB";
43const char* LLCalc::TWIST_END = "TE";
44const char* LLCalc::X_SHEAR = "SHX";
45const char* LLCalc::Y_SHEAR = "SHY";
46const char* LLCalc::X_TAPER = "TPX";
47const char* LLCalc::Y_TAPER = "TPY";
48const char* LLCalc::RADIUS_OFFSET = "ROF";
49const char* LLCalc::REVOLUTIONS = "REV";
50const char* LLCalc::SKEW = "SKW";
51const char* LLCalc::X_HOLE = "HLX";
52const char* LLCalc::Y_HOLE = "HLY";
53const char* LLCalc::TEX_U_SCALE = "TSU";
54const char* LLCalc::TEX_V_SCALE = "TSV";
55const char* LLCalc::TEX_U_OFFSET = "TOU";
56const char* LLCalc::TEX_V_OFFSET = "TOV";
57const char* LLCalc::TEX_ROTATION = "TROT";
58const char* LLCalc::TEX_TRANSPARENCY = "TRNS";
59const char* LLCalc::TEX_GLOW = "GLOW";
60
61
62LLCalc* LLCalc::sInstance = NULL;
63
64LLCalc::LLCalc() : mLastErrorPos(0)
65{
66// mUserVariables = new calc_map_t;
67 mVariables = new calc_map_t;
68 mConstants = new calc_map_t;
69
70 // Init table of constants
71 (*mConstants)["PI"] = F_PI;
72 (*mConstants)["TWO_PI"] = F_TWO_PI;
73 (*mConstants)["PI_BY_TWO"] = F_PI_BY_TWO;
74 (*mConstants)["SQRT2"] = F_SQRT2;
75 (*mConstants)["DEG_TO_RAD"] = DEG_TO_RAD;
76 (*mConstants)["RAD_TO_DEG"] = RAD_TO_DEG;
77 (*mConstants)["GRAVITY"] = GRAVITY;
78}
79
80LLCalc::~LLCalc()
81{
82 delete mConstants;
83 delete mVariables;
84// delete mUserVariables;
85}
86
87//static
88void LLCalc::cleanUp()
89{
90 delete sInstance;
91 sInstance = NULL;
92}
93
94//static
95LLCalc* LLCalc::getInstance()
96{
97 if (!sInstance) sInstance = new LLCalc();
98 return sInstance;
99}
100
101void LLCalc::setVar(const std::string& name, const F32& value)
102{
103 (*mVariables)[name] = value;
104}
105
106void LLCalc::clearVar(const std::string& name)
107{
108 mVariables->erase(name);
109}
110
111void LLCalc::clearAllVariables()
112{
113 mVariables->clear();
114}
115
116/*
117void LLCalc::updateVariables(LLSD& vars)
118{
119 LLSD::map_iterator cIt = vars.beginMap();
120 for(; cIt != vars.endMap(); cIt++)
121 {
122 setVar(cIt->first, (F32)(LLSD::Real)cIt->second);
123 }
124}
125*/
126
127bool LLCalc::evalString(const std::string& expression, F32& result)
128{
129 using namespace boost::spirit;
130
131 std::string expr_upper = expression;
132 LLStringUtil::toUpper(expr_upper);
133
134 LLCalcParser calc(result, mConstants, mVariables);
135
136 mLastErrorPos = 0;
137 std::string::iterator start = expr_upper.begin();
138 parse_info<std::string::iterator> info;
139
140 try
141 {
142 info = parse(start, expr_upper.end(), calc, space_p);
143 lldebugs << "Math expression: " << expression << " = " << result << llendl;
144 }
145 catch(parser_error<std::string, std::string::iterator> &e)
146 {
147 mLastErrorPos = e.where - expr_upper.begin();
148
149 llinfos << "Calc parser exception: " << e.descriptor << " at " << mLastErrorPos << " in expression: " << expression << llendl;
150 return false;
151 }
152
153 if (!info.full)
154 {
155 mLastErrorPos = info.stop - expr_upper.begin();
156 llinfos << "Unhandled syntax error at " << mLastErrorPos << " in expression: " << expression << llendl;
157 return false;
158 }
159
160 return true;
161}