From 40f7487cfdd062c4a696c39c4edbc7eb508df52f Mon Sep 17 00:00:00 2001 From: Alexandru Popovici Date: Tue, 24 May 2022 16:46:45 +0300 Subject: [PATCH] Acos fix (#766) * Fixed an issue where floating point precision would lead to acos returning NaN * Wrong paranthesis close --- packages/viewer-sandbox/src/main.ts | 2 +- packages/viewer/src/modules/converter/Converter.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/viewer-sandbox/src/main.ts b/packages/viewer-sandbox/src/main.ts index ea4afbeb0..d7735b4ca 100644 --- a/packages/viewer-sandbox/src/main.ts +++ b/packages/viewer-sandbox/src/main.ts @@ -32,7 +32,7 @@ pane.addInput(PARAMS, 'color') // Load demo object viewer.loadObject( - 'https://speckle.xyz/streams/99abc74dd4/objects/ab503a2025e706717bff467ef8f96488' + 'https://speckle.xyz/streams/9217731fc1/objects/111a9dc2ed245f26a6584354b11b083f' ) viewer.on<{ progress: number; id: string; url: string }>('load-progress', (a) => { diff --git a/packages/viewer/src/modules/converter/Converter.js b/packages/viewer/src/modules/converter/Converter.js index 3450df320..f9484ef12 100644 --- a/packages/viewer/src/modules/converter/Converter.js +++ b/packages/viewer/src/modules/converter/Converter.js @@ -639,9 +639,14 @@ export default class Coverter { v2.normalize() const v3 = new Vector3().crossVectors(v2, v0) v3.normalize() - + /** + * We clamp the dot value to [-1,1] since that's the domain acos is defined on. Normally dot won't return + * values outside that interval, but due to floating point precision, you sometimes get -1.0000000004, which + * makes acos return NaN + */ + const dot = Math.min(Math.max(v0.dot(v1), -1), 1) // This is just the angle between the start and end points. Should be same as obj.angleRadians(or something) - const angle = Math.acos(v0.dot(v1)) + const angle = Math.acos(dot) const radius = obj.radius // We draw the arc in a local un-rotated coordinate system. We rotate it later on via transformation const curve = new THREE.EllipseCurve(