]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Fix parallel perspective toggle to keep view direction 53/3453/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 5 Nov 2019 17:06:12 +0000 (19:06 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 5 Nov 2019 17:06:12 +0000 (19:06 +0200)
gitlab #47

Change-Id: Ie30f8b117c085870703599ebe0a0928f6aa31e7b

org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java

index 70eb3a63d25a3afe94fac46ec31e77b8c91817a8..43ec97a090dda0c78c4c2b6d6bf205d7b569373b 100644 (file)
@@ -11,9 +11,6 @@
  *******************************************************************************/
 package org.simantics.g3d.vtk.handlers;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import javax.vecmath.Vector3d;
 
 import org.eclipse.core.commands.AbstractHandler;
@@ -22,15 +19,17 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.handlers.HandlerUtil;
+import org.simantics.g3d.math.MathTools;
 import org.simantics.g3d.vtk.common.VtkView;
 import org.simantics.utils.threads.AWTThread;
 import org.simantics.utils.threads.ThreadUtils;
 
+import vtk.vtkCamera;
+import vtk.vtkRenderer;
+
 public class ParallelPerspectiveHandler extends AbstractHandler {
 
        
-       private Map<VtkView,Vector3d> cameraPos = new HashMap<VtkView, Vector3d>();
-       
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
                Command command = event.getCommand();
@@ -44,30 +43,41 @@ public class ParallelPerspectiveHandler extends AbstractHandler {
 
                        @Override
                        public void run() {
+                           vtkRenderer ren = panel.getRenderer();
+                           vtkCamera cam = ren.GetActiveCamera();
                                if (activate){
+
                                        Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
                                        Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
-                                       cameraPos.put(panel, pos);
+                                       double dist = MathTools.distance(pos, focal);
+                                       cam.SetParallelScale(dist/4.0);
+                                       // camera must be moved backwards, or graphics get clipped when parallel view is zoomed out
+                                       // TODO : is there a better way to do this?
                                        Vector3d dir = new Vector3d(pos);
                                        dir.sub(focal);
                                        dir.normalize();
                                        dir.scale(100);
                                        dir.add(focal);
-                                       panel.getRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);
-
+                                       cam.SetPosition(dir.x, dir.y, dir.z);
 
-                                       panel.getRenderer().GetActiveCamera().SetParallelProjection(1);
-                                       panel.getRenderer().ResetCameraClippingRange();
+                                       cam.SetParallelProjection(1);
+                                       ren.ResetCameraClippingRange();
                                } else {
-                                       panel.getRenderer().GetActiveCamera().SetParallelProjection(0);
-                                       Vector3d pos = cameraPos.get(panel);
-                                       if (pos != null) {
-                                               panel.getRenderer().GetActiveCamera().SetPosition(pos.x, pos.y, pos.z);
-                                       }
-                                       panel.getRenderer().ResetCameraClippingRange();
+                                   
+                                   double scale = cam.GetParallelScale();
+                                       cam.SetParallelProjection(0);
+                                       
+                                       Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
+                    Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
+                    Vector3d dir = new Vector3d(pos);
+                    dir.sub(focal);
+                    dir.normalize();
+                    dir.scale(scale*4.0);
+                    dir.add(focal);
+                    cam.SetPosition(dir.x, dir.y, dir.z);
+                                       ren.ResetCameraClippingRange();
 
                                }
-                               //                              panel.UpdateLight();
                                panel.refresh();
 
                        }
index 7eb4cfc93d7d0df69ea3a6adc181b0360b693256..9c444b4f7eac40aff2e6d5fe9af0f8a92c4681c3 100644 (file)
@@ -168,6 +168,7 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                        zoomFactor = Math.pow(1.02, (y - lastY));
                        if (cam.GetParallelProjection() == 1) {
                                cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
+                               resetCameraClippingRange();
                        } else {
                                cam.Dolly(zoomFactor);
                                resetCameraClippingRange();