Skip to content

Commit

Permalink
axes: fix snapping to nearest rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
ochafik committed Dec 26, 2024
1 parent cce4b6b commit edf74b2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/components/ViewerPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ function euclideanDist(a: [number, number, number], b: [number, number, number])
const dz = a[2] - b[2];
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
const radDist = (a: number, b: number) => Math.min(Math.abs(a - b), Math.abs(a - b + 2 * Math.PI), Math.abs(a - b - 2 * Math.PI));

function getClosestPredefinedOrbitIndex(theta: number, phi: number): [number, number] {
function getClosestPredefinedOrbitIndex(theta: number, phi: number): [number, number, number] {
const point = spherePoint(theta, phi);
const points = PREDEFINED_ORBITS.map(([_, t, p]) => spherePoint(t, p));
const distances = points.map(p => euclideanDist(point, p));
const result = distances.reduce((acc, d, i) => d < acc[1] ? [i, d] : acc, [0, Infinity]) as [number, number];
return result;
const radDistances = PREDEFINED_ORBITS.map(([_, ptheta, pphi]) => Math.max(radDist(theta, ptheta), radDist(phi, pphi)));
const [index, dist] = distances.reduce((acc, d, i) => d < acc[1] ? [i, d] : acc, [0, Infinity]) as [number, number];
return [index, dist, radDistances[index]];
}

const originalOrbit = (([name, theta, phi]) => `${theta}rad ${phi}rad auto`)(PREDEFINED_ORBITS[0]);
Expand Down Expand Up @@ -90,16 +92,18 @@ export default function ViewerPanel({className, style}: {className?: string, sty
}
function onMouseUp(e: MouseEvent) {
if (e.target === axesViewerRef.current) {
const euclEps = 0.01;
const radEps = 0.1;

const spherePoint = getSpherePoint();
const clickDist = mouseDownSpherePoint ? euclideanDist(spherePoint, mouseDownSpherePoint) : Infinity;
if (clickDist > 0.01) {
if (clickDist > euclEps) {
return;
}
// Cycle through orbits
const orbit = modelViewerRef.current.getCameraOrbit();
const [currentIndex, dist] = getClosestPredefinedOrbitIndex(orbit.theta, orbit.phi);
const eps = 0.01;
const newIndex = dist < eps ? (currentIndex + 1) % PREDEFINED_ORBITS.length : currentIndex;
const [currentIndex, dist, radDist] = getClosestPredefinedOrbitIndex(orbit.theta, orbit.phi);
const newIndex = dist < euclEps && radDist < radEps ? (currentIndex + 1) % PREDEFINED_ORBITS.length : currentIndex;
const [name, theta, phi] = PREDEFINED_ORBITS[newIndex];
Object.assign(orbit, {theta, phi});
const newOrbit = modelViewerRef.current.cameraOrbit = axesViewerRef.current.cameraOrbit = orbit.toString();
Expand All @@ -109,7 +113,9 @@ export default function ViewerPanel({className, style}: {className?: string, sty
}
window.addEventListener('mousedown', onMouseDown);
window.addEventListener('mouseup', onMouseUp);
// window.addEventListener('click', onClick);
return () => {
// window.removeEventListener('click', onClick);
window.removeEventListener('mousedown', onMouseDown);
window.removeEventListener('mouseup', onMouseUp);
};
Expand Down

0 comments on commit edf74b2

Please sign in to comment.