mConsoleFunctions.cpp 10 KB

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