]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / CameraPositionHandler.java
diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java
new file mode 100644 (file)
index 0000000..da31a24
--- /dev/null
@@ -0,0 +1,66 @@
+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.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 CameraPositionHandler 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
+            \r
+            final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);\r
+            final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);\r
+            \r
+            String param = event.getParameter("org.simantics.g3d.viewDirection");\r
+            String vals[] = param.split(",");\r
+            final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2]));\r
+\r
+            \r
+            ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
+                       \r
+                       @Override\r
+                       public void run() {\r
+\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
+                               double distance = dir.length();\r
+                               \r
+                               dir.set(direction);\r
+                               dir.scale(distance);\r
+                               dir.add(focal);\r
+                               panel.GetRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);\r
+                               if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95)\r
+                                       panel.GetRenderer().GetActiveCamera().SetViewUp(0, 1, 0);\r
+                               else\r
+                                       panel.GetRenderer().GetActiveCamera().SetViewUp(1, 0, 0);\r
+                               \r
+                               panel.GetRenderer().ResetCameraClippingRange();\r
+                               \r
+                               panel.UpdateLight();\r
+                               panel.repaint();\r
+                       }\r
+                });\r
+           \r
+            return null;\r
+            \r
+       }\r
+       \r
+       \r
+}\r