Skip to content

Commit

Permalink
Handles new measurements reverting to meters even if a different unit…
Browse files Browse the repository at this point in the history
… was previously set. Handles units not changing properly when changing the unit value
  • Loading branch information
AlexandruPopovici committed Dec 20, 2024
1 parent 1a3bb53 commit 33ebcd1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 45 deletions.
6 changes: 3 additions & 3 deletions packages/viewer-sandbox/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ const createViewer = async (containerName: string, _stream: string) => {
Object.assign(sandbox.sceneParams.worldSize, viewer.World.worldSize)
Object.assign(sandbox.sceneParams.worldOrigin, viewer.World.worldOrigin)
sandbox.refresh()
const snowPipeline = new SnowPipeline(viewer.getRenderer())
viewer.getRenderer().pipeline = snowPipeline
void snowPipeline.start()
// const snowPipeline = new SnowPipeline(viewer.getRenderer())
// viewer.getRenderer().pipeline = snowPipeline
// void snowPipeline.start()
})

viewer.on(ViewerEvent.UnloadComplete, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export abstract class Measurement extends Object3D {
this.renderingSize.copy(size)
}

public abstract update(): void
public abstract update(): Promise<void>
public abstract raycast(_raycaster: Raycaster, _intersects: Array<Intersection>): void
public abstract highlight(_value: boolean): void
public abstract updateClippingPlanes(_planes: Plane[]): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SpeckleText } from '../../objects/SpeckleText.js'
import SpeckleTextMaterial from '../../materials/SpeckleTextMaterial.js'
import SpeckleBasicMaterial from '../../materials/SpeckleBasicMaterial.js'
import { ObjectLayers } from '../../../IViewer.js'
import Logger from '../../utils/Logger.js'

