]> gerrit.simantics Code Review - simantics/3d.git/blob - 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
1 package org.simantics.g3d.vtk.handlers;\r
2 \r
3 import java.util.HashMap;\r
4 import java.util.Map;\r
5 \r
6 import javax.vecmath.Vector3d;\r
7 \r
8 import org.eclipse.core.commands.AbstractHandler;\r
9 import org.eclipse.core.commands.ExecutionEvent;\r
10 import org.eclipse.core.commands.ExecutionException;\r
11 import org.eclipse.ui.IWorkbenchPart;\r
12 import org.eclipse.ui.handlers.HandlerUtil;\r
13 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;\r
14 import org.simantics.utils.threads.AWTThread;\r
15 import org.simantics.utils.threads.ThreadUtils;\r
16 \r
17 public class CameraPositionHandler extends AbstractHandler {\r
18 \r
19         \r
20         private Map<InteractiveVtkPanel,Vector3d> cameraPos = new HashMap<InteractiveVtkPanel, Vector3d>();\r
21         \r
22         @Override\r
23         public Object execute(ExecutionEvent event) throws ExecutionException {\r
24              \r
25              final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);\r
26              final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);\r
27              \r
28              String param = event.getParameter("org.simantics.g3d.viewDirection");\r
29              String vals[] = param.split(",");\r
30              final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2]));\r
31 \r
32              \r
33              ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
34                         \r
35                         @Override\r
36                         public void run() {\r
37 \r
38                                 Vector3d focal = new Vector3d(panel.GetRenderer().GetActiveCamera().GetFocalPoint());\r
39                                 Vector3d pos = new Vector3d(panel.GetRenderer().GetActiveCamera().GetPosition());\r
40                                 cameraPos.put(panel, pos);\r
41                                 Vector3d dir = new Vector3d(pos);\r
42                                 dir.sub(focal);\r
43                                 double distance = dir.length();\r
44                                 \r
45                                 dir.set(direction);\r
46                                 dir.scale(distance);\r
47                                 dir.add(focal);\r
48                                 panel.GetRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);\r
49                                 if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95)\r
50                                         panel.GetRenderer().GetActiveCamera().SetViewUp(0, 1, 0);\r
51                                 else\r
52                                         panel.GetRenderer().GetActiveCamera().SetViewUp(1, 0, 0);\r
53                                 \r
54                                 panel.GetRenderer().ResetCameraClippingRange();\r
55                                 \r
56                                 panel.UpdateLight();\r
57                                 panel.repaint();\r
58                         }\r
59                  });\r
60             \r
61              return null;\r
62              \r
63         }\r
64         \r
65         \r
66 }\r