math.scalar.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * Scalar computation library
  3. */
  4. export class Scalar {
  5. /**
  6. * Two pi constants convenient for computation.
  7. */
  8. public static TwoPi: number = Math.PI * 2;
  9. /**
  10. * Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
  11. * @param a number
  12. * @param b number
  13. * @param epsilon (default = 1.401298E-45)
  14. * @returns true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
  15. */
  16. public static WithinEpsilon(a: number, b: number, epsilon: number = 1.401298E-45): boolean {
  17. var num = a - b;
  18. return -epsilon <= num && num <= epsilon;
  19. }
  20. /**
  21. * Returns a string : the upper case translation of the number i to hexadecimal.
  22. * @param i number
  23. * @returns the upper case translation of the number i to hexadecimal.
  24. */
  25. public static ToHex(i: number): string {
  26. var str = i.toString(16);
  27. if (i <= 15) {
  28. return ("0" + str).toUpperCase();
  29. }
  30. return str.toUpperCase();
  31. }
  32. /**
  33. * Returns -1 if value is negative and +1 is value is positive.
  34. * @param value the value
  35. * @returns the value itself if it's equal to zero.
  36. */
  37. public static Sign(value: number): number {
  38. value = +value; // convert to a number
  39. if (value === 0 || isNaN(value)) {
  40. return value;
  41. }
  42. return value > 0 ? 1 : -1;
  43. }
  44. /**
  45. * Returns the value itself if it's between min and max.
  46. * Returns min if the value is lower than min.
  47. * Returns max if the value is greater than max.
  48. * @param value the value to clmap
  49. * @param min the min value to clamp to (default: 0)
  50. * @param max the max value to clamp to (default: 1)
  51. * @returns the clamped value
  52. */
  53. public static Clamp(value: number, min = 0, max = 1): number {
  54. return Math.min(max, Math.max(min, value));
  55. }
  56. /**
  57. * the log2 of value.
  58. * @param value the value to compute log2 of
  59. * @returns the log2 of value.
  60. */
  61. public static Log2(value: number): number {
  62. return Math.log(value) * Math.LOG2E;
  63. }
  64. /**
  65. * Loops the value, so that it is never larger than length and never smaller than 0.
  66. *
  67. * This is similar to the modulo operator but it works with floating point numbers.
  68. * For example, using 3.0 for t and 2.5 for length, the result would be 0.5.
  69. * With t = 5 and length = 2.5, the result would be 0.0.
  70. * Note, however, that the behaviour is not defined for negative numbers as it is for the modulo operator
  71. * @param value the value
  72. * @param length the length
  73. * @returns the looped value
  74. */
  75. public static Repeat(value: number, length: number): number {
  76. return value - Math.floor(value / length) * length;
  77. }
  78. /**
  79. * Normalize the value between 0.0 and 1.0 using min and max values
  80. * @param value value to normalize
  81. * @param min max to normalize between
  82. * @param max min to normalize between
  83. * @returns the normalized value
  84. */
  85. public static Normalize(value: number, min: number, max: number): number {
  86. return (value - min) / (max - min);
  87. }
  88. /**
  89. * Denormalize the value from 0.0 and 1.0 using min and max values
  90. * @param normalized value to denormalize
  91. * @param min max to denormalize between
  92. * @param max min to denormalize between
  93. * @returns the denormalized value
  94. */
  95. public static Denormalize(normalized: number, min: number, max: number): number {
  96. return (normalized * (max - min) + min);
  97. }
  98. /**
  99. * Calculates the shortest difference between two given angles given in degrees.
  100. * @param current current angle in degrees
  101. * @param target target angle in degrees
  102. * @returns the delta
  103. */
  104. public static DeltaAngle(current: number, target: number): number {
  105. var num: number = Scalar.Repeat(target - current, 360.0);
  106. if (num > 180.0) {
  107. num -= 360.0;
  108. }
  109. return num;
  110. }
  111. /**
  112. * PingPongs the value t, so that it is never larger than length and never smaller than 0.
  113. * @param tx value
  114. * @param length length
  115. * @returns The returned value will move back and forth between 0 and length
  116. */
  117. public static PingPong(tx: number, length: number): number {
  118. var t: number = Scalar.Repeat(tx, length * 2.0);
  119. return length - Math.abs(t - length);
  120. }
  121. /**
  122. * Interpolates between min and max with smoothing at the limits.
  123. *
  124. * This function interpolates between min and max in a similar way to Lerp. However, the interpolation will gradually speed up
  125. * from the start and slow down toward the end. This is useful for creating natural-looking animation, fading and other transitions.
  126. * @param from from
  127. * @param to to
  128. * @param tx value
  129. * @returns the smooth stepped value
  130. */
  131. public static SmoothStep(from: number, to: number, tx: number): number {
  132. var t: number = Scalar.Clamp(tx);
  133. t = -2.0 * t * t * t + 3.0 * t * t;
  134. return to * t + from * (1.0 - t);
  135. }
  136. /**
  137. * Moves a value current towards target.
  138. *
  139. * This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta.
  140. * Negative values of maxDelta pushes the value away from target.
  141. * @param current current value
  142. * @param target target value
  143. * @param maxDelta max distance to move
  144. * @returns resulting value
  145. */
  146. public static MoveTowards(current: number, target: number, maxDelta: number): number {
  147. var result: number = 0;
  148. if (Math.abs(target - current) <= maxDelta) {
  149. result = target;
  150. } else {
  151. result = current + Scalar.Sign(target - current) * maxDelta;
  152. }
  153. return result;
  154. }
  155. /**
  156. * Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.
  157. *
  158. * Variables current and target are assumed to be in degrees. For optimization reasons, negative values of maxDelta
  159. * are not supported and may cause oscillation. To push current away from a target angle, add 180 to that angle instead.
  160. * @param current current value
  161. * @param target target value
  162. * @param maxDelta max distance to move
  163. * @returns resulting angle
  164. */
  165. public static MoveTowardsAngle(current: number, target: number, maxDelta: number): number {
  166. var num: number = Scalar.DeltaAngle(current, target);
  167. var result: number = 0;
  168. if (-maxDelta < num && num < maxDelta) {
  169. result = target;
  170. } else {
  171. target = current + num;
  172. result = Scalar.MoveTowards(current, target, maxDelta);
  173. }
  174. return result;
  175. }
  176. /**
  177. * Creates a new scalar with values linearly interpolated of "amount" between the start scalar and the end scalar.
  178. * @param start start value
  179. * @param end target value
  180. * @param amount amount to lerp between
  181. * @returns the lerped value
  182. */
  183. public static Lerp(start: number, end: number, amount: number): number {
  184. return start + ((end - start) * amount);
  185. }
  186. /**
  187. * Same as Lerp but makes sure the values interpolate correctly when they wrap around 360 degrees.
  188. * The parameter t is clamped to the range [0, 1]. Variables a and b are assumed to be in degrees.
  189. * @param start start value
  190. * @param end target value
  191. * @param amount amount to lerp between
  192. * @returns the lerped value
  193. */
  194. public static LerpAngle(start: number, end: number, amount: number): number {
  195. var num: number = Scalar.Repeat(end - start, 360.0);
  196. if (num > 180.0) {
  197. num -= 360.0;
  198. }
  199. return start + num * Scalar.Clamp(amount);
  200. }
  201. /**
  202. * Calculates the linear parameter t that produces the interpolant value within the range [a, b].
  203. * @param a start value
  204. * @param b target value
  205. * @param value value between a and b
  206. * @returns the inverseLerp value
  207. */
  208. public static InverseLerp(a: number, b: number, value: number): number {
  209. var result: number = 0;
  210. if (a != b) {
  211. result = Scalar.Clamp((value - a) / (b - a));
  212. } else {
  213. result = 0.0;
  214. }
  215. return result;
  216. }
  217. /**
  218. * Returns a new scalar located for "amount" (float) on the Hermite spline defined by the scalars "value1", "value3", "tangent1", "tangent2".
  219. * @see http://mathworld.wolfram.com/HermitePolynomial.html
  220. * @param value1 spline value
  221. * @param tangent1 spline value
  222. * @param value2 spline value
  223. * @param tangent2 spline value
  224. * @param amount input value
  225. * @returns hermite result
  226. */
  227. public static Hermite(value1: number, tangent1: number, value2: number, tangent2: number, amount: number): number {
  228. var squared = amount * amount;
  229. var cubed = amount * squared;
  230. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  231. var part2 = (-2.0 * cubed) + (3.0 * squared);
  232. var part3 = (cubed - (2.0 * squared)) + amount;
  233. var part4 = cubed - squared;
  234. return (((value1 * part1) + (value2 * part2)) + (tangent1 * part3)) + (tangent2 * part4);
  235. }
  236. /**
  237. * Returns a random float number between and min and max values
  238. * @param min min value of random
  239. * @param max max value of random
  240. * @returns random value
  241. */
  242. public static RandomRange(min: number, max: number): number {
  243. if (min === max) { return min; }
  244. return ((Math.random() * (max - min)) + min);
  245. }
  246. /**
  247. * This function returns percentage of a number in a given range.
  248. *
  249. * RangeToPercent(40,20,60) will return 0.5 (50%)
  250. * RangeToPercent(34,0,100) will return 0.34 (34%)
  251. * @param number to convert to percentage
  252. * @param min min range
  253. * @param max max range
  254. * @returns the percentage
  255. */
  256. public static RangeToPercent(number: number, min: number, max: number): number {
  257. return ((number - min) / (max - min));
  258. }
  259. /**
  260. * This function returns number that corresponds to the percentage in a given range.
  261. *
  262. * PercentToRange(0.34,0,100) will return 34.
  263. * @param percent to convert to number
  264. * @param min min range
  265. * @param max max range
  266. * @returns the number
  267. */
  268. public static PercentToRange(percent: number, min: number, max: number): number {
  269. return ((max - min) * percent + min);
  270. }
  271. /**
  272. * Returns the angle converted to equivalent value between -Math.PI and Math.PI radians.
  273. * @param angle The angle to normalize in radian.
  274. * @return The converted angle.
  275. */
  276. public static NormalizeRadians(angle: number): number {
  277. // More precise but slower version kept for reference.
  278. // angle = angle % Tools.TwoPi;
  279. // angle = (angle + Tools.TwoPi) % Tools.TwoPi;
  280. //if (angle > Math.PI) {
  281. // angle -= Tools.TwoPi;
  282. //}
  283. angle -= (Scalar.TwoPi * Math.floor((angle + Math.PI) / Scalar.TwoPi));
  284. return angle;
  285. }
  286. }