PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/llcalc.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 162 lines | 101 code | 22 blank | 39 comment | 2 complexity | a6ec03832d9be9fdddc133e557821144 MD5 | raw file
  1/*
  2 *  LLCalc.cpp
  3 * Copyright 2008 Aimee Walton.
  4 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2008, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 *
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "llcalc.h"
 30
 31#include "llcalcparser.h"
 32#include "llmath.h"
 33
 34
 35// Variable names for use in the build floater
 36const char* LLCalc::X_POS = "PX";
 37const char* LLCalc::Y_POS = "PY";
 38const char* LLCalc::Z_POS = "PZ";
 39const char* LLCalc::X_SCALE = "SX";
 40const char* LLCalc::Y_SCALE = "SY";
 41const char* LLCalc::Z_SCALE = "SZ";
 42const char* LLCalc::X_ROT = "RX";
 43const char* LLCalc::Y_ROT = "RY";
 44const char* LLCalc::Z_ROT = "RZ";
 45const char* LLCalc::HOLLOW = "HLW";
 46const char* LLCalc::CUT_BEGIN = "CB";
 47const char* LLCalc::CUT_END = "CE";
 48const char* LLCalc::PATH_BEGIN = "PB";
 49const char* LLCalc::PATH_END = "PE";
 50const char* LLCalc::TWIST_BEGIN = "TB";
 51const char* LLCalc::TWIST_END = "TE";
 52const char* LLCalc::X_SHEAR = "SHX";
 53const char* LLCalc::Y_SHEAR = "SHY";
 54const char* LLCalc::X_TAPER = "TPX";
 55const char* LLCalc::Y_TAPER = "TPY";
 56const char* LLCalc::RADIUS_OFFSET = "ROF";
 57const char* LLCalc::REVOLUTIONS = "REV";
 58const char* LLCalc::SKEW = "SKW";
 59const char* LLCalc::X_HOLE = "HLX";
 60const char* LLCalc::Y_HOLE = "HLY";
 61const char* LLCalc::TEX_U_SCALE = "TSU";
 62const char* LLCalc::TEX_V_SCALE = "TSV";
 63const char* LLCalc::TEX_U_OFFSET = "TOU";
 64const char* LLCalc::TEX_V_OFFSET = "TOV";
 65const char* LLCalc::TEX_ROTATION = "TROT";
 66const char* LLCalc::TEX_TRANSPARENCY = "TRNS";
 67const char* LLCalc::TEX_GLOW = "GLOW";
 68
 69
 70LLCalc* LLCalc::sInstance = NULL;
 71
 72LLCalc::LLCalc() : mLastErrorPos(0)
 73{
 74	// Init table of constants
 75	mConstants["PI"] = F_PI;
 76	mConstants["TWO_PI"] = F_TWO_PI;
 77	mConstants["PI_BY_TWO"] = F_PI_BY_TWO;
 78	mConstants["SQRT_TWO_PI"] = F_SQRT_TWO_PI;
 79	mConstants["SQRT2"] = F_SQRT2;
 80	mConstants["SQRT3"] = F_SQRT3;
 81	mConstants["DEG_TO_RAD"] = DEG_TO_RAD;
 82	mConstants["RAD_TO_DEG"] = RAD_TO_DEG;
 83	mConstants["GRAVITY"] = GRAVITY;
 84}
 85
 86LLCalc::~LLCalc()
 87{
 88}
 89
 90//static
 91void LLCalc::cleanUp()
 92{
 93	delete sInstance;
 94	sInstance = NULL;
 95}
 96
 97//static
 98LLCalc* LLCalc::getInstance()
 99{
100    if (!sInstance)	sInstance = new LLCalc();
101	return sInstance;
102}
103
104void LLCalc::setVar(const std::string& name, const F32& value)
105{
106	mVariables[name] = value;
107}
108
109void LLCalc::clearVar(const std::string& name)
110{
111	mVariables.erase(name);
112}
113
114void LLCalc::clearAllVariables()
115{
116	mVariables.clear();
117}
118
119/*
120void LLCalc::updateVariables(LLSD& vars)
121{
122	LLSD::map_iterator cIt = vars.beginMap();
123	for(; cIt != vars.endMap(); cIt++)
124	{
125		setVar(cIt->first, (F32)(LLSD::Real)cIt->second);
126	}
127}
128*/
129
130bool LLCalc::evalString(const std::string& expression, F32& result)
131{
132	std::string expr_upper = expression;
133	LLStringUtil::toUpper(expr_upper);
134	
135	LLCalcParser calc(result, &mConstants, &mVariables);
136
137	mLastErrorPos = 0;
138	std::string::iterator start = expr_upper.begin();
139 	parse_info<std::string::iterator> info;
140	
141	try
142	{
143		info = parse(start, expr_upper.end(), calc, space_p);
144		lldebugs << "Math expression: " << expression << " = " << result << llendl;
145	}
146	catch(parser_error<std::string, std::string::iterator> &e)
147	{
148		mLastErrorPos = e.where - expr_upper.begin();
149		
150		llinfos << "Calc parser exception: " << e.descriptor << " at " << mLastErrorPos << " in expression: " << expression << llendl;
151		return false;
152	}
153	
154	if (!info.full)
155	{
156		mLastErrorPos = info.stop - expr_upper.begin();
157		llinfos << "Unhandled syntax error at " << mLastErrorPos << " in expression: " << expression << llendl;
158		return false;
159	}
160	
161	return true;
162}