-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathindex.ts
459 lines (375 loc) · 13.2 KB
/
index.ts
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
import * as WEBIFC from "web-ifc";
import * as THREE from "three";
import * as FRAGS from "@thatopen/fragments";
import { Components, Disposable, Event, Component } from "../../core";
import { isPointInFrontOfPlane, obbFromPoints } from "../../utils";
import { IfcStreamingSettings, StreamedGeometries, StreamedAsset } from "./src";
import {
SpatialStructure,
CivilReader,
IfcMetadataReader,
SpatialIdsFinder,
} from "../IfcLoader/src";
export * from "./src";
/**
* A component that handles the tiling of IFC geometries for efficient streaming. 📕 [Tutorial](https://docs.thatopen.com/Tutorials/Components/Core/IfcGeometryTiler). 📘 [API](https://docs.thatopen.com/api/@thatopen/components/classes/IfcGeometryTiler).
*/
export class IfcGeometryTiler extends Component implements Disposable {
/**
* A unique identifier for the component.
* This UUID is used to register the component within the Components system.
*/
static readonly uuid = "d9999a00-e1f5-4d3f-8cfe-c56e08609764" as const;
/**
* Event triggered when geometry is streamed.
* Contains the streamed geometry data and its buffer.
*/
readonly onGeometryStreamed = new Event<{
buffer: Uint8Array;
data: StreamedGeometries;
}>();
/**
* Event triggered when assets are streamed.
* Contains the streamed assets.
*/
readonly onAssetStreamed = new Event<StreamedAsset[]>();
/**
* Event triggered to indicate the progress of the streaming process.
* Contains the progress percentage.
*/
readonly onProgress = new Event<number>();
/**
* Event triggered when the IFC file is loaded.
* Contains the loaded IFC file data.
*/
readonly onIfcLoaded = new Event<Uint8Array>();
/** {@link Disposable.onDisposed} */
readonly onDisposed = new Event();
/**
* Settings for the IfcGeometryTiler.
*/
settings = new IfcStreamingSettings();
/** {@link Component.enabled} */
enabled: boolean = true;
/**
* The WebIFC API instance used for IFC file processing.
*/
webIfc = new WEBIFC.IfcAPI();
private _spatialTree = new SpatialStructure();
private _metaData = new IfcMetadataReader();
private _visitedGeometries = new Map<
number,
{ uuid: string; index: number }
>();
private _streamSerializer = new FRAGS.StreamSerializer();
private _geometries: Map<
number,
{
position: Float32Array;
normal: Float32Array;
index: Uint32Array;
boundingBox: Float32Array;
hasHoles: boolean;
}
> = new Map();
private _geometryCount = 0;
private _civil = new CivilReader();
private _groupSerializer = new FRAGS.Serializer();
private _assets: StreamedAsset[] = [];
private _meshesWithHoles = new Set<number>();
constructor(components: Components) {
super(components);
this.components.add(IfcGeometryTiler.uuid, this);
this.settings.excludedCategories.add(WEBIFC.IFCOPENINGELEMENT);
}
/** {@link Disposable.dispose} */
dispose() {
this.onIfcLoaded.reset();
this.onGeometryStreamed.reset();
this.onAssetStreamed.reset();
(this.webIfc as any) = null;
this.onDisposed.trigger();
this.onDisposed.reset();
}
/**
* This method streams the IFC file from a given buffer.
*
* @param data - The Uint8Array containing the IFC file data.
* @returns A Promise that resolves when the streaming process is complete.
*
* @remarks
* This method cleans up any resources after the streaming process is complete.
*
* @example
* ```typescript
* const ifcData = await fetch('path/to/ifc/file.ifc');
* const rawBuffer = await response.arrayBuffer();
* const ifcBuffer = new Uint8Array(rawBuffer);
* await ifcGeometryTiler.streamFromBuffer(ifcBuffer);
* ```
*/
async streamFromBuffer(data: Uint8Array) {
// const before = performance.now();
await this.readIfcFile(data);
await this.streamAllGeometries();
this.cleanUp();
// console.log(`Streaming the IFC took ${performance.now() - before} ms!`);
}
/**
* This method streams the IFC file from a given callback.
*
* @param loadCallback - The callback function that will be used to load the IFC file.
* @returns A Promise that resolves when the streaming process is complete.
*
* @remarks
* This method cleans up any resources after the streaming process is complete.
*
*/
async streamFromCallBack(loadCallback: WEBIFC.ModelLoadCallback) {
// const before = performance.now();
await this.streamIfcFile(loadCallback);
await this.streamAllGeometries();
this.cleanUp();
// console.log(`Streaming the IFC took ${performance.now() - before} ms!`);
}
private async readIfcFile(data: Uint8Array) {
const { path, absolute, logLevel } = this.settings.wasm;
this.webIfc.SetWasmPath(path, absolute);
await this.webIfc.Init();
if (logLevel) {
this.webIfc.SetLogLevel(logLevel);
}
this.webIfc.OpenModel(data, this.settings.webIfc);
}
private async streamIfcFile(loadCallback: WEBIFC.ModelLoadCallback) {
const { path, absolute, logLevel } = this.settings.wasm;
this.webIfc.SetWasmPath(path, absolute);
await this.webIfc.Init();
if (logLevel) {
this.webIfc.SetLogLevel(logLevel);
}
this.webIfc.OpenModelFromCallback(loadCallback, this.settings.webIfc);
}
private async streamAllGeometries() {
const { minGeometrySize, minAssetsSize } = this.settings;
// Precompute the level to which each item belongs
this._spatialTree.setUp(this.webIfc);
// Get all IFC objects and group them in chunks of specified size
const allIfcEntities = this.webIfc.GetIfcEntityList(0);
const chunks: number[][] = [[]];
const group = new FRAGS.FragmentsGroup();
group.ifcMetadata = {
name: "",
description: "",
...this._metaData.getNameInfo(this.webIfc),
...this._metaData.getDescriptionInfo(this.webIfc),
schema: (this.webIfc.GetModelSchema(0) as FRAGS.IfcSchema) || "IFC2X3",
maxExpressID: this.webIfc.GetMaxExpressID(0),
};
let counter = 0;
let index = 0;
for (const type of allIfcEntities) {
if (!this.webIfc.IsIfcElement(type) && type !== WEBIFC.IFCSPACE) {
continue;
}
if (this.settings.excludedCategories.has(type)) {
continue;
}
const result = this.webIfc.GetLineIDsWithType(0, type);
const size = result.size();
for (let i = 0; i < size; i++) {
if (counter > minGeometrySize) {
counter = 0;
index++;
chunks.push([]);
}
const itemID = result.get(i);
chunks[index].push(itemID);
const props = this.webIfc.GetLine(0, itemID);
if (props.GlobalId) {
const globalID = props?.GlobalId.value || props?.GlobalId;
group.globalToExpressIDs.set(globalID, itemID);
}
const level = this._spatialTree.itemsByFloor[itemID] || 0;
group.data.set(itemID, [[], [level, type]]);
counter++;
}
}
this._spatialTree.cleanUp();
let nextProgress = 0.01;
let chunkCounter = 0;
for (const chunk of chunks) {
chunkCounter++;
this.webIfc.StreamMeshes(0, chunk, (mesh) => {
this.getMesh(this.webIfc, mesh, group);
});
if (this._geometryCount > minGeometrySize) {
await this.streamGeometries();
}
if (this._assets.length > minAssetsSize) {
await this.streamAssets();
}
const currentProgress = chunkCounter / chunks.length;
if (currentProgress > nextProgress) {
nextProgress += 0.01;
nextProgress = Math.max(nextProgress, currentProgress);
this.onProgress.trigger(Math.round(nextProgress * 100) / 100);
}
}
// Stream remaining assets and geometries
if (this._geometryCount) {
await this.streamGeometries();
}
if (this._assets.length) {
await this.streamAssets();
}
const { opaque, transparent } = group.geometryIDs;
for (const [id, { index, uuid }] of this._visitedGeometries) {
group.keyFragments.set(index, uuid);
const geometryID = id > 1 ? opaque : transparent;
geometryID.set(id, index);
}
// Delete assets that have no geometric representation
// const ids = group.data.keys();
// for (const id of ids) {
// const [keys] = group.data.get(id)!;
// if (!keys.length) {
// group.data.delete(id);
// }
// }
SpatialIdsFinder.get(group, this.webIfc);
const matrix = this.webIfc.GetCoordinationMatrix(0);
group.coordinationMatrix.fromArray(matrix);
group.civilData = this._civil.read(this.webIfc);
const buffer = this._groupSerializer.export(group);
this.onIfcLoaded.trigger(buffer);
group.dispose(true);
}
private cleanUp() {
try {
this.webIfc.Dispose();
} catch (e) {
// Problem disposing memory (maybe already disposed?)
}
(this.webIfc as any) = null;
this.webIfc = new WEBIFC.IfcAPI();
this._visitedGeometries.clear();
this._geometries.clear();
this._assets = [];
this._meshesWithHoles.clear();
}
private getMesh(
webIfc: WEBIFC.IfcAPI,
mesh: WEBIFC.FlatMesh,
group: FRAGS.FragmentsGroup,
) {
const size = mesh.geometries.size();
const id = mesh.expressID;
const asset: StreamedAsset = { id, geometries: [] };
for (let i = 0; i < size; i++) {
const geometry = mesh.geometries.get(i);
const geometryID = geometry.geometryExpressID;
// Distinguish between opaque and transparent geometries
const factor = geometry.color.w === 1 ? 1 : -1;
const transpGeometryID = geometryID * factor;
if (!this._visitedGeometries.has(transpGeometryID)) {
if (!this._visitedGeometries.has(geometryID)) {
// This is the first time we see this geometry
this.getGeometry(webIfc, geometryID);
}
// Save geometry for fragment generation
// separating transparent and opaque geometries
const index = this._visitedGeometries.size;
const uuid = THREE.MathUtils.generateUUID();
this._visitedGeometries.set(transpGeometryID, { uuid, index });
}
const geometryData = this._visitedGeometries.get(transpGeometryID);
if (geometryData === undefined) {
throw new Error("Error getting geometry data for streaming!");
}
const data = group.data.get(id);
if (!data) {
throw new Error("Data not found!");
}
data[0].push(geometryData.index);
const { x, y, z, w } = geometry.color;
const color = [x, y, z, w];
const transformation = geometry.flatTransformation;
asset.geometries.push({ color, geometryID, transformation });
}
this._assets.push(asset);
}
private getGeometry(webIfc: WEBIFC.IfcAPI, id: number) {
const geometry = webIfc.GetGeometry(0, id);
const index = webIfc.GetIndexArray(
geometry.GetIndexData(),
geometry.GetIndexDataSize(),
) as Uint32Array;
const vertexData = webIfc.GetVertexArray(
geometry.GetVertexData(),
geometry.GetVertexDataSize(),
) as Float32Array;
const position = new Float32Array(vertexData.length / 2);
const normal = new Float32Array(vertexData.length / 2);
for (let i = 0; i < vertexData.length; i += 6) {
position[i / 2] = vertexData[i];
position[i / 2 + 1] = vertexData[i + 1];
position[i / 2 + 2] = vertexData[i + 2];
normal[i / 2] = vertexData[i + 3];
normal[i / 2 + 1] = vertexData[i + 4];
normal[i / 2 + 2] = vertexData[i + 5];
}
// const bbox = makeApproxBoundingBox(position, index);
const obb = obbFromPoints(position);
const boundingBox = new Float32Array(obb.transformation.elements);
// Simple hole test: see if all triangles are facing away the center
// Using the vertex normal because it's easier
// Geometries with holes are treated as transparent items
// in the visibility test for geometry streaming
// Not perfect, but it will work for most cases and all the times it fails
// are false positives, so it's always on the safety side
const centerArray = [obb.center.x, obb.center.y, obb.center.z];
let hasHoles = false;
for (let i = 0; i < position.length - 2; i += 3) {
const x = position[i];
const y = position[i + 1];
const z = position[i + 2];
const nx = normal[i];
const ny = normal[i + 1];
const nz = normal[i + 2];
const pos = [x, y, z];
const nor = [nx, ny, nz];
if (isPointInFrontOfPlane(centerArray, pos, nor)) {
hasHoles = true;
break;
}
}
this._geometries.set(id, {
position,
normal,
index,
boundingBox,
hasHoles,
});
geometry.delete();
this._geometryCount++;
}
private async streamAssets() {
await this.onAssetStreamed.trigger(this._assets);
this._assets = null as any;
this._assets = [];
}
private async streamGeometries() {
let buffer = this._streamSerializer.export(this._geometries) as Uint8Array;
let data: StreamedGeometries = {};
for (const [id, { boundingBox, hasHoles }] of this._geometries) {
data[id] = { boundingBox, hasHoles };
}
this.onGeometryStreamed.trigger({ data, buffer });
// Force memory disposal of all created items
data = null as any;
buffer = null as any;
this._geometries.clear();
this._geometryCount = 0;
}
}