Spaces:
Running
Running
Download source/src/geometry.js from Segizu/geometria3d-lab: direct link, hf CLI and curl.
- Browser
- Download file 11.3 kB
-
https://huggingface.co/spaces/Segizu/geometria3d-lab/resolve/main/source/src/geometry.js
- Command line
-
hf download hf://spaces/Segizu/geometria3d-lab/source/src/geometry.js
-
curl -L -o geometry.js https://huggingface.co/spaces/Segizu/geometria3d-lab/resolve/main/source/src/geometry.js
11.3 kB
| // Post-proceso geométrico de MoGe-2 portado a JS (sin dependencias). | |
| // Referencia: microsoft/MoGe@74fbce0 moge/model/v2.py::infer y moge/utils/geometry_torch.py (MIT). | |
| // El ONNX oficial sólo devuelve la salida cruda: mapa de puntos afín-invariante, normales, máscara y escala métrica. | |
| /** UV del plano de imagen normalizado (MoGe normalized_view_plane_uv): esquinas ±(W/diag, H/diag). */ | |
| export function uvGrid(W, H) { | |
| const ar = W / H; | |
| const sx = ar / Math.sqrt(1 + ar * ar), sy = 1 / Math.sqrt(1 + ar * ar); | |
| const u = new Float64Array(W), v = new Float64Array(H); | |
| const lin = (a, b, n, i) => (n === 1 ? a : a + ((b - a) * i) / (n - 1)); | |
| for (let j = 0; j < W; j++) u[j] = lin(-sx * (W - 1) / W, sx * (W - 1) / W, W, j); | |
| for (let i = 0; i < H; i++) v[i] = lin(-sy * (H - 1) / H, sy * (H - 1) / H, H, i); | |
| return { u, v }; | |
| } | |
| /** Índices de F.interpolate(mode='nearest'): src = floor(dst * in/out). */ | |
| export function nearestIdx(nIn, nOut) { | |
| const r = new Int32Array(nOut); | |
| for (let k = 0; k < nOut; k++) r[k] = Math.min(Math.floor(k * (nIn / nOut)), nIn - 1); | |
| return r; | |
| } | |
| /** Residuo de MoGe solve_optimal_focal_shift para un shift dado. Devuelve {cost, focal}. */ | |
| export function focalShiftCost(uv, xyz, shift) { | |
| const n = uv.length / 2; | |
| let num = 0, den = 0; | |
| const px = new Float64Array(n), py = new Float64Array(n); | |
| for (let k = 0; k < n; k++) { | |
| const z = xyz[3 * k + 2] + shift; | |
| px[k] = xyz[3 * k] / z; py[k] = xyz[3 * k + 1] / z; | |
| num += px[k] * uv[2 * k] + py[k] * uv[2 * k + 1]; | |
| den += px[k] * px[k] + py[k] * py[k]; | |
| } | |
| const f = num / den; | |
| let cost = 0; | |
| for (let k = 0; k < n; k++) { | |
| const ex = f * px[k] - uv[2 * k], ey = f * py[k] - uv[2 * k + 1]; | |
| cost += ex * ex + ey * ey; | |
| } | |
| return { cost: 0.5 * cost, focal: f }; | |
| } | |
| /** | |
| * Minimiza el residuo respecto a `shift` (escalar). MoGe usa scipy least_squares(method='lm', x0=0, ftol=1e-3); | |
| * aquí: Gauss-Newton/Levenberg-Marquardt 1-D con derivada numérica, restringido a z+shift>0, hasta convergencia. | |
| */ | |
| export function solveFocalShift(uv, xyz, { maxIter = 100 } = {}) { | |
| const n = uv.length / 2; | |
| let zmin = Infinity; | |
| for (let k = 0; k < n; k++) zmin = Math.min(zmin, xyz[3 * k + 2]); | |
| const lo = -zmin + 1e-6; // z + shift > 0 | |
| let s = Math.max(0, lo + 1e-6); | |
| let { cost } = focalShiftCost(uv, xyz, s); | |
| let lambda = 1e-3; | |
| for (let it = 0; it < maxIter; it++) { | |
| const h = 1e-6 * Math.max(1, Math.abs(s)); | |
| const c1 = focalShiftCost(uv, xyz, s + h).cost, c0 = focalShiftCost(uv, xyz, Math.max(lo, s - h)).cost; | |
| const g = (c1 - c0) / (s + h - Math.max(lo, s - h)); | |
| const H2 = (c1 - 2 * cost + c0) / (h * h); | |
| let improved = false; | |
| for (let tries = 0; tries < 30; tries++) { | |
| const step = -g / (Math.max(H2, 1e-12) * (1 + lambda) + lambda); | |
| const sn = Math.max(lo, s + step); | |
| const cn = focalShiftCost(uv, xyz, sn).cost; | |
| if (cn < cost) { | |
| const rel = (cost - cn) / Math.max(cost, 1e-30); | |
| s = sn; cost = cn; lambda = Math.max(lambda / 10, 1e-9); improved = true; | |
| if (rel < 1e-10 || Math.abs(step) < 1e-12) return { shift: s, focal: focalShiftCost(uv, xyz, s).focal, iters: it + 1 }; | |
| break; | |
| } | |
| lambda *= 10; | |
| } | |
| if (!improved) break; | |
| } | |
| return { shift: s, focal: focalShiftCost(uv, xyz, s).focal, iters: maxIter }; | |
| } | |
| /** | |
| * Pipeline completo de MoGeModel.infer (force_projection=True, apply_mask=True, fov_x desconocido). | |
| * points: Float32Array H*W*3 (afín), mask: Float32Array H*W (sigmoide), scale: escalar métrico. | |
| */ | |
| export function postprocess(points, maskProb, W, H, scale, { lr = 64 } = {}) { | |
| const { u, v } = uvGrid(W, H); | |
| const ri = nearestIdx(H, lr), ci = nearestIdx(W, lr); | |
| const uvs = [], xyzs = []; | |
| for (const i of ri) for (const j of ci) { | |
| const p = i * W + j; | |
| if (maskProb[p] > 0.5) { uvs.push(u[j], v[i]); xyzs.push(points[3 * p], points[3 * p + 1], points[3 * p + 2]); } | |
| } | |
| let shift = 0, focal = 1; | |
| if (uvs.length >= 4) ({ shift, focal } = solveFocalShift(Float64Array.from(uvs), Float64Array.from(xyzs))); | |
| const ar = W / H; | |
| const fx = (focal / 2) * Math.sqrt(1 + ar * ar) / ar, fy = (focal / 2) * Math.sqrt(1 + ar * ar); | |
| const N = W * H; | |
| const out = new Float32Array(N * 3), depth = new Float32Array(N), valid = new Uint8Array(N); | |
| for (let i = 0; i < H; i++) { | |
| const vv = (i + 0.5) / H - 0.5; | |
| for (let j = 0; j < W; j++) { | |
| const p = i * W + j; | |
| const d = points[3 * p + 2] + shift; | |
| const ok = maskProb[p] > 0.5 && d > 0; | |
| valid[p] = ok ? 1 : 0; | |
| const dm = d * scale; | |
| depth[p] = ok ? dm : NaN; | |
| out[3 * p] = ok ? (((j + 0.5) / W - 0.5) / fx) * dm : NaN; | |
| out[3 * p + 1] = ok ? (vv / fy) * dm : NaN; | |
| out[3 * p + 2] = ok ? dm : NaN; | |
| } | |
| } | |
| const fovX = (2 * Math.atan(0.5 / fx) * 180) / Math.PI, fovY = (2 * Math.atan(0.5 / fy) * 180) / Math.PI; | |
| return { points: out, depth, valid, W, H, shift, focal, fx, fy, fxPx: fx * W, fyPx: fy * H, fovX, fovY, scale }; | |
| } | |
| /** Estadísticos de profundidad (m) sobre píxeles válidos. */ | |
| export function depthStats(depth) { | |
| const a = []; | |
| for (const d of depth) if (Number.isFinite(d)) a.push(d); | |
| if (!a.length) return { validPct: 0 }; | |
| a.sort((x, y) => x - y); | |
| const q = (t) => a[Math.min(a.length - 1, Math.floor(t * (a.length - 1)))]; | |
| return { min: a[0], p05: q(0.05), median: q(0.5), p95: q(0.95), max: a[a.length - 1], validPct: (100 * a.length) / depth.length }; | |
| } | |
| /** Punto 3D robusto en (x,y) de la imagen de trabajo: mediana por componente en una ventana (2r+1)². */ | |
| export function samplePoint(geo, x, y, r = 1) { | |
| const { W, H, points } = geo; | |
| const cx = Math.round(x), cy = Math.round(y); | |
| const xs = [], ys = [], zs = []; | |
| for (let i = Math.max(0, cy - r); i <= Math.min(H - 1, cy + r); i++) | |
| for (let j = Math.max(0, cx - r); j <= Math.min(W - 1, cx + r); j++) { | |
| const p = i * W + j; | |
| if (Number.isFinite(points[3 * p + 2])) { xs.push(points[3 * p]); ys.push(points[3 * p + 1]); zs.push(points[3 * p + 2]); } | |
| } | |
| if (!zs.length) return null; | |
| const med = (a) => { a.sort((m, n) => m - n); return a[a.length >> 1]; }; | |
| return [med(xs), med(ys), med(zs)]; | |
| } | |
| export const dist3 = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); | |
| /** Mide un segmento entre dos píxeles (coordenadas de la imagen ORIGINAL), aplicando factor de calibración. */ | |
| export function measure(geo, aPx, bPx, origW, origH, factor = 1) { | |
| const sx = geo.W / origW, sy = geo.H / origH; | |
| const A = samplePoint(geo, aPx[0] * sx - 0.5, aPx[1] * sy - 0.5), B = samplePoint(geo, bPx[0] * sx - 0.5, bPx[1] * sy - 0.5); | |
| if (!A || !B) return null; | |
| return { a3d: A.map((t) => t * factor), b3d: B.map((t) => t * factor), length: dist3(A, B) * factor, depthA: A[2] * factor, depthB: B[2] * factor }; | |
| } | |
| /** Factor de calibración: longitud real conocida / longitud medida. */ | |
| export function calibrationFactor(measuredM, knownM) { | |
| if (!(measuredM > 0) || !(knownM > 0) || !Number.isFinite(knownM)) throw new Error('Longitudes no válidas'); | |
| return knownM / measuredM; | |
| } | |
| /** Dimensiones de trabajo: lado largo = longSide (múltiplo entero), manteniendo aspecto. */ | |
| export function workSize(w, h, longSide) { | |
| const s = Math.min(1, longSide / Math.max(w, h)); | |
| return [Math.max(1, Math.round(w * s)), Math.max(1, Math.round(h * s))]; | |
| } | |
| /** HWC RGBA uint8 -> NCHW float32 [0,1] (el modelo normaliza internamente con media/desv. de ImageNet). */ | |
| export function rgbaToNCHW(rgba, W, H) { | |
| const N = W * H, out = new Float32Array(3 * N); | |
| for (let p = 0; p < N; p++) { | |
| out[p] = rgba[4 * p] / 255; out[N + p] = rgba[4 * p + 1] / 255; out[2 * N + p] = rgba[4 * p + 2] / 255; | |
| } | |
| return out; | |
| } | |
| // Paleta propia (cerca = cálido, lejos = frío). Interpolación lineal entre anclas. | |
| const PAL = [[255, 245, 160], [253, 174, 97], [215, 48, 39], [116, 41, 138], [49, 54, 149], [15, 20, 60]]; | |
| export function colormap(t) { | |
| t = Math.min(1, Math.max(0, t)) * (PAL.length - 1); | |
| const i = Math.min(PAL.length - 2, Math.floor(t)), f = t - i; | |
| return PAL[i].map((c, k) => Math.round(c + (PAL[i + 1][k] - c) * f)); | |
| } | |
| /** Imagen RGBA del mapa de profundidad (escala log entre p05 y p95; inválido = gris). */ | |
| export function depthToRGBA(depth, stats) { | |
| const out = new Uint8ClampedArray(depth.length * 4); | |
| const lo = Math.log(stats.p05), hi = Math.log(Math.max(stats.p95, stats.p05 * 1.0001)); | |
| for (let p = 0; p < depth.length; p++) { | |
| const d = depth[p]; | |
| const c = Number.isFinite(d) ? colormap((Math.log(d) - lo) / (hi - lo)) : [128, 128, 128]; | |
| out[4 * p] = c[0]; out[4 * p + 1] = c[1]; out[4 * p + 2] = c[2]; out[4 * p + 3] = 255; | |
| } | |
| return out; | |
| } | |
| /** Normales (OpenCV: x der., y abajo, z adelante) -> RGB: R=x, G=-y, B=-z (hacia la cámara = azul). */ | |
| export function normalsToRGBA(normal, valid) { | |
| const N = valid.length, out = new Uint8ClampedArray(N * 4); | |
| for (let p = 0; p < N; p++) { | |
| if (!valid[p]) { out[4 * p] = out[4 * p + 1] = out[4 * p + 2] = 128; out[4 * p + 3] = 255; continue; } | |
| out[4 * p] = Math.round((normal[3 * p] * 0.5 + 0.5) * 255); | |
| out[4 * p + 1] = Math.round((-normal[3 * p + 1] * 0.5 + 0.5) * 255); | |
| out[4 * p + 2] = Math.round((-normal[3 * p + 2] * 0.5 + 0.5) * 255); | |
| out[4 * p + 3] = 255; | |
| } | |
| return out; | |
| } | |
| /** PLY binario (little-endian) con xyz float32 (metros, OpenCV) + rgb uint8. step = submuestreo. */ | |
| export function toPLY(geo, rgba, factor = 1, step = 1) { | |
| const idx = []; | |
| for (let i = 0; i < geo.H; i += step) for (let j = 0; j < geo.W; j += step) { const p = i * geo.W + j; if (geo.valid[p]) idx.push(p); } | |
| const header = `ply\nformat binary_little_endian 1.0\ncomment Geometria3D Lab - MoGe-2 ViT-S (metros, ejes OpenCV x der. y abajo z adelante)\nelement vertex ${idx.length}\nproperty float x\nproperty float y\nproperty float z\nproperty uchar red\nproperty uchar green\nproperty uchar blue\nend_header\n`; | |
| const hb = new TextEncoder().encode(header); | |
| const buf = new ArrayBuffer(hb.length + idx.length * 15); | |
| new Uint8Array(buf).set(hb); | |
| const dv = new DataView(buf); | |
| let o = hb.length; | |
| for (const p of idx) { | |
| dv.setFloat32(o, geo.points[3 * p] * factor, true); dv.setFloat32(o + 4, geo.points[3 * p + 1] * factor, true); dv.setFloat32(o + 8, geo.points[3 * p + 2] * factor, true); | |
| dv.setUint8(o + 12, rgba[4 * p]); dv.setUint8(o + 13, rgba[4 * p + 1]); dv.setUint8(o + 14, rgba[4 * p + 2]); | |
| o += 15; | |
| } | |
| return { buffer: buf, count: idx.length }; | |
| } | |
| /** Validación de archivo de entrada. */ | |
| export function validateFile({ type, size }, { maxBytes, types }) { | |
| if (!types.includes(type)) return `Formato no admitido (${type || 'desconocido'}). Usa JPG, PNG o WebP.`; | |
| if (size > maxBytes) return `Archivo demasiado grande (${(size / 1e6).toFixed(1)} MB). Máximo ${(maxBytes / 1e6).toFixed(0)} MB.`; | |
| return null; | |
| } | |
| export function validateDims(w, h, maxPixels) { | |
| if (w * h > maxPixels) return `Imagen demasiado grande (${((w * h) / 1e6).toFixed(1)} MP). Máximo ${(maxPixels / 1e6).toFixed(0)} MP.`; | |
| if (Math.min(w, h) < 64) return 'Imagen demasiado pequeña (mínimo 64 px de lado).'; | |
| if (Math.max(w, h) / Math.min(w, h) > 4) return 'Relación de aspecto extrema (máx. 4:1).'; | |
| return null; | |
| } | |