]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java
218668ec74b5d935c0f48d2c5b59440112649af3
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / CameraPositionHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.vtk.handlers;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.eclipse.core.commands.AbstractHandler;\r
20 import org.eclipse.core.commands.ExecutionEvent;\r
21 import org.eclipse.core.commands.ExecutionException;\r
22 import org.eclipse.ui.IWorkbenchPart;\r
23 import org.eclipse.ui.handlers.HandlerUtil;\r
24 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;\r
25 import org.simantics.utils.threads.AWTThread;\r
26 import org.simantics.utils.threads.ThreadUtils;\r
27 \r
28 public class CameraPositionHandler extends AbstractHandler {\r
29 \r
30         \r
31         private Map<InteractiveVtkPanel,Vector3d> cameraPos = new HashMap<InteractiveVtkPanel, Vector3d>();\r
32         \r
33         @Override\r
34         public Object execute(ExecutionEvent event) throws ExecutionException {\r
35              \r
36              final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);\r
37              final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);\r
38              \r
39              String param = event.getParameter("org.simantics.g3d.viewDirection");\r
40              String vals[] = param.split(",");\r
41              final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2]));\r
42 \r
43              \r
44              ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
45                         \r
46                         @Override\r
47                         public void run() {\r
48 \r
49                                 Vector3d focal = new Vector3d(panel.GetRenderer().GetActiveCamera().GetFocalPoint());\r
50                                 Vector3d pos = new Vector3d(panel.GetRenderer().GetActiveCamera().GetPosition());\r
51                                 cameraPos.put(panel, pos);\r
52                                 Vector3d dir = new Vector3d(pos);\r
53                                 dir.sub(focal);\r
54                                 double distance = dir.length();\r
55                                 \r
56                                 dir.set(direction);\r
57                                 dir.scale(distance);\r
58                                 dir.add(focal);\r
59                                 panel.GetRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);\r
60                                 if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95)\r
61                                         panel.GetRenderer().GetActiveCamera().SetViewUp(0, 1, 0);\r
62                                 else\r
63                                         panel.GetRenderer().GetActiveCamera().SetViewUp(1, 0, 0);\r
64                                 \r
65                                 panel.GetRenderer().ResetCameraClippingRange();\r
66                                 \r
67                                 panel.UpdateLight();\r
68                                 panel.repaint();\r
69                         }\r
70                  });\r
71             \r
72              return null;\r
73              \r
74         }\r
75         \r
76         \r
77 }\r