mConsoleFunctions.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "core/strings/stringFunctions.h"
  23. #include "console/console.h"
  24. #include "math/mMathFn.h"
  25. #include "math/mRandom.h"
  26. #include "console/engineAPI.h"
  27. DefineConsoleFunction( mSolveQuadratic, const char*, ( F32 a, F32 b, F32 c ),,
  28. "Solve a quadratic equation (2nd degree polynomial) of form a*x^2 + b*x + c = 0.\n"
  29. "@param a First Coefficient."
  30. "@param b Second Coefficient."
  31. "@param c Third Coefficient."
  32. "@returns A triple, containing: (sol x0 x1). (sol) is the number of solutions"
  33. "(being 0, 1, or 2), and (x0) and (x1) are the solutions, if any."
  34. "@ingroup Math" )
  35. {
  36. F32 x[2];
  37. U32 sol = mSolveQuadratic( a, b, c, x );
  38. static const U32 bufSize = 256;
  39. char * retBuffer = Con::getReturnBuffer(bufSize);
  40. dSprintf(retBuffer, bufSize, "%d %g %g", sol, x[0], x[1]);
  41. return retBuffer;
  42. }
  43. DefineConsoleFunction( mSolveCubic, const char*, ( F32 a, F32 b, F32 c, F32 d ),,
  44. "Solve a cubic equation (3rd degree polynomial) of form a*x^3 + b*x^2 + c*x + d = 0.\n"
  45. "@param a First Coefficient."
  46. "@param b Second Coefficient."
  47. "@param c Third Coefficient."
  48. "@param d Fourth Coefficient."
  49. "@returns A 4-tuple, containing: (sol x0 x1 x2). (sol) is the number of solutions"
  50. "(being 0, 1, 2 or 3), and (x0), (x1) and (x2) are the solutions, if any."
  51. "@ingroup Math" )
  52. {
  53. F32 x[3];
  54. U32 sol = mSolveCubic( a, b, c, d, x );
  55. static const U32 bufSize = 256;
  56. char * retBuffer = Con::getReturnBuffer(bufSize);
  57. dSprintf(retBuffer, bufSize, "%d %g %g %g", sol, x[0], x[1], x[2]);
  58. return retBuffer;
  59. }
  60. DefineConsoleFunction( mSolveQuartic, const char*, ( F32 a, F32 b, F32 c, F32 d, F32 e ),,
  61. "Solve a quartic equation (4th degree polynomial) of form a*x^4 + b*x^3 + c*x^2 + d*x + e = 0.\n"
  62. "@param a First Coefficient."
  63. "@param b Second Coefficient."
  64. "@param c Third Coefficient."
  65. "@param d Fourth Coefficient."
  66. "@param e Fifth Coefficient."
  67. "@returns A 5-tuple, containing: (sol x0 x1 x2 c3). (sol) is the number of solutions"
  68. "(being 0, 1, 2, 3 or 4), and (x0), (x1), (x2) and (x3) are the solutions, if any."
  69. "@ingroup Math" )
  70. {
  71. F32 x[4];
  72. static const U32 bufSize = 256;
  73. char * retBuffer = Con::getReturnBuffer(bufSize);
  74. U32 sol = mSolveQuartic(a, b, c, d, e, x);
  75. dSprintf(retBuffer, bufSize, "%d %g %g %g %g", sol, x[0], x[1], x[2], x[3]);
  76. return retBuffer;
  77. }
  78. DefineConsoleFunction( mFloor, S32, ( F32 v ),,
  79. "Round v down to the nearest integer.\n"
  80. "@param v Number to convert to integer."
  81. "@returns Number converted to integer."
  82. "@ingroup Math" )
  83. {
  84. return (S32)mFloor( v );
  85. }
  86. DefineConsoleFunction( mRound, S32, ( F32 v ),,
  87. "Round v to the nth decimal place or the nearest whole number by default."
  88. "@param v Value to roundn"
  89. "@return The rounded value as a S32."
  90. "@ingroup Math" )
  91. {
  92. return mRound(v);
  93. }
  94. DefineConsoleFunction( mRoundColour, F32, ( F32 v, S32 n ), (0),
  95. "Round v to the nth decimal place or the nearest whole number by default."
  96. "@param v Value to roundn"
  97. "@param n Number of decimal places to round to, 0 by defaultn"
  98. "@return The rounded value as a S32."
  99. "@ingroup Math")
  100. {
  101. if (n <= 0)
  102. return mRound(v);
  103. else
  104. return mRound(v, n);
  105. }
  106. DefineConsoleFunction( mCeil, S32, ( F32 v ),,
  107. "Round v up to the nearest integer.\n"
  108. "@param v Number to convert to integer."
  109. "@returns Number converted to integer."
  110. "@ingroup Math" )
  111. {
  112. return (S32)mCeil( v );
  113. }
  114. DefineConsoleFunction( mFloatLength, const char*, ( F32 v, U32 precision ),,
  115. "Formats the specified number to the given number of decimal places.\n"
  116. "@param v Number to format."
  117. "@param precision Number of decimal places to format to (1-9)."
  118. "@returns Number formatted to the specified number of decimal places."
  119. "@ingroup Math" )
  120. {
  121. char fmtString[8] = "%.9f";
  122. if (precision >= 9)
  123. precision = 9;
  124. fmtString[2] = '0' + precision;
  125. static const U32 bufSize = 256;
  126. char * outBuffer = Con::getReturnBuffer(bufSize);
  127. dSprintf(outBuffer, bufSize, fmtString, v);
  128. return outBuffer;
  129. }
  130. //------------------------------------------------------------------------------
  131. DefineConsoleFunction( mAbs, F32, ( F32 v ),,
  132. "Calculate absolute value of specified value.\n"
  133. "@param v Input Value."
  134. "@returns Absolute value of specified value."
  135. "@ingroup Math" )
  136. {
  137. return mFabs( v );
  138. }
  139. DefineConsoleFunction( mFMod, F32, ( F32 v, F32 d ),,
  140. "Calculate the remainder of v/d.\n"
  141. "@param v Input Value."
  142. "@param d Divisor Value."
  143. "@returns The remainder of v/d."
  144. "@ingroup Math" )
  145. {
  146. return mFmod( v, d );
  147. }
  148. DefineConsoleFunction( mSqrt, F32, ( F32 v ),,
  149. "Calculate the square-root of v.\n"
  150. "@param v Input Value."
  151. "@returns The square-root of the input value."
  152. "@ingroup Math" )
  153. {
  154. return mSqrt (v );
  155. }
  156. DefineConsoleFunction( mPow, F32, ( F32 v, F32 p ),,
  157. "Calculate b raised to the p-th power.\n"
  158. "@param v Input Value."
  159. "@param p Power to raise value by."
  160. "@returns v raised to the p-th power."
  161. "@ingroup Math" )
  162. {
  163. return mPow( v, p );
  164. }
  165. DefineConsoleFunction( mLog, F32, ( F32 v ),,
  166. "Calculate the natural logarithm of v.\n"
  167. "@param v Input Value."
  168. "@returns The natural logarithm of the input value."
  169. "@ingroup Math" )
  170. {
  171. return mLog( v );
  172. }
  173. DefineConsoleFunction( mSin, F32, ( F32 v ),,
  174. "Calculate the sine of v.\n"
  175. "@param v Input Value (in radians)."
  176. "@returns The sine of the input value."
  177. "@ingroup Math" )
  178. {
  179. return mSin( v );
  180. }
  181. DefineConsoleFunction( mCos, F32, ( F32 v ),,
  182. "Calculate the cosine of v.\n"
  183. "@param v Input Value (in radians)."
  184. "@returns The cosine of the input value."
  185. "@ingroup Math" )
  186. {
  187. return mCos( v );
  188. }
  189. DefineConsoleFunction( mTan, F32, ( F32 v ),,
  190. "Calculate the tangent of v.\n"
  191. "@param v Input Value (in radians)."
  192. "@returns The tangent of the input value."
  193. "@ingroup Math" )
  194. {
  195. return mTan( v );
  196. }
  197. DefineConsoleFunction( mAsin, F32, ( F32 v ),,
  198. "Calculate the arc-sine of v.\n"
  199. "@param v Input Value (in radians)."
  200. "@returns The arc-sine of the input value."
  201. "@ingroup Math" )
  202. {
  203. return mAsin( v );
  204. }
  205. DefineConsoleFunction( mAcos, F32, ( F32 v ),,
  206. "Calculate the arc-cosine of v.\n"
  207. "@param v Input Value (in radians)."
  208. "@returns The arc-cosine of the input value."
  209. "@ingroup Math" )
  210. {
  211. return mAcos( v );
  212. }
  213. DefineConsoleFunction( mAtan, F32, ( F32 rise, F32 run ),,
  214. "Calculate the arc-tangent (slope) of a line defined by rise and run.\n"
  215. "@param rise of line."
  216. "@param run of line."
  217. "@returns The arc-tangent (slope) of a line defined by rise and run."
  218. "@ingroup Math" )
  219. {
  220. return mAtan2( rise, run );
  221. }
  222. DefineConsoleFunction( mRadToDeg, F32, ( F32 radians ),,
  223. "Convert specified radians into degrees.\n"
  224. "@param radians Input Value (in radians)."
  225. "@returns The specified radians value converted to degrees."
  226. "@ingroup Math" )
  227. {
  228. return mRadToDeg( radians );
  229. }
  230. DefineConsoleFunction( mDegToRad, F32, ( F32 degrees ),,
  231. "Convert specified degrees into radians.\n"
  232. "@param degrees Input Value (in degrees)."
  233. "@returns The specified degrees value converted to radians."
  234. "@ingroup Math" )
  235. {
  236. return mDegToRad( degrees );
  237. }
  238. DefineConsoleFunction( mClamp, F32, ( F32 v, F32 min, F32 max ),,
  239. "Clamp the specified value between two bounds.\n"
  240. "@param v Input value."
  241. "@param min Minimum Bound."
  242. "@param max Maximum Bound."
  243. "@returns The specified value clamped to the specified bounds."
  244. "@ingroup Math" )
  245. {
  246. return mClampF( v, min, max );
  247. }
  248. DefineConsoleFunction( mSaturate, F32, ( F32 v ),,
  249. "Clamp the specified value between 0 and 1 (inclusive).\n"
  250. "@param v Input value."
  251. "@returns The specified value clamped between 0 and 1 (inclusive)."
  252. "@ingroup Math" )
  253. {
  254. return mClampF( v, 0.0f, 1.0f );
  255. }
  256. DefineConsoleFunction( getMax, F32, ( F32 v1, F32 v2 ),,
  257. "Calculate the greater of two specified numbers.\n"
  258. "@param v1 Input value."
  259. "@param v2 Input value."
  260. "@returns The greater value of the two specified values."
  261. "@ingroup Math" )
  262. {
  263. return getMax( v1, v2 );
  264. }
  265. DefineConsoleFunction( getMin, F32, ( F32 v1, F32 v2 ),,
  266. "Calculate the lesser of two specified numbers.\n"
  267. "@param v1 Input value."
  268. "@param v2 Input value."
  269. "@returns The lesser value of the two specified values."
  270. "@ingroup Math" )
  271. {
  272. return getMin( v1, v2 );
  273. }
  274. DefineConsoleFunction( mLerp, F32, ( F32 v1, F32 v2, F32 time ),,
  275. "Calculate linearly interpolated value between two specified numbers using specified normalized time.\n"
  276. "@param v1 Interpolate From Input value."
  277. "@param v2 Interpolate To Input value."
  278. "@param time Normalized time used to interpolate values (0-1)."
  279. "@returns The interpolated value between the two specified values at normalized time t."
  280. "@ingroup Math" )
  281. {
  282. return mLerp( v1, v2, time );
  283. }
  284. DefineConsoleFunction( mPi, F32, (),,
  285. "Return the value of PI (half-circle in radians).\n"
  286. "@returns The value of PI."
  287. "@ingroup Math" )
  288. {
  289. return M_PI_F;
  290. }
  291. DefineConsoleFunction( m2Pi, F32, (),,
  292. "Return the value of 2*PI (full-circle in radians).\n"
  293. "@returns The value of 2*PI."
  294. "@ingroup Math" )
  295. {
  296. return M_2PI_F;
  297. }
  298. DefineConsoleFunction( mIsPow2, bool, ( S32 v ),,
  299. "Returns whether the value is an exact power of two.\n"
  300. "@param v Input value."
  301. "@returns Whether the specified value is an exact power of two."
  302. "@ingroup Math" )
  303. {
  304. return isPow2( v );
  305. }