]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
Remove dependencies on log4j
[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 org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.Command;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.ui.IWorkbenchPart;
19 import org.eclipse.ui.handlers.HandlerUtil;
20 import org.simantics.g3d.vtk.common.VtkView;
21 import org.simantics.utils.threads.AWTThread;
22 import org.simantics.utils.threads.ThreadUtils;
23
24 import vtk.vtkCamera;
25 import vtk.vtkRenderer;
26
27 public class ParallelPerspectiveHandler extends AbstractHandler {
28
29         
30         @Override
31         public Object execute(ExecutionEvent event) throws ExecutionException {
32                 Command command = event.getCommand();
33                 boolean oldValue = HandlerUtil.toggleCommandState(command);
34                 final boolean activate = !oldValue;
35
36                 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
37                 final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
38
39                 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
40
41                         @Override
42                         public void run() {
43                             vtkRenderer ren = panel.getRenderer();
44                             vtkCamera cam = ren.GetActiveCamera();
45                                 if (activate){
46                                         double distance = cam.GetDistance();
47                                         double angle = cam.GetViewAngle();
48                                         double scale = Math.tan(Math.toRadians(angle / 2)) * distance;
49                                         cam.SetParallelScale(scale);
50                                         cam.SetParallelProjection(1);
51                                         ren.ResetCameraClippingRange();
52                                 } else {
53                                         cam.SetParallelProjection(0);
54                                         ren.ResetCameraClippingRange();
55                                 }
56                                 panel.refresh();
57
58                         }
59                 });
60
61                 return null;
62
63         }
64         
65         
66         
67 }