]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java
Perform view direction switching via vtkCameraAndSelectorAction
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / CameraPositionHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.vtk.handlers;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import javax.vecmath.Vector3d;
18
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;
28
29 public class CameraPositionHandler extends AbstractHandler {
30
31         
32         private Map<VtkView,Vector3d> cameraPos = new HashMap<VtkView, Vector3d>();
33         
34         @Override
35         public Object execute(ExecutionEvent event) throws ExecutionException {
36
37                 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
38                 final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
39
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]));
43
44
45                 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
46
47                         @Override
48                         public void run() {
49
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);
54                                 dir.sub(focal);
55                                 double distance = dir.length();
56
57                                 dir.set(direction);
58                                 dir.scale(distance);
59                                 dir.add(focal);
60                                 
61                                 panel.getRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);
62                                 
63                                 vtkAction action = panel.getDefaultAction();
64                                 if (action instanceof vtkCameraAndSelectorAction)
65                                         ((vtkCameraAndSelectorAction)action).focus(focal.x, focal.y, focal.z);
66                                 else {
67                                         if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95)
68                                                 panel.getRenderer().GetActiveCamera().SetViewUp(0, 1, 0);
69                                         else
70                                                 panel.getRenderer().GetActiveCamera().SetViewUp(1, 0, 0);
71
72                                         panel.getRenderer().ResetCameraClippingRange();
73                                 }
74                                 
75                                 //panel.UpdateLight();
76                                 panel.refresh();
77                         }
78                 });
79
80                 return null;
81
82         }
83
84         
85 }