Skip to content

Commit

Permalink
feat(front): allow to get bounding boxes of unstreamed items
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Oct 16, 2024
1 parent 6ca4408 commit ba8a389
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/front/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@thatopen/components-front",
"description": "Collection of frontend tools to author BIM apps.",
"version": "2.4.0-alpha.1",
"version": "2.4.0-alpha.2",
"author": "That Open Company",
"contributors": [
"Antonio Gonzalez Viegas (https://github.com/agviegas)",
Expand Down
70 changes: 68 additions & 2 deletions packages/front/src/fragments/IfcStreamer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ export class IfcStreamer extends OBC.Component implements OBC.Disposable {
*/
models: {
[modelID: string]: {
assets: OBC.StreamedAsset[];
assetsMap: Map<number, OBC.StreamedAsset>;
geometries: OBC.StreamedGeometries;
/** @deprecated use assetsMap instead */
assets: OBC.StreamedAsset[];
};
} = {};

Expand Down Expand Up @@ -241,7 +243,25 @@ export class IfcStreamer extends OBC.Component implements OBC.Disposable {
}

this.culler.add(group.uuid, assets, geometries);
this.models[group.uuid] = { assets, geometries };

const assetsMap = new Map<number, OBC.StreamedAsset>();
for (const asset of assets) {
assetsMap.set(asset.id, asset);
}

const object = { assetsMap, geometries };
Object.defineProperty(object, "assets", {
get: () => {
return Array.from(object.assetsMap.values());
},
});

this.models[group.uuid] = object as {
assetsMap: Map<number, OBC.StreamedAsset>;
geometries: OBC.StreamedGeometries;
assets: OBC.StreamedAsset[];
};

const instances: StreamedInstances = new Map();

for (const asset of assets) {
Expand Down Expand Up @@ -430,6 +450,52 @@ export class IfcStreamer extends OBC.Component implements OBC.Disposable {
}
}

/**
* Gets a FragmentsGroup with the OBB of the specified items. Keep in mind that you will need to dispose this group yourself using the dispoe() method.
*
* @param items - The items whose bounding boxes to get.
*/
getBoundingBoxes(items: FRAG.FragmentIdMap) {
const data: { [modelID: string]: Set<number> } = {};

const fragments = this.components.get(OBC.FragmentsManager);

const groupsOfFragments = new Map<string, string>();

for (const [groupID, group] of fragments.groups) {
for (const [, fragID] of group.keyFragments) {
groupsOfFragments.set(fragID, groupID);
}
}

const visitedModels = new Set<string>();

for (const fragID in items) {
const modelID = groupsOfFragments.get(fragID);
if (modelID === undefined) {
console.log("Fragment group not found!");
continue;
}
const assetsIDs = items[fragID];
if (!visitedModels.has(modelID)) {
data[modelID] = new Set<number>();
visitedModels.add(modelID);
}

for (const assetID of assetsIDs) {
const found = this.models[modelID].assetsMap.get(assetID);
if (!found) continue;
for (const geom of found.geometries) {
const geomID = geom.geometryID;
const instanceID = this.culler.getInstanceID(assetID, geomID);
data[modelID].add(instanceID);
}
}
}

return this.culler.getBoundingBoxes(data);
}

private async loadFoundGeometries(
seen: {
[modelID: string]: Map<number, Set<number>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ export class GeometryCullerRenderer extends OBC.CullerRenderer {
>();

for (const asset of assets) {
// if (asset.id !== 9056429) continue;
for (const geometryData of asset.geometries) {
const { geometryID, transformation, color } = geometryData;

Expand Down Expand Up @@ -531,6 +530,33 @@ export class GeometryCullerRenderer extends OBC.CullerRenderer {
}
}

getBoundingBoxes(items: { [modelID: number]: Iterable<number> }) {
const boxGroup = new FRAGS.FragmentsGroup();
for (const modelID in items) {
const ids = items[modelID];
const modelIndex = this._modelIDIndex.get(modelID);
if (modelIndex === undefined) {
continue;
}
const bboxes = this.boxes.get(modelIndex);
if (!bboxes) {
continue;
}
const clone = bboxes.clone(ids);
boxGroup.add(clone.mesh);
boxGroup.items.push(clone);
}
return boxGroup;
}

getInstanceID(assetID: number, geometryID: number) {
// src: https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript
// eslint-disable-next-line no-bitwise
const size = (Math.log(geometryID) * Math.LOG10E + 1) | 0;
const factor = 10 ** size;
return assetID + geometryID / factor;
}

private setGeometryVisibility(
geometry: CullerBoundingBox,
visible: boolean,
Expand Down Expand Up @@ -686,12 +712,4 @@ export class GeometryCullerRenderer extends OBC.CullerRenderer {
this._indexModelID.set(count, modelID);
return count;
}

private getInstanceID(assetID: number, geometryID: number) {
// src: https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript
// eslint-disable-next-line no-bitwise
const size = (Math.log(geometryID) * Math.LOG10E + 1) | 0;
const factor = 10 ** size;
return assetID + geometryID / factor;
}
}

0 comments on commit ba8a389

Please sign in to comment.