math_ScriptBinding.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "math/mMathFn.h"
  25. #include "math/mRandom.h"
  26. #include "string/stringUnit.h"
  27. ConsoleFunctionGroupBegin( GeneralMath, "General math functions. Use these whenever possible, as they'll run much faster than script equivalents.");
  28. /*! @defgroup MathFunctions Math (Non-Vector)
  29. @ingroup TorqueScriptFunctions
  30. @{
  31. */
  32. /*!
  33. Solve a quadratic equation of form a*x^2 + b*x + c = 0.
  34. @returns A triple, contanining: sol x0 x1. sol is the number of
  35. solutions (being 0, 1, or 2), and x0 and x1 are the solutions, if any.
  36. Unused x's are undefined.
  37. */
  38. ConsoleFunctionWithDocs( mSolveQuadratic, ConsoleString, 4, 4, (float a, float b, float c))
  39. {
  40. char * retBuffer = Con::getReturnBuffer(256);
  41. F32 x[2];
  42. U32 sol = mSolveQuadratic(dAtof(argv[1]), dAtof(argv[2]), dAtof(argv[3]), x);
  43. dSprintf(retBuffer, 256, "%d %g %g", sol, x[0], x[1]);
  44. return retBuffer;
  45. }
  46. /*!
  47. Solve a cubic equation of form a*x^3 + b*x^2 + c*x + d = 0.
  48. @returns A 4-tuple, contanining: sol x0 x1 x2. sol is the number of
  49. solutions (being 0, 1, 2, or 3), and x0, x1, x2 are the solutions, if any.
  50. Unused x's are undefined.
  51. */
  52. ConsoleFunctionWithDocs( mSolveCubic, ConsoleString, 5, 5, (float a, float b, float c, float d))
  53. {
  54. char * retBuffer = Con::getReturnBuffer(256);
  55. F32 x[3];
  56. U32 sol = mSolveCubic(dAtof(argv[1]), dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]), x);
  57. dSprintf(retBuffer, 256, "%d %g %g %g", sol, x[0], x[1], x[2]);
  58. return retBuffer;
  59. }
  60. /*!
  61. Solve a quartic equation of form a*x^4 + b*x^3 + c*x^2 + d*x + e = 0.
  62. @returns A 5-tuple, contanining: sol x0 x1 x2 x3. sol is the number of
  63. solutions (ranging from 0-4), and x0, x1, x2 and x3 are the solutions, if any.
  64. Unused x's are undefined.
  65. */
  66. ConsoleFunctionWithDocs( mSolveQuartic, ConsoleString, 6, 6, (float a, float b, float c, float d, float e))
  67. {
  68. char * retBuffer = Con::getReturnBuffer(256);
  69. F32 x[4];
  70. U32 sol = mSolveQuartic(dAtof(argv[1]), dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]), dAtof(argv[5]), x);
  71. dSprintf(retBuffer, 256, "%d %g %g %g %g", sol, x[0], x[1], x[2], x[3]);
  72. return retBuffer;
  73. }
  74. /*! Use the mFloor function to calculate the next lowest integer value from val.
  75. @param val A floating-point value.
  76. @return Returns an integer representing the next lowest integer from val.
  77. @sa mCeil
  78. */
  79. ConsoleFunctionWithDocs( mFloor, ConsoleInt, 2, 2, ( val ))
  80. {
  81. return (S32)mFloor(dAtof(argv[1]));
  82. }
  83. /*! Rounds a number. 0.5 is rounded up.
  84. @param val A floating-point value
  85. @return Returns the integer value closest to the given float
  86. */
  87. ConsoleFunctionWithDocs( mRound, ConsoleFloat, 2, 2, (float v))
  88. {
  89. return mRound( dAtof(argv[1]) );
  90. }
  91. /*! Use the mCeil function to calculate the next highest integer value from val.
  92. @param val A floating-point value.
  93. @return Returns an integer representing the next highest integer from val.
  94. @sa mFloor
  95. */
  96. ConsoleFunctionWithDocs( mCeil, ConsoleInt, 2, 2, ( val ))
  97. {
  98. return (S32)mCeil(dAtof(argv[1]));
  99. }
  100. /*! Use the mFloatLength function to limit the number of decimal places in val to numDecimals.
  101. @param val A floating-point value.
  102. @param numDecimals An integer between 0 and inf representing the number of decimal places to allow val to have.
  103. @return Returns a floating-point value equivalent to a truncated version of val, where the new version has numDecimals decimal places
  104. */
  105. ConsoleFunctionWithDocs( mFloatLength, ConsoleString, 3, 3, ( val , numDecimals ))
  106. {
  107. char * outBuffer = Con::getReturnBuffer(256);
  108. char fmtString[8] = "%.0f";
  109. U32 precision = dAtoi(argv[2]);
  110. if (precision > 9)
  111. precision = 9;
  112. fmtString[2] = '0' + precision;
  113. dSprintf(outBuffer, 255, fmtString, dAtof(argv[1]));
  114. return outBuffer;
  115. }
  116. //------------------------------------------------------------------------------
  117. /*! Use the mAbs function to get the magnitude of val.
  118. @param val An integer or a floating-point value.
  119. @return Returns the magnitude of val
  120. */
  121. ConsoleFunctionWithDocs( mAbs, ConsoleFloat, 2, 2, ( val ))
  122. {
  123. return(mFabs(dAtof(argv[1])));
  124. }
  125. /*! Use the mSqrt function to calculated the square root of val.
  126. @param val A numeric value.
  127. @return Returns the the squareroot of val
  128. */
  129. ConsoleFunctionWithDocs( mSqrt, ConsoleFloat, 2, 2, ( val ))
  130. {
  131. return(mSqrt(dAtof(argv[1])));
  132. }
  133. /*! Use the mPow function to calculated val raised to the power of power.
  134. @param val A numeric (integer or floating-point) value to be raised to a power.
  135. @param power A numeric (integer or floating-point) power to raise val to.
  136. @return Returns val^power
  137. */
  138. ConsoleFunctionWithDocs( mPow, ConsoleFloat, 3, 3, ( val , power ))
  139. {
  140. return(mPow(dAtof(argv[1]), dAtof(argv[2])));
  141. }
  142. /*! Use the mLog function to calculate the natural logarithm of val.
  143. @param val A numeric value.
  144. @return Returns the natural logarithm of val
  145. */
  146. ConsoleFunctionWithDocs( mLog, ConsoleFloat, 2, 2, ( val ))
  147. {
  148. return(mLog(dAtof(argv[1])));
  149. }
  150. /*! Use the mSin function to get the sine of the angle val.
  151. @param val A value in degrees.
  152. @return Returns the sine of val. This value will be in the range [ -1.0 , 1.0 ].
  153. @sa mAsin
  154. */
  155. ConsoleFunctionWithDocs( mSin, ConsoleFloat, 2, 2, ( val ))
  156. {
  157. return(mSin(mDegToRad(dAtof(argv[1]))));
  158. }
  159. /*! Use the mCos function to get the cosine of the angle val.
  160. @param val A value in degrees.
  161. @return Returns the cosine of val. This value will be in the range [ -1.0 , 1.0 ].
  162. @sa mAcos
  163. */
  164. ConsoleFunctionWithDocs( mCos, ConsoleFloat, 2, 2, ( val ))
  165. {
  166. return(mCos(mDegToRad(dAtof(argv[1]))));
  167. }
  168. /*! Use the mTan function to get the tangent of the angle val.
  169. @param val A value in degrees.
  170. @return Returns the tangent of val. This value will be in the range [ -inf.0 , inf.0 ].
  171. @sa mAtan
  172. */
  173. ConsoleFunctionWithDocs( mTan, ConsoleFloat, 2, 2, ( val ))
  174. {
  175. return(mTan(mDegToRad(dAtof(argv[1]))));
  176. }
  177. /*! Use the mAsin function to get the inverse sine of val in degrees.
  178. @param val A value between -1.0 and 1.0 equal to the sine of some angle theta.
  179. @return Returns the inverse sine of val in degrees. This value will be in the range [ -90, 90 ].
  180. @sa mSin
  181. */
  182. ConsoleFunctionWithDocs( mAsin, ConsoleFloat, 2, 2, ( val ))
  183. {
  184. return(mRadToDeg(mAsin(dAtof(argv[1]))));
  185. }
  186. /*! Use the mAcos function to get the inverse cosine of val in degrees.
  187. @param val A value between -1.0 and 1.0 equal to the cosine of some angle theta.
  188. @return Returns the inverse cosine of val in radians. This value will be in the range [ 0 , 180 ].
  189. @sa mCos
  190. */
  191. ConsoleFunctionWithDocs( mAcos, ConsoleFloat, 2, 2, ( val ))
  192. {
  193. return(mRadToDeg(mAcos(dAtof(argv[1]))));
  194. }
  195. /*! Use the mAtan function to get the inverse tangent of rise/run in degrees.
  196. May be called as mAtan( deltaX, deltaY ) or mAtan( "deltaX deltaY" ).
  197. @param x-axis run Horizontal component of a line.
  198. @param y-axis rise Vertical component of a line.
  199. @return Returns the slope in degrees (the arc-tangent) of a line with the given run and rise.
  200. @sa mTan
  201. */
  202. ConsoleFunctionWithDocs( mAtan, ConsoleFloat, 2, 3, ( val ))
  203. {
  204. F32 xRun, yRise;
  205. if( argc == 3 )
  206. {
  207. xRun = dAtof( argv[1] );
  208. yRise = dAtof( argv[2] );
  209. }
  210. else if( StringUnit::getUnitCount( argv[1], " " ) == 2 )
  211. {
  212. dSscanf( argv[1], "%g %g", &xRun, &yRise );
  213. }
  214. else
  215. {
  216. Con::warnf( "mAtan - Invalid parameters." );
  217. return 0;
  218. }
  219. return(mRadToDeg(mAtan(xRun, yRise)));
  220. }
  221. /*! Use the mRadToDeg function to convert radians to degrees.
  222. @param val A floating-point number representing some number of radians.
  223. @return Returns the equivalent of the radian value val in degrees.
  224. @sa mDegToRad
  225. */
  226. ConsoleFunctionWithDocs( mRadToDeg, ConsoleFloat, 2, 2, ( val ))
  227. {
  228. return(mRadToDeg(dAtof(argv[1])));
  229. }
  230. /*! Use the mDegToRad function to convert degrees to radians.
  231. @param val A floating-point number representing some number of degrees.
  232. @return Returns the equivalent of the degree value val in radians.
  233. @sa mRadToDeg
  234. */
  235. ConsoleFunctionWithDocs( mDegToRad, ConsoleFloat, 2, 2, ( val ))
  236. {
  237. return(mDegToRad(dAtof(argv[1])));
  238. }
  239. /*! Clamp a value between two other values.
  240. @param number A float value representing the number to clamp
  241. @param min The lower bound
  242. @param max The upper bound
  243. @return A float value the is within the given range
  244. */
  245. ConsoleFunctionWithDocs( mClamp, ConsoleFloat, 4, 4, (float number, float min, float max))
  246. {
  247. F32 value = dAtof( argv[1] );
  248. F32 min = dAtof( argv[2] );
  249. F32 max = dAtof( argv[3] );
  250. return mClampF( value, min, max );
  251. }
  252. //-----------------------------------------------------------------------------
  253. /*! Returns the Minimum of two values.
  254. */
  255. ConsoleFunctionWithDocs( mGetMin, ConsoleFloat, 3, 3, (a, b))
  256. {
  257. return getMin(dAtof(argv[1]), dAtof(argv[2]));
  258. }
  259. //-----------------------------------------------------------------------------
  260. /*! Returns the Maximum of two values.
  261. */
  262. ConsoleFunctionWithDocs( mGetMax, ConsoleFloat, 3, 3, (a, b))
  263. {
  264. return getMax(dAtof(argv[1]), dAtof(argv[2]));
  265. }
  266. //-----------------------------------------------------------------------------
  267. /*! Converts a list of bit-positions into a value.
  268. */
  269. ConsoleFunctionWithDocs( bits, ConsoleString, 2, 2, ())
  270. {
  271. // Calculate Element Count.
  272. U32 elementCount = StringUnit::getUnitCount( argv[1], " \t\n" );
  273. // Return nothing if there's no elements.
  274. if ( elementCount < 1 )
  275. {
  276. // Error!
  277. Con::printf("bits() - Invalid number of parameters!");
  278. return "0";
  279. }
  280. // Reset Bit Value.
  281. U32 bitValue = 0;
  282. // Parse Bits.
  283. for ( U32 n = 0; n < elementCount; n++ )
  284. {
  285. // Merge Bit Value.
  286. bitValue |= (U32)BIT(dAtoi(StringUnit::getUnit(argv[1], n, " \t\n")));
  287. }
  288. // Create Returnable Buffer.
  289. char* pBuffer = Con::getReturnBuffer(16);
  290. // Format Output.
  291. dSprintf( pBuffer, 16, "%u", bitValue );
  292. // Return Buffer.
  293. return pBuffer;
  294. }
  295. //-----------------------------------------------------------------------------
  296. /*! Converts a bit-position into a value.
  297. */
  298. ConsoleFunctionWithDocs( bit, ConsoleString, 2, 2, ())
  299. {
  300. // Create Returnable Buffer.
  301. char* pBuffer = Con::getReturnBuffer(16);
  302. // Format Output.
  303. dSprintf( pBuffer, 16, "%u", U32(BIT(dAtoi(argv[1]))) );
  304. // Return Buffer.
  305. return pBuffer;
  306. }
  307. /*! Returns the ones complement of a bit.
  308. */
  309. ConsoleFunctionWithDocs( bitInverse, ConsoleString, 2, 2, ())
  310. {
  311. // Create Returnable Buffer.
  312. char* pBuffer = Con::getReturnBuffer(16);
  313. // Format Output.
  314. dSprintf( pBuffer, 16, "%u", U32(~BIT(dAtoi(argv[1]))) );
  315. // Return Buffer.
  316. return pBuffer;
  317. }
  318. /*! Returns the mask with a bit added to it
  319. */
  320. ConsoleFunctionWithDocs( addBitToMask, ConsoleInt, 3, 3, ( mask, bit ))
  321. {
  322. U32 mask;
  323. dSscanf( argv[1], "%u", &mask );
  324. U32 bit = BIT( dAtoi( argv[2] ) );
  325. return mask | bit;
  326. }
  327. /*! Returns the mask with a bit removed from it
  328. */
  329. ConsoleFunctionWithDocs( removeBitFromMask, ConsoleInt, 3, 3, ( mask, bit ))
  330. {
  331. U32 mask;
  332. dSscanf( argv[1], "%u", &mask );
  333. U32 bit = BIT( dAtoi( argv[2] ) );
  334. return mask & ~bit;
  335. }
  336. ConsoleFunctionGroupEnd(GeneralMath)
  337. /*! @} */ // group MathFunctions