]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java
Using SWT thread with Plant3d
[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.common.VtkView;
25 import org.simantics.utils.threads.ThreadUtils;
26
27 public class CameraPositionHandler extends AbstractHandler {
28
29         
30         private Map<VtkView,Vector3d> cameraPos = new HashMap<VtkView, Vector3d>();
31         
32         @Override
33         public Object execute(ExecutionEvent event) throws ExecutionException {
34              
35              final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
36              final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
37              
38              String param = event.getParameter("org.simantics.g3d.viewDirection");
39              String vals[] = param.split(",");
40              final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2]));
41
42              
43              ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
44                         
45                         @Override
46                         public void run() {
47
48                                 Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
49                                 Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
50                                 cameraPos.put(panel, pos);
51                                 Vector3d dir = new Vector3d(pos);
52                                 dir.sub(focal);
53                                 double distance = dir.length();
54                                 
55                                 dir.set(direction);
56                                 dir.scale(distance);
57                                 dir.add(focal);
58                                 panel.getRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z);
59                                 if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95)
60                                         panel.getRenderer().GetActiveCamera().SetViewUp(0, 1, 0);
61                                 else
62                                         panel.getRenderer().GetActiveCamera().SetViewUp(1, 0, 0);
63                                 
64                                 panel.getRenderer().ResetCameraClippingRange();
65                                 
66                                 //panel.UpdateLight();
67                                 panel.refresh();
68                         }
69                  });
70             
71              return null;
72              
73         }
74         
75         
76 }