1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.handlers;
14 import java.util.HashMap;
17 import javax.vecmath.Vector3d;
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.handlers.HandlerUtil;
24 import org.simantics.g3d.vtk.action.vtkAction;
25 import org.simantics.g3d.vtk.common.VtkView;
26 import org.simantics.g3d.vtk.swt.vtkCameraAndSelectorAction;
27 import org.simantics.utils.threads.ThreadUtils;
29 public class CameraPositionHandler extends AbstractHandler {
32 private Map<VtkView,Vector3d> cameraPos = new HashMap<VtkView, Vector3d>();
35 public Object execute(ExecutionEvent event) throws ExecutionException {
37 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
38 final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
40 String param = event.getParameter("org.simantics.g3d.viewDirection");
41 String vals[] = param.split(",");
42 final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2]));
45 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
50 Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
51 Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
52 cameraPos.put(panel, pos);
53 Vector3d dir = new Vector3d(pos);
55 double distance = dir.length();
61 panel.getRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);
63 vtkAction action = panel.getDefaultAction();
64 if (action instanceof vtkCameraAndSelectorAction)
65 ((vtkCameraAndSelectorAction)action).focus(focal.x, focal.y, focal.z);
67 if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95)
68 panel.getRenderer().GetActiveCamera().SetViewUp(0, 1, 0);
70 panel.getRenderer().GetActiveCamera().SetViewUp(1, 0, 0);
72 panel.getRenderer().ResetCameraClippingRange();
75 //panel.UpdateLight();