1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.vtk.handlers;
\r
14 import java.util.HashMap;
\r
15 import java.util.Map;
\r
17 import javax.vecmath.Vector3d;
\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
28 public class CameraPositionHandler extends AbstractHandler {
\r
31 private Map<InteractiveVtkPanel,Vector3d> cameraPos = new HashMap<InteractiveVtkPanel, Vector3d>();
\r
34 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
36 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
\r
37 final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class);
\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
44 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
\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
54 double distance = dir.length();
\r
57 dir.scale(distance);
\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
63 panel.GetRenderer().GetActiveCamera().SetViewUp(1, 0, 0);
\r
65 panel.GetRenderer().ResetCameraClippingRange();
\r
67 panel.UpdateLight();
\r