PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  27. #include "llcalc.h"
  28. #include "llcalcparser.h"
  29. #include "llmath.h"
  30. // Variable names for use in the build floater
  31. const char* LLCalc::X_POS = "PX";
  32. const char* LLCalc::Y_POS = "PY";
  33. const char* LLCalc::Z_POS = "PZ";
  34. const char* LLCalc::X_SCALE = "SX";
  35. const char* LLCalc::Y_SCALE = "SY";
  36. const char* LLCalc::Z_SCALE = "SZ";
  37. const char* LLCalc::X_ROT = "RX";
  38. const char* LLCalc::Y_ROT = "RY";
  39. const char* LLCalc::Z_ROT = "RZ";
  40. const char* LLCalc::HOLLOW = "HLW";
  41. const char* LLCalc::CUT_BEGIN = "CB";
  42. const char* LLCalc::CUT_END = "CE";
  43. const char* LLCalc::PATH_BEGIN = "PB";
  44. const char* LLCalc::PATH_END = "PE";
  45. const char* LLCalc::TWIST_BEGIN = "TB";
  46. const char* LLCalc::TWIST_END = "TE";
  47. const char* LLCalc::X_SHEAR = "SHX";
  48. const char* LLCalc::Y_SHEAR = "SHY";
  49. const char* LLCalc::X_TAPER = "TPX";
  50. const char* LLCalc::Y_TAPER = "TPY";
  51. const char* LLCalc::RADIUS_OFFSET = "ROF";
  52. const char* LLCalc::REVOLUTIONS = "REV";
  53. const char* LLCalc::SKEW = "SKW";
  54. const char* LLCalc::X_HOLE = "HLX";
  55. const char* LLCalc::Y_HOLE = "HLY";
  56. const char* LLCalc::TEX_U_SCALE = "TSU";
  57. const char* LLCalc::TEX_V_SCALE = "TSV";
  58. const char* LLCalc::TEX_U_OFFSET = "TOU";
  59. const char* LLCalc::TEX_V_OFFSET = "TOV";
  60. const char* LLCalc::TEX_ROTATION = "TROT";
  61. const char* LLCalc::TEX_TRANSPARENCY = "TRNS";
  62. const char* LLCalc::TEX_GLOW = "GLOW";
  63. LLCalc* LLCalc::sInstance = NULL;
  64. LLCalc::LLCalc() : mLastErrorPos(0)
  65. {
  66. // Init table of constants
  67. mConstants["PI"] = F_PI;
  68. mConstants["TWO_PI"] = F_TWO_PI;
  69. mConstants["PI_BY_TWO"] = F_PI_BY_TWO;
  70. mConstants["SQRT_TWO_PI"] = F_SQRT_TWO_PI;
  71. mConstants["SQRT2"] = F_SQRT2;
  72. mConstants["SQRT3"] = F_SQRT3;
  73. mConstants["DEG_TO_RAD"] = DEG_TO_RAD;
  74. mConstants["RAD_TO_DEG"] = RAD_TO_DEG;
  75. mConstants["GRAVITY"] = GRAVITY;
  76. }
  77. LLCalc::~LLCalc()
  78. {
  79. }
  80. //static
  81. void LLCalc::cleanUp()
  82. {
  83. delete sInstance;
  84. sInstance = NULL;
  85. }
  86. //static
  87. LLCalc* LLCalc::getInstance()
  88. {
  89. if (!sInstance) sInstance = new LLCalc();
  90. return sInstance;
  91. }
  92. void LLCalc::setVar(const std::string& name, const F32& value)
  93. {
  94. mVariables[name] = value;
  95. }
  96. void LLCalc::clearVar(const std::string& name)
  97. {
  98. mVariables.erase(name);
  99. }
  100. void LLCalc::clearAllVariables()
  101. {
  102. mVariables.clear();
  103. }
  104. /*
  105. void LLCalc::updateVariables(LLSD& vars)
  106. {
  107. LLSD::map_iterator cIt = vars.beginMap();
  108. for(; cIt != vars.endMap(); cIt++)
  109. {
  110. setVar(cIt->first, (F32)(LLSD::Real)cIt->second);
  111. }
  112. }
  113. */
  114. bool LLCalc::evalString(const std::string& expression, F32& result)
  115. {
  116. std::string expr_upper = expression;
  117. LLStringUtil::toUpper(expr_upper);
  118. LLCalcParser calc(result, &mConstants, &mVariables);
  119. mLastErrorPos = 0;
  120. std::string::iterator start = expr_upper.begin();
  121. parse_info<std::string::iterator> info;
  122. try
  123. {
  124. info = parse(start, expr_upper.end(), calc, space_p);
  125. lldebugs << "Math expression: " << expression << " = " << result << llendl;
  126. }
  127. catch(parser_error<std::string, std::string::iterator> &e)
  128. {
  129. mLastErrorPos = e.where - expr_upper.begin();
  130. llinfos << "Calc parser exception: " << e.descriptor << " at " << mLastErrorPos << " in expression: " << expression << llendl;
  131. return false;
  132. }
  133. if (!info.full)
  134. {
  135. mLastErrorPos = info.stop - expr_upper.begin();
  136. llinfos << "Unhandled syntax error at " << mLastErrorPos << " in expression: " << expression << llendl;
  137. return false;
  138. }
  139. return true;
  140. }