1 package org.simantics.g3d.vtk.handlers;
\r
3 import java.util.HashMap;
\r
4 import java.util.Map;
\r
6 import javax.vecmath.Vector3d;
\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
17 public class CameraPositionHandler extends AbstractHandler {
\r
20 private Map<InteractiveVtkPanel,Vector3d> cameraPos = new HashMap<InteractiveVtkPanel, Vector3d>();
\r
23 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
25 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
\r
26 final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);
\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
33 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
\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
43 double distance = dir.length();
\r
46 dir.scale(distance);
\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
52 panel.GetRenderer().GetActiveCamera().SetViewUp(1, 0, 0);
\r
54 panel.GetRenderer().ResetCameraClippingRange();
\r
56 panel.UpdateLight();
\r