From: Reino Ruusu Date: Mon, 2 Mar 2020 09:24:54 +0000 (+0200) Subject: Camera rotation with stable rotation speed X-Git-Tag: v1.43.0~53 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=9077bfb55726c21bbaa09b5d2b6e11310b677c67;p=simantics%2F3d.git Camera rotation with stable rotation speed gitlab #96 Change-Id: Iaf9a1a42f04dcf83f168e2fe0aa691c26876b024 --- diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java index ebdfb245..7a0cc031 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java @@ -118,10 +118,10 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti int y = e.getY(); // rotate if (this.InteractionMode == 1) { - cam.Azimuth(lastX - x); - cam.Elevation(y - lastY); + cam.Elevation(clampElevationDelta(y - lastY)); if (doNotRotate) cam.SetViewUp(upDirection); + cam.Azimuth(lastX - x); cam.OrthogonalizeViewUp(); resetCameraClippingRange(); // panel.UpdateLight(); @@ -184,6 +184,25 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti return true; } + private static double dot(double[] a, double[] b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; + } + + /** Calculate change in elevation, clamped to vertical directions. */ + private double clampElevationDelta(double elevationDelta) { + if (!doNotRotate) + return elevationDelta; + + double[] direction = cam.GetDirectionOfProjection(); + double d = Math.min(1.0, Math.max(-1.0, dot(direction, upDirection))); + double elevation = Math.toDegrees(Math.acos(d)) + elevationDelta; + if (elevation < 0) + elevationDelta -= elevation - 1e-5; + else if (elevation > 180) + elevationDelta -= elevation - 180 + 1e-5; + return elevationDelta; + } + @Override public boolean mouseWheelMoved(MouseWheelEvent e) { double zoomFactor;