From 08f6e325c5a171a8f7a4e65960fa5e8ba2934582 Mon Sep 17 00:00:00 2001 From: rakadu1 Date: Wed, 10 Sep 2025 10:43:40 +0530 Subject: [PATCH] Ellipse fix, now using the radii and angle in JSON to draw directly. --- .hintrc | 13 +++++ steps/src/drawUtils.js | 95 +++++++++++++++++++------------------ steps/src/p5/radarSketch.js | 9 ++-- 3 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 .hintrc diff --git a/.hintrc b/.hintrc new file mode 100644 index 0000000..5b02c11 --- /dev/null +++ b/.hintrc @@ -0,0 +1,13 @@ +{ + "extends": [ + "development" + ], + "hints": { + "axe/forms": [ + "default", + { + "label": "off" + } + ] + } +} \ No newline at end of file diff --git a/steps/src/drawUtils.js b/steps/src/drawUtils.js index c8b26a0..076c1f4 100644 --- a/steps/src/drawUtils.js +++ b/steps/src/drawUtils.js @@ -605,53 +605,14 @@ export function handleCloseUpDisplay(p, plotScales) { export function drawCovarianceEllipse( p, position, - covarianceP, + radii, + angle, plotScales, isStationary ) { // Only draw the ellipse for tracks that are not stationary. if (isStationary) return; - - const pPos = [ - [covarianceP[0][0], covarianceP[0][1]], - [covarianceP[1][0], covarianceP[1][1]], - ]; - - const a = pPos[0][0]; - const b = pPos[0][1]; - const d = pPos[1][1]; - const trace = a + d; - const determinant = a * d - b * b; - - //const lambda1 = trace / 2 + Math.sqrt(Math.pow(trace, 2) / 4 - determinant); - //const lambda2 = trace / 2 - Math.sqrt(Math.pow(trace, 2) / 4 - determinant); - // --- START: New robust calculation with logging --- - let sqrtTermVal = Math.pow(trace, 2) / 4 - determinant; - - // Check for a negative value, which causes NaN errors - if (sqrtTermVal < 0) { - // Log a warning so we know it happened, as you suggested - console.warn( - `Clamping negative sqrtTermVal in frame ${appState.currentFrame} to prevent NaN. Original value: ${sqrtTermVal}` - ); - // Clamp the value to 0. This allows drawing to continue instead of breaking. - sqrtTermVal = 0; - } - - const sqrtTerm = Math.sqrt(sqrtTermVal); - const lambda1 = trace / 2 + sqrtTerm; - const lambda2 = trace / 2 - sqrtTerm; - // --- END: New robust calculation with logging --- - const chi2 = 5.991; - const majorAxis = Math.sqrt(chi2 * lambda1); - const minorAxis = Math.sqrt(chi2 * lambda2); - - let eigenvector = [1, 0]; - if (b !== 0) { - eigenvector = [lambda1 - d, b]; - } - const angle = Math.atan2(eigenvector[1], eigenvector[0]); - + const [radiusA, radiusB] = radii; p.push(); p.noFill(); p.stroke(255, 0, 0, 150); @@ -661,13 +622,53 @@ export function drawCovarianceEllipse( position[1] * plotScales.plotScaleY ); p.rotate(angle); - p.ellipse( - 0, - 0, - majorAxis * 2 * plotScales.plotScaleX, - minorAxis * 2 * plotScales.plotScaleY + p.ellipse(0,0, + radiusA * 2 * plotScales.plotScaleX, // multiplied by 2 because ellipse function + radiusB * 2 * plotScales.plotScaleY // in p5 library expect ); p.pop(); + + //---old ellipse logic using covariance from data directly// + // const pPos = [ + // [covarianceP[0][0], covarianceP[0][1]], + // [covarianceP[1][0], covarianceP[1][1]], + // ]; + + // const a = pPos[0][0]; + // const b = pPos[0][1]; + // const d = pPos[1][1]; + // const trace = a + d; + // const determinant = a * d - b * b; + + //const lambda1 = trace / 2 + Math.sqrt(Math.pow(trace, 2) / 4 - determinant); + //const lambda2 = trace / 2 - Math.sqrt(Math.pow(trace, 2) / 4 - determinant); + // --- START: New robust calculation with logging --- + // let sqrtTermVal = Math.pow(trace, 2) / 4 - determinant; + + // Check for a negative value, which causes NaN errors + // if (sqrtTermVal < 0) { + // // Log a warning so we know it happened, as you suggested + // console.warn( + // `Clamping negative sqrtTermVal in frame ${appState.currentFrame} to prevent NaN. Original value: ${sqrtTermVal}` + // ); + // // Clamp the value to 0. This allows drawing to continue instead of breaking. + // sqrtTermVal = 0; + // } + + // const sqrtTerm = Math.sqrt(sqrtTermVal); + // const lambda1 = trace / 2 + sqrtTerm; + // const lambda2 = trace / 2 - sqrtTerm; + // // --- END: New robust calculation with logging --- + // const chi2 = 5.991; + // const majorAxis = Math.sqrt(chi2 * lambda1); + // const minorAxis = Math.sqrt(chi2 * lambda2); + + // let eigenvector = [1, 0]; + // if (b !== 0) { + // eigenvector = [lambda1 - d, b]; + // } + // const angle = Math.atan2(eigenvector[1], eigenvector[0]); + //---old ellipse logic using covariance from data directly// } // In src/drawUtils.js diff --git a/steps/src/p5/radarSketch.js b/steps/src/p5/radarSketch.js index 784c368..86b3ae3 100644 --- a/steps/src/p5/radarSketch.js +++ b/steps/src/p5/radarSketch.js @@ -125,16 +125,15 @@ export const radarSketch = function (p) { drawTrajectories(p, plotScales); if (toggleCovariance.checked) { for (const track of appState.vizData.tracks) { - const log = track.historyLog.find( - (log) => log.frameIdx === appState.currentFrame + 1 - ); - if (log && log.covarianceP) { + const log = track.historyLog.find((log) => log.frameIdx === appState.currentFrame + 1); + if (log && log.ellipseRadii && typeof log.ellipseAngle !== 'undefined') { const pos = log.predictedPosition; if (pos && pos[0] !== null) { drawCovarianceEllipse( p, pos, - log.covarianceP, + log.ellipseRadii, + log.ellipseAngle, plotScales, log.isStationary );