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