diff options
Diffstat (limited to 'linden/indra/llmath/llcalc.cpp')
-rw-r--r-- | linden/indra/llmath/llcalc.cpp | 161 |
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 | ||
28 | const char* LLCalc::X_POS = "PX"; | ||
29 | const char* LLCalc::Y_POS = "PY"; | ||
30 | const char* LLCalc::Z_POS = "PZ"; | ||
31 | const char* LLCalc::X_SCALE = "SX"; | ||
32 | const char* LLCalc::Y_SCALE = "SY"; | ||
33 | const char* LLCalc::Z_SCALE = "SZ"; | ||
34 | const char* LLCalc::X_ROT = "RX"; | ||
35 | const char* LLCalc::Y_ROT = "RY"; | ||
36 | const char* LLCalc::Z_ROT = "RZ"; | ||
37 | const char* LLCalc::HOLLOW = "HLW"; | ||
38 | const char* LLCalc::CUT_BEGIN = "CB"; | ||
39 | const char* LLCalc::CUT_END = "CE"; | ||
40 | const char* LLCalc::PATH_BEGIN = "PB"; | ||
41 | const char* LLCalc::PATH_END = "PE"; | ||
42 | const char* LLCalc::TWIST_BEGIN = "TB"; | ||
43 | const char* LLCalc::TWIST_END = "TE"; | ||
44 | const char* LLCalc::X_SHEAR = "SHX"; | ||
45 | const char* LLCalc::Y_SHEAR = "SHY"; | ||
46 | const char* LLCalc::X_TAPER = "TPX"; | ||
47 | const char* LLCalc::Y_TAPER = "TPY"; | ||
48 | const char* LLCalc::RADIUS_OFFSET = "ROF"; | ||
49 | const char* LLCalc::REVOLUTIONS = "REV"; | ||
50 | const char* LLCalc::SKEW = "SKW"; | ||
51 | const char* LLCalc::X_HOLE = "HLX"; | ||
52 | const char* LLCalc::Y_HOLE = "HLY"; | ||
53 | const char* LLCalc::TEX_U_SCALE = "TSU"; | ||
54 | const char* LLCalc::TEX_V_SCALE = "TSV"; | ||
55 | const char* LLCalc::TEX_U_OFFSET = "TOU"; | ||
56 | const char* LLCalc::TEX_V_OFFSET = "TOV"; | ||
57 | const char* LLCalc::TEX_ROTATION = "TROT"; | ||
58 | const char* LLCalc::TEX_TRANSPARENCY = "TRNS"; | ||
59 | const char* LLCalc::TEX_GLOW = "GLOW"; | ||
60 | |||
61 | |||
62 | LLCalc* LLCalc::sInstance = NULL; | ||
63 | |||
64 | LLCalc::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 | |||
80 | LLCalc::~LLCalc() | ||
81 | { | ||
82 | delete mConstants; | ||
83 | delete mVariables; | ||
84 | // delete mUserVariables; | ||
85 | } | ||
86 | |||
87 | //static | ||
88 | void LLCalc::cleanUp() | ||
89 | { | ||
90 | delete sInstance; | ||
91 | sInstance = NULL; | ||
92 | } | ||
93 | |||
94 | //static | ||
95 | LLCalc* LLCalc::getInstance() | ||
96 | { | ||
97 | if (!sInstance) sInstance = new LLCalc(); | ||
98 | return sInstance; | ||
99 | } | ||
100 | |||
101 | void LLCalc::setVar(const std::string& name, const F32& value) | ||
102 | { | ||
103 | (*mVariables)[name] = value; | ||
104 | } | ||
105 | |||
106 | void LLCalc::clearVar(const std::string& name) | ||
107 | { | ||
108 | mVariables->erase(name); | ||
109 | } | ||
110 | |||
111 | void LLCalc::clearAllVariables() | ||
112 | { | ||
113 | mVariables->clear(); | ||
114 | } | ||
115 | |||
116 | /* | ||
117 | void 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 | |||
127 | bool 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 | } | ||