aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmath/llcalc.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--linden/indra/llmath/llcalc.cpp155
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
22const char* LLCalc::X_POS = "X";
23const char* LLCalc::Y_POS = "Y";
24const char* LLCalc::Z_POS = "Z";
25const char* LLCalc::X_SCALE = "XS";
26const char* LLCalc::Y_SCALE = "YS";
27const char* LLCalc::Z_SCALE = "ZS";
28const char* LLCalc::X_ROT = "XR";
29const char* LLCalc::Y_ROT = "YR";
30const char* LLCalc::Z_ROT = "ZR";
31const char* LLCalc::HOLLOW = "HLW";
32const char* LLCalc::CUT_BEGIN = "CB";
33const char* LLCalc::CUT_END = "CE";
34const char* LLCalc::PATH_BEGIN = "PB";
35const char* LLCalc::PATH_END = "PE";
36const char* LLCalc::TWIST_BEGIN = "TB";
37const char* LLCalc::TWIST_END = "TE";
38const char* LLCalc::X_SHEAR = "XSH";
39const char* LLCalc::Y_SHEAR = "YSH";
40const char* LLCalc::X_TAPER = "XTP";
41const char* LLCalc::Y_TAPER = "YTP";
42const char* LLCalc::RADIUS_OFFSET = "ROF";
43const char* LLCalc::REVOLUTIONS = "REV";
44const char* LLCalc::SKEW = "SKW";
45const char* LLCalc::X_HOLE = "XHL";
46const char* LLCalc::Y_HOLE = "YHL";
47const char* LLCalc::TEX_U_SCALE = "TSU";
48const char* LLCalc::TEX_V_SCALE = "TSV";
49const char* LLCalc::TEX_U_OFFSET = "TOU";
50const char* LLCalc::TEX_V_OFFSET = "TOV";
51const char* LLCalc::TEX_ROTATION = "TROT";
52const char* LLCalc::TEX_TRANSPARENCY = "TRNS";
53const char* LLCalc::TEX_GLOW = "GLOW";
54
55
56LLCalc* LLCalc::sInstance = NULL;
57
58LLCalc::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
74LLCalc::~LLCalc()
75{
76 delete mConstants;
77 delete mVariables;
78// delete mUserVariables;
79}
80
81//static
82void LLCalc::cleanUp()
83{
84 delete sInstance;
85 sInstance = NULL;
86}
87
88//static
89LLCalc* LLCalc::getInstance()
90{
91 if (!sInstance) sInstance = new LLCalc();
92 return sInstance;
93}
94
95void LLCalc::setVar(const std::string& name, const F32& value)
96{
97 (*mVariables)[name] = value;
98}
99
100void LLCalc::clearVar(const std::string& name)
101{
102 mVariables->erase(name);
103}
104
105void LLCalc::clearAllVariables()
106{
107 mVariables->clear();
108}
109
110/*
111void 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
121bool 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}