MathEx.cs 21 KB

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