ifcviewer-web: mint session model ids when a load is requested

A federated pick could be attributed to the wrong file. The model slot a
host sees — ElementRef::model_index, modelProgress's index — is a rank in
session_model_id order, and on web that id was minted at the END of the
sidecar read chain, after three network round trips. So the ranking was
the order the models' reads happened to finish in, not the order the host
added them. With ~40 similarly-sized models over HTTP, adjacent models
swapped and a click reported its neighbour's file; the host page then
asked for a GUID the file does not contain.

Mint the id at the top of loadSidecarMetadataWeb instead, which runs
synchronously from load_sidecar_from_source_c and therefore in the order
the host asked for its models. A load that fails partway just abandons
its id, and the ranks compact over the surviving models as before.

Positions are still positions, though: if one model fails to load, every
later index shifts down one and a host mapping index into its own list
silently drifts again. So also carry the source id — the handle the host
minted itself when it registered the file — through ElementRef into the
pick payload and getObjects rows, and document it as the way to attribute
an object to a file. ModelGpuData::web_source_id defaults to -1 now, since
0 is a real source id and cannot double as "none".

The test server grows a ?delay=<ms> knob so a test can force the losing
interleaving: georef-a is added first and served slowly, and its objects
must still come back as model 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-08-21 11:30:56 +10:00
parent d86f89090b
commit 2c1d445d5b
7 changed files with 162 additions and 30 deletions
+2 -1
View File
@@ -359,7 +359,7 @@ EM_BOOL onMouseUp(int, const EmscriptenMouseEvent* e, void* user) {
if (id != 0) {
app->core.logSelectedObjectGuidWeb(id);
} else if (!add && !remove) {
EM_ASM({ if (Module.__ifcvOnSelect) Module.__ifcvOnSelect(0, '', -1); });
EM_ASM({ if (Module.__ifcvOnSelect) Module.__ifcvOnSelect(0, '', -1, -1); });
}
app->host.requestFrame();
});
@@ -835,6 +835,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void ifcv_request_objects_c(int token) {
first = false;
json += "{\"objectId\":" + std::to_string(e.object_id)
+ ",\"model\":" + std::to_string(e.model_index)
+ ",\"sourceId\":" + std::to_string(e.source_id)
+ ",\"guid\":" + jsonString(e.guid)
+ ",\"name\":" + jsonString(e.name)
+ ",\"type\":" + jsonString(e.type) + '}';
@@ -0,0 +1,84 @@
import { test, expect } from '@playwright/test';
// Which file did this object come from? Every host page answers that by taking
// the `model` index the viewer reports and looking it up in its own list of
// models, in the order it added them — the mapping the API documents. The
// index is only worth anything if it survives federated models finishing their
// loads out of order, which is exactly what happens over a real network.
//
// The two georef fixtures carry fixed GUIDs, so an object can be attributed to
// its file here without trusting the very index under test.
const GUIDS = {
'georef-a': ['13r0IXtWf5pf18Q1EGzHXl', '22CLYZYiz8ZhbpLaYDVIu6'],
'georef-b': ['3DkP2KRu5AIRxhhAz$DcQH', '2ueyz_jIr2QgMKs4v0fWl2'],
};
test('model index follows add order when the first model loads last', async ({ page }) => {
const errors = [];
page.on('pageerror', (e) => errors.push('pageerror: ' + e.message));
await page.goto('/scripting.html');
await page.waitForFunction(() => !!(window.viewer && window.viewer.isLive()), null,
{ timeout: 30_000 });
// georef-a is added first but served slowly, so every one of its range reads
// lands after georef-b's. Without a stable ordering the core hands out its
// load-order slots in completion order and the two models come back swapped.
const sourceIds = await page.evaluate(async () => {
const a = await window.viewer.addUrl('/georef-a.ifcview?delay=120', { replace: true });
const b = await window.viewer.addUrl('/georef-b.ifcview');
return [a, b];
});
expect(sourceIds[0]).toBeLessThan(sourceIds[1]);
await page.waitForFunction(() => window.viewer.modelCount() === 2, null, { timeout: 30_000 });
const objects = await page.evaluate(() => window.viewer.getObjects());
const rowFor = (guid) => objects.find((o) => o.guid === guid) || {};
for (const guid of GUIDS['georef-a']) {
expect(rowFor(guid).model, `${guid} belongs to georef-a, added first`).toBe(0);
expect(rowFor(guid).sourceId, `${guid} came from georef-a's source`).toBe(sourceIds[0]);
}
for (const guid of GUIDS['georef-b']) {
expect(rowFor(guid).model, `${guid} belongs to georef-b, added second`).toBe(1);
expect(rowFor(guid).sourceId, `${guid} came from georef-b's source`).toBe(sourceIds[1]);
}
expect(errors, errors.join('\n')).toEqual([]);
});
test('a pick reports the source the model was added from', async ({ page }) => {
const errors = [];
page.on('pageerror', (e) => errors.push('pageerror: ' + e.message));
await page.goto('/scripting.html');
await page.waitForFunction(() => !!(window.viewer && window.viewer.isLive()), null,
{ timeout: 30_000 });
await page.evaluate(async () => {
await window.viewer.addUrl('/georef-a.ifcview?delay=120', { replace: true });
await window.viewer.addUrl('/georef-b.ifcview');
});
await page.waitForFunction(() => window.viewer.modelCount() === 2, null, { timeout: 30_000 });
// The pick payload is built from the element table, so make sure it is
// resident and take the same table to check the answer against.
const objects = await page.evaluate(() => window.viewer.getObjects());
await page.evaluate(() => window.viewer.viewAll());
await page.waitForTimeout(800);
// Whichever box the click lands on is fine — what is under test is that the
// pick and the object table agree about which file the object came from.
await page.evaluate(() => {
window.__pick = new Promise((resolve) => window.viewer.onSelect(resolve));
});
const box = await page.locator('#viewer-canvas').boundingBox();
// Web preset: RMB selects (LMB orbits).
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2, { button: 'right' });
const detail = await page.evaluate(() => window.__pick);
expect(detail.guid, 'click hit empty space').toBeTruthy();
const row = objects.find((o) => o.guid === detail.guid);
expect(row, 'picked a GUID that is not in the object table').toBeTruthy();
expect(detail.sourceId, 'pick and object table disagree on the source').toBe(row.sourceId);
expect(detail.modelIndex).toBe(row.model);
expect(detail.sourceId).not.toBeNull();
expect(errors, errors.join('\n')).toEqual([]);
});
+6
View File
@@ -31,6 +31,12 @@ http.createServer(async (req, res) => {
try {
const url = new URL(req.url, `http://localhost:${PORT}`);
let p = decodeURIComponent(url.pathname);
// ?delay=<ms> stalls every response for this URL, HEAD and Range alike.
// Load order across federated models is decided by whichever model's
// async read chain finishes first, so a test that wants a specific
// interleaving has to be able to make one source slower than another.
const delay = Number(url.searchParams.get('delay') || 0);
if (delay > 0) await new Promise((r) => setTimeout(r, delay));
if (p === '/') p = '/IfcViewerWeb.html';
const inRoot = path.join(ROOT, p);
const inSrc = path.join(SRC, p);
+25 -12
View File
@@ -11,7 +11,7 @@
// await viewer.addFile(file, { replace: true });
// await viewer.addUrl('/model.ifcview'); // appends (federation)
//
// const objects = await viewer.getObjects(); // [{objectId, guid, name, type, model}]
// const objects = await viewer.getObjects(); // [{objectId, guid, name, type, model, sourceId}]
// viewer.setSelection(['3vB2YO$MX4xv5uCqZZG05x']);
// viewer.setColor(objects.filter(o => o.type === 'IfcWall'), '#ff8800');
// viewer.setCamera({ yaw: 45, pitch: 30 });
@@ -21,6 +21,14 @@
// The canvas element MUST have id="viewer-canvas" — the wasm side hard-codes
// that selector for its WebGPU surface and input handlers.
//
// Model identity. addFile/addUrl return a source id: the handle for that model,
// minted the moment it is registered and stable for the session. Objects come
// back tagged with both their `sourceId` and a `model` index (the model's slot
// in load order). Map an object to the file it came from through the source id
// — the index is a POSITION, so it shifts down if an earlier model fails to
// load, and a host keying its own list off it then attributes objects to the
// wrong file.
//
// Object identity. Everything the scripting API takes or returns is keyed by
// `objectId`: a u32 the renderer assigns, unique across the federation but only
// meaningful for this session. IFC GlobalIds are the stable identity, and every
@@ -197,15 +205,17 @@
// a remote URL (HTTP Range). load_sidecar_from_source_c(sid) streams one.
Module.__ifcvSources = Module.__ifcvSources || [];
// The wasm calls this on every single-object pick; (0, '', -1) means the
// The wasm calls this on every single-object pick; (0, '', -1, -1) means the
// selection was cleared. modelIndex is the picked object's model in load
// order (matches the modelProgress index), or -1. A marquee box-select does
// NOT fire this (it has no single object) — use onSelectionChange for that.
Module.__ifcvOnSelect = function (objectId, guid, modelIndex) {
// order (matches the modelProgress index) and sourceId the source it was
// added from, either null when unknown. A marquee box-select does NOT fire
// this (it has no single object) — use onSelectionChange for that.
Module.__ifcvOnSelect = function (objectId, guid, modelIndex, sourceId) {
const detail = {
objectId: objectId >>> 0,
guid: guid || null,
modelIndex: (typeof modelIndex === 'number' && modelIndex >= 0) ? modelIndex : null,
sourceId: (typeof sourceId === 'number' && sourceId >= 0) ? sourceId : null,
};
selectListeners.forEach(function (cb) {
try { cb(detail); } catch (e) { console.error(e); }
@@ -279,8 +289,8 @@
// ---- Events ----------------------------------------------------------
// Single-object picks (click). Fires with {objectId, guid, modelIndex}.
// Returns an unsubscribe function.
// Single-object picks (click). Fires with
// {objectId, guid, modelIndex, sourceId}. Returns an unsubscribe function.
onSelect: function (cb) {
selectListeners.push(cb);
return function () {
@@ -421,11 +431,14 @@
// ---- Objects ---------------------------------------------------------
// Every object in the scene: [{objectId, guid, name, type, model}], where
// `model` is the index into the load-ordered model list (same index as
// modelProgress). Asynchronous — the element tables are fetched lazily per
// model so first paint never waits on them. Resolving this is also what
// lets every other call accept GlobalIds; the result is cached for that.
// Every object in the scene:
// [{objectId, guid, name, type, model, sourceId}], where `model` is the
// index into the load-ordered model list (same index as modelProgress)
// and `sourceId` the source the model was added from — see the model
// identity note at the top of the file. Asynchronous — the element tables
// are fetched lazily per model so first paint never waits on them.
// Resolving this is also what lets every other call accept GlobalIds; the
// result is cached for that.
getObjects: function () {
const token = ++objectsToken;
return new Promise(function (resolve) {