MathEx.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System;
  2. namespace BansheeEngine
  3. {
  4. /// <summary>
  5. /// Values that represent in which order are euler angles applied when used in transformations.
  6. /// </summary>
  7. public enum EulerAngleOrder
  8. {
  9. XYZ,
  10. XZY,
  11. YXZ,
  12. YZX,
  13. ZXY,
  14. ZYX
  15. };
  16. /// <summary>
  17. /// Utility class providing common scalar math operations.
  18. /// </summary>
  19. class MathEx
  20. {
  21. /// <summary>
  22. /// Pi constant.
  23. /// </summary>
  24. public const float Pi = 3.141593f;
  25. /// <summary>
  26. /// Two times pi constant.
  27. /// </summary>
  28. public const float TwoPi = (2.0f * Pi);
  29. /// <summary>
  30. /// Half of pi constant.
  31. /// </summary>
  32. public const float HalfPi = (0.5f * Pi);
  33. /// <summary>
  34. /// Constant that converts degrees to radians.
  35. /// </summary>
  36. public const float Deg2Rad = Pi / 180.0f;
  37. /// <summary>
  38. /// Constant that converts radians to degrees.
  39. /// </summary>
  40. public const float Rad2Deg = 180.0f / Pi;
  41. /// <summary>
  42. /// Returns the minimum value of the two provided.
  43. /// </summary>
  44. /// <param name="a">First value to compare.</param>
  45. /// <param name="b">Second value to compare.</param>
  46. /// <returns>Minimum of the two values.</returns>
  47. public static float Min(float a, float b)
  48. {
  49. if (a < b)
  50. return a;
  51. return b;
  52. }
  53. /// <summary>
  54. /// Returns the minimum value of all the values provided.
  55. /// </summary>
  56. /// <param name="values">Values to compare.</param>
  57. /// <returns>Minimum of all the values.</returns>
  58. public static float Min(params float[] values)
  59. {
  60. int length = values.Length;
  61. if (length == 0)
  62. return 0.0f;
  63. float min = values[0];
  64. for (int i = 1; i < length; i++)
  65. {
  66. if (values[i] < min)
  67. min = values[i];
  68. }
  69. return min;
  70. }
  71. /// <summary>
  72. /// Returns the minimum value of the two provided.
  73. /// </summary>
  74. /// <param name="a">First value to compare.</param>
  75. /// <param name="b">Second value to compare.</param>
  76. /// <returns>Minimum of the two values.</returns>
  77. public static int Min(int a, int b)
  78. {
  79. if (a < b)
  80. return a;
  81. return b;
  82. }
  83. /// <summary>
  84. /// Returns the minimum value of all the values provided.
  85. /// </summary>
  86. /// <param name="values">Values to compare.</param>
  87. /// <returns>Minimum of all the values.</returns>
  88. public static int Min(params int[] values)
  89. {
  90. int length = values.Length;
  91. if (length == 0)
  92. return 0;
  93. int min = values[0];
  94. for (int i = 1; i < length; i++)
  95. {
  96. if (values[i] < min)
  97. min = values[i];
  98. }
  99. return min;
  100. }
  101. /// <summary>
  102. /// Returns the maximum value of the two provided.
  103. /// </summary>
  104. /// <param name="a">First value to compare.</param>
  105. /// <param name="b">Second value to compare.</param>
  106. /// <returns>Maximum of the two values.</returns>
  107. public static float Max(float a, float b)
  108. {
  109. if (a > b)
  110. return a;
  111. return b;
  112. }
  113. /// <summary>
  114. /// Returns the maximum value of all the values provided.
  115. /// </summary>
  116. /// <param name="values">Values to compare.</param>
  117. /// <returns>Maximum of all the values.</returns>
  118. public static float Max(params float[] values)
  119. {
  120. int length = values.Length;
  121. if (length == 0)
  122. return 0.0f;
  123. float max = values[0];
  124. for (int i = 1; i < length; i++)
  125. {
  126. if (values[i] > max)
  127. max = values[i];
  128. }
  129. return max;
  130. }
  131. /// <summary>
  132. /// Returns the maximum value of the two provided.
  133. /// </summary>
  134. /// <param name="a">First value to compare.</param>
  135. /// <param name="b">Second value to compare.</param>
  136. /// <returns>Maximum of the two values.</returns>
  137. public static int Max(int a, int b)
  138. {
  139. if (a > b)
  140. return a;
  141. else
  142. return b;
  143. }
  144. /// <summary>
  145. /// Returns the maximum value of all the values provided.
  146. /// </summary>
  147. /// <param name="values">Values to compare.</param>
  148. /// <returns>Maximum of all the values.</returns>
  149. public static int Max(params int[] values)
  150. {
  151. int length = values.Length;
  152. if (length == 0)
  153. return 0;
  154. int max = values[0];
  155. for (int i = 1; i < length; ++i)
  156. {
  157. if (values[i] > max)
  158. max = values[i];
  159. }
  160. return max;
  161. }
  162. /// <summary>
  163. /// Returns the absolute value of the provided parameter.
  164. /// </summary>
  165. /// <param name="f">Parameter to take absolute value of.</param>
  166. /// <returns>Absolute value of <paramref name="f"/>.</returns>
  167. public static float Abs(float f)
  168. {
  169. return Math.Abs(f);
  170. }
  171. /// <summary>
  172. /// Returns the absolute value of the provided parameter.
  173. /// </summary>
  174. /// <param name="value">Parameter to take absolute value of.</param>
  175. /// <returns>Absolute value of <paramref name="value"/>.</returns>
  176. public static int Abs(int value)
  177. {
  178. return Math.Abs(value);
  179. }
  180. /// <summary>
  181. /// Raises <paramref name="f"/> to the power of <paramref name="p"/>.
  182. /// </summary>
  183. /// <param name="f">Value to raise to a power.</param>
  184. /// <param name="p">Power to raise the value to.</param>
  185. /// <returns><paramref name="f"/> raised to the power of <paramref name="p"/>.</returns>
  186. public static float Pow(float f, float p)
  187. {
  188. return (float)Math.Pow(f, p);
  189. }
  190. /// <summary>
  191. /// Raises e to the power of <paramref name="power"/>.
  192. /// </summary>
  193. /// <param name="power">Power to raise e to.</param>
  194. /// <returns>e raised to the power of <paramref name="power"/>.</returns>
  195. public static float Exp(float power)
  196. {
  197. return (float)Math.Exp(power);
  198. }
  199. /// <summary>
  200. /// Returns the logarithm of a number in a specified base.
  201. /// </summary>
  202. /// <param name="f">Value to get the logarithm of.</param>
  203. /// <param name="p">Base of the logarithm</param>
  204. /// <returns>Logarithm of a number in the specified base.</returns>
  205. public static float Log(float f, float p)
  206. {
  207. return (float)Math.Log(f, p);
  208. }
  209. /// <summary>
  210. /// Returns the natural logarithm (base e).
  211. /// </summary>
  212. /// <param name="f">Value to get the logarithm of.</param>
  213. /// <returns>Natural logarithm of a number.</returns>
  214. public static float Log(float f)
  215. {
  216. return (float)Math.Log(f);
  217. }
  218. /// <summary>
  219. /// Returns the logarithm of a number in base 10.
  220. /// </summary>
  221. /// <param name="f">Value to get the logarithm of.</param>
  222. /// <returns>Logarithm of a number in base 10.</returns>
  223. public static float Log10(float f)
  224. {
  225. return (float)Math.Log10(f);
  226. }
  227. /// <summary>
  228. /// Returns the smallest integral value that is greater than or equal to the provided value.
  229. /// </summary>
  230. /// <param name="f">Value to round.</param>
  231. /// <returns>Smallest integral value that is greater than or equal to the provided value.</returns>
  232. public static float Ceil(float f)
  233. {
  234. return (float)Math.Ceiling(f);
  235. }
  236. /// <summary>
  237. /// Returns the largest integral value that is lesser than or equal to the provided value.
  238. /// </summary>
  239. /// <param name="f">Value to round.</param>
  240. /// <returns>Largest integral value that is lessert than or equal to the provided value.</returns>
  241. public static float Floor(float f)
  242. {
  243. return (float)Math.Floor(f);
  244. }
  245. /// <summary>
  246. /// Rounds the provided value to the nearest integral.
  247. /// </summary>
  248. /// <param name="f">Value to round.</param>
  249. /// <returns>Value rounded to the nearest integral.</returns>
  250. public static float Round(float f)
  251. {
  252. return (float)Math.Round(f);
  253. }
  254. /// <summary>
  255. /// Returns the smallest integral value that is greater than or equal to the provided value.
  256. /// </summary>
  257. /// <param name="f">Value to round.</param>
  258. /// <returns>Smallest integral value that is greater than or equal to the provided value.</returns>
  259. public static int CeilToInt(float f)
  260. {
  261. return (int)Math.Ceiling(f);
  262. }
  263. /// <summary>
  264. /// Returns the largest integral value that is lesser than or equal to the provided value.
  265. /// </summary>
  266. /// <param name="f">Value to round.</param>
  267. /// <returns>Largest integral value that is lessert than or equal to the provided value.</returns>
  268. public static int FloorToInt(float f)
  269. {
  270. return (int)Math.Floor(f);
  271. }
  272. /// <summary>
  273. /// Rounds the provided value to the nearest integral.
  274. /// </summary>
  275. /// <param name="f">Value to round.</param>
  276. /// <returns>Value rounded to the nearest integral.</returns>
  277. public static int RoundToInt(float f)
  278. {
  279. return (int)Math.Round(f);
  280. }
  281. /// <summary>
  282. /// Returns the sign of the provided value (positive or negative).
  283. /// </summary>
  284. /// <param name="f">Value to get the sign of.</param>
  285. /// <returns>-1.0f if negative or 1.0f if positive.</returns>
  286. public static float Sign(float f)
  287. {
  288. return f >= 0.0f ? 1.0f : -1.0f;
  289. }
  290. /// <summary>
  291. /// Returns the sine of the provided value.
  292. /// </summary>
  293. /// <param name="f">Angle in radians.</param>
  294. /// <returns>Sine of the angle.</returns>
  295. public static float Sin(float f)
  296. {
  297. return (float)Math.Sin(f);
  298. }
  299. /// <summary>
  300. /// Returns the cosine of the provided value.
  301. /// </summary>
  302. /// <param name="f">Angle in radians.</param>
  303. /// <returns>Cosine of the angle.</returns>
  304. public static float Cos(float f)
  305. {
  306. return (float)Math.Cos(f);
  307. }
  308. /// <summary>
  309. /// Returns the tangent of the provided value.
  310. /// </summary>
  311. /// <param name="f">Angle in radians.</param>
  312. /// <returns>Tangent of the angle.</returns>
  313. public static float Tan(float f)
  314. {
  315. return (float)Math.Tan(f);
  316. }
  317. /// <summary>
  318. /// Returns the angle whose sine is the specified number.
  319. /// </summary>
  320. /// <param name="f">Sine of an angle.</param>
  321. /// <returns>Angle in radians.</returns>
  322. public static float Asin(float f)
  323. {
  324. return (float)Math.Asin(f);
  325. }
  326. /// <summary>
  327. /// Returns the angle whose cosine is the specified number.
  328. /// </summary>
  329. /// <param name="f">Cosine of an angle.</param>
  330. /// <returns>Angle in radians.</returns>
  331. public static float Acos(float f)
  332. {
  333. return (float)Math.Acos(f);
  334. }
  335. /// <summary>
  336. /// Returns the angle whose tangent is the specified number.
  337. /// </summary>
  338. /// <param name="f">Tangent of an angle.</param>
  339. /// <returns>Angle in radians.</returns>
  340. public static float Atan(float f)
  341. {
  342. return (float)Math.Atan(f);
  343. }
  344. /// <summary>
  345. /// Returns an angle of a point.
  346. /// </summary>
  347. /// <param name="y">Y coordinate of the point.</param>
  348. /// <param name="x">X coordinate of the point.</param>
  349. /// <returns>Angle in radians in range [Pi, -Pi].</returns>
  350. public static float Atan2(float y, float x)
  351. {
  352. return (float)Math.Atan2(y, x);
  353. }
  354. /// <summary>
  355. /// Returns the sine of the provided value.
  356. /// </summary>
  357. /// <param name="f">Angle in radians.</param>
  358. /// <returns>Sine of the angle.</returns>
  359. public static float Sin(Radian f)
  360. {
  361. return (float)Math.Sin(f.Radians);
  362. }
  363. /// <summary>
  364. /// Returns the cosine of the provided value.
  365. /// </summary>
  366. /// <param name="f">Angle in radians.</param>
  367. /// <returns>Cosine of the angle.</returns>
  368. public static float Cos(Radian f)
  369. {
  370. return (float)Math.Cos(f.Radians);
  371. }
  372. /// <summary>
  373. /// Returns the tangent of the provided value.
  374. /// </summary>
  375. /// <param name="f">Angle in radians.</param>
  376. /// <returns>Tangent of the angle.</returns>
  377. public static float Tan(Radian f)
  378. {
  379. return (float)Math.Tan(f.Radians);
  380. }
  381. /// <summary>
  382. /// Returns the angle whose sine is the specified number.
  383. /// </summary>
  384. /// <param name="f">Sine of an angle.</param>
  385. /// <returns>Angle in radians.</returns>
  386. public static float Asin(Radian f)
  387. {
  388. return (float)Math.Asin(f.Radians);
  389. }
  390. /// <summary>
  391. /// Returns the angle whose cosine is the specified number.
  392. /// </summary>
  393. /// <param name="f">Cosine of an angle.</param>
  394. /// <returns>Angle in radians.</returns>
  395. public static float Acos(Radian f)
  396. {
  397. return (float)Math.Acos(f.Radians);
  398. }
  399. /// <summary>
  400. /// Returns the angle whose tangent is the specified number.
  401. /// </summary>
  402. /// <param name="f">Tangent of an angle.</param>
  403. /// <returns>Angle in radians.</returns>
  404. public static float Atan(Radian f)
  405. {
  406. return (float)Math.Atan(f.Radians);
  407. }
  408. /// <summary>
  409. /// Returns an angle of a point.
  410. /// </summary>
  411. /// <param name="y">Y coordinate of the point.</param>
  412. /// <param name="x">X coordinate of the point.</param>
  413. /// <returns>Angle in radians in range [Pi, -Pi].</returns>
  414. public static float Atan2(Radian y, Radian x)
  415. {
  416. return (float)Math.Atan2(y.Radians, x.Radians);
  417. }
  418. /// <summary>
  419. /// Returns a square root of the provided value.
  420. /// </summary>
  421. /// <param name="f">Value to take the square root of. Must not be negative.</param>
  422. /// <returns>Square root of the provided value.</returns>
  423. public static float Sqrt(float f)
  424. {
  425. return (float)Math.Sqrt(f);
  426. }
  427. /// <summary>
  428. /// Returns an inverse square root (1/sqrt(x)) of the provided value.
  429. /// </summary>
  430. /// <param name="f">Value to take the inverse square root of. Must not be negative or zero.</param>
  431. /// <returns>Inverse square root of the provided value.</returns>
  432. public static float InvSqrt(float f)
  433. {
  434. return 1.0f/(float) Math.Sqrt(f);
  435. }
  436. /// <summary>
  437. /// Clamps a value between two other values.
  438. /// </summary>
  439. /// <param name="value">Value to clamp.</param>
  440. /// <param name="min">Minimum value of the range to clamp. Must be lower than <paramref name="max"/></param>
  441. /// <param name="max">Maximum value of the range to clamp. Must be higher than <paramref name="min"/></param>
  442. /// <returns>Returns unchanged value if it is in valid range, otherwise returns value clamped to the range
  443. /// extremes. </returns>
  444. public static float Clamp(float value, float min, float max)
  445. {
  446. if (value < min)
  447. value = min;
  448. else if (value > max)
  449. value = max;
  450. return value;
  451. }
  452. /// <summary>
  453. /// Clamps a value between two other values.
  454. /// </summary>
  455. /// <param name="value">Value to clamp.</param>
  456. /// <param name="min">Minimum value of the range to clamp. Must be lower than <paramref name="max"/></param>
  457. /// <param name="max">Maximum value of the range to clamp. Must be higher than <paramref name="min"/></param>
  458. /// <returns>Returns unchanged value if it is in valid range, otherwise returns value clamped to the range
  459. /// extremes. </returns>
  460. public static int Clamp(int value, int min, int max)
  461. {
  462. if (value < min)
  463. value = min;
  464. else if (value > max)
  465. value = max;
  466. return value;
  467. }
  468. /// <summary>
  469. /// Clamps a value between zero and one.
  470. /// </summary>
  471. /// <param name="value">Value to clamp.</param>
  472. /// <returns>Returns unchanged value if it is in [0, 1] range, otherwise returns value clamped to the range.
  473. /// </returns>
  474. public static float Clamp01(float value)
  475. {
  476. if (value < 0.0)
  477. return 0.0f;
  478. if (value > 1.0)
  479. return 1f;
  480. return value;
  481. }
  482. /// <summary>
  483. /// Wraps an angle in [0, 360) range. Values lower than zero, or higher or equal to 360
  484. /// will get wrapped around back into [0, 360) range.
  485. /// </summary>
  486. /// <param name="angle">Angle to wrap.</param>
  487. /// <returns>Angle in [0, 360) range.</returns>
  488. public static Degree WrapAngle(Degree angle)
  489. {
  490. const float inv360 = 1.0f/360.0f;
  491. float angleVal = angle.Degrees;
  492. float wrapCount = (float)MathEx.Floor(MathEx.Abs(angleVal * inv360));
  493. if (angleVal > 0.0f)
  494. angleVal -= 360.0f * wrapCount;
  495. else
  496. angleVal += 360.0f * wrapCount;
  497. return new Degree(angleVal);
  498. }
  499. }
  500. }