]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
Perform view direction switching via vtkCameraAndSelectorAction
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / ParallelPerspectiveHandler.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 javax.vecmath.Vector3d;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.Command;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22 import org.simantics.g3d.math.MathTools;
23 import org.simantics.g3d.vtk.common.VtkView;
24 import org.simantics.utils.threads.AWTThread;
25 import org.simantics.utils.threads.ThreadUtils;
26
27 import vtk.vtkCamera;
28 import vtk.vtkRenderer;
29
30 public class ParallelPerspectiveHandler extends AbstractHandler {
31
32         
33         @Override
34         public Object execute(ExecutionEvent event) throws ExecutionException {
35                 Command command = event.getCommand();
36                 boolean oldValue = HandlerUtil.toggleCommandState(command);
37                 final boolean activate = !oldValue;
38
39                 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
40                 final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
41
42                 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
43
44                         @Override
45                         public void run() {
46                             vtkRenderer ren = panel.getRenderer();
47                             vtkCamera cam = ren.GetActiveCamera();
48                                 if (activate){
49
50                                         Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
51                                         Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
52                                         double dist = MathTools.distance(pos, focal);
53                                         cam.SetParallelScale(dist/4.0);
54                                         // camera must be moved backwards, or graphics get clipped when parallel view is zoomed out
55                                         // TODO : is there a better way to do this?
56                                         Vector3d dir = new Vector3d(pos);
57                                         dir.sub(focal);
58                                         dir.normalize();
59                                         dir.scale(100);
60                                         dir.add(focal);
61                                         cam.SetPosition(dir.x, dir.y, dir.z);
62
63                                         cam.SetParallelProjection(1);
64                                         ren.ResetCameraClippingRange();
65                                 } else {
66                                     
67                                     double scale = cam.GetParallelScale();
68                                         cam.SetParallelProjection(0);
69                                         
70                                         Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
71                     Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
72                     Vector3d dir = new Vector3d(pos);
73                     dir.sub(focal);
74                     dir.normalize();
75                     dir.scale(scale*4.0);
76                     dir.add(focal);
77                     cam.SetPosition(dir.x, dir.y, dir.z);
78                                         ren.ResetCameraClippingRange();
79
80                                 }
81                                 panel.refresh();
82
83                         }
84                 });
85
86                 return null;
87
88         }
89         
90         
91         
92 }