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 javax.vecmath.Vector3d;
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;
28 import vtk.vtkRenderer;
30 public class ParallelPerspectiveHandler extends AbstractHandler {
34 public Object execute(ExecutionEvent event) throws ExecutionException {
35 Command command = event.getCommand();
36 boolean oldValue = HandlerUtil.toggleCommandState(command);
37 final boolean activate = !oldValue;
39 final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event);
40 final VtkView panel = (VtkView)ap.getAdapter(VtkView.class);
42 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
46 vtkRenderer ren = panel.getRenderer();
47 vtkCamera cam = ren.GetActiveCamera();
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);
61 cam.SetPosition(dir.x, dir.y, dir.z);
63 cam.SetParallelProjection(1);
64 ren.ResetCameraClippingRange();
67 double scale = cam.GetParallelScale();
68 cam.SetParallelProjection(0);
70 Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
71 Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
72 Vector3d dir = new Vector3d(pos);
77 cam.SetPosition(dir.x, dir.y, dir.z);
78 ren.ResetCameraClippingRange();