mConsoleFunctions.cpp 12 KB

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