]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
Fix parallel perspective toggle to keep view direction
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / ParallelPerspectiveHandler.java
index 7400308df8e3ee1ab4d53581c4ad6d4f1d87108a..43ec97a090dda0c78c4c2b6d6bf205d7b569373b 100644 (file)
@@ -1,71 +1,92 @@
-package org.simantics.g3d.vtk.handlers;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.Command;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.simantics.g3d.vtk.common.InteractiveVtkPanel;\r
-import org.simantics.utils.threads.AWTThread;\r
-import org.simantics.utils.threads.ThreadUtils;\r
-\r
-public class ParallelPerspectiveHandler extends AbstractHandler {\r
-\r
-       \r
-       private Map<InteractiveVtkPanel,Vector3d> cameraPos = new HashMap<InteractiveVtkPanel, Vector3d>();\r
-       \r
-       @Override\r
-       public Object execute(ExecutionEvent event) throws ExecutionException {\r
-                Command command = event.getCommand();\r
-            boolean oldValue = HandlerUtil.toggleCommandState(command);\r
-            final boolean activate = !oldValue;\r
-            \r
-            final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);\r
-            final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);\r
-            \r
-            ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
-                       \r
-                       @Override\r
-                       public void run() {\r
-                               if (activate){\r
-                                       Vector3d focal = new Vector3d(panel.GetRenderer().GetActiveCamera().GetFocalPoint());\r
-                                       Vector3d pos = new Vector3d(panel.GetRenderer().GetActiveCamera().GetPosition());\r
-                                       cameraPos.put(panel, pos);\r
-                                       Vector3d dir = new Vector3d(pos);\r
-                                       dir.sub(focal);\r
-                                       dir.normalize();\r
-                                       dir.scale(100);\r
-                                       dir.add(focal);\r
-                                       panel.GetRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);\r
-                                       \r
-                                       \r
-                                       panel.GetRenderer().GetActiveCamera().SetParallelProjection(1);\r
-                                       panel.GetRenderer().ResetCameraClippingRange();\r
-                               } else {\r
-                                       panel.GetRenderer().GetActiveCamera().SetParallelProjection(0);\r
-                                       Vector3d pos = cameraPos.get(panel);\r
-                                       if (pos != null) {\r
-                                               panel.GetRenderer().GetActiveCamera().SetPosition(pos.x, pos.y, pos.z);\r
-                                       }\r
-                                       panel.GetRenderer().ResetCameraClippingRange();\r
-                                       \r
-                               }\r
-                               panel.UpdateLight();\r
-                               panel.repaint();\r
-\r
-                       }\r
-                });\r
-           \r
-            return null;\r
-            \r
-       }\r
-       \r
-       \r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.handlers;
+
+import javax.vecmath.Vector3d;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.Command;
+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 {
+
+       
+       @Override
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+               Command command = event.getCommand();
+               boolean oldValue = HandlerUtil.toggleCommandState(command);
+               final boolean activate = !oldValue;
+
+               final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
+               final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
+
+               ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
+
+                       @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());
+                                       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);
+                                       cam.SetPosition(dir.x, dir.y, dir.z);
+
+                                       cam.SetParallelProjection(1);
+                                       ren.ResetCameraClippingRange();
+                               } else {
+                                   
+                                   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.refresh();
+
+                       }
+               });
+
+               return null;
+
+       }
+       
+       
+       
+}