kriging-webgl
Version:
kriging.js webgl accelerate version
699 lines (677 loc) • 24 kB
JavaScript
import { LRUCache as H } from "lru-cache";
function w(t, e) {
return new Array(e).fill(t);
}
function G(t, e) {
const r = w(0, e * e);
for (let o = 0; o < e; o++) r[o * e + o] = t;
return r;
}
function q(t, e, r) {
const o = Array(r * e);
for (let n = 0; n < e; n++)
for (let f = 0; f < r; f++)
o[f * e + n] = t[n * r + f];
return o;
}
function M(t, e, r, o) {
const n = Array(r * o);
for (let f = 0; f < r; f++)
for (let l = 0; l < o; l++) {
const u = f * o + l;
n[u] = t[u] + e[u];
}
return n;
}
function v(t, e, r, o, n) {
const f = Array(r * n).fill(0);
for (let l = 0; l < r; l++)
for (let u = 0; u < n; u++)
for (let i = 0, s = l * n + u; i < o; i++)
f[s] += t[l * o + i] * e[i * n + u];
return f;
}
function O(t, e) {
const r = Array(e);
for (let o = 0; o < e; o++)
r[o] = t[o * e + o];
for (let o = 0; o < e; o++) {
for (let n = 0; n < o; n++)
r[o] -= t[o * e + n] ** 2;
if (r[o] <= 0) return !1;
r[o] = Math.sqrt(r[o]);
for (let n = o + 1; n < e; n++) {
for (let f = 0; f < o; f++)
t[n * e + o] -= t[n * e + f] * t[o * e + f];
t[n * e + o] /= r[o];
}
}
for (let o = 0; o < e; o++) t[o * e + o] = r[o];
return !0;
}
function X(t, e) {
let r, o, n, f;
for (r = 0; r < e; r++)
for (t[r * e + r] = 1 / t[r * e + r], o = r + 1; o < e; o++) {
for (f = 0, n = r; n < o; n++) f -= t[o * e + n] * t[n * e + r];
t[o * e + r] = f / t[o * e + o];
}
for (r = 0; r < e; r++) for (o = r + 1; o < e; o++) t[r * e + o] = 0;
for (r = 0; r < e; r++) {
for (t[r * e + r] *= t[r * e + r], n = r + 1; n < e; n++) t[r * e + r] += t[n * e + r] * t[n * e + r];
for (o = r + 1; o < e; o++) for (n = o; n < e; n++) t[r * e + o] += t[n * e + r] * t[n * e + o];
}
for (r = 0; r < e; r++) for (o = 0; o < r; o++) t[r * e + o] = t[o * e + r];
}
function z(t, e) {
let r = e, o = Array(e * e), n = Array(e), f = Array(e), l = Array(e), u, i, s, c, _, a, m, d, g, p, E;
for (u = 0; u < e; u++)
for (c = 0; c < e; c++)
u == c ? o[u * e + c] = 1 : o[u * e + c] = 0;
for (c = 0; c < e; c++) l[c] = 0;
for (u = 0; u < e; u++) {
for (d = 0, c = 0; c < e; c++)
if (l[c] != 1)
for (_ = 0; _ < e; _++)
l[_] == 0 && Math.abs(t[c * e + _]) >= d && (d = Math.abs(t[c * e + _]), s = c, i = _);
if (++l[i], s != i) {
for (a = 0; a < e; a++)
E = t[s * e + a], t[s * e + a] = t[i * e + a], t[i * e + a] = E;
for (a = 0; a < r; a++)
E = o[s * e + a], o[s * e + a] = o[i * e + a], o[i * e + a] = E;
}
if (f[u] = s, n[u] = i, t[i * e + i] == 0) return !1;
for (p = 1 / t[i * e + i], t[i * e + i] = 1, a = 0; a < e; a++) t[i * e + a] *= p;
for (a = 0; a < r; a++) o[i * e + a] *= p;
for (m = 0; m < e; m++)
if (m != i) {
for (g = t[m * e + i], t[m * e + i] = 0, a = 0; a < e; a++) t[m * e + a] -= t[i * e + a] * g;
for (a = 0; a < r; a++) o[m * e + a] -= o[i * e + a] * g;
}
}
for (a = e - 1; a >= 0; a--)
if (f[a] != n[a])
for (_ = 0; _ < e; _++)
E = t[_ * e + f[a]], t[_ * e + f[a]] = t[_ * e + n[a]], t[_ * e + n[a]] = E;
return !0;
}
function Z(t, e, r, o, n) {
return e + (o - e) / r * (1 - Math.exp(-(1 / n) * Math.pow(t / r, 2)));
}
function Q(t, e, r, o, n) {
return e + (o - e) / r * (1 - Math.exp(-(1 / n) * (t / r)));
}
function J(t, e, r, o, n) {
return t > r ? e + (o - e) / r : e + (o - e) / r * (1.5 * (t / r) - 0.5 * Math.pow(t / r, 3));
}
function W(t) {
switch (t) {
case "gaussian":
return Z;
case "exponential":
return Q;
case "spherical":
return J;
}
}
function he(t, e, r, o, n, f) {
const l = {
t,
x: e,
y: r,
nugget: 0,
range: 0,
sill: 0,
A: 0.3333333333333333,
n: 0,
model: o
}, u = W(o);
var i, s, c, _, a = t.length, m = Array((a * a - a) / 2);
for (i = 0, c = 0; i < a; i++)
for (s = 0; s < i; s++, c++)
m[c] = Array(2), m[c][0] = Math.pow(Math.pow(e[i] - e[s], 2) + Math.pow(r[i] - r[s], 2), 0.5), m[c][1] = Math.abs(t[i] - t[s]);
m.sort(function(K, Y) {
return K[0] - Y[0];
}), l.range = m[(a * a - a) / 2 - 1][0];
var d = (a * a - a) / 2 > 30 ? 30 : (a * a - a) / 2, g = l.range / d, p = w(0, d), E = w(0, d);
if (d < 30)
for (_ = 0; _ < d; _++)
p[_] = m[_][0], E[_] = m[_][1];
else {
for (i = 0, s = 0, c = 0, _ = 0; i < d && s < (a * a - a) / 2; i++, c = 0) {
for (; m[s][0] <= (i + 1) * g && (p[_] += m[s][0], E[_] += m[s][1], s++, c++, !(s >= (a * a - a) / 2)); )
;
c > 0 && (p[_] /= c, E[_] /= c, _++);
}
if (_ < 2) return l;
}
a = _, l.range = p[a - 1] - p[0];
var b = w(1, 2 * a), P = Array(a), y = l.A;
for (i = 0; i < a; i++) {
switch (o) {
case "gaussian":
b[i * 2 + 1] = 1 - Math.exp(-(1 / y) * Math.pow(p[i] / l.range, 2));
break;
case "exponential":
b[i * 2 + 1] = 1 - Math.exp(-(1 / y) * p[i] / l.range);
break;
case "spherical":
b[i * 2 + 1] = 1.5 * (p[i] / l.range) - 0.5 * Math.pow(p[i] / l.range, 3);
break;
}
P[i] = E[i];
}
var B = q(b, a, 2), T = v(B, b, 2, a, 2);
T = M(T, G(1 / f, 2), 2, 2);
var L = T.slice(0);
O(T, 2) ? X(T, 2) : (z(L, 2), T = L);
var I = v(v(T, B, 2, 2, a), P, 2, a, 1);
l.nugget = I[0], l.sill = I[1] * l.range + l.nugget, l.n = e.length, a = e.length;
var R = Array(a * a);
for (i = 0; i < a; i++) {
for (s = 0; s < i; s++)
R[i * a + s] = u(
Math.pow(Math.pow(e[i] - e[s], 2) + Math.pow(r[i] - r[s], 2), 0.5),
l.nugget,
l.range,
l.sill,
l.A
), R[s * a + i] = R[i * a + s];
R[i * a + i] = u(0, l.nugget, l.range, l.sill, l.A);
}
var h = M(R, G(n, a), a, a), N = h.slice(0);
O(h, a) ? X(h, a) : (z(N, a), h = N);
var R = h.slice(0), $ = v(h, t, a, a, 1);
return l.K = R, l.M = $, l;
}
function Ae(t, e, r) {
const o = Array(r.n), n = W(r.model);
for (let f = 0; f < r.n; f++)
o[f] = n(
Math.hypot(t - r.x[f], e - r.y[f]),
r.nugget,
r.range,
r.sill,
r.A
);
return v(o, r.M, 1, r.n, 1)[0];
}
const j = typeof importScripts < "u", S = typeof OffscreenCanvas < "u", F = {
gaussian: 1,
exponential: 2,
spherical: 3
}, U = {
"packed-imagebitmap": 1,
"value-buffer": 2,
imagebitmap: 3
}, A = 256;
class D {
_model;
_n;
//variogram.n
_params;
//variogram.nugget, variogram.range, variogram.sill, variogram.A
_texture;
_textureSize;
dispose;
}
class k {
_texture;
_textureSize;
_stopCount;
dispose;
}
const ee = /* @__PURE__ */ (() => {
let t = !1, e, r;
const o = {};
return (n) => {
if (t || (e = j ? new OffscreenCanvas(1, 1) : document.createElement("canvas"), e.width = e.height = 1, r = e.getContext("2d", { willReadFrequently: !0 }), t = !0), !o[n]) {
r.fillStyle = n, r.fillRect(0, 0, 1, 1);
const [f, l, u, i] = r.getImageData(0, 0, 1, 1).data;
o[n] = [f, l, u, i];
}
return o[n];
};
})();
function te() {
let t, e;
return { promise: new Promise((o, n) => {
t = o, e = n;
}), resolve: t, reject: e };
}
function re(t) {
return t & 7 ? t & 3 ? t & 1 ? 1 : 2 : 4 : 8;
}
function oe(t) {
if (!t) throw new Error("长度不存在!");
const e = ae(Math.ceil(t)), r = Math.log2(e), o = Math.ceil(r / 2), n = r - o;
return [2 ** o, 2 ** n];
}
function ae(t) {
return t & t - 1 ? (t |= t >> 1, t |= t >> 2, t |= t >> 4, t |= t >> 8, t |= t >> 16, t + 1) : t === 0 ? 1 : t;
}
function x(t, e) {
if (!t) throw new Error(e);
}
const ie = `
vec4 packNormalizeFloatToRGBA( in float v ) {
vec4 enc = vec4(v, fract(vec3(255.0, 65025.0, 16581375.0) * v));
enc.xyz -= enc.yzw / 255.0;
return enc;
}
float unpackRGBAToNormalizeFloat( const in vec4 v ) {
return dot(v, vec4(1, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 16581375.0));
}
vec3 packNormalizeFloatToRGB( in float v ) {
return packNormalizeFloatToRGBA( v ).xyz;
}
float unpackRGBToNormalizeFloat( const in vec3 v ) {
return unpackRGBAToNormalizeFloat( vec4( v, 0 ) );
}
`, ne = `
#define MODEL_GAUSSIAN ${F.gaussian.toFixed(1)}
#define MODEL_EXPONENTIAL ${F.exponential.toFixed(1)}
#define MODEL_SPHERICAL ${F.spherical.toFixed(1)}
struct Variogram {
float nugget;
float range;
float sill;
float A;
};
float variogram_gaussian(float h, const Variogram p){
float i = -(1.0 / p.A) * pow(h / p.range, 2.0);
return p.nugget + ((p.sill - p.nugget) / p.range) * (1.0 - exp(i));
}
float variogram_exponential(float h, const Variogram p){
float i = -(1.0 / p.A) * (h / p.range);
return p.nugget + ((p.sill - p.nugget) / p.range) * (1.0 - exp(i));
}
float variogram_spherical(float h, const Variogram p){
if (h > p.range) return p.nugget + (p.sill - p.nugget) / p.range;
return p.nugget + ((p.sill - p.nugget) / p.range) * (1.5 * (h / p.range) - 0.5 * pow(h / p.range, 3.0));
}
float modelValue(float model, float h, const Variogram p){
return model == MODEL_GAUSSIAN
? variogram_gaussian(h, p)
: model == MODEL_EXPONENTIAL
? variogram_exponential(h, p)
: variogram_spherical(h, p);
}`, le = `
struct Node {
float min;
float max;
vec4 color;
};
Node decode_classbreak(vec4 data){
float pack_rg = data.b;
float pack_ba = data.a;
vec4 color = vec4(
floor(pack_rg),
clamp(fract(pack_rg) * 1000.0, 0.0, 255.0),
floor(pack_ba),
clamp(fract(pack_ba) * 1000.0, 0.0, 255.0)
) / 255.0;
return Node(data.r, data.g, color);
}
vec4 mappingColor(
sampler2D map,
int stopCount,
float value
){
int left = 0;
int right = stopCount - 1;
vec4 headColor = vec4(0);
vec4 tailColor = vec4(0);
for(int i = 0; i < ${Math.log2(A)}; i++){
if(left > right) break;
int middle = (left + right) / 2;
float x = (float(middle) + 0.5) / ${A.toFixed(1)};
vec4 encodeData = texture2D(map, vec2(x, 0.5));
Node node = decode_classbreak(encodeData);
if(middle == 0) headColor = node.color;
if(middle == ${A} - 1) tailColor = node.color;
if(node.min > value){
right = middle - 1;
}else if(node.max <= value){
left = middle + 1;
}else{
return node.color;
}
}
if(right < 0) return headColor;
if(left >= stopCount) return tailColor;
}
`, ue = `
#define PACKED_IMAGEBITMAP ${U["packed-imagebitmap"].toFixed(1)}
#define VALUE_BUFFER ${U["value-buffer"].toFixed(1)}
#define IMAGEBITMA ${U.imagebitmap.toFixed(1)}
#ifdef webgl2
layout(std140) uniform DefaultUBO {
vec3 u_gridInfo;
float u_dimension;
vec2 u_variogramMxySize;
vec2 u_packValueRange;
vec4 u_variogramParam;
float u_model;
float u_classbreakCount;
float u_outputFormat;
};
#else
uniform vec3 u_gridInfo; // xmin, ymin, cellSize
uniform float u_dimension;
uniform vec2 u_variogramMxySize;
uniform vec2 u_packValueRange;
uniform vec4 u_variogramParam; //nugget, range, sill, A
uniform float u_model;
uniform float u_classbreakCount;
uniform float u_outputFormat;
#endif
uniform sampler2D u_variogramMxyTexture;
uniform sampler2D u_colormappingTexture;
${ie}
${ne}
${le}
vec3 lookup(float index){
float col = mod(index, u_variogramMxySize.x);
float row = floor(index / u_variogramMxySize.x);
vec2 pixel = 1.0 / u_variogramMxySize;
vec2 uv = vec2(col, row) * pixel + pixel * 0.5;
return texture2D(u_variogramMxyTexture, uv).xyz;
}
float hypot(float a, float b){
return pow(pow(a,2.0) + pow(b,2.0), 0.5);
}
void main(){
Variogram variogram = Variogram(
u_variogramParam.x,
u_variogramParam.y,
u_variogramParam.z,
u_variogramParam.w
);
vec2 inputCoord = gl_FragCoord.xy * u_gridInfo.z + u_gridInfo.xy;
int max_i = int(u_dimension);
float sum = 0.0;
for(int i = 0; i < 1024; i++){
if(i == max_i) break;
vec3 mxy = lookup(float(i));
sum += modelValue(
u_model,
hypot(inputCoord.x - mxy[1], inputCoord.y - mxy[2]),
variogram
) * mxy[0];
}
if(u_outputFormat == PACKED_IMAGEBITMAP){
float normalizedSum = (sum - u_packValueRange.x) / (u_packValueRange.y - u_packValueRange.x);
gl_FragColor.rgb = packNormalizeFloatToRGB(normalizedSum);
gl_FragColor.a = 1.0;
}else if(u_outputFormat == VALUE_BUFFER){
gl_FragColor = vec4(sum, 0, 0, 1);
}else{
gl_FragColor = mappingColor(u_colormappingTexture, int(u_classbreakCount), sum);
}
}
`;
function fe() {
let t;
j ? (x(S, "OffscreenCanvas unsupport"), t = new OffscreenCanvas(1, 1)) : t = S ? new OffscreenCanvas(1, 1) : document.createElement("canvas");
const e = {
alpha: !1,
depth: !1,
stencil: !1
}, r = t.getContext("webgl2", e) || t.getContext("webgl", e);
x(!!r, "webgl unsupport");
const o = typeof WebGL2RenderingContext < "u" && r instanceof WebGL2RenderingContext, n = {};
return {
canvas: t,
gl: r,
isWEBGL2: o,
getExtension: (l) => n[l] ??= r.getExtension(l) ?? !1
};
}
let ce;
function C() {
return ce ??= fe();
}
function V(t, e, r) {
const o = t.createShader(e);
if (t.shaderSource(o, r), t.compileShader(o), t.getShaderParameter(o, t.COMPILE_STATUS)) return o;
const f = t.getShaderInfoLog(o);
throw t.deleteShader(o), new Error(f);
}
function se(t, [e, r]) {
const o = t.createProgram(), n = V(t, t.VERTEX_SHADER, e), f = V(t, t.FRAGMENT_SHADER, r);
t.attachShader(o, n), t.attachShader(o, f), t.linkProgram(o);
const l = t.getProgramParameter(o, t.LINK_STATUS);
if (t.deleteShader(n), t.deleteShader(f), l) return o;
const u = t.getProgramInfoLog(o);
throw t.deleteProgram(o), new Error(u);
}
function _e(t) {
const { gl: e, getExtension: r, isWEBGL2: o } = C();
x(t?.length && t.length <= 256, "stops length must in [1, 256]");
for (let u = 0; u < t.length; u++) {
const { min: i, max: s, color: c } = t[u];
if (x(i < s, "stop not satisify: min < max"), x(!!c, "stop color not exist"), u === 0) continue;
const _ = t[u - 1];
x(_.max === i, "stop not satisify item[i].max == item[i+1].min");
}
const n = new Float32Array(A * 4);
for (let u = 0; u < t.length; u++) {
const { min: i, max: s, color: c } = t[u], [_, a, m, d] = ee(c), g = u * 4;
n[g] = i, n[g + 1] = s, n[g + 2] = _ + a / 1e3, n[g + 3] = m + d / 1e3;
}
o || x(r("OES_texture_float"), "webgl float texture unsupport");
const f = e.createTexture();
e.bindTexture(e.TEXTURE_2D, f), e.pixelStorei(e.UNPACK_ALIGNMENT, 8), e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL, !1), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
e.TEXTURE_2D,
0,
o ? e.RGBA32F : e.RGBA,
A,
1,
0,
e.RGBA,
e.FLOAT,
n
), e.bindTexture(e.TEXTURE_2D, null);
const l = new k();
return l._texture = f, l._textureSize = [A, 1], l._stopCount = t.length, l.dispose = () => e.deleteTexture(f), l;
}
function me(t) {
const { gl: e, getExtension: r, isWEBGL2: o } = C();
x(t.n <= 1024, "Supports up to 1024 points");
const [n, f] = oe(t.n), { M: l, x: u, y: i } = t, s = new Float32Array(n * f * 4);
for (let a = 0; a < t.n; a++) {
const m = a * 4;
s[m] = l[a], s[m + 1] = u[a], s[m + 2] = i[a];
}
o || x(r("OES_texture_float"), "webgl float texture unsupport");
const c = e.createTexture();
e.bindTexture(e.TEXTURE_2D, c), e.pixelStorei(e.UNPACK_ALIGNMENT, re(n)), e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL, !1), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_S, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_WRAP_T, e.CLAMP_TO_EDGE), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MIN_FILTER, e.NEAREST), e.texParameteri(e.TEXTURE_2D, e.TEXTURE_MAG_FILTER, e.NEAREST), e.texImage2D(
e.TEXTURE_2D,
0,
o ? e.RGBA32F : e.RGBA,
n,
f,
0,
e.RGBA,
e.FLOAT,
s
), e.bindTexture(e.TEXTURE_2D, null);
const _ = new D();
return _._model = t.model, _._n = t.n, _._params = [t.nugget, t.range, t.sill, t.A], _._texture = c, _._textureSize = [n, f], _.dispose = () => e.deleteTexture(c), _;
}
let ge;
function pe() {
return ge ??= de();
}
function de() {
const { gl: t, isWEBGL2: e, getExtension: r } = C(), o = e ? `#version 300 es
#define attribute in
#define varying out
#define webgl2
` : "", n = e ? `#version 300 es
#define varying in
#define webgl2
#define texture2D texture
#define gl_FragColor out_color
precision highp float;
precision highp sampler2D;
out vec4 out_color;
` : `
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
precision highp sampler2D;
#else
precision mediump float;
precision mediump sampler2D;
#endif
`, f = o + `
attribute vec2 position;
void main(){
gl_Position = vec4(position * 2.0 - 1.0, 0, 1);
}
`, l = n + ue, u = se(t, [f, l]);
t.useProgram(u);
const i = t.getAttribLocation(u, "position"), s = t.createBuffer();
t.bindBuffer(t.ARRAY_BUFFER, s), t.bufferData(t.ARRAY_BUFFER, new Float32Array([0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1]), t.STATIC_DRAW), t.enableVertexAttribArray(i), t.vertexAttribPointer(i, 2, t.FLOAT, !1, 0, 0), t.bindBuffer(t.ARRAY_BUFFER, null);
const c = new H({
max: 10,
dispose: ({ fbo: a, attachment: m }) => {
t.deleteFramebuffer(a), t.deleteTexture(m);
}
});
return {
setUniforms: Ee(u),
createBufferRT: _
};
function _(a, m) {
const d = `${a}-${m}`;
if (c.has(d)) return c.get(d);
const g = t.createTexture();
t.bindTexture(t.TEXTURE_2D, g), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MIN_FILTER, t.NEAREST), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_MAG_FILTER, t.NEAREST), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_WRAP_S, t.CLAMP_TO_EDGE), t.texParameteri(t.TEXTURE_2D, t.TEXTURE_WRAP_T, t.CLAMP_TO_EDGE), e ? x(r("EXT_color_buffer_float"), "webgl2 EXT_color_buffer_float unsupport") : x(r("OES_texture_float"), "webgl float texture unsupport"), t.texImage2D(
t.TEXTURE_2D,
0,
e ? t.R32F : t.RGBA,
a,
m,
0,
e ? t.RED : t.RGBA,
t.FLOAT,
null
);
const p = t.createFramebuffer();
t.bindFramebuffer(t.FRAMEBUFFER, p), t.framebufferTexture2D(t.FRAMEBUFFER, t.COLOR_ATTACHMENT0, t.TEXTURE_2D, g, 0), t.bindTexture(t.TEXTURE_2D, null), t.bindFramebuffer(t.FRAMEBUFFER, null);
const E = {
fbo: p,
attachment: g
};
return c.set(d, E), E;
}
}
function Ee(t) {
const { gl: e, isWEBGL2: r } = C(), o = e.getUniformLocation(t, "u_variogramMxyTexture"), n = e.getUniformLocation(t, "u_colormappingTexture");
if (e.uniform1i(o, 0), e.uniform1i(n, 1), r) {
const f = e.createBuffer();
e.bindBuffer(e.UNIFORM_BUFFER, f), e.bufferData(e.UNIFORM_BUFFER, 16 * Float32Array.BYTES_PER_ELEMENT, e.DYNAMIC_DRAW);
const l = e.getUniformBlockIndex(t, "DefaultUBO");
return e.uniformBlockBinding(t, l, 0), e.bindBufferBase(e.UNIFORM_BUFFER, 0, f), ({ outputFormat: u, variogram: i, packValueRange: s, llCorner: c, cellSize: _, colorMapping: a }) => {
e.bindBuffer(e.UNIFORM_BUFFER, f), e.bufferSubData(e.UNIFORM_BUFFER, 0, new Float32Array([
//ROW
c[0],
c[1],
_,
i._n,
//ROW
i._textureSize[0],
i._textureSize[1],
u === "packed-imagebitmap" ? s[0] : NaN,
u === "packed-imagebitmap" ? s[1] : NaN,
//ROW
i._params[0],
i._params[1],
i._params[2],
i._params[3],
//ROW
F[i._model],
u === "imagebitmap" ? a._stopCount : NaN,
U[u],
NaN
])), e.bindBuffer(e.UNIFORM_BUFFER, null), e.activeTexture(e.TEXTURE0), e.bindTexture(e.TEXTURE_2D, i._texture), u === "imagebitmap" && (e.activeTexture(e.TEXTURE0 + 1), e.bindTexture(e.TEXTURE_2D, a._texture));
};
} else {
const l = [
"outputFormat",
"gridInfo",
"dimension",
"variogramMxySize",
"packValueRange",
"variogramParam",
"model",
"classbreakCount"
].reduce((u, i) => (u[i] = e.getUniformLocation(t, "u_" + i), u), {});
return ({ outputFormat: u, variogram: i, packValueRange: s, llCorner: c, cellSize: _, colorMapping: a }) => {
e.uniform3fv(l.gridInfo, [c[0], c[1], _]), e.uniform1f(l.dimension, i._n), e.uniform2fv(l.variogramMxySize, i._textureSize), u === "packed-imagebitmap" && e.uniform2fv(l.packValueRange, s), e.uniform4fv(l.variogramParam, i._params), e.uniform1f(l.model, F[i._model]), u === "imagebitmap" && (e.uniform1f(l.classbreakCount, a._stopCount), e.activeTexture(e.TEXTURE0 + 1), e.bindTexture(e.TEXTURE_2D, a._texture)), e.uniform1f(l.outputFormat, U[u]), e.activeTexture(e.TEXTURE0), e.bindTexture(e.TEXTURE_2D, i._texture);
};
}
}
function xe(t) {
const e = [];
let r;
return (...n) => {
const { promise: f, resolve: l, reject: u } = te(), i = { resolve: l, reject: u, args: n };
return e.unshift(i), Promise.resolve().then(o), f.cancel = () => {
if (u(), i !== r) {
const s = e.findIndex((c) => c === i);
e.splice(s, 1);
}
}, f;
};
function o() {
r || e.length && (r = e.pop(), t.apply(null, r.args).then((n) => r.resolve(n)).catch((n) => r.reject(n)).finally(() => {
r = null, o();
}));
}
}
function Te(t) {
const { gl: e, canvas: r, isWEBGL2: o } = C(), { createBufferRT: n, setUniforms: f } = pe(), { llCorner: l, gridSize: u, cellSize: i, packValueRange: s, outputFormat: c } = t;
x(u[0] > 0 && u[1] > 0, "gridsize can not be 0");
const _ = t.variogram instanceof D ? t.variogram : me(t.variogram), a = c === "imagebitmap" ? t.colorMapping instanceof k ? t.colorMapping : _e(t.colorMapping) : null;
r.width = u[0], r.height = u[1];
let m;
if (c === "value-buffer" ? (m = n(r.width, r.height), e.bindFramebuffer(e.FRAMEBUFFER, m.fbo)) : e.bindFramebuffer(e.FRAMEBUFFER, null), e.viewport(0, 0, r.width, r.height), e.clearColor(0, 0, 0, 0), e.clear(e.COLOR_BUFFER_BIT), f({
outputFormat: c,
variogram: _,
packValueRange: s,
llCorner: l,
cellSize: i,
colorMapping: a
}), e.drawArrays(e.TRIANGLES, 0, 6), c !== "value-buffer")
return d(), S ? Promise.resolve(r.transferToImageBitmap()) : createImageBitmap(r, {
imageOrientation: "none"
});
{
let g;
if (o)
g = new Float32Array(r.width * r.height), e.readPixels(0, 0, r.width, r.height, e.RED, e.FLOAT, g);
else {
g = new Float32Array(r.width * r.height);
const p = new Float32Array(r.width * r.height * 4);
e.readPixels(0, 0, r.width, r.height, e.RGBA, e.FLOAT, p);
for (let E = 0; E < p.length; E += 4)
g[E / 4] = p[E];
}
return d(), Promise.resolve(g);
}
function d() {
t.variogram instanceof D || _.dispose(), c === "imagebitmap" && !(t.colorMapping instanceof k) && a.dispose();
}
}
const be = xe(Te);
export {
_e as createColorMappingObject,
me as createVariogramObject,
be as generate_WEBGL,
ie as glsl_pack,
Ae as predict,
he as train
};