File size: 4,238 Bytes
4eecd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Floor overlay (off by default). The floor and the wall/floor line are computed on the server the first time the layer is
// switched on for a job (floor_detect.py: general ADE20K floor model, made consistent with the wall model), then cached.
"use strict";
(() => {
  const $ = (sel) => document.querySelector(sel);
  const toggles = $("#layer-toggles"), stage = $("#stage");
  if (!toggles || !stage) return;
  const st = { jobId: null, loaded: false, loading: false, info: null };

  const chip = document.createElement("label");
  chip.className = "chip";
  chip.innerHTML = `<input type="checkbox" data-layer="floor"><i class="sw sw-floor"></i>Floor`;
  toggles.appendChild(chip);
  const box = chip.querySelector("input");

  const mk = (cls) => { const i = document.createElement("img"); i.className = cls; i.alt = ""; i.dataset.layer = "floor"; return i; };
  const fill = mk("layer off"), edge = mk("layer edge off");
  const anchor = document.querySelector('img.layer[data-layer="paint"]:not(.edge)') || $("#photo");
  anchor.after(fill);
  fill.after(edge);

  const NS = "http://www.w3.org/2000/svg";
  const line = document.createElementNS(NS, "svg");
  line.setAttribute("class", "layer floor-line");
  line.dataset.layer = "floor";
  line.setAttribute("preserveAspectRatio", "none");
  fill.parentNode.insertBefore(line, $("#vector") || null);

  const note = document.createElement("div");
  note.className = "floor-note small muted";
  note.hidden = true;
  const legend = $("#legend");
  if (legend) legend.after(note); else stage.after(note);

  function sync() { if (typeof applyLayerToggles === "function") applyLayerToggles(); }

  function drawLine(info) {
    line.replaceChildren();
    line.setAttribute("viewBox", `0 0 ${info.width} ${info.height}`);
    const fit = info.line && info.line.fit;
    if (!fit) return;
    const mk2 = (name, attrs) => { const e = document.createElementNS(NS, name); for (const [k, v] of Object.entries(attrs)) e.setAttribute(k, v); return e; };
    line.appendChild(mk2("line", { x1: 0, y1: fit.y_at_left, x2: info.width, y2: fit.y_at_right, class: "fl-halo" }));
    line.appendChild(mk2("line", { x1: 0, y1: fit.y_at_left, x2: info.width, y2: fit.y_at_right, class: "fl-line" }));
  }

  function describe(info) {
    if (!info.found) return "No floor found in this photo (it may not be in frame).";
    const pct = Math.round(info.share_of_image * 100);
    const fit = info.line && info.line.fit;
    if (!fit) return `Floor covers ${pct}% of the photo. The wall/floor edge is not a clean straight line here (furniture, a corner, or a curved edge).`;
    const a = fit.angle_deg, tilt = Math.abs(a);
    return `Floor covers ${pct}% of the photo. Wall/floor line ${tilt < 0.8 ? "is level" : `is tilted ${tilt.toFixed(1)}°`} (±${fit.rms_px} px). General-purpose model, checked by eye: correct it with the Fix-detection tools if it is wrong.`;
  }

  async function ensure() {
    if (!job || st.loading) return;
    if (st.loaded && st.jobId === job.id) return;
    st.loading = true;
    note.hidden = false;
    note.textContent = "Finding the floor… (first time per job takes a few seconds)";
    try {
      const r = await fetch(`/api/jobs/${job.id}/floor`);
      const info = await r.json();
      if (!r.ok) throw new Error(info.error || r.statusText);
      const v = `?v=${info.zones_stamp}-${info.seconds}`;
      fill.src = `/api/jobs/${job.id}/layer_floor.png${v}`;
      edge.src = `/api/jobs/${job.id}/layer_floor_edge.png${v}`;
      st.info = info; st.jobId = job.id; st.loaded = true;
      drawLine(info);
      note.textContent = describe(info);
    } catch (e) {
      note.textContent = `Floor detection is unavailable: ${e.message}`;
    } finally { st.loading = false; sync(); }
  }

  box.addEventListener("change", () => {
    note.hidden = !box.checked;
    sync();
    if (box.checked) ensure();
  });

  const prev = showJob;
  showJob = function (j) {
    prev(j);
    st.loaded = false; st.info = null;
    fill.removeAttribute("src"); edge.removeAttribute("src"); line.replaceChildren();
    note.hidden = !box.checked;
    if (box.checked) ensure(); else sync();
  };
  window.BuildPaintFloor = { get info() { return st.info; } };
})();