X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fhandlers%2FCameraPositionHandler.java;h=e3697be2a8295992bb63adb23e05f3bd764a6ad1;hb=efb52587babd1b858c8e77fedc44a5b4ef0c1088;hp=da31a243ede4b982a6f224dce5188c7b3b441f61;hpb=87b3241ec277ba3d8e414b26186a032c9cdcaeed;p=simantics%2F3d.git diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java index da31a243..e3697be2 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/CameraPositionHandler.java @@ -1,66 +1,85 @@ -package org.simantics.g3d.vtk.handlers; - -import java.util.HashMap; -import java.util.Map; - -import javax.vecmath.Vector3d; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.g3d.vtk.common.InteractiveVtkPanel; -import org.simantics.utils.threads.AWTThread; -import org.simantics.utils.threads.ThreadUtils; - -public class CameraPositionHandler extends AbstractHandler { - - - private Map cameraPos = new HashMap(); - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - - final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event); - final InteractiveVtkPanel panel = (InteractiveVtkPanel)ap.getAdapter(InteractiveVtkPanel.class); - - String param = event.getParameter("org.simantics.g3d.viewDirection"); - String vals[] = param.split(","); - final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2])); - - - ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { - - @Override - public void run() { - - Vector3d focal = new Vector3d(panel.GetRenderer().GetActiveCamera().GetFocalPoint()); - Vector3d pos = new Vector3d(panel.GetRenderer().GetActiveCamera().GetPosition()); - cameraPos.put(panel, pos); - Vector3d dir = new Vector3d(pos); - dir.sub(focal); - double distance = dir.length(); - - dir.set(direction); - dir.scale(distance); - dir.add(focal); - panel.GetRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z); - if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95) - panel.GetRenderer().GetActiveCamera().SetViewUp(0, 1, 0); - else - panel.GetRenderer().GetActiveCamera().SetViewUp(1, 0, 0); - - panel.GetRenderer().ResetCameraClippingRange(); - - panel.UpdateLight(); - panel.repaint(); - } - }); - - return null; - - } - - -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g3d.vtk.handlers; + +import java.util.HashMap; +import java.util.Map; + +import javax.vecmath.Vector3d; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.g3d.vtk.action.vtkAction; +import org.simantics.g3d.vtk.common.VtkView; +import org.simantics.g3d.vtk.swt.vtkCameraAndSelectorAction; +import org.simantics.utils.threads.ThreadUtils; + +public class CameraPositionHandler extends AbstractHandler { + + + private Map cameraPos = new HashMap(); + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + final IWorkbenchPart ap = HandlerUtil.getActiveEditor(event); + final VtkView panel = (VtkView)ap.getAdapter(VtkView.class); + + String param = event.getParameter("org.simantics.g3d.viewDirection"); + String vals[] = param.split(","); + final Vector3d direction = new Vector3d(Double.parseDouble(vals[0]),Double.parseDouble(vals[1]),Double.parseDouble(vals[2])); + + + ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() { + + @Override + public void run() { + + Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint()); + Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition()); + cameraPos.put(panel, pos); + Vector3d dir = new Vector3d(pos); + dir.sub(focal); + double distance = dir.length(); + + dir.set(direction); + dir.scale(distance); + dir.add(focal); + + panel.getRenderer().GetActiveCamera().SetPosition(dir.x, dir.y, dir.z); + + vtkAction action = panel.getDefaultAction(); + if (action instanceof vtkCameraAndSelectorAction) + ((vtkCameraAndSelectorAction)action).focus(focal.x, focal.y, focal.z); + else { + if (Math.abs(direction.dot(new Vector3d(0,1,0))) < 0.95) + panel.getRenderer().GetActiveCamera().SetViewUp(0, 1, 0); + else + panel.getRenderer().GetActiveCamera().SetViewUp(1, 0, 0); + + panel.getRenderer().ResetCameraClippingRange(); + } + + //panel.UpdateLight(); + panel.refresh(); + } + }); + + return null; + + } + + +}