mConsoleFunctions.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 nearest integer.\n"
  88. "@param v Number to convert to integer."
  89. "@returns Number converted to integer."
  90. "@ingroup Math" )
  91. {
  92. return (S32)mFloor( v + 0.5f );
  93. }
  94. DefineConsoleFunction( mCeil, S32, ( F32 v ),,
  95. "Round v up to the nearest integer.\n"
  96. "@param v Number to convert to integer."
  97. "@returns Number converted to integer."
  98. "@ingroup Math" )
  99. {
  100. return (S32)mCeil( v );
  101. }
  102. DefineConsoleFunction( mFloatLength, const char*, ( F32 v, U32 precision ),,
  103. "Formats the specified number to the given number of decimal places.\n"
  104. "@param v Number to format."
  105. "@param precision Number of decimal places to format to (1-9)."
  106. "@returns Number formatted to the specified number of decimal places."
  107. "@ingroup Math" )
  108. {
  109. char fmtString[8] = "%.0f";
  110. if (precision > 9)
  111. precision = 9;
  112. fmtString[2] = '0' + precision;
  113. static const U32 bufSize = 256;
  114. char * outBuffer = Con::getReturnBuffer(bufSize);
  115. dSprintf(outBuffer, bufSize, fmtString, v);
  116. return outBuffer;
  117. }
  118. //------------------------------------------------------------------------------
  119. DefineConsoleFunction( mAbs, F32, ( F32 v ),,
  120. "Calculate absolute value of specified value.\n"
  121. "@param v Input Value."
  122. "@returns Absolute value of specified value."
  123. "@ingroup Math" )
  124. {
  125. return mFabs( v );
  126. }
  127. DefineConsoleFunction( mFMod, F32, ( F32 v, F32 d ),,
  128. "Calculate the remainder of v/d.\n"
  129. "@param v Input Value."
  130. "@param d Divisor Value."
  131. "@returns The remainder of v/d."
  132. "@ingroup Math" )
  133. {
  134. return mFmod( v, d );
  135. }
  136. DefineConsoleFunction( mSqrt, F32, ( F32 v ),,
  137. "Calculate the square-root of v.\n"
  138. "@param v Input Value."
  139. "@returns The square-root of the input value."
  140. "@ingroup Math" )
  141. {
  142. return mSqrt (v );
  143. }
  144. DefineConsoleFunction( mPow, F32, ( F32 v, F32 p ),,
  145. "Calculate b raised to the p-th power.\n"
  146. "@param v Input Value."
  147. "@param p Power to raise value by."
  148. "@returns v raised to the p-th power."
  149. "@ingroup Math" )
  150. {
  151. return mPow( v, p );
  152. }
  153. DefineConsoleFunction( mLog, F32, ( F32 v ),,
  154. "Calculate the natural logarithm of v.\n"
  155. "@param v Input Value."
  156. "@returns The natural logarithm of the input value."
  157. "@ingroup Math" )
  158. {
  159. return mLog( v );
  160. }
  161. DefineConsoleFunction( mSin, F32, ( F32 v ),,
  162. "Calculate the sine of v.\n"
  163. "@param v Input Value (in radians)."
  164. "@returns The sine of the input value."
  165. "@ingroup Math" )
  166. {
  167. return mSin( v );
  168. }
  169. DefineConsoleFunction( mCos, F32, ( F32 v ),,
  170. "Calculate the cosine of v.\n"
  171. "@param v Input Value (in radians)."
  172. "@returns The cosine of the input value."
  173. "@ingroup Math" )
  174. {
  175. return mCos( v );
  176. }
  177. DefineConsoleFunction( mTan, F32, ( F32 v ),,
  178. "Calculate the tangent of v.\n"
  179. "@param v Input Value (in radians)."
  180. "@returns The tangent of the input value."
  181. "@ingroup Math" )
  182. {
  183. return mTan( v );
  184. }
  185. DefineConsoleFunction( mAsin, F32, ( F32 v ),,
  186. "Calculate the arc-sine of v.\n"
  187. "@param v Input Value (in radians)."
  188. "@returns The arc-sine of the input value."
  189. "@ingroup Math" )
  190. {
  191. return mAsin( v );
  192. }
  193. DefineConsoleFunction( mAcos, F32, ( F32 v ),,
  194. "Calculate the arc-cosine of v.\n"
  195. "@param v Input Value (in radians)."
  196. "@returns The arc-cosine of the input value."
  197. "@ingroup Math" )
  198. {
  199. return mAcos( v );
  200. }
  201. DefineConsoleFunction( mAtan, F32, ( F32 rise, F32 run ),,
  202. "Calculate the arc-tangent (slope) of a line defined by rise and run.\n"
  203. "@param rise of line."
  204. "@param run of line."
  205. "@returns The arc-tangent (slope) of a line defined by rise and run."
  206. "@ingroup Math" )
  207. {
  208. return mAtan2( rise, run );
  209. }
  210. DefineConsoleFunction( mRadToDeg, F32, ( F32 radians ),,
  211. "Convert specified radians into degrees.\n"
  212. "@param radians Input Value (in radians)."
  213. "@returns The specified radians value converted to degrees."
  214. "@ingroup Math" )
  215. {
  216. return mRadToDeg( radians );
  217. }
  218. DefineConsoleFunction( mDegToRad, F32, ( F32 degrees ),,
  219. "Convert specified degrees into radians.\n"
  220. "@param degrees Input Value (in degrees)."
  221. "@returns The specified degrees value converted to radians."
  222. "@ingroup Math" )
  223. {
  224. return mDegToRad( degrees );
  225. }
  226. DefineConsoleFunction( mClamp, F32, ( F32 v, F32 min, F32 max ),,
  227. "Clamp the specified value between two bounds.\n"
  228. "@param v Input value."
  229. "@param min Minimum Bound."
  230. "@param max Maximum Bound."
  231. "@returns The specified value clamped to the specified bounds."
  232. "@ingroup Math" )
  233. {
  234. return mClampF( v, min, max );
  235. }
  236. DefineConsoleFunction( mSaturate, F32, ( F32 v ),,
  237. "Clamp the specified value between 0 and 1 (inclusive).\n"
  238. "@param v Input value."
  239. "@returns The specified value clamped between 0 and 1 (inclusive)."
  240. "@ingroup Math" )
  241. {
  242. return mClampF( v, 0.0f, 1.0f );
  243. }
  244. DefineConsoleFunction( getMax, F32, ( F32 v1, F32 v2 ),,
  245. "Calculate the greater of two specified numbers.\n"
  246. "@param v1 Input value."
  247. "@param v2 Input value."
  248. "@returns The greater value of the two specified values."
  249. "@ingroup Math" )
  250. {
  251. return getMax( v1, v2 );
  252. }
  253. DefineConsoleFunction( getMin, F32, ( F32 v1, F32 v2 ),,
  254. "Calculate the lesser of two specified numbers.\n"
  255. "@param v1 Input value."
  256. "@param v2 Input value."
  257. "@returns The lesser value of the two specified values."
  258. "@ingroup Math" )
  259. {
  260. return getMin( v1, v2 );
  261. }
  262. DefineConsoleFunction( mLerp, F32, ( F32 v1, F32 v2, F32 time ),,
  263. "Calculate linearly interpolated value between two specified numbers using specified normalized time.\n"
  264. "@param v1 Interpolate From Input value."
  265. "@param v2 Interpolate To Input value."
  266. "@param time Normalized time used to interpolate values (0-1)."
  267. "@returns The interpolated value between the two specified values at normalized time t."
  268. "@ingroup Math" )
  269. {
  270. return mLerp( v1, v2, time );
  271. }
  272. DefineConsoleFunction( mPi, F32, (),,
  273. "Return the value of PI (half-circle in radians).\n"
  274. "@returns The value of PI."
  275. "@ingroup Math" )
  276. {
  277. return M_PI_F;
  278. }
  279. DefineConsoleFunction( m2Pi, F32, (),,
  280. "Return the value of 2*PI (full-circle in radians).\n"
  281. "@returns The value of 2*PI."
  282. "@ingroup Math" )
  283. {
  284. return M_2PI_F;
  285. }
  286. DefineConsoleFunction( mIsPow2, bool, ( S32 v ),,
  287. "Returns whether the value is an exact power of two.\n"
  288. "@param v Input value."
  289. "@returns Whether the specified value is an exact power of two."
  290. "@ingroup Math" )
  291. {
  292. return isPow2( v );
  293. }