@trillboards/edge-sdk 0.2.258 → 0.2.260

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,961 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FLOOR_OBJECT_LEFT_V1 = exports.FLOOR_OBJECT_LEFT_METHOD = void 0;
4
+ exports.decodeFloorGrey = decodeFloorGrey;
5
+ exports.readFloorObjectMemory = readFloorObjectMemory;
6
+ exports.floorPeopleHold = floorPeopleHold;
7
+ exports.floorObjectDueAtMs = floorObjectDueAtMs;
8
+ exports.decideFloorObjects = decideFloorObjects;
9
+ const crypto_1 = require("crypto");
10
+ const SceneChangeDetector_1 = require("./SceneChangeDetector");
11
+ const zoneContainment_1 = require("./zoneContainment");
12
+ const SceneMarks_1 = require("./SceneMarks");
13
+ /**
14
+ * floor_object_left_v1 — something left on the floor inside the owner's drawn area: a box in the handicap aisle, a
15
+ * cart parked in front of the fire exit, a visible spill. No model: the area's empty floor, learned from the
16
+ * camera's own quiet looks, compared cell by cell with each look, minus the people the detector already found.
17
+ *
18
+ * WHAT IT ANSWERS. True while an object the box decided is still on the floor of the area: a region of the floor
19
+ * that differs from the empty floor, that no person stands on or touches, and that has not changed for `dwellMs`.
20
+ * A state, not an arrival: one alert per blocked spell (the route's episode), and an object still there when the
21
+ * route's hours begin alerts then (the carts parked overnight in front of the drinking fountains on Texas camera
22
+ * 10 were there at opening). The route's own review wait adds to the dwell.
23
+ *
24
+ * THE EMPTY FLOOR. Per 8x8 cell of a 240-pixel-high grey look (decodeFloorGrey below), the cell's pixels on looks
25
+ * with no person near the area, blended slowly
26
+ * (time constant `reference.tauMs`), only where the cell matches it. A cell that differs is never learned into it:
27
+ * an object never becomes floor by standing still. Two things move it on purpose:
28
+ * - a ghost: the floor where an object WAS (the reference held it, e.g. carts present when the box first looked)
29
+ * shows less structure now than the reference does there, and looks like the floor around it. It is learned at
30
+ * once and never alerts;
31
+ * - the owner's mark: a "False alarm" on a floor alert absorbs what is there when the mark arrives. A mark applies
32
+ * once, to that object, so the spot still alerts for the next thing left there.
33
+ * A lighting change (more than `lighting.changedShare` of the free area differs at once, and the floor's pattern
34
+ * survived it where it has one) relearns the free cells, forgets undecided objects, and for `lighting.settleMs` takes
35
+ * any new change as the lights' own (SOMEBODY LEFT IT, below). The lights are still settling while the area's
36
+ * brightness keeps moving: a look whose gain moved more than `lighting.settleMove` since the last look, while they
37
+ * settle, keeps them settling for another settleMs (Texas camera 9, Oct 25 18:00 CDT: the store's lights dimmed and the
38
+ * camera's exposure swung 5-30% look to look for three minutes, and a band of floor changed with it). Outside a
39
+ * settle window the swing starts nothing: some cameras' exposure moves 5% look to look all day. Decided objects stay:
40
+ * a light switched on does not move a box. It is judged only while `lighting.minFreeShare` of the area is free of
41
+ * people: two shoppers filling a narrow aisle leave a free area too small to tell lights from their reflections (Texas
42
+ * camera 13, Oct 25 13:08 CDT). Lights that change the floor's pattern too much to pass the test are a change nobody
43
+ * brought over as much of the area: once it holds still, the whole free floor is relearned the same way.
44
+ *
45
+ * THE COMPARISON. Gain-compensated (the median brightness ratio of the free area), per cell: the mean, the
46
+ * contrast, and the pattern (zero-mean normalized cross-correlation where both cells have texture). A person, their
47
+ * shadow and their reflection on a glossy floor are masked by their detector box, widened by `personPad`; anything a
48
+ * person box touches restarts its clock (`stillMs`), so a customer's cart counts only once they walk away from it.
49
+ *
50
+ * SOMEBODY LEFT IT. Something left on the floor was left by someone. Each cell remembers whether, when it started to
51
+ * differ from the floor, a person scored at least `object.bringerConfidence` (widened by `reference.quietRing` cells)
52
+ * had been near it within `object.broughtMs`, outside a lighting settle; an object can alert only when most of its
53
+ * cells began that way. Any other change is the floor changing by itself — a bank of lights going off over one stretch
54
+ * of the aisle at closing, a second bank a few seconds after the first, a shadow, the camera's exposure — and is learned
55
+ * as floor once it has held still for `ghost.afterMs` (Texas cameras 2, 4 and 9 on Oct 25: the far end of an aisle, a
56
+ * strip at a shelf base and a band of floor darkened as the store closed, with nobody at any of them). Per cell, not
57
+ * per object, and a cell keeps how it began until it has matched the floor for `object.missGraceMs`: an object that a
58
+ * passer-by splits and rejoins, or whose edge flickers, is still the change nobody brought. Held still, not at once: a
59
+ * shopper the detector missed is a change too. And unlearned when it goes back: each learned cell keeps the floor it
60
+ * replaced for `reference.ownMs`, and a cell that matches that floor again takes it back at once — the shadow of
61
+ * someone standing under the camera, out of its view, leaves the floor as it was (Texas camera 13, Oct 25 13:16 CDT:
62
+ * the floor the shadow had covered came back brighter than the learned shadow, with shoppers near it, and alerted).
63
+ * The bringer's score is higher than the mask's: anything person-like is masked, but the far end of a long aisle
64
+ * scores shelves and shadows at 0.15-0.3 all day (Texas camera 2: median 0.20 there, against 0.78 for the people of
65
+ * camera 10's aisle).
66
+ *
67
+ * A DECIDED OBJECT keeps its alert until it is carried off. It goes on over its own cells that still differ and the
68
+ * changed cells beside it that someone brought; it takes no part in the gain or the lighting vote, and its floor
69
+ * follows the floor around it when the lights change. A change someone brought touching where a lost decided object
70
+ * stood is that object, nudged: it keeps the decision. Seen standing a dwell past its decision, it outlasts a dwell of
71
+ * looks on which its cells pass for the floor. Carried off a floor the reference never saw, its cells that now look
72
+ * like the floor around them are absorbed one by one, so an object set down beside it keeps alerting.
73
+ *
74
+ * COST. One 320x240 grey decode (~3 ms) and integer cell sums over the area; no detector call of its own. A look
75
+ * whose area did not change needs no fresh person boxes: the last ones are still the truth, which is what lets the
76
+ * look scheduler answer unchanged frames without the detector and still run this.
77
+ *
78
+ * Pure: the same inputs always give the same answer, trace and memory. The memory is plain data
79
+ * (UniformSceneStore keeps it on disk), and a malformed one costs relearning, never a refusal.
80
+ */
81
+ exports.FLOOR_OBJECT_LEFT_METHOD = 'floor_object_left_v1';
82
+ /**
83
+ * The pinned numbers (every value but `field`): the parser refuses any other. Measured on the Texas aisle cameras'
84
+ * recorded days (docs/runbooks/cctv-detector-tier.md section 5l).
85
+ */
86
+ exports.FLOOR_OBJECT_LEFT_V1 = Object.freeze({
87
+ minPersonConfidence: 0.15,
88
+ personPad: Object.freeze({ side: 0.15, above: 0.05, below: 0.35 }),
89
+ grid: Object.freeze({ height: 240, cell: 8, ring: 2 }),
90
+ change: Object.freeze({ meanAbs: 10, meanRel: 0.12, textureStd: 4, minZncc: 0.5, stdAbs: 6, stdRel: 0.6 }),
91
+ object: Object.freeze({ minCells: 2, minInside: 0.5, stableDiff: 12, missGraceMs: 12000, dwellMs: 60000, maxObjects: 32,
92
+ broughtMs: 30000, bringerConfidence: 0.5 }),
93
+ ghost: Object.freeze({ afterMs: 15000, edgeRatio: 1.3, floorMeanAbs: 10, floorMeanRel: 0.1 }),
94
+ reference: Object.freeze({ tauMs: 600000, maxStepMs: 120000, quietRing: 3, ownMs: 1800000 }),
95
+ lighting: Object.freeze({ changedShare: 0.3, keptZncc: 0.6, keptShare: 0.6, flatShare: 0.6, settleMs: 60000,
96
+ settleMove: 0.08, minFreeShare: 0.5 }),
97
+ people: Object.freeze({ reuseDiff: 5, refreshMs: 60000 }),
98
+ });
99
+ const B64 = (bytes) => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('base64');
100
+ const fromB64 = (s) => {
101
+ const b = Buffer.from(s, 'base64');
102
+ return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
103
+ };
104
+ const sha = (v) => (0, crypto_1.createHash)('sha256').update(v).digest('hex');
105
+ const time = (v) => Number.isSafeInteger(v);
106
+ /**
107
+ * The look at `height` rows, grey: for the store PC's 480-row look, one full decode and a 2x2 average (BT.601 luma),
108
+ * a third of the CPU of a resized decode on the same frames (8.4 vs 24.4 ms on the devbox, 300 Texas looks); any
109
+ * other size takes the connector's grey decode (SceneChangeDetector.decodeGreyFrame). Null when nothing can decode it.
110
+ */
111
+ async function decodeFloorGrey(jpeg, height) {
112
+ let sharp = null;
113
+ try {
114
+ sharp = require('sharp');
115
+ }
116
+ catch {
117
+ sharp = null;
118
+ }
119
+ if (!sharp)
120
+ return (0, SceneChangeDetector_1.decodeGreyFrame)(jpeg, height);
121
+ const { data, info } = await sharp(jpeg).raw().toBuffer({ resolveWithObject: true });
122
+ if (info.height !== 2 * height || info.channels < 3 || info.width < 4)
123
+ return (0, SceneChangeDetector_1.decodeGreyFrame)(jpeg, height);
124
+ const C = info.channels, W = info.width, w = W >> 1, out = new Uint8Array(w * height);
125
+ for (let y = 0; y < height; y++) {
126
+ const r0 = 2 * y * W * C, r1 = r0 + W * C;
127
+ for (let x = 0; x < w; x++) {
128
+ const a = r0 + 2 * x * C, b = a + C, c = r1 + 2 * x * C, d = c + C;
129
+ const sum = 77 * (data[a] + data[b] + data[c] + data[d]) + 150 * (data[a + 1] + data[b + 1] + data[c + 1] + data[d + 1])
130
+ + 29 * (data[a + 2] + data[b + 2] + data[c + 2] + data[d + 2]);
131
+ out[y * w + x] = (sum + 512) >> 10;
132
+ }
133
+ }
134
+ return { width: w, height, data: out };
135
+ }
136
+ const grids = new Map();
137
+ function regionPoints(region) {
138
+ if (!Array.isArray(region) || region.length < 3 || region.length > 64)
139
+ throw Error('Floor objects need the drawn area');
140
+ for (const p of region)
141
+ (0, zoneContainment_1.assertNormalizedPoint)(p);
142
+ let area = 0;
143
+ for (let i = 0; i < region.length; i++) {
144
+ const a = region[i], b = region[(i + 1) % region.length];
145
+ area += a.x * b.y - b.x * a.y;
146
+ }
147
+ if (Math.abs(area) < 1e-6)
148
+ throw Error('Floor objects need a drawn area with room for an object');
149
+ return region.map(p => ({ x: p.x, y: p.y }));
150
+ }
151
+ function floorGrid(region, width, height, m) {
152
+ const points = regionPoints(region);
153
+ const key = sha(JSON.stringify({ points, width, height, cell: m.grid.cell, ring: m.grid.ring }));
154
+ const cached = grids.get(key);
155
+ if (cached)
156
+ return cached;
157
+ const cell = m.grid.cell, cols = Math.floor(width / cell), rows = Math.floor(height / cell);
158
+ const inside = new Uint8Array(cols * rows);
159
+ for (let r = 0; r < rows; r++)
160
+ for (let c = 0; c < cols; c++) {
161
+ inside[r * cols + c] = (0, zoneContainment_1.pointInPolygon)((c + 0.5) / cols, (r + 0.5) / rows, points) ? 1 : 0;
162
+ }
163
+ const insideCount = inside.reduce((a, v) => a + v, 0);
164
+ if (insideCount < m.object.minCells)
165
+ throw Error('Floor objects need a drawn area with room for an object');
166
+ const ring = m.grid.ring, take = new Uint8Array(cols * rows);
167
+ for (let r = 0; r < rows; r++)
168
+ for (let c = 0; c < cols; c++) {
169
+ if (!inside[r * cols + c])
170
+ continue;
171
+ for (let dr = -ring; dr <= ring; dr++)
172
+ for (let dc = -ring; dc <= ring; dc++) {
173
+ const rr = r + dr, cc = c + dc;
174
+ if (rr >= 0 && rr < rows && cc >= 0 && cc < cols)
175
+ take[rr * cols + cc] = 1;
176
+ }
177
+ }
178
+ const area = [];
179
+ const slot = new Int32Array(cols * rows).fill(-1);
180
+ for (let i = 0; i < take.length; i++)
181
+ if (take[i]) {
182
+ slot[i] = area.length;
183
+ area.push(i);
184
+ }
185
+ const grid = { key, width, height, cols, rows, cell, area, slot, inside, insideCount };
186
+ if (grids.size > 64)
187
+ grids.clear();
188
+ grids.set(key, grid);
189
+ return grid;
190
+ }
191
+ /** The look's pixels per area cell (64 a cell, row by row), with each cell's mean and standard deviation. */
192
+ function cellPixels(frame, g) {
193
+ const n = g.area.length, px = new Uint8Array(n * 64), mean = new Float64Array(n), std = new Float64Array(n);
194
+ const cell = g.cell, scale = cell / 8;
195
+ for (let k = 0; k < n; k++) {
196
+ const r = Math.floor(g.area[k] / g.cols), c = g.area[k] % g.cols;
197
+ let s = 0, s2 = 0;
198
+ for (let y = 0; y < 8; y++)
199
+ for (let x = 0; x < 8; x++) {
200
+ const v = frame.data[(r * cell + Math.floor(y * scale)) * frame.width + c * cell + Math.floor(x * scale)];
201
+ px[k * 64 + y * 8 + x] = v;
202
+ s += v;
203
+ s2 += v * v;
204
+ }
205
+ mean[k] = s / 64;
206
+ std[k] = Math.sqrt(Math.max(0, s2 / 64 - mean[k] * mean[k]));
207
+ }
208
+ return { px, mean, std };
209
+ }
210
+ /** Area cells a person box (widened by personPad) covers, dilated by `grow` cells. */
211
+ function personCells(detections, frame, g, m, grow) {
212
+ const mask = new Uint8Array(g.cols * g.rows);
213
+ for (const d of detections) {
214
+ if (d.label !== 'person' || !(d.confidence >= m.minPersonConfidence))
215
+ continue;
216
+ const b = d.box, P = m.personPad;
217
+ const x0 = (b.x - P.side * b.width) / frame.width, x1 = (b.x + b.width + P.side * b.width) / frame.width;
218
+ const y0 = (b.y - P.above * b.height) / frame.height, y1 = (b.y + b.height + P.below * b.height) / frame.height;
219
+ const c0 = Math.max(0, Math.floor(x0 * g.cols) - grow), c1 = Math.min(g.cols - 1, Math.ceil(x1 * g.cols) - 1 + grow);
220
+ const r0 = Math.max(0, Math.floor(y0 * g.rows) - grow), r1 = Math.min(g.rows - 1, Math.ceil(y1 * g.rows) - 1 + grow);
221
+ for (let r = r0; r <= r1; r++)
222
+ for (let c = c0; c <= c1; c++)
223
+ mask[r * g.cols + c] = 1;
224
+ }
225
+ return mask;
226
+ }
227
+ /** The cells' bounding box, normalized to the frame. */
228
+ function cellsBox(cells, g) {
229
+ let r0 = Infinity, r1 = -1, c0 = Infinity, c1 = -1;
230
+ for (const i of cells) {
231
+ const r = Math.floor(i / g.cols), c = i % g.cols;
232
+ r0 = Math.min(r0, r);
233
+ r1 = Math.max(r1, r);
234
+ c0 = Math.min(c0, c);
235
+ c1 = Math.max(c1, c);
236
+ }
237
+ const q = (v) => Math.round(Math.min(1, Math.max(0, v)) * 10000) / 10000;
238
+ const x = q(c0 * g.cell / g.width), y = q(r0 * g.cell / g.height);
239
+ return { x, y, width: Math.min(q((c1 + 1 - c0) * g.cell / g.width), 1 - x), height: Math.min(q((r1 + 1 - r0) * g.cell / g.height), 1 - y) };
240
+ }
241
+ /** A stored memory for this area and grid, or a fresh one: a malformed memory costs relearning, never a refusal. */
242
+ function readFloorObjectMemory(value, key) {
243
+ const v = value;
244
+ const fresh = { version: 1, key: key ?? '', lastAtMs: -1, quietAtMs: -1, ref: '', seen: '',
245
+ objects: [], marks: [], settleUntilMs: -1 };
246
+ const valid = !!v && v.version === 1 && typeof v.key === 'string' && (key === undefined || v.key === key)
247
+ && time(v.lastAtMs) && time(v.quietAtMs) && typeof v.ref === 'string' && typeof v.seen === 'string'
248
+ && time(v.settleUntilMs) && Array.isArray(v.marks) && v.marks.length <= 256 && v.marks.every(id => typeof id === 'string')
249
+ && Array.isArray(v.objects) && v.objects.length <= 64 && v.objects.every(o => !!o && typeof o.id === 'string'
250
+ && Array.isArray(o.cells) && o.cells.length > 0 && o.cells.length <= 4096 && o.cells.every(Number.isSafeInteger)
251
+ && time(o.firstMs) && time(o.stillMs) && time(o.seenMs) && (o.decidedMs === null || time(o.decidedMs))
252
+ && typeof o.snap === 'string' && typeof o.brought === 'boolean')
253
+ && (v.near === undefined || typeof v.near === 'string') && (v.onset === undefined || typeof v.onset === 'string')
254
+ && (v.gain === undefined || Number.isFinite(v.gain) && v.gain > 0)
255
+ && (v.own === undefined || Array.isArray(v.own) && v.own.length <= 4096 && v.own.every(e => Array.isArray(e)
256
+ && e.length === 3 && Number.isSafeInteger(e[0]) && time(e[1]) && typeof e[2] === 'string'))
257
+ && (v.people === undefined || !!v.people && time(v.people.atMs) && typeof v.people.means === 'string'
258
+ && Array.isArray(v.people.boxes) && v.people.boxes.length <= 64 && v.people.boxes.every(b => Array.isArray(b)
259
+ && b.length === 6 && typeof b[0] === 'string' && b.slice(1).every(n => Number.isFinite(n))));
260
+ return valid ? v : fresh;
261
+ }
262
+ /**
263
+ * Whether this look can decide with the person boxes of the last detector look instead of a detector call: nothing in
264
+ * the area and its ring moved more than people.reuseDiff (cell mean, grey levels) since that look, and it is at most
265
+ * people.refreshMs old. A person who walked in moved the area, so a box that matters is never missed; a person who
266
+ * stands still keeps the box they had.
267
+ */
268
+ function floorPeopleHold(input) {
269
+ const m = input.method, f = input.frame, t = input.capturedAtMs;
270
+ if (!time(t) || !(f.width > m.grid.cell && f.height === m.grid.height) || f.data.length !== f.width * f.height)
271
+ return false;
272
+ // An area this look's grid has no room in (fewer than object.minCells cell centres inside it) is not decided at all:
273
+ // decideFloorObjects says so inside its scope's own answer, which leaves the look's other questions answered. This
274
+ // runs before that answer is guarded, so it asks for a detector look rather than failing the whole look.
275
+ let g;
276
+ try {
277
+ g = floorGrid(input.region, f.width, f.height, m);
278
+ }
279
+ catch {
280
+ return false;
281
+ }
282
+ const prior = readFloorObjectMemory(input.memory, g.key);
283
+ const p = prior.people;
284
+ if (!p || t <= p.atMs || t - p.atMs > m.people.refreshMs || t <= prior.lastAtMs)
285
+ return false;
286
+ const then = fromB64(p.means);
287
+ if (then.length !== g.area.length)
288
+ return false;
289
+ const { mean } = cellPixels(f, g);
290
+ for (let k = 0; k < mean.length; k++)
291
+ if (Math.abs(mean[k] - then[k]) > m.people.reuseDiff)
292
+ return false;
293
+ return true;
294
+ }
295
+ /**
296
+ * When this memory next needs a look to stay on time: the first moment an undecided object would reach its dwell,
297
+ * or null when nothing is pending. For a look scheduler that answers unchanged frames without a fresh review (W8):
298
+ * the area did not change, so the last person boxes still hold and the method can decide on the look it is due,
299
+ * without a detector call of its own.
300
+ */
301
+ function floorObjectDueAtMs(memory, method) {
302
+ const m = readFloorObjectMemory(memory);
303
+ const due = m.objects.filter(o => o.decidedMs === null && o.brought)
304
+ .map(o => Math.max(o.stillMs + method.object.dwellMs, m.settleUntilMs));
305
+ return due.length ? Math.min(...due) : null;
306
+ }
307
+ /**
308
+ * floor_object_left_v1 on one look. `frame` is the look's grey decode at grid.height (decodeFloorGrey), `size` the
309
+ * original frame the detections are pixels of. A look not captured after the memory's last one decides from the
310
+ * memory and teaches nothing.
311
+ */
312
+ function decideFloorObjects(input) {
313
+ const m = input.method, t = input.capturedAtMs, f = input.frame;
314
+ if (!time(t) || t < 0)
315
+ throw Error('Floor objects need the capture time');
316
+ if (!(f.width > m.grid.cell && f.height === m.grid.height) || f.data.length !== f.width * f.height) {
317
+ throw Error('Floor objects need the look at the grid height');
318
+ }
319
+ if (!(input.size.width > 1 && input.size.height > 1))
320
+ throw Error('Floor objects need the original frame size');
321
+ const g = floorGrid(input.region, f.width, f.height, m);
322
+ const prior = readFloorObjectMemory(input.memory, g.key);
323
+ const n = g.area.length;
324
+ const { px, mean, std } = cellPixels(f, g);
325
+ const reused = input.detections === 'reuse';
326
+ if (reused && !prior.people)
327
+ throw Error('Floor objects have no person boxes to reuse');
328
+ const detections = input.detections === 'reuse'
329
+ ? prior.people.boxes.map(([label, confidence, x, y, width, height]) => ({ label, confidence, box: { x, y, width, height } }))
330
+ : input.detections;
331
+ // The person boxes this look decided with, and the area's cell means they belong to.
332
+ const people = reused ? prior.people : { atMs: t,
333
+ boxes: detections.filter(d => d.label === 'person' && d.confidence >= m.minPersonConfidence).slice(0, 64)
334
+ .map(d => [d.label, Math.round(d.confidence * 1000) / 1000, Math.round(d.box.x * 10) / 10, Math.round(d.box.y * 10) / 10,
335
+ Math.round(d.box.width * 10) / 10, Math.round(d.box.height * 10) / 10]),
336
+ means: B64(Uint8Array.from(mean, v => Math.round(v))) };
337
+ const peopleTrace = { people: reused ? 'reused' : 'fresh', peopleAtMs: people.atMs };
338
+ const person = personCells(detections, input.size, g, m, 0);
339
+ const near = personCells(detections, input.size, g, m, m.reference.quietRing);
340
+ const decidedBoxes = (objects) => objects.filter(o => o.decidedMs !== null)
341
+ .map(o => ({ ...cellsBox(o.cells, g), confidence: 1 }));
342
+ const trace = (memory, extra) => ({
343
+ method: 'floor_object_left_v1', imageSha256: input.imageSha256, policySha256: input.policySha256,
344
+ cells: g.insideCount, changed: 0, gain: 1, quiet: false, lighting: false, ghosts: 0, marked: 0, unbrought: 0,
345
+ returned: 0, ...peopleTrace,
346
+ objects: memory.objects.map(o => ({ box: cellsBox(o.cells, g), firstMs: o.firstMs, stillMs: o.stillMs,
347
+ decidedMs: o.decidedMs, brought: o.brought })), ...extra
348
+ });
349
+ const answer = (memory) => memory.objects.some(o => o.decidedMs !== null);
350
+ if (t <= prior.lastAtMs) {
351
+ return { value: answer(prior), memory: prior, trace: trace(prior, {}), causes: decidedBoxes(prior.objects) };
352
+ }
353
+ const quiet = g.area.every(i => !near[i]);
354
+ // Whole seconds since a bringer was near each cell, and how each differing cell began (SOMEBODY LEFT IT).
355
+ const O = m.object;
356
+ const bringers = personCells(detections.filter(d => d.confidence >= O.bringerConfidence), input.size, g, m, m.reference.quietRing);
357
+ const nearAge = new Uint16Array(n).fill(65535);
358
+ if (prior.near) {
359
+ const b = fromB64(prior.near);
360
+ if (b.byteLength === n * 2)
361
+ for (let k = 0; k < n; k++)
362
+ nearAge[k] = b[2 * k] | (b[2 * k + 1] << 8);
363
+ }
364
+ const elapsed = prior.lastAtMs >= 0 ? Math.round((t - prior.lastAtMs) / 1000) : 0;
365
+ for (let k = 0; k < n; k++)
366
+ nearAge[k] = bringers[g.area[k]] ? 0 : Math.min(65535, nearAge[k] + elapsed);
367
+ const nearB64 = () => {
368
+ const out = new Uint8Array(n * 2);
369
+ for (let k = 0; k < n; k++) {
370
+ out[2 * k] = nearAge[k] & 255;
371
+ out[2 * k + 1] = nearAge[k] >> 8;
372
+ }
373
+ return B64(out);
374
+ };
375
+ const onset = new Uint8Array(n);
376
+ if (prior.onset) {
377
+ const b = fromB64(prior.onset);
378
+ if (b.byteLength === n)
379
+ onset.set(b);
380
+ }
381
+ // Learned changes nobody brought, still able to go back (cell index -> [learned at, the floor it replaced]).
382
+ const own = new Map();
383
+ for (const [i, at, prev] of prior.own ?? []) {
384
+ const b = fromB64(prev);
385
+ if (g.slot[i] >= 0 && b.byteLength === 64 && t - at <= m.reference.ownMs)
386
+ own.set(i, [at, Uint8Array.from(b)]);
387
+ }
388
+ // The empty floor, x16. A memory without one starts it from the first quiet look.
389
+ let ref, seen;
390
+ if (prior.ref && prior.seen) {
391
+ const r = fromB64(prior.ref), s = fromB64(prior.seen);
392
+ if (r.byteLength !== n * 128 || s.byteLength !== n)
393
+ return restart();
394
+ ref = new Uint16Array(n * 64);
395
+ for (let i = 0; i < n * 64; i++)
396
+ ref[i] = r[2 * i] | (r[2 * i + 1] << 8);
397
+ seen = Uint8Array.from(s);
398
+ }
399
+ else
400
+ return restart();
401
+ function restart() {
402
+ const memory = { version: 1, key: g.key, lastAtMs: t, quietAtMs: -1, ref: '', seen: '',
403
+ objects: [], marks: [...prior.marks], settleUntilMs: -1, people, near: nearB64(), onset: B64(new Uint8Array(n)) };
404
+ if (quiet) {
405
+ const r = new Uint8Array(n * 128), s = new Uint8Array(n).fill(1);
406
+ for (let i = 0; i < n * 64; i++) {
407
+ const v = px[i] * 16;
408
+ r[2 * i] = v & 255;
409
+ r[2 * i + 1] = v >> 8;
410
+ }
411
+ memory.ref = B64(r);
412
+ memory.seen = B64(s);
413
+ memory.quietAtMs = t;
414
+ }
415
+ return { value: false, memory, trace: trace(memory, { quiet }), causes: [] };
416
+ }
417
+ const refMean = new Float64Array(n), refStd = new Float64Array(n);
418
+ for (let k = 0; k < n; k++) {
419
+ let s = 0, s2 = 0;
420
+ for (let j = 0; j < 64; j++) {
421
+ const v = ref[k * 64 + j] / 16;
422
+ s += v;
423
+ s2 += v * v;
424
+ }
425
+ refMean[k] = s / 64;
426
+ refStd[k] = Math.sqrt(Math.max(0, s2 / 64 - refMean[k] * refMean[k]));
427
+ }
428
+ // A decided object's cells show the object, not the floor: they set no gain and cast no vote on the lights. Counted,
429
+ // a pallet over half the aisle sets the gain from its own boards, and the floor around it all differs (Texas camera
430
+ // 10, Oct 30 13:47 CDT).
431
+ const blocked = new Uint8Array(g.cols * g.rows);
432
+ for (const o of prior.objects)
433
+ if (o.decidedMs !== null)
434
+ for (const i of o.cells)
435
+ blocked[i] = 1;
436
+ // The area's gain: the median brightness ratio of its free, taught cells.
437
+ const ratios = [];
438
+ for (let k = 0; k < n; k++) {
439
+ const i = g.area[k];
440
+ if (g.inside[i] && seen[k] && !near[i] && !blocked[i] && refMean[k] > 8)
441
+ ratios.push(mean[k] / refMean[k]);
442
+ }
443
+ ratios.sort((a, b) => a - b);
444
+ const gain = ratios.length >= 8 ? Math.min(2, Math.max(0.5, ratios[ratios.length >> 1])) : 1;
445
+ const C = m.change;
446
+ /** Whether area cell k of this look differs from a floor (its 64 pixels, grey levels, before the area's gain). */
447
+ const differs = (k, at, fm, fs) => {
448
+ const gm = gain * fm, gs = gain * fs;
449
+ // The pattern, where both cells have one: unchanged by any brightness change, changed by anything put there.
450
+ let zncc = NaN;
451
+ if (std[k] >= C.textureStd && gs >= C.textureStd) {
452
+ let cov = 0;
453
+ for (let j = 0; j < 64; j++)
454
+ cov += (px[k * 64 + j] - mean[k]) * (at(j) - fm);
455
+ zncc = cov / (64 * std[k] * fs);
456
+ }
457
+ return { zncc, d: Math.abs(mean[k] - gm) > Math.max(C.meanAbs, C.meanRel * gm)
458
+ || Math.abs(std[k] - gs) > Math.max(C.stdAbs, C.stdRel * Math.max(std[k], gs)) || zncc < C.minZncc };
459
+ };
460
+ const changed = new Uint8Array(g.cols * g.rows), back = [];
461
+ let free = 0, floor = 0, differ = 0, lit = 0, reshaped = 0;
462
+ for (let k = 0; k < n; k++) {
463
+ if (!seen[k])
464
+ continue;
465
+ const i = g.area[k];
466
+ let { d, zncc } = differs(k, j => ref[k * 64 + j] / 16, refMean[k], refStd[k]);
467
+ // A learned change nobody brought that went back: the floor it replaced is the floor again.
468
+ const was = d && !person[i] ? own.get(i) : undefined;
469
+ if (was) {
470
+ let sm = 0, s2 = 0;
471
+ for (let j = 0; j < 64; j++) {
472
+ sm += was[1][j];
473
+ s2 += was[1][j] * was[1][j];
474
+ }
475
+ const fm = sm / 64;
476
+ if (!differs(k, j => was[1][j], fm, Math.sqrt(Math.max(0, s2 / 64 - fm * fm))).d) {
477
+ d = false;
478
+ back.push(i);
479
+ }
480
+ }
481
+ if (d)
482
+ changed[i] = 1;
483
+ if (g.inside[i] && !blocked[i])
484
+ floor++;
485
+ if (g.inside[i] && !person[i] && !blocked[i]) {
486
+ free++;
487
+ if (d) {
488
+ differ++;
489
+ if (zncc >= m.lighting.keptZncc)
490
+ lit++;
491
+ else if (zncc < m.lighting.keptZncc)
492
+ reshaped++;
493
+ }
494
+ }
495
+ }
496
+ // Lighting: much of the free area differs at once and, where the floor has a pattern, it kept it (a bank of lights,
497
+ // the sun through a door). A pallet over the same share of the area replaces the pattern and stays an object.
498
+ // A floor with no pattern to judge by: lighting only when most of the free area changed together.
499
+ const L = m.lighting, share = free >= 8 && free >= L.minFreeShare * floor ? differ / free : 0;
500
+ const lighting = share > L.changedShare && (lit + reshaped >= 8 ? lit >= L.keptShare * (lit + reshaped) : share > L.flatShare);
501
+ // Still settling: the lights are settling and the area's brightness moved again since the last look.
502
+ const moving = !lighting && t <= prior.settleUntilMs && prior.gain !== undefined
503
+ && Math.abs(gain / prior.gain - 1) > L.settleMove;
504
+ // How each differing cell began: with a bringer near lately and the lights neither changing nor settling (2), or by
505
+ // itself (1). A cell keeps it until it has matched the floor for missGraceMs (an edge that flickers began once).
506
+ // Under a person at a lighting change too: not relearned, it differs once they leave, and the lights changed it.
507
+ for (let k = 0; k < n; k++) {
508
+ let state = onset[k] & 3, age = onset[k] >> 2;
509
+ if (changed[g.area[k]]) {
510
+ if (!state)
511
+ state = !lighting && t >= prior.settleUntilMs && nearAge[k] * 1000 <= O.broughtMs ? 2 : 1;
512
+ age = 0;
513
+ }
514
+ else {
515
+ age = Math.min(63, age + elapsed);
516
+ if (age * 1000 >= O.missGraceMs)
517
+ state = 0;
518
+ }
519
+ onset[k] = state | (age << 2);
520
+ }
521
+ const began = (k) => onset[k] & 3;
522
+ const settled = (cells) => { for (const i of cells)
523
+ onset[g.slot[i]] = 63 << 2; };
524
+ const broughtOf = (cells) => 2 * cells.filter(i => began(g.slot[i]) === 2).length >= cells.length;
525
+ const objects = [];
526
+ let ghosts = 0, marked = 0, unbrought = 0, relit = false;
527
+ const appliedMarks = [...prior.marks];
528
+ const snapOf = (cells) => {
529
+ const out = new Uint8Array(cells.length * 64);
530
+ cells.forEach((i, j) => out.set(px.subarray(g.slot[i] * 64, g.slot[i] * 64 + 64), j * 64));
531
+ return B64(out);
532
+ };
533
+ // Cells whose floor this look learned in bulk (a relearn, a quiet look): the light the floor is learned in.
534
+ const learned = new Uint8Array(g.cols * g.rows);
535
+ const absorb = (cells) => {
536
+ for (const i of cells) {
537
+ const k = g.slot[i];
538
+ for (let j = 0; j < 64; j++)
539
+ ref[k * 64 + j] = px[k * 64 + j] * 16;
540
+ seen[k] = Math.max(seen[k], 1);
541
+ }
542
+ };
543
+ // The whole free floor at once, at this look's brightness (the gain starts again from 1). The floors that learned
544
+ // changes replaced are of the old brightness: forgotten.
545
+ const relearn = (cells) => { absorb(cells); settled(cells); own.clear(); for (const i of cells)
546
+ learned[i] = 1; };
547
+ // Learned changes nobody brought that went back: the floor again, at once.
548
+ let returned = 0;
549
+ if (!lighting) {
550
+ absorb(back);
551
+ settled(back);
552
+ for (const i of back)
553
+ own.delete(i);
554
+ returned = back.length;
555
+ }
556
+ if (lighting) {
557
+ // Relearn every free cell; keep the decided objects (still blocking), forget the undecided ones.
558
+ for (const o of prior.objects)
559
+ if (o.decidedMs !== null)
560
+ objects.push({ ...o, cells: [...o.cells] });
561
+ const held = new Set(objects.flatMap(o => o.cells));
562
+ relearn(g.area.filter(i => !person[i] && !held.has(i)));
563
+ }
564
+ else {
565
+ // A decided object stays what was decided: it goes on in its own cells that still differ (and those a person
566
+ // hides), and takes in only the changes next to it that a person brought (it was moved, or something was set down
567
+ // beside it). A change that began by itself around it (the lights, a shadow) is the floor's own and is judged on its
568
+ // own; a pallet that took the lights in grew over the whole aisle and held the alert into the next day (Texas
569
+ // camera 10, Oct 30).
570
+ const claimed = new Uint8Array(g.cols * g.rows), stays = new Map();
571
+ prior.objects.forEach((o, oi) => {
572
+ if (o.decidedMs === null)
573
+ return;
574
+ const cells = o.cells.filter(i => changed[i] && !person[i] && !claimed[i]);
575
+ for (const i of cells)
576
+ claimed[i] = 1;
577
+ for (let q = 0; q < cells.length; q++) {
578
+ const r = Math.floor(cells[q] / g.cols), c = cells[q] % g.cols;
579
+ for (let dr = -1; dr <= 1; dr++)
580
+ for (let dc = -1; dc <= 1; dc++) {
581
+ const rr = r + dr, cc = c + dc, j = rr * g.cols + cc;
582
+ if (rr < 0 || rr >= g.rows || cc < 0 || cc >= g.cols || g.slot[j] < 0)
583
+ continue;
584
+ if (changed[j] && !person[j] && !claimed[j] && began(g.slot[j]) === 2) {
585
+ claimed[j] = 1;
586
+ cells.push(j);
587
+ }
588
+ }
589
+ }
590
+ const inside = cells.filter(i => g.inside[i]).length;
591
+ if (inside < O.minCells || inside < O.minInside * cells.length) {
592
+ for (const i of cells)
593
+ claimed[i] = 0;
594
+ return;
595
+ }
596
+ stays.set(oi, [...cells, ...o.cells.filter(i => person[i])].sort((a, b) => a - b));
597
+ });
598
+ // This look's candidate objects: changed cells no person covers and no decided object holds, 8-connected.
599
+ const comp = new Int32Array(g.cols * g.rows).fill(-1), parts = [];
600
+ for (const start of g.area) {
601
+ if (!changed[start] || person[start] || claimed[start] || comp[start] >= 0)
602
+ continue;
603
+ const cells = [start];
604
+ comp[start] = parts.length;
605
+ for (let q = 0; q < cells.length; q++) {
606
+ const r = Math.floor(cells[q] / g.cols), c = cells[q] % g.cols;
607
+ for (let dr = -1; dr <= 1; dr++)
608
+ for (let dc = -1; dc <= 1; dc++) {
609
+ const rr = r + dr, cc = c + dc, j = rr * g.cols + cc;
610
+ if (rr < 0 || rr >= g.rows || cc < 0 || cc >= g.cols || g.slot[j] < 0)
611
+ continue;
612
+ if (changed[j] && !person[j] && !claimed[j] && comp[j] < 0) {
613
+ comp[j] = parts.length;
614
+ cells.push(j);
615
+ }
616
+ }
617
+ }
618
+ parts.push(cells.sort((a, b) => a - b));
619
+ }
620
+ const found = parts.filter(cells => {
621
+ const inside = cells.filter(i => g.inside[i]).length;
622
+ return inside >= O.minCells && inside / cells.length >= O.minInside;
623
+ });
624
+ // One-to-one: each earlier undecided object continues the candidate it overlaps most (half the smaller of the two).
625
+ const pairs = [];
626
+ prior.objects.forEach((o, oi) => {
627
+ if (o.decidedMs !== null)
628
+ return;
629
+ const mine = new Set(o.cells);
630
+ found.forEach((cells, pi) => {
631
+ const common = cells.filter(i => mine.has(i)).length;
632
+ if (common >= 0.5 * Math.min(cells.length, o.cells.length))
633
+ pairs.push({ o: oi, p: pi, v: common });
634
+ });
635
+ });
636
+ pairs.sort((a, b) => b.v - a.v || a.o - b.o || a.p - b.p);
637
+ const usedO = new Set(stays.keys()), usedP = new Set();
638
+ for (const [oi, cells] of stays)
639
+ objects.push({ ...prior.objects[oi], cells, seenMs: t });
640
+ const touched = (cells) => cells.some(i => {
641
+ const r = Math.floor(i / g.cols), c = i % g.cols;
642
+ for (let dr = -1; dr <= 1; dr++)
643
+ for (let dc = -1; dc <= 1; dc++) {
644
+ const rr = r + dr, cc = c + dc;
645
+ if (rr >= 0 && rr < g.rows && cc >= 0 && cc < g.cols && person[rr * g.cols + cc])
646
+ return true;
647
+ }
648
+ return false;
649
+ });
650
+ for (const { o: oi, p: pi } of pairs) {
651
+ if (usedO.has(oi) || usedP.has(pi))
652
+ continue;
653
+ usedO.add(oi);
654
+ usedP.add(pi);
655
+ const o = prior.objects[oi], cells = found[pi];
656
+ // Still: the same cells (most of them) showing the same pixels as when it last settled.
657
+ const snap = fromB64(o.snap), at = new Map(o.cells.map((i, j) => [i, j]));
658
+ let diff = 0, count = 0;
659
+ for (const i of cells) {
660
+ const j = at.get(i);
661
+ if (j === undefined)
662
+ continue;
663
+ const k = g.slot[i];
664
+ for (let x = 0; x < 64; x++)
665
+ diff += Math.abs(px[k * 64 + x] - snap[j * 64 + x]);
666
+ count += 64;
667
+ }
668
+ const union = new Set([...cells, ...o.cells]).size;
669
+ const moved = count === 0 || diff / count > O.stableDiff || (count / 64) / union < 0.5 || touched(cells);
670
+ objects.push({ id: o.id, cells, firstMs: o.firstMs, stillMs: moved ? t : o.stillMs, seenMs: t,
671
+ decidedMs: o.decidedMs, snap: moved ? snapOf(cells) : o.snap, brought: broughtOf(cells) });
672
+ }
673
+ prior.objects.forEach((o, oi) => {
674
+ if (usedO.has(oi))
675
+ return;
676
+ // Hidden behind a person: still there, but touched. Missed on one look: kept, untouched, for missGraceMs. An object
677
+ // seen standing a dwell past its decision is kept for a dwell, so its alert does not end and come back when a small
678
+ // object's cells pass for the floor on a few looks (Texas camera 10, Oct 26 15:02 CDT: a wet-floor sign at the far
679
+ // end, 16 s, nobody near). One gone within a dwell of its decision keeps missGraceMs: on that camera over 13 days,
680
+ // five such were an employee standing at the far end and passing changes with nothing on the floor, and a dwell's
681
+ // grace would have alerted on each.
682
+ const hidden = o.cells.filter(i => person[i]).length >= 0.5 * o.cells.length;
683
+ const grace = o.decidedMs !== null && o.seenMs - o.decidedMs >= O.dwellMs ? O.dwellMs : O.missGraceMs;
684
+ if (hidden)
685
+ objects.push({ ...o, cells: [...o.cells], stillMs: t, seenMs: t });
686
+ else if (t - o.seenMs <= grace)
687
+ objects.push({ ...o, cells: [...o.cells] });
688
+ });
689
+ found.forEach((cells, pi) => {
690
+ if (usedP.has(pi) || objects.length >= O.maxObjects)
691
+ return;
692
+ objects.push({ id: `${t}-${pi}`, cells, firstMs: t, stillMs: t, seenMs: t, decidedMs: null, snap: snapOf(cells),
693
+ brought: broughtOf(cells) });
694
+ });
695
+ // A decided object that lost its cells on this look, and a change someone brought touching where it stood: the same
696
+ // object, found again (a passer-by nudged it, or its edge crossed into the next cell). It keeps its decision, so its
697
+ // alert goes on instead of ending and coming back a dwell later (Texas camera 10, Oct 26: a wet-floor sign at the
698
+ // far end of the aisle, brushed by shoppers walking past it, alerted five times in 75 minutes).
699
+ prior.objects.forEach((o, oi) => {
700
+ if (o.decidedMs === null || stays.has(oi))
701
+ return;
702
+ const lost = objects.findIndex(x => x.id === o.id);
703
+ if (lost >= 0 && objects[lost].seenMs === t)
704
+ return;
705
+ const around = new Set();
706
+ for (const i of o.cells) {
707
+ const r = Math.floor(i / g.cols), c = i % g.cols;
708
+ for (let dr = -1; dr <= 1; dr++)
709
+ for (let dc = -1; dc <= 1; dc++) {
710
+ const rr = r + dr, cc = c + dc;
711
+ if (rr >= 0 && rr < g.rows && cc >= 0 && cc < g.cols)
712
+ around.add(rr * g.cols + cc);
713
+ }
714
+ }
715
+ let best = -1, most = 0;
716
+ objects.forEach((u, ui) => {
717
+ if (u.decidedMs !== null || u.seenMs !== t || !u.brought)
718
+ return;
719
+ const touching = u.cells.filter(i => around.has(i)).length;
720
+ if (touching > most) {
721
+ best = ui;
722
+ most = touching;
723
+ }
724
+ });
725
+ if (best < 0)
726
+ return;
727
+ objects[best] = { ...objects[best], id: o.id, firstMs: o.firstMs, decidedMs: o.decidedMs };
728
+ if (lost >= 0)
729
+ objects.splice(lost, 1);
730
+ });
731
+ // Held still for afterMs: a change nobody brought is the floor's own (learned); a ghost is where the floor now
732
+ // looks like the floor around it and holds less structure than the reference did (learned). A decided object can
733
+ // become a ghost too: carried off a floor the reference never saw, because something else stood there when it was
734
+ // learned (Texas camera 10, Oct 30-31: pallets, then bins set down where a cart had stood; the bins left by 13:00
735
+ // the next day and the alert held two more days).
736
+ const G = m.ghost;
737
+ for (let k = objects.length - 1; k >= 0; k--) {
738
+ const o = objects[k];
739
+ if (o.seenMs !== t || t - o.stillMs < G.afterMs)
740
+ continue;
741
+ if (o.decidedMs !== null) {
742
+ if (o.cells.some(i => person[i]) || !isGhost(o.cells, px, ref, gain, g, person, G))
743
+ continue;
744
+ // Cell by cell: what is itself a ghost is the floor; what is left goes on if it is still an object (a basket
745
+ // set down among the cells a step ladder had stood in, Texas camera 8, Oct 28). Not an object any more: all
746
+ // of it is the floor.
747
+ const gone = new Set(ghostCells(o.cells, px, ref, gain, g, person, G));
748
+ const left = o.cells.filter(i => !gone.has(i)), inside = left.filter(i => g.inside[i]).length;
749
+ if (inside >= O.minCells && inside >= O.minInside * left.length) {
750
+ if (!gone.size)
751
+ continue;
752
+ absorb([...gone]);
753
+ objects[k] = { ...o, cells: left };
754
+ }
755
+ else {
756
+ absorb(o.cells);
757
+ objects.splice(k, 1);
758
+ }
759
+ ghosts++;
760
+ continue;
761
+ }
762
+ if (!o.brought && o.cells.length >= L.changedShare * free) {
763
+ // Over as much of the floor as a lighting change, nobody near: the lights (or the camera), held still. Learned
764
+ // alone it would move the gain, and the floor left out would differ at once (Texas camera 16, Oct 25 09:05
765
+ // CDT: the opening lights changed the floor's pattern too much to pass for lighting, and once learned, the
766
+ // rest of the aisle alerted). The whole free floor is relearned, as for lighting.
767
+ relit = true;
768
+ unbrought++;
769
+ }
770
+ else if (!o.brought) {
771
+ // The floor it replaces stays able to come back (the oldest, where a cell learned twice).
772
+ for (const i of o.cells) {
773
+ const k2 = g.slot[i];
774
+ if (!own.has(i))
775
+ own.set(i, [t, Uint8Array.from({ length: 64 }, (_, j) => Math.min(255, Math.round(ref[k2 * 64 + j] / 16)))]);
776
+ }
777
+ absorb(o.cells);
778
+ settled(o.cells);
779
+ objects.splice(k, 1);
780
+ unbrought++;
781
+ }
782
+ else if (isGhost(o.cells, px, ref, gain, g, person, G)) {
783
+ absorb(o.cells);
784
+ objects.splice(k, 1);
785
+ ghosts++;
786
+ }
787
+ }
788
+ if (relit) {
789
+ // Keep the decided objects (still blocking); forget the undecided ones, their cells relearned with the floor.
790
+ const kept = objects.filter(o => o.decidedMs !== null);
791
+ objects.splice(0, objects.length, ...kept);
792
+ const held = new Set(kept.flatMap(o => o.cells));
793
+ relearn(g.area.filter(i => !person[i] && !held.has(i)));
794
+ }
795
+ // The owner's marks (family floor, a false alarm): each absorbs the object it names once, when it arrives.
796
+ for (const mark of input.marks ?? []) {
797
+ if (mark.family !== 'floor' || mark.verdict !== 'ignore' || appliedMarks.includes(mark.id))
798
+ continue;
799
+ for (let k = objects.length - 1; k >= 0; k--) {
800
+ const box = cellsBox(objects[k].cells, g);
801
+ if (mark.boxes.some(b => (0, SceneMarks_1.sceneIou)(box, b) >= 0.5)) {
802
+ absorb(objects[k].cells);
803
+ objects.splice(k, 1);
804
+ marked++;
805
+ }
806
+ }
807
+ appliedMarks.push(mark.id);
808
+ }
809
+ for (const o of objects) {
810
+ if (o.brought && o.decidedMs === null && o.seenMs === t && t - o.stillMs >= O.dwellMs && t >= prior.settleUntilMs) {
811
+ o.decidedMs = t;
812
+ }
813
+ }
814
+ }
815
+ // Learn the empty floor on quiet looks, in cells that match it and hold no object.
816
+ let quietAtMs = prior.quietAtMs;
817
+ if (quiet && !lighting && !relit) {
818
+ const held = new Set(objects.flatMap(o => o.cells));
819
+ const step = prior.quietAtMs >= 0 ? Math.min(m.reference.maxStepMs, t - prior.quietAtMs) : m.reference.maxStepMs;
820
+ const alpha = 1 - Math.exp(-step / m.reference.tauMs);
821
+ for (let k = 0; k < n; k++) {
822
+ const i = g.area[k];
823
+ if (held.has(i) || changed[i] && seen[k])
824
+ continue;
825
+ if (!seen[k]) {
826
+ absorb([i]);
827
+ continue;
828
+ }
829
+ for (let j = 0; j < 64; j++)
830
+ ref[k * 64 + j] = Math.round(ref[k * 64 + j] + alpha * (px[k * 64 + j] * 16 - ref[k * 64 + j]));
831
+ if (seen[k] < 255)
832
+ seen[k]++;
833
+ learned[i] = 1;
834
+ }
835
+ quietAtMs = t;
836
+ }
837
+ else if (lighting || relit)
838
+ quietAtMs = t;
839
+ // The floor under a decided object cannot be seen, so it follows the floor around it: scaled as the floor learned
840
+ // this look moved (a bank of lights, a slow drift), the floor within two cells of it first, else the whole area's.
841
+ // Left in the light it was learned in, the floor an object is carried off from differs from it, and the object
842
+ // outlives itself.
843
+ const moved = new Float64Array(g.cols * g.rows).fill(NaN), all = [];
844
+ for (const i of g.area) {
845
+ const k = g.slot[i];
846
+ if (!learned[i] || !g.inside[i] || refMean[k] <= 8)
847
+ continue;
848
+ let sum = 0;
849
+ for (let j = 0; j < 64; j++)
850
+ sum += ref[k * 64 + j];
851
+ all.push(moved[i] = sum / 1024 / refMean[k]);
852
+ }
853
+ const median = (v) => v.sort((a, b) => a - b)[v.length >> 1];
854
+ const whole = all.length >= 8 ? median(all) : 1;
855
+ for (const o of objects) {
856
+ if (o.decidedMs === null)
857
+ continue;
858
+ const mine = new Set(o.cells), near = [], seenNear = new Set();
859
+ for (const i of o.cells) {
860
+ const r = Math.floor(i / g.cols), c = i % g.cols;
861
+ for (let dr = -2; dr <= 2; dr++)
862
+ for (let dc = -2; dc <= 2; dc++) {
863
+ const rr = r + dr, cc = c + dc, j = rr * g.cols + cc;
864
+ if (rr < 0 || rr >= g.rows || cc < 0 || cc >= g.cols || mine.has(j) || seenNear.has(j))
865
+ continue;
866
+ seenNear.add(j);
867
+ if (!Number.isNaN(moved[j]))
868
+ near.push(moved[j]);
869
+ }
870
+ }
871
+ const f = near.length >= 4 ? median(near) : whole;
872
+ if (f !== 1)
873
+ for (const i of o.cells) {
874
+ const k = g.slot[i];
875
+ for (let j = 0; j < 64; j++)
876
+ ref[k * 64 + j] = Math.min(4080, Math.round(ref[k * 64 + j] * f));
877
+ }
878
+ }
879
+ const r = new Uint8Array(n * 128);
880
+ for (let i = 0; i < n * 64; i++) {
881
+ r[2 * i] = ref[i] & 255;
882
+ r[2 * i + 1] = ref[i] >> 8;
883
+ }
884
+ const memory = { version: 1, key: g.key, lastAtMs: t, quietAtMs, ref: B64(r), seen: B64(seen),
885
+ objects, marks: appliedMarks.slice(-256),
886
+ settleUntilMs: lighting || relit || moving ? Math.max(prior.settleUntilMs, t + m.lighting.settleMs) : prior.settleUntilMs,
887
+ gain: lighting || relit ? 1 : gain, people, near: nearB64(), onset: B64(onset),
888
+ own: [...own].slice(-4096).map(([i, [at, prev]]) => [i, at, B64(prev)]) };
889
+ return { value: answer(memory), memory, causes: decidedBoxes(objects),
890
+ trace: trace(memory, { changed: differ, gain: Math.round(gain * 1000) / 1000, quiet, lighting: lighting || relit, ghosts, marked,
891
+ unbrought, returned }) };
892
+ }
893
+ /**
894
+ * A ghost: the reference holds more structure in these cells than the look does (the edges were the object's), and
895
+ * the look there matches the free floor around the cells. A new object does the opposite: it brings edges, or it
896
+ * stands out from the floor around it. `ghostCells` answers it per cell, against the same floor around.
897
+ */
898
+ function isGhost(cells, px, ref, gain, g, person, G) {
899
+ const around = floorAround(cells, px, g, person);
900
+ if (around === null)
901
+ return false;
902
+ let eNow = 0, eRef = 0, sNow = 0;
903
+ for (const i of cells) {
904
+ const c = cellStructure(g.slot[i], px, ref, gain);
905
+ eNow += c.eNow;
906
+ eRef += c.eRef;
907
+ sNow += c.sum;
908
+ }
909
+ if (!(eRef > G.edgeRatio * eNow))
910
+ return false;
911
+ const inner = sNow / (64 * cells.length);
912
+ return Math.abs(inner - around) <= Math.max(G.floorMeanAbs, G.floorMeanRel * around);
913
+ }
914
+ /** The cells of these that are each a ghost, against the free floor around all of them. */
915
+ function ghostCells(cells, px, ref, gain, g, person, G) {
916
+ const around = floorAround(cells, px, g, person);
917
+ if (around === null)
918
+ return [];
919
+ return cells.filter(i => {
920
+ const c = cellStructure(g.slot[i], px, ref, gain);
921
+ return c.eRef > G.edgeRatio * c.eNow && Math.abs(c.sum / 64 - around) <= Math.max(G.floorMeanAbs, G.floorMeanRel * around);
922
+ });
923
+ }
924
+ /** One area cell's edge energy now and in the reference (times the gain), and its pixel sum now. */
925
+ function cellStructure(k, px, ref, gain) {
926
+ let eNow = 0, eRef = 0, sum = 0;
927
+ for (let y = 0; y < 8; y++)
928
+ for (let x = 0; x < 8; x++) {
929
+ const j = k * 64 + y * 8 + x;
930
+ if (x < 7) {
931
+ eNow += Math.abs(px[j + 1] - px[j]);
932
+ eRef += Math.abs(ref[j + 1] - ref[j]) / 16 * gain;
933
+ }
934
+ if (y < 7) {
935
+ eNow += Math.abs(px[j + 8] - px[j]);
936
+ eRef += Math.abs(ref[j + 8] - ref[j]) / 16 * gain;
937
+ }
938
+ sum += px[j];
939
+ }
940
+ return { eNow, eRef, sum };
941
+ }
942
+ /** The mean grey of the free area cells touching these cells, outside them; null when none is free. */
943
+ function floorAround(cells, px, g, person) {
944
+ const mine = new Set(cells);
945
+ let sum = 0, count = 0;
946
+ for (const i of cells) {
947
+ const r = Math.floor(i / g.cols), c = i % g.cols;
948
+ for (let dr = -1; dr <= 1; dr++)
949
+ for (let dc = -1; dc <= 1; dc++) {
950
+ const rr = r + dr, cc = c + dc, j = rr * g.cols + cc;
951
+ if (rr < 0 || rr >= g.rows || cc < 0 || cc >= g.cols || mine.has(j) || person[j] || g.slot[j] < 0)
952
+ continue;
953
+ const k = g.slot[j];
954
+ for (let x = 0; x < 64; x++)
955
+ sum += px[k * 64 + x];
956
+ count += 64;
957
+ }
958
+ }
959
+ return count ? sum / count : null;
960
+ }
961
+ //# sourceMappingURL=FloorObjects.js.map