MathEx.cs 22 KB

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