export interface MeasurementPointGizmoStyle {
fixedSize?: number | boolean
Expand Down Expand Up @@ -325,8 +326,8 @@ export class MeasurementPointGizmo extends Group {
position?: Vector3,
quaternion?: Quaternion,
scale?: Vector3
) {
void this.text
): Promise<void> {
return this.text
.update({
textValue: value,
height: 1,
Expand All @@ -343,6 +344,9 @@ export class MeasurementPointGizmo extends Group {
if (this.text.backgroundMesh) this.text.backgroundMesh.renderOrder = 3
this.text.textMesh.renderOrder = 4
})
.catch((reason) => {
Logger.log(`Could not update text: ${reason}`)
})
}

public updateStyle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class MeasurementsExtension extends Extension {
this._enabled = value
if (this._activeMeasurement) {
this._activeMeasurement.isVisible = value
this._activeMeasurement.update()
void this._activeMeasurement.update()
if (!value) this.cancelMeasurement()
}
this.viewer.requestRender()
Expand Down Expand Up @@ -180,9 +180,10 @@ export class MeasurementsExtension extends Extension {
this._activeMeasurement.endPoint.copy(this.pointBuff)
this._activeMeasurement.endNormal.copy(this.normalBuff)
}
this._activeMeasurement.update()
void this._activeMeasurement.update().then(() => {
this.viewer.requestRender()
})

this.viewer.requestRender()
this._frameLock = true
this._sceneHit = true
// console.log('Time -> ', performance.now() - start)
Expand Down Expand Up @@ -282,8 +283,9 @@ export class MeasurementsExtension extends Extension {
this._activeMeasurement.endPoint.copy(perpResult[0].point)
this._activeMeasurement.endNormal.copy(perpResult[0].face.normal)
this._activeMeasurement.state = MeasurementState.DANGLING_END
this._activeMeasurement.update()
this.finishMeasurement()
void this._activeMeasurement.update().then(() => {
this.finishMeasurement()
})
}

protected startMeasurement(): Measurement {
Expand All @@ -295,12 +297,21 @@ export class MeasurementsExtension extends Extension {
else throw new Error('Unsupported measurement type!')

measurement.state = MeasurementState.DANGLING_START
measurement.units =
this._options.units !== undefined
? this._options.units
: DefaultMeasurementsOptions.units
measurement.precision =
this._options.precision !== undefined
? this._options.precision
: DefaultMeasurementsOptions.precision
measurement.frameUpdate(
this.renderer.renderingCamera,
this.screenBuff0,
this.renderer.sceneBox
)
this.renderer.scene.add(measurement)

return measurement
}

Expand All @@ -314,7 +325,7 @@ export class MeasurementsExtension extends Extension {
if (!this._activeMeasurement) return

this._activeMeasurement.state = MeasurementState.COMPLETE
this._activeMeasurement.update()
void this._activeMeasurement.update()
if (this._activeMeasurement.value > 0) {
this.measurements.push(this._activeMeasurement)
} else {
Expand Down Expand Up @@ -421,6 +432,7 @@ export class MeasurementsExtension extends Extension {

protected applyOptions() {
const all = [this._activeMeasurement, ...this.measurements]
const updatePromises: Promise<void>[] = []
all.forEach((value) => {
if (value) {
value.units =
Expand All @@ -431,7 +443,7 @@ export class MeasurementsExtension extends Extension {
this._options.precision !== undefined
? this._options.precision
: DefaultMeasurementsOptions.precision
value.update()
updatePromises.push(value.update())
}
})
this.viewer
Expand All @@ -440,18 +452,19 @@ export class MeasurementsExtension extends Extension {

if (this._options.visible) this.raycaster.layers.enable(ObjectLayers.MEASUREMENTS)
else this.raycaster.layers.disable(ObjectLayers.MEASUREMENTS)

this.viewer.requestRender()
void Promise.all(updatePromises).then(() => {
this.viewer.requestRender()
})
}

public fromMeasurementData(startPoint: Vector3, endPoint: Vector3) {
public async fromMeasurementData(startPoint: Vector3, endPoint: Vector3) {
const measurement = new PointToPointMeasurement()
measurement.startPoint.copy(startPoint)
measurement.endPoint.copy(endPoint)
measurement.state = MeasurementState.DANGLING_END
measurement.update()
await measurement.update()
measurement.state = MeasurementState.COMPLETE
measurement.update()
await measurement.update()
this.measurements.push(measurement)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ export class PerpendicularMeasurement extends Measurement {
* won't look correct while zooming
*/
if (this._state === MeasurementState.DANGLING_START) {
this.update()
void this.update()
}
}

public update() {
if (isNaN(this.startPoint.length())) return
if (!this.renderingCamera) return
public update(): Promise<void> {
let ret = Promise.resolve()

if (isNaN(this.startPoint.length())) return ret
if (!this.renderingCamera) return ret

this.startGizmo?.updateDisc(this.startPoint, this.startNormal)
this.startGizmo?.updatePoint(this.startPoint)
Expand Down Expand Up @@ -180,23 +182,27 @@ export class PerpendicularMeasurement extends Measurement {
)

this.value = this.midPoint.distanceTo(this.startPoint)
this.startGizmo?.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`,
textPos
)
if (this.startGizmo)
ret = this.startGizmo.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`,
textPos
)
this.endGizmo?.enable(true, true, true, true)
}
if (this._state === MeasurementState.COMPLETE) {
this.startGizmo?.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`
)
if (this.startGizmo)
ret = this.startGizmo.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`
)
this.startGizmo?.enable(false, true, true, true)
this.endGizmo?.enable(false, false, true, false)
}

return ret
}

public raycast(raycaster: Raycaster, intersects: Array<Intersection>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class PointToPointMeasurement extends Measurement {
this.endGizmo?.frameUpdate(camera, bounds)
}

public update() {
public update(): Promise<void> {
let ret: Promise<void> = Promise.resolve()
this.startGizmo?.updateDisc(this.startPoint, this.startNormal)
this.startGizmo?.updatePoint(this.startPoint)
this.endGizmo?.updateDisc(this.endPoint, this.endNormal)
Expand Down Expand Up @@ -71,23 +72,26 @@ export class PointToPointMeasurement extends Measurement {

this.startGizmo?.updateLine([this.startPoint, lineEndPoint])
this.endGizmo?.updatePoint(lineEndPoint)
this.startGizmo?.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`,
textPos
)
if (this.startGizmo)
ret = this.startGizmo.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`,
textPos
)
this.endGizmo?.enable(true, true, true, true)
}
if (this._state === MeasurementState.COMPLETE) {
this.startGizmo?.enable(false, true, true, true)
this.endGizmo?.enable(false, false, true, false)
this.startGizmo?.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`
)
if (this.startGizmo)
ret = this.startGizmo.updateText(
`${(this.value * getConversionFactor('m', this.units)).toFixed(
this.precision
)} ${this.units}`
)
}
return ret
}

public raycast(raycaster: Raycaster, intersects: Array<Intersection>) {
Expand Down

0 comments on commit 33ebcd1

Please sign in to comment.