diff --git a/packages/viewer-sandbox/src/main.ts b/packages/viewer-sandbox/src/main.ts index c86ddd1a9c..b8e50f6775 100644 --- a/packages/viewer-sandbox/src/main.ts +++ b/packages/viewer-sandbox/src/main.ts @@ -447,6 +447,9 @@ const getStream = () => { // Far away house section tool // 'https://app.speckle.systems/projects/817c4e8daa/models/f0601ef5f9@80db5ff26a' + + // Perfectly flat + // 'https://app.speckle.systems/projects/344f803f81/models/5582ab673e' ) } diff --git a/packages/viewer/src/modules/Intersections.ts b/packages/viewer/src/modules/Intersections.ts index 9572e0d020..91441694f2 100644 --- a/packages/viewer/src/modules/Intersections.ts +++ b/packages/viewer/src/modules/Intersections.ts @@ -17,6 +17,7 @@ import { SpeckleRaycaster } from './objects/SpeckleRaycaster.js' import { ObjectLayers } from '../IViewer.js' +import { World } from './World.js' export class Intersections { protected raycaster: SpeckleRaycaster @@ -197,21 +198,13 @@ export class Intersections { return a.distance - b.distance }) if (bounds) { - this.boundsBuffer.copy(bounds) /** We slightly increase the tested bounds to account for fp precision issues which * have proven to arise exactly at the edge of the bounds. Our BVH returns intersection * points ever so slightly off the actual surface, so for very thin geometries it might * fall outside of the bounds */ - const offsetSize = new Vector3( - 0.001 * (this.boundsBuffer.max.x - this.boundsBuffer.min.x), - 0.001 * (this.boundsBuffer.max.y - this.boundsBuffer.min.y), - 0.001 * (this.boundsBuffer.max.z - this.boundsBuffer.min.z) - ) + this.boundsBuffer.copy(World.expandBoxRelative(bounds)) - this.boundsBuffer.expandByVector( - new Vector3(offsetSize.x, offsetSize.y, offsetSize.z) - ) results = results.filter((result) => { return ( this.boundsBuffer.containsPoint(result.point) || diff --git a/packages/viewer/src/modules/World.ts b/packages/viewer/src/modules/World.ts index 1359002ded..2921bb53b3 100644 --- a/packages/viewer/src/modules/World.ts +++ b/packages/viewer/src/modules/World.ts @@ -98,4 +98,31 @@ export class World { offsetBox.applyMatrix4(MatBuff1) return offsetBox } + + public static expandBoxRelative(box: Box3, offsetAmount: number = 0.001) { + const center = box.getCenter(new Vector3()) + const size = box.getSize(new Vector3()) + MatBuff1.makeTranslation(center.x, center.y, center.z) + MatBuff2.copy(MatBuff1).invert() + MatBuff0.identity() + MatBuff0.makeScale(1 + offsetAmount, 1 + offsetAmount, 1 + offsetAmount) + const offsetBox = new Box3().copy(box) + if (size.x === 0) { + offsetBox.min.x += -offsetAmount * 0.5 + offsetBox.max.x += offsetAmount * 0.5 + } + if (size.y === 0) { + offsetBox.min.y += -offsetAmount * 0.5 + offsetBox.max.y += offsetAmount * 0.5 + } + if (size.z === 0) { + offsetBox.min.z += -offsetAmount * 0.5 + offsetBox.max.z += offsetAmount * 0.5 + } + offsetBox.applyMatrix4(MatBuff2) + offsetBox.applyMatrix4(MatBuff0) + offsetBox.applyMatrix4(MatBuff1) + + return offsetBox + } }