2
0

MathEx.cs 22 KB

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