From: Marko Luukkainen Date: Mon, 27 May 2019 07:54:40 +0000 (+0300) Subject: Adjustments to picking and camera orientation on focus command X-Git-Tag: v1.43.0~260 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=68d82ca06cc8a3ccd57c2eef04bf2460bf766a39;p=simantics%2F3d.git Adjustments to picking and camera orientation on focus command gitlab #5 Change-Id: If199287d5ef83f5d1ad410c8175cc4af67b1e50a --- diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/action/vtkCameraAndSelectorAction.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/action/vtkCameraAndSelectorAction.java index f4bd2a07..b4a387bc 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/action/vtkCameraAndSelectorAction.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/action/vtkCameraAndSelectorAction.java @@ -384,6 +384,11 @@ public class vtkCameraAndSelectorAction extends vtkAction implements ISelectionP public void focus(double x, double y, double z) { Lock(); cam.SetFocalPoint(x, y, z); + if (doNotRotate) + cam.SetRoll(0); + cam.OrthogonalizeViewUp(); + resetCameraClippingRange(); + panel.UpdateLight(); UnLock(); } diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/InteractiveVtkPanel.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/InteractiveVtkPanel.java index 1f0a968c..e6129b5d 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/InteractiveVtkPanel.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/InteractiveVtkPanel.java @@ -1,413 +1,415 @@ -/******************************************************************************* - * 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.common; - -import java.awt.event.KeyEvent; -import java.awt.event.MouseEvent; -import java.util.ArrayList; -import java.util.List; - -import org.simantics.g3d.scenegraph.RenderListener; -import org.simantics.g3d.vtk.action.vtkAction; - -import vtk.vtkAbstractPicker; -import vtk.vtkAreaPicker; -import vtk.vtkAssemblyNode; -import vtk.vtkAssemblyPath; -import vtk.vtkCellPicker; -import vtk.vtkGenericRenderWindowInteractor; -import vtk.vtkInteractorStyleTrackballCamera; -import vtk.vtkObjectBase; -import vtk.vtkPanel; -import vtk.vtkPointPicker; -import vtk.vtkProp; -import vtk.vtkProp3DCollection; -import vtk.vtkPropCollection; -import vtk.vtkPropPicker; -import vtk.vtkScenePicker; - -public class InteractiveVtkPanel extends vtkPanel { - - protected vtkGenericRenderWindowInteractor iren; - - - public vtkGenericRenderWindowInteractor getRenderWindowInteractor() { - return this.iren; - } - private static final long serialVersionUID = 2815073937537950615L; - - - public InteractiveVtkPanel() { - super(); - iren = new vtkGenericRenderWindowInteractor(); - iren.SetRenderWindow(rw); - iren.TimerEventResetsTimerOff(); - iren.SetSize(200, 200); - iren.ConfigureEvent(); - vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera(); - iren.SetInteractorStyle(style); - addDeletable(style); - addDeletable(iren); - } - - @Override - public void mouseClicked(MouseEvent e) { - - } - - @Override - public void mouseMoved(MouseEvent e) { - - } - - @Override - public void mouseEntered(MouseEvent e) { - super.mouseEntered(e); - } - - @Override - public void mouseExited(MouseEvent e) { - - } - - @Override - public void mousePressed(MouseEvent e) { - - } - - @Override - public void mouseDragged(MouseEvent e) { - - } - - @Override - public void mouseReleased(MouseEvent e) { - - } - - @Override - public void keyPressed(KeyEvent e) { - - } - - @Override - public void keyTyped(KeyEvent e) { - - } - - public void setSize(int x, int y) { - if (windowset == 1) { - Lock(); - rw.SetSize(x, y); - iren.SetSize(x, y); - iren.ConfigureEvent(); - UnLock(); - } - } - - private vtkScenePicker scenePicker; - - int pickType = 4; - - public int getPickType() { - return pickType; - } - - public void setPickType(int pickType) { - this.pickType = pickType; - } - - public vtkProp[] pick(int x, int y) { - - -// vtkPicker picker = new vtkPicker(); -// vtkAbstractPicker picker = new vtkAbstractPicker(); -// picker.Pick(x, rw.GetSize()[1] - y, ren); -// // see page 60 of VTK user's guide -// - if (pickType == 0) { - - vtkPropPicker picker = new vtkPropPicker(); - Lock(); - picker.PickProp(x, rw.GetSize()[1] - y, ren); - - UnLock(); - - vtkAssemblyPath apath = picker.GetPath(); - return processPick(picker, apath); - - } else if (pickType == 1) { - if (scenePicker == null) { - scenePicker = new vtkScenePicker(); - scenePicker.SetRenderer(ren); - scenePicker.EnableVertexPickingOn(); - - } - Lock(); - - vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y); - //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y}); - - UnLock(); - - if (apath != null) { - apath.InitTraversal(); -// System.out.println("Pick, actors " + apath.GetNumberOfItems() ); -// for (int i = 0; i < apath.GetNumberOfItems(); i++) { -// vtkAssemblyNode node = apath.GetNextNode(); -// vtkProp test = (vtkProp) node.GetViewProp(); -// System.out.println(test.GetClassName()); -// } - - vtkAssemblyNode node = apath.GetLastNode(); - vtkProp test = (vtkProp) node.GetViewProp(); - apath.Delete(); - node.Delete(); - return new vtkProp[]{test}; - - } - - } else if (pickType == 2) { - vtkPointPicker picker = new vtkPointPicker(); - picker.SetTolerance(2.0/(double)rw.GetSize()[0]); - Lock(); - picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); - UnLock(); - - vtkAssemblyPath apath = picker.GetPath(); - return processPick(picker, apath); - } else if (pickType == 3) { - vtkAreaPicker picker = new vtkAreaPicker(); - Lock(); - picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); - //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren); - UnLock(); - vtkAssemblyPath apath = picker.GetPath(); - return processPick(picker, apath); - } else if (pickType == 4) { - vtkCellPicker picker = new vtkCellPicker(); - picker.SetTolerance(2.0/(double)rw.GetSize()[0]); - Lock(); - picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); - UnLock(); - vtkAssemblyPath apath = picker.GetPath(); - return processPick(picker, apath); - } - - return null; - } - - public vtkProp[] pick2(int x, int y) { - - -// vtkPicker picker = new vtkPicker(); -// vtkAbstractPicker picker = new vtkAbstractPicker(); -// picker.Pick(x, rw.GetSize()[1] - y, ren); -// // see page 60 of VTK user's guide -// - if (pickType == 0) { - - vtkPropPicker picker = new vtkPropPicker(); - Lock(); - picker.PickProp(x, rw.GetSize()[1] - y, ren); - - UnLock(); - vtkPropCollection coll = picker.GetPickList(); - return processPick(picker, coll); - - } else if (pickType == 1) { - if (scenePicker == null) { - scenePicker = new vtkScenePicker(); - scenePicker.SetRenderer(ren); - scenePicker.EnableVertexPickingOn(); - - } - Lock(); - - - vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y); - - UnLock(); - - if (apath != null) { - apath.InitTraversal(); - - - vtkAssemblyNode node = apath.GetLastNode(); - vtkProp test = (vtkProp) node.GetViewProp(); - apath.Delete(); - node.Delete(); - return new vtkProp[]{test}; - - } - - } else if (pickType == 2) { - vtkPointPicker picker = new vtkPointPicker(); - picker.SetTolerance(2.0/(double)rw.GetSize()[0]); - Lock(); - picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); - UnLock(); - vtkProp3DCollection coll = picker.GetProp3Ds(); - return processPick(picker, coll); - } else if (pickType == 3) { - vtkAreaPicker picker = new vtkAreaPicker(); - Lock(); - picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); - //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren); - UnLock(); - vtkProp3DCollection coll = picker.GetProp3Ds(); - return processPick(picker, coll); - } else if (pickType == 4) { - vtkCellPicker picker = new vtkCellPicker(); - picker.SetTolerance(2.0/(double)rw.GetSize()[0]); - Lock(); - picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); - UnLock(); - vtkProp3DCollection coll = picker.GetProp3Ds(); - return processPick(picker, coll); - } - - return null; - } - - private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) { -// double[] pickPos = picker.GetPickPosition(); - picker.Delete(); - if (apath != null) { - apath.InitTraversal(); - vtkProp result[] = new vtkProp[apath.GetNumberOfItems()]; - for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) { - vtkAssemblyNode node = apath.GetNextNode(); - vtkProp test = (vtkProp) node.GetViewProp(); -// System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId()); - result[i] = test; - node.Delete(); - } - apath.Delete(); - return result; - - } - return null; - } - - private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) { -// double[] pickPos = picker.GetPickPosition(); - picker.Delete(); - if (coll != null) { - coll.InitTraversal(); - vtkProp result[] = new vtkProp[coll.GetNumberOfItems()]; - for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) { - vtkProp test = coll.GetNextProp(); - -// System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId()); - result[i] = test; - - } - coll.Delete(); - return result; - - } - return null; - } - - - private vtkAction defaultAction; - private vtkAction currentAction; - - public void setActiveAction(vtkAction action) { - if (action.equals(currentAction)) - return; - if (currentAction != null) - currentAction.deattach(); - currentAction = action; - if (action != null) - action.attach(); - } - - public void setDefaultAction(vtkAction defaultAction) { - this.defaultAction = defaultAction; - } - - public void useDefaultAction() { - setActiveAction(defaultAction); - } - - public vtkAction getDefaultAction() { - return defaultAction; - } - - - @Override - public synchronized void Render() { - //System.out.println("Render " + rendering); - if (rendering) - return; - - firePreRender(); - super.Render(); - firePostRender(); - } - - - public void addListener(RenderListener l) { - listeners.add(l); - } - - public void removeListener(RenderListener l) { - listeners.remove(l); - } - - private List listeners = new ArrayList(); - - List list = new ArrayList(); - - private void firePreRender() { - if (listeners.size() > 0) { - list.addAll(listeners); - for (RenderListener l : list) - l.preRender(); - list.clear(); - } - } - - private void firePostRender() { - if (listeners.size() > 0) { - list.addAll(listeners); - for (RenderListener l : list) - l.postRender(); - list.clear(); - } - } - - - private List deletable = new ArrayList(); - - public void addDeletable(vtkObjectBase o) { - deletable.add(o); - } - - public void removeDeletable (vtkObjectBase o) { - deletable.remove(o); - } - - @Override - public void Delete() { - for (vtkObjectBase o : deletable) { - if (o.GetVTKId() != 0) { - o.Delete(); - } - } - deletable.clear(); - - super.Delete(); - } -} +/******************************************************************************* + * 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.common; + +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.List; + +import org.simantics.g3d.scenegraph.RenderListener; +import org.simantics.g3d.vtk.action.vtkAction; + +import vtk.vtkAbstractPicker; +import vtk.vtkAreaPicker; +import vtk.vtkAssemblyNode; +import vtk.vtkAssemblyPath; +import vtk.vtkCellPicker; +import vtk.vtkGenericRenderWindowInteractor; +import vtk.vtkInteractorStyleTrackballCamera; +import vtk.vtkObjectBase; +import vtk.vtkPanel; +import vtk.vtkPointPicker; +import vtk.vtkProp; +import vtk.vtkProp3DCollection; +import vtk.vtkPropCollection; +import vtk.vtkPropPicker; +import vtk.vtkScenePicker; + +public class InteractiveVtkPanel extends vtkPanel { + + protected vtkGenericRenderWindowInteractor iren; + + + public vtkGenericRenderWindowInteractor getRenderWindowInteractor() { + return this.iren; + } + private static final long serialVersionUID = 2815073937537950615L; + + + public InteractiveVtkPanel() { + super(); + iren = new vtkGenericRenderWindowInteractor(); + iren.SetRenderWindow(rw); + iren.TimerEventResetsTimerOff(); + iren.SetSize(200, 200); + iren.ConfigureEvent(); + vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera(); + iren.SetInteractorStyle(style); + addDeletable(style); + addDeletable(iren); + } + + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mouseMoved(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + super.mouseEntered(e); + } + + @Override + public void mouseExited(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseDragged(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyTyped(KeyEvent e) { + + } + + public void setSize(int x, int y) { + if (windowset == 1) { + Lock(); + rw.SetSize(x, y); + iren.SetSize(x, y); + iren.ConfigureEvent(); + UnLock(); + } + } + + private vtkScenePicker scenePicker; + + int pickType = 4; + + public int getPickType() { + return pickType; + } + + public void setPickType(int pickType) { + this.pickType = pickType; + } + + public vtkProp[] pick(int x, int y) { + + +// vtkPicker picker = new vtkPicker(); +// vtkAbstractPicker picker = new vtkAbstractPicker(); +// picker.Pick(x, rw.GetSize()[1] - y, ren); +// // see page 60 of VTK user's guide +// + if (pickType == 0) { + + vtkPropPicker picker = new vtkPropPicker(); + Lock(); + picker.PickProp(x, rw.GetSize()[1] - y, ren); + + UnLock(); + + vtkAssemblyPath apath = picker.GetPath(); + return processPick(picker, apath); + + } else if (pickType == 1) { + if (scenePicker == null) { + scenePicker = new vtkScenePicker(); + scenePicker.SetRenderer(ren); + scenePicker.EnableVertexPickingOn(); + + } + Lock(); + + vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y); + //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y}); + + UnLock(); + + if (apath != null) { + apath.InitTraversal(); +// System.out.println("Pick, actors " + apath.GetNumberOfItems() ); +// for (int i = 0; i < apath.GetNumberOfItems(); i++) { +// vtkAssemblyNode node = apath.GetNextNode(); +// vtkProp test = (vtkProp) node.GetViewProp(); +// System.out.println(test.GetClassName()); +// } + + vtkAssemblyNode node = apath.GetLastNode(); + vtkProp test = (vtkProp) node.GetViewProp(); + apath.Delete(); + node.Delete(); + return new vtkProp[]{test}; + + } + + } else if (pickType == 2) { + vtkPointPicker picker = new vtkPointPicker(); + //picker.SetTolerance(2.0/(double)rw.GetSize()[0]); + picker.SetTolerance(0.00001); + Lock(); + picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); + UnLock(); + + vtkAssemblyPath apath = picker.GetPath(); + return processPick(picker, apath); + } else if (pickType == 3) { + vtkAreaPicker picker = new vtkAreaPicker(); + Lock(); + picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); + //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren); + UnLock(); + vtkAssemblyPath apath = picker.GetPath(); + return processPick(picker, apath); + } else if (pickType == 4) { + vtkCellPicker picker = new vtkCellPicker(); + //picker.SetTolerance(2.0/(double)rw.GetSize()[0]); + picker.SetTolerance(0.00001); + Lock(); + picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); + UnLock(); + vtkAssemblyPath apath = picker.GetPath(); + return processPick(picker, apath); + } + + return null; + } + + public vtkProp[] pick2(int x, int y) { + + +// vtkPicker picker = new vtkPicker(); +// vtkAbstractPicker picker = new vtkAbstractPicker(); +// picker.Pick(x, rw.GetSize()[1] - y, ren); +// // see page 60 of VTK user's guide +// + if (pickType == 0) { + + vtkPropPicker picker = new vtkPropPicker(); + Lock(); + picker.PickProp(x, rw.GetSize()[1] - y, ren); + + UnLock(); + vtkPropCollection coll = picker.GetPickList(); + return processPick(picker, coll); + + } else if (pickType == 1) { + if (scenePicker == null) { + scenePicker = new vtkScenePicker(); + scenePicker.SetRenderer(ren); + scenePicker.EnableVertexPickingOn(); + + } + Lock(); + + + vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y); + + UnLock(); + + if (apath != null) { + apath.InitTraversal(); + + + vtkAssemblyNode node = apath.GetLastNode(); + vtkProp test = (vtkProp) node.GetViewProp(); + apath.Delete(); + node.Delete(); + return new vtkProp[]{test}; + + } + + } else if (pickType == 2) { + vtkPointPicker picker = new vtkPointPicker(); + picker.SetTolerance(2.0/(double)rw.GetSize()[0]); + Lock(); + picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); + UnLock(); + vtkProp3DCollection coll = picker.GetProp3Ds(); + return processPick(picker, coll); + } else if (pickType == 3) { + vtkAreaPicker picker = new vtkAreaPicker(); + Lock(); + picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); + //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren); + UnLock(); + vtkProp3DCollection coll = picker.GetProp3Ds(); + return processPick(picker, coll); + } else if (pickType == 4) { + vtkCellPicker picker = new vtkCellPicker(); + picker.SetTolerance(2.0/(double)rw.GetSize()[0]); + Lock(); + picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren); + UnLock(); + vtkProp3DCollection coll = picker.GetProp3Ds(); + return processPick(picker, coll); + } + + return null; + } + + private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) { +// double[] pickPos = picker.GetPickPosition(); + picker.Delete(); + if (apath != null) { + apath.InitTraversal(); + vtkProp result[] = new vtkProp[apath.GetNumberOfItems()]; + for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) { + vtkAssemblyNode node = apath.GetNextNode(); + vtkProp test = (vtkProp) node.GetViewProp(); +// System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId()); + result[i] = test; + node.Delete(); + } + apath.Delete(); + return result; + + } + return null; + } + + private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) { +// double[] pickPos = picker.GetPickPosition(); + picker.Delete(); + if (coll != null) { + coll.InitTraversal(); + vtkProp result[] = new vtkProp[coll.GetNumberOfItems()]; + for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) { + vtkProp test = coll.GetNextProp(); + +// System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId()); + result[i] = test; + + } + coll.Delete(); + return result; + + } + return null; + } + + + private vtkAction defaultAction; + private vtkAction currentAction; + + public void setActiveAction(vtkAction action) { + if (action.equals(currentAction)) + return; + if (currentAction != null) + currentAction.deattach(); + currentAction = action; + if (action != null) + action.attach(); + } + + public void setDefaultAction(vtkAction defaultAction) { + this.defaultAction = defaultAction; + } + + public void useDefaultAction() { + setActiveAction(defaultAction); + } + + public vtkAction getDefaultAction() { + return defaultAction; + } + + + @Override + public synchronized void Render() { + //System.out.println("Render " + rendering); + if (rendering) + return; + + firePreRender(); + super.Render(); + firePostRender(); + } + + + public void addListener(RenderListener l) { + listeners.add(l); + } + + public void removeListener(RenderListener l) { + listeners.remove(l); + } + + private List listeners = new ArrayList(); + + List list = new ArrayList(); + + private void firePreRender() { + if (listeners.size() > 0) { + list.addAll(listeners); + for (RenderListener l : list) + l.preRender(); + list.clear(); + } + } + + private void firePostRender() { + if (listeners.size() > 0) { + list.addAll(listeners); + for (RenderListener l : list) + l.postRender(); + list.clear(); + } + } + + + private List deletable = new ArrayList(); + + public void addDeletable(vtkObjectBase o) { + deletable.add(o); + } + + public void removeDeletable (vtkObjectBase o) { + deletable.remove(o); + } + + @Override + public void Delete() { + for (vtkObjectBase o : deletable) { + if (o.GetVTKId() != 0) { + o.Delete(); + } + } + deletable.clear(); + + super.Delete(); + } +}