{"version":3,"file":"math.mjs","sources":["../../../../../../../packages/components/overlays/wind/grid/math.ts"],"sourcesContent":["import { isArrayLike } from 'casc-cesium-utils/util'\nimport { DEG, RAD } from './consts'\n\n/**\n * @param {number} v\n * @param {number} n\n * @returns {number} remainder of floored division, i.e., floor(v / n). Useful for consistent modulo of negative\n *          numbers. See http://en.wikipedia.org/wiki/Modulo_operation.\n */\nexport function floorMod(v, n) {\n  const f = v - n * Math.floor(v / n)\n  // hack: when v is within an ulp of n, f can be equal to n (because the subtraction has no effect). But the\n  // result should be in the range [0, n), so check for this case. Example: floorMod(-1e-16, 10)\n  return f === n ? 0 : f\n}\n\n/**\n * Round to closest whole number using banker's rounding. From Java's Math.rint method.\n * @param {number} v\n * @returns {number} the value rounded half even\n */\nexport function rint(v) {\n  const TWOP52 = 4503599627370496 // Math.pow(2, 52)\n  let x = Math.abs(v)\n  if (x < TWOP52) {\n    x += TWOP52\n    x -= TWOP52\n  }\n  return Math.sign(v) * x\n}\n\n/**\n * Round to the closest multiple using banker's rounding.\n * @param {number} v\n * @param {number} m the multiple\n * @returns {number} the value rounded to the nearest increment of m.\n */\nexport function rintToMultiple(v, m) {\n  return rint(v / m) * m\n}\n\n/**\n * @param {number} v the value to clamp\n * @param {number} low the range lower bound, inclusive\n * @param {number} high the range upper bound, inclusive\n * @returns {number} the value v clamped to the range [low, high].\n */\nexport function clamp(v, low, high) {\n  return Math.max(low, Math.min(v, high))\n}\n\n/**\n * @param {number} v the value to rescale\n * @param {number} lowA the source range lower bound, inclusive\n * @param {number} highA the source range upper bound, inclusive\n * @param {number} lowB the target range lower bound, inclusive\n * @param {number} highB the target range upper bound, inclusive\n * @returns {number} the value v rescaled, but not clamped, to the range [lowB, highB] inclusive\n */\nexport function rescale(v, lowA, highA, lowB, highB) {\n  return ((v - lowA) / (highA - lowA)) * (highB - lowB) + lowB\n}\n\n/**\n * @param {number[]} vec2 [x, y]\n * @returns {number} euclidean length of the 2-d vector\n */\nexport function length(vec2) {\n  const [x, y] = vec2\n  return Math.sqrt(x * x + y * y)\n}\n\n/**\n * @param {number[]} a [ax, ay]\n * @param {number[]} b [bx, by]\n * @returns {number} euclidean distance between two 2-d points\n */\nexport function distance(a, b) {\n  return length([b[0] - a[0], b[1] - a[1]])\n}\n\n/**\n * @param {number[]} vec2 [x, y]\n * @returns {number} the direction of the 2-d vector on the compass rose in degrees in the range [0, 360).\n */\nexport function toCardinalDegrees(vec2) {\n  const deg = Math.atan2(vec2[0], vec2[1]) * DEG\n  return (deg + 360) % 360 // map (-180, 180] to [0, 360)\n}\n\n/**\n * Returns the indicatrix of the specified projection at the given point.\n *\n * This method uses finite difference estimates to calculate warping by adding a very small amount (h) to\n * both the longitude and latitude to create two lines. These lines are then projected to pixel space, where\n * they become diagonals of triangles that represent how much the projection warps longitude and latitude at\n * that location.\n *\n * <pre>\n *        (λ, φ+h)                  (xλ, yλ)\n *           .                         .\n *           |               ==>        \\\n *           |                           \\   __. (xφ, yφ)\n *    (λ, φ) .____. (λ+h, φ)       (x, y) .--\n * </pre>\n *\n * See:\n *     Map Projections: A Working Manual, Snyder, John P: pubs.er.usgs.gov/publication/pp1395\n *     gis.stackexchange.com/questions/5068/how-to-create-an-accurate-tissot-indicatrix\n *     www.jasondavies.com/maps/tissot\n *\n * @param {Function} project the projection function(λ, φ) -> [x, y]\n * @param {number} λ geographic coord lon\n * @param {number} φ geographic coord lat\n * @param {number} x corresponding screen coord x\n * @param {number} y corresponding screen coord y\n * @returns {number[]} array of scaled derivatives [dx/dλ, dy/dλ, dx/dφ, dy/dφ]\n */\nexport function indicatrix(project, λ, φ, x, y) {\n  const H = 0.0000001 // ~= 1cm\n  const Hφ = φ < 0 ? H : -H // avoid overflow at the poles\n  const pλ = project(λ + H, φ)\n  const pφ = project(λ, φ + Hφ)\n\n  // Meridian scale factor (see Snyder, equation 4-3), where R = 1. This handles issue where length of 1° λ\n  // changes depending on φ. Without this, there is a pinching effect at the poles.\n  const k = Math.cos(φ * RAD)\n  const Hk = H * k\n\n  return [\n    (pλ[0] - x) / Hk, // dx/dλ\n    (pλ[1] - y) / Hk, // dy/dλ\n    (pφ[0] - x) / Hφ, // dx/dφ\n    (pφ[1] - y) / Hφ // dy/dφ\n  ]\n}\n\n/**\n * Converts the argument to a number, including special cases for fractions:\n *     0.25  -> 0.25\n *     \"1/4\" -> 0.25\n *     [1,4] -> 0.25\n *     \".25\" -> 0.25\n *\n * @param x any object. When an array, then interpreted as the fraction: a[0] / a[1]. When a string containing\n *        a slash, the value is first converted to an array by splitting on \"/\".\n * @returns {number} the specified argument converted to a number.\n */\nexport function decimalize(x) {\n  if (typeof x === 'string' && x.indexOf('/') >= 0) {\n    x = x.split('/')\n  }\n  // CONSIDER: remove dependency on isArrayLike\n  return isArrayLike(x) && x.length === 2 ? x[0] / x[1] : +x\n}\n\nexport function mulvec2(vec, c) {\n  return [vec[0] * c, vec[1] * c]\n}\n"],"names":[],"mappings":";;;AAEO,SAAS,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE;AAC/B,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AACtC,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC;AACM,SAAS,IAAI,CAAC,CAAC,EAAE;AACxB,EAAE,MAAM,MAAM,GAAG,gBAAgB,CAAC;AAClC,EAAE,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACtB,EAAE,IAAI,CAAC,GAAG,MAAM,EAAE;AAClB,IAAI,CAAC,IAAI,MAAM,CAAC;AAChB,IAAI,CAAC,IAAI,MAAM,CAAC;AAChB,GAAG;AACH,EAAE,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAC1B,CAAC;AACM,SAAS,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE;AACrC,EAAE,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