-
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathbasketVisibleSet.ts
More file actions
566 lines (486 loc) · 18.5 KB
/
Copy pathbasketVisibleSet.ts
File metadata and controls
566 lines (486 loc) · 18.5 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import {
IfcTypeEnum,
isSpaceLikeSpatialTypeName,
isSpatialStructureTypeName,
isStoreyLikeSpatialTypeName,
type SpatialNode,
type SpatialHierarchy,
} from '@ifc-lite/data';
import type { IfcDataStore } from '@ifc-lite/parser';
import type { EntityRef } from './types.js';
import { entityRefToString, stringToEntityRef } from './types.js';
import { useViewerStore } from './index.js';
import { toGlobalIdFromModels } from './globalId.js';
import { collectAggregatedDescendants, type AggregationRelationships } from '../utils/aggregation.js';
type ViewerStateSnapshot = ReturnType<typeof useViewerStore.getState>;
type VisibleCandidate = {
globalId: number;
modelId: string;
expressId: number;
ifcType?: string;
};
type BasketVisibleStats = {
visibleCount: number;
addCount: number;
removeCount: number;
basketCount: number;
};
export type BasketInputSource = 'selection' | 'hierarchy' | 'visible' | 'empty';
type CacheEntry = { key: string; refs: EntityRef[] };
let _visibleCache: CacheEntry | null = null;
function digestNumberSet(values: Iterable<number>): string {
let count = 0;
let xor = 0;
let sum = 0;
for (const v of values) {
const n = Number.isFinite(v) ? (v | 0) : 0;
count++;
xor ^= n;
sum = (sum + (n >>> 0)) >>> 0;
}
return `${count}:${xor >>> 0}:${sum >>> 0}`;
}
function digestModelEntityMap(map: Map<string, Set<number>>): string {
if (map.size === 0) return '0';
const parts: string[] = [];
for (const [modelId, ids] of map) {
parts.push(`${modelId}:${digestNumberSet(ids)}`);
}
parts.sort();
return parts.join('|');
}
function visibilityFingerprint(state: ViewerStateSnapshot): string {
const tv = state.typeVisibility;
// Include per-model visible flag and geometry mesh count so the cache
// invalidates when model visibility is toggled or geometry finishes loading.
const modelParts: string[] = [];
for (const [modelId, model] of state.models) {
modelParts.push(`${modelId}:${model.visible ? 1 : 0}:${model.geometryResult?.meshes?.length ?? 0}`);
}
modelParts.sort();
return [
digestNumberSet(state.hiddenEntities),
state.isolatedEntities ? digestNumberSet(state.isolatedEntities) : 'none',
state.classFilter ? digestNumberSet(state.classFilter.ids) : 'none',
digestNumberSet(state.lensHiddenIds),
digestModelEntityMap(state.hiddenEntitiesByModel),
digestModelEntityMap(state.isolatedEntitiesByModel),
digestNumberSet(state.selectedStoreys),
tv.spaces ? 1 : 0,
tv.openings ? 1 : 0,
tv.virtualElements ? 1 : 0,
tv.site ? 1 : 0,
state.models.size,
modelParts.join(';'),
state.geometryResult?.meshes?.length ?? 0,
state.activeBasketViewId ?? 'none',
].join(':');
}
export function invalidateVisibleBasketCache(): void {
_visibleCache = null;
}
function dedupeRefs(refs: EntityRef[]): EntityRef[] {
const out: EntityRef[] = [];
const seen = new Set<string>();
for (const ref of refs) {
const key = entityRefToString(ref);
if (seen.has(key)) continue;
seen.add(key);
out.push(ref);
}
return out;
}
function matchesTypeVisibility(ifcType: string | undefined, typeVisibility: ViewerStateSnapshot['typeVisibility']): boolean {
if (ifcType === 'IfcSpace' && !typeVisibility.spaces) return false;
if (ifcType === 'IfcSpatialZone' && !typeVisibility.spatialZones) return false;
if (ifcType === 'IfcOpeningElement' && !typeVisibility.openings) return false;
if (ifcType === 'IfcVirtualElement' && !typeVisibility.virtualElements) return false;
if (ifcType === 'IfcSite' && !typeVisibility.site) return false;
// IfcAnnotation 3D mesh geometry (e.g. Bonsai plan-view boxes) tracks the
// same toggle that hides the 2D symbolic curve overlay (issue #1354).
if (ifcType === 'IfcAnnotation' && !typeVisibility.ifcAnnotations) return false;
return true;
}
function getDataStoreForModel(state: ViewerStateSnapshot, modelId: string): IfcDataStore | null {
if (modelId === 'legacy') {
return state.ifcDataStore;
}
return state.models.get(modelId)?.ifcDataStore ?? null;
}
function getEntityTypeName(state: ViewerStateSnapshot, ref: EntityRef): string {
const dataStore = getDataStoreForModel(state, ref.modelId);
if (!dataStore) return '';
return dataStore.entities.getTypeName(ref.expressId) || '';
}
function findSpatialNode(root: SpatialNode, expressId: number): SpatialNode | null {
const stack: SpatialNode[] = [root];
while (stack.length > 0) {
const current = stack.pop()!;
if (current.expressId === expressId) {
return current;
}
for (const child of current.children || []) {
stack.push(child);
}
}
return null;
}
function getContainerElementIds(dataStore: IfcDataStore, containerExpressId: number): number[] {
return collectSpatialSubtreeElementsWithIfcSpace(
dataStore.spatialHierarchy,
containerExpressId,
dataStore.relationships as AggregationRelationships | undefined
) ?? [];
}
function expandRefToElements(state: ViewerStateSnapshot, ref: EntityRef): EntityRef[] {
const dataStore = getDataStoreForModel(state, ref.modelId);
if (!dataStore) return [ref];
const entityType = dataStore.entities.getTypeName(ref.expressId) || '';
if (isSpatialStructureTypeName(entityType) && !isSpaceLikeSpatialTypeName(entityType)) {
const localIds = getContainerElementIds(dataStore, ref.expressId);
const ids = localIds.includes(ref.expressId)
? localIds
: [ref.expressId, ...localIds];
return ids.map((expressId) => ({ modelId: ref.modelId, expressId }));
}
return [ref];
}
function toGlobalId(modelId: string, expressId: number, state: ViewerStateSnapshot): number {
return toGlobalIdFromModels(state.models, modelId, expressId);
}
function globalIdToRef(state: ViewerStateSnapshot, globalId: number): EntityRef | null {
const resolved = state.resolveGlobalIdFromModels(globalId);
if (resolved) {
return { modelId: resolved.modelId, expressId: resolved.expressId };
}
if (state.models.size > 0) return null;
if (state.ifcDataStore) {
return { modelId: 'legacy', expressId: globalId };
}
return null;
}
function basketToGlobalIds(state: ViewerStateSnapshot): Set<number> {
const ids = new Set<number>();
for (const str of state.pinboardEntities) {
const ref = stringToEntityRef(str);
ids.add(toGlobalId(ref.modelId, ref.expressId, state));
}
return ids;
}
function getSelectedStoreyElementRefs(state: ViewerStateSnapshot): EntityRef[] {
if (state.selectedStoreys.size === 0) return [];
const refs: EntityRef[] = [];
if (state.models.size > 0) {
for (const [modelId, model] of state.models) {
const hierarchy = model.ifcDataStore?.spatialHierarchy;
if (!hierarchy) continue;
const offset = model.idOffset ?? 0;
for (const storeyId of state.selectedStoreys) {
const storeyElementIds = hierarchy.byStorey.get(storeyId) || hierarchy.byStorey.get(storeyId - offset);
if (!storeyElementIds) continue;
for (const localId of storeyElementIds) {
refs.push({ modelId, expressId: localId });
}
}
}
} else if (state.ifcDataStore?.spatialHierarchy) {
for (const storeyId of state.selectedStoreys) {
const storeyElementIds = state.ifcDataStore.spatialHierarchy.byStorey.get(storeyId);
if (!storeyElementIds) continue;
for (const id of storeyElementIds) {
refs.push({ modelId: 'legacy', expressId: id });
}
}
}
return dedupeRefs(refs);
}
function getSelectionBaseRefs(state: ViewerStateSnapshot): EntityRef[] {
const refs: EntityRef[] = [];
if (state.selectedEntitiesSet.size > 0) {
for (const str of state.selectedEntitiesSet) {
refs.push(stringToEntityRef(str));
}
return refs;
}
if (state.selectedEntityIds.size > 0) {
for (const globalId of state.selectedEntityIds) {
const resolved = globalIdToRef(state, globalId);
if (resolved) refs.push(resolved);
}
return refs;
}
if (state.selectedEntities.length > 0) {
return [...state.selectedEntities];
}
if (state.selectedEntity) {
return [state.selectedEntity];
}
if (state.selectedEntityId !== null) {
const resolved = globalIdToRef(state, state.selectedEntityId);
if (resolved) refs.push(resolved);
}
return refs;
}
function getExpandedSelectionRefs(state: ViewerStateSnapshot): EntityRef[] {
const baseRefs = getSelectionBaseRefs(state);
if (baseRefs.length === 0) return [];
return dedupeRefs(baseRefs.flatMap((ref) => expandRefToElements(state, ref)));
}
/**
* Collect all element IDs for an IfcBuildingStorey, including elements
* contained in descendant IfcSpace nodes and the space geometry itself.
*/
export function collectIfcBuildingStoreyElementsWithIfcSpace(
hierarchy: SpatialHierarchy,
storeyId: number,
relationships?: AggregationRelationships
): number[] | null {
if (!hierarchy.byStorey.has(storeyId)) return null;
return collectSpatialSubtreeElementsWithIfcSpace(hierarchy, storeyId, relationships);
}
export function collectSpatialSubtreeElementsWithIfcSpace(
hierarchy: SpatialHierarchy | undefined,
expressId: number,
relationships?: AggregationRelationships
): number[] | null {
if (!hierarchy?.project) return null;
const startNode = findSpatialNode(hierarchy.project, expressId);
if (!startNode) return null;
const combined: number[] = [];
const seen = new Set<number>();
const add = (id: number) => {
if (seen.has(id)) return;
seen.add(id);
combined.push(id);
};
const stack: SpatialNode[] = [startNode];
while (stack.length > 0) {
const current = stack.pop()!;
if (current.type === IfcTypeEnum.IfcSpace) {
add(current.expressId);
}
for (const elementId of current.elements || []) {
add(elementId);
// A decomposing assembly (IfcElementAssembly, IfcStair-as-container, …)
// keeps its parts off the spatial tree, so without this they have no
// storey assignment and storey isolation would drop the stair flights /
// railings / landing slabs / virtual clearance volumes (#1133).
if (relationships) {
for (const descId of collectAggregatedDescendants(relationships, elementId)) {
add(descId);
}
}
}
for (const child of current.children || []) {
stack.push(child);
}
}
return combined;
}
function computeStoreyIsolation(state: ViewerStateSnapshot): Set<number> | null {
if (state.selectedStoreys.size === 0) return null;
const ids = new Set<number>();
if (state.models.size > 0) {
for (const [, model] of state.models) {
const hierarchy = model.ifcDataStore?.spatialHierarchy;
if (!hierarchy) continue;
const relationships = model.ifcDataStore?.relationships as AggregationRelationships | undefined;
const offset = model.idOffset ?? 0;
for (const storeyId of state.selectedStoreys) {
const localStoreyId = hierarchy.byStorey.has(storeyId) ? storeyId : storeyId - offset;
const storeyElementIds = collectIfcBuildingStoreyElementsWithIfcSpace(hierarchy, localStoreyId, relationships);
if (!storeyElementIds) continue;
for (const localId of storeyElementIds) {
ids.add(toGlobalIdFromModels(state.models, model.id, localId));
}
}
}
} else if (state.ifcDataStore?.spatialHierarchy) {
const hierarchy = state.ifcDataStore.spatialHierarchy;
const relationships = state.ifcDataStore.relationships as AggregationRelationships | undefined;
for (const storeyId of state.selectedStoreys) {
const storeyElementIds = collectIfcBuildingStoreyElementsWithIfcSpace(hierarchy, storeyId, relationships);
if (!storeyElementIds) continue;
for (const id of storeyElementIds) {
ids.add(id);
}
}
}
return ids.size > 0 ? ids : null;
}
function collectVisibleCandidates(state: ViewerStateSnapshot): VisibleCandidate[] {
const candidates: VisibleCandidate[] = [];
if (state.models.size > 0) {
for (const [modelId, model] of state.models) {
if (!model.visible) continue;
// Native-metadata models have no parsed geometry result. Skip them
// — they can't contribute mesh-level visible candidates.
if (!model.geometryResult) continue;
const offset = model.idOffset ?? 0;
for (const mesh of model.geometryResult.meshes) {
if (!matchesTypeVisibility(mesh.ifcType, state.typeVisibility)) continue;
const globalId = mesh.expressId;
candidates.push({
globalId,
modelId,
expressId: globalId - offset,
ifcType: mesh.ifcType,
});
}
}
} else if (state.geometryResult) {
for (const mesh of state.geometryResult.meshes) {
if (!matchesTypeVisibility(mesh.ifcType, state.typeVisibility)) continue;
candidates.push({
globalId: mesh.expressId,
modelId: 'legacy',
expressId: mesh.expressId,
ifcType: mesh.ifcType,
});
}
}
return candidates;
}
function getVisibleGlobalIds(state: ViewerStateSnapshot): Set<number> {
const candidates = collectVisibleCandidates(state);
const globalHidden = new Set<number>(state.hiddenEntities);
for (const id of state.lensHiddenIds) {
globalHidden.add(id);
}
// Collect all active filter sets and intersect them
const filters: Set<number>[] = [];
const storeyIsolation = computeStoreyIsolation(state);
if (storeyIsolation !== null) filters.push(storeyIsolation);
if (state.classFilter !== null) filters.push(state.classFilter.ids);
if (state.isolatedEntities !== null) filters.push(state.isolatedEntities);
let globalIsolation: Set<number> | null = null;
if (filters.length === 1) {
globalIsolation = filters[0];
} else if (filters.length > 1) {
// Intersect all active filters — start from smallest for efficiency
const sorted = filters.sort((a, b) => a.size - b.size);
globalIsolation = new Set<number>();
for (const id of sorted[0]) {
if (sorted.every(s => s.has(id))) {
globalIsolation.add(id);
}
}
}
const visible = new Set<number>();
for (const candidate of candidates) {
if (globalIsolation !== null && !globalIsolation.has(candidate.globalId)) continue;
if (globalHidden.has(candidate.globalId)) continue;
const modelHidden = state.hiddenEntitiesByModel.get(candidate.modelId);
if (modelHidden?.has(candidate.expressId)) continue;
const modelIsolated = state.isolatedEntitiesByModel.get(candidate.modelId);
if (modelIsolated && !modelIsolated.has(candidate.expressId)) continue;
visible.add(candidate.globalId);
}
return visible;
}
export function getVisibleBasketEntityRefsFromStore(): EntityRef[] {
const state = useViewerStore.getState();
const key = visibilityFingerprint(state);
if (_visibleCache?.key === key) return _visibleCache.refs;
const visibleIds = getVisibleGlobalIds(state);
if (visibleIds.size === 0) {
_visibleCache = { key, refs: [] };
return [];
}
const refs: EntityRef[] = [];
for (const globalId of visibleIds) {
const resolved = state.resolveGlobalIdFromModels(globalId);
if (resolved) {
refs.push({ modelId: resolved.modelId, expressId: resolved.expressId });
} else {
refs.push({ modelId: 'legacy', expressId: globalId });
}
}
const result = dedupeRefs(refs);
_visibleCache = { key, refs: result };
return result;
}
/**
* Resolve active entity selection into basket refs.
* Explicit selected entities are preferred; if empty, selected storeys are expanded.
*/
export function getBasketSelectionRefsFromStore(): EntityRef[] {
const state = useViewerStore.getState();
const expandedSelection = getExpandedSelectionRefs(state);
if (expandedSelection.length > 0) {
return expandedSelection;
}
return getSelectedStoreyElementRefs(state);
}
/**
* Resolve hierarchy-derived basket source.
* Priority: explicit hierarchy source snapshot -> selected storeys -> selected hierarchy container/entity.
*/
export function getHierarchyBasketEntityRefsFromStore(): EntityRef[] {
const state = useViewerStore.getState();
if (state.hierarchyBasketSelection.size > 0) {
const hierarchyRefs = Array.from(state.hierarchyBasketSelection).map((key) => stringToEntityRef(key));
const expandedHierarchy = dedupeRefs(hierarchyRefs.flatMap((ref) => expandRefToElements(state, ref)));
if (expandedHierarchy.length > 0) {
return expandedHierarchy;
}
}
const storeyRefs = getSelectedStoreyElementRefs(state);
if (storeyRefs.length > 0) {
return storeyRefs;
}
const selectionRefs = getExpandedSelectionRefs(state);
if (selectionRefs.length > 0) {
const hasContainer = selectionRefs.some((ref) => {
const typeName = getEntityTypeName(state, ref);
return isStoreyLikeSpatialTypeName(typeName) || (isSpatialStructureTypeName(typeName) && !isSpaceLikeSpatialTypeName(typeName));
});
if (hasContainer || getSelectionBaseRefs(state).length > 0) {
return selectionRefs;
}
}
return [];
}
export function getSmartBasketInputFromStore(): { refs: EntityRef[]; source: BasketInputSource } {
const selectionRefs = getBasketSelectionRefsFromStore();
if (selectionRefs.length > 0) {
return { refs: selectionRefs, source: 'selection' };
}
const hierarchyRefs = getHierarchyBasketEntityRefsFromStore();
if (hierarchyRefs.length > 0) {
return { refs: hierarchyRefs, source: 'hierarchy' };
}
const visibleRefs = getVisibleBasketEntityRefsFromStore();
if (visibleRefs.length > 0) {
return { refs: visibleRefs, source: 'visible' };
}
return { refs: [], source: 'empty' };
}
export function isBasketIsolationActiveFromStore(): boolean {
const state = useViewerStore.getState();
if (state.pinboardEntities.size === 0 || state.isolatedEntities === null) return false;
const basketIds = basketToGlobalIds(state);
if (basketIds.size !== state.isolatedEntities.size) return false;
for (const id of basketIds) {
if (!state.isolatedEntities.has(id)) return false;
}
return true;
}
export function getVisibleBasketStatsFromStore(): BasketVisibleStats {
const state = useViewerStore.getState();
const visibleRefs = getVisibleBasketEntityRefsFromStore();
const visibleKeys = new Set<string>(visibleRefs.map(entityRefToString));
let removeCount = 0;
for (const key of state.pinboardEntities) {
if (visibleKeys.has(key)) removeCount++;
}
return {
visibleCount: visibleKeys.size,
addCount: Math.max(0, visibleKeys.size - removeCount),
removeCount,
basketCount: state.pinboardEntities.size,
};
}