X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fcommon%2FInteractiveVtkPanel.java;fp=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fcommon%2FInteractiveVtkPanel.java;h=40fe6c67b45763e2cec7e5cfa54e758f430d8c00;hb=87b3241ec277ba3d8e414b26186a032c9cdcaeed;hp=0000000000000000000000000000000000000000;hpb=1f0bcd66274375f2278d2e6c486cb28257a5f7b2;p=simantics%2F3d.git 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 new file mode 100644 index 00000000..40fe6c67 --- /dev/null +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/InteractiveVtkPanel.java @@ -0,0 +1,402 @@ +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(); + } +}