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.common;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.MouseEvent;
16 import java.util.ArrayList;
17 import java.util.List;
19 import org.simantics.g3d.scenegraph.RenderListener;
20 import org.simantics.g3d.vtk.action.vtkAction;
22 import vtk.vtkAbstractPicker;
23 import vtk.vtkAreaPicker;
24 import vtk.vtkAssemblyNode;
25 import vtk.vtkAssemblyPath;
26 import vtk.vtkCellPicker;
27 import vtk.vtkGenericRenderWindowInteractor;
28 import vtk.vtkInteractorStyleTrackballCamera;
29 import vtk.vtkObjectBase;
31 import vtk.vtkPointPicker;
33 import vtk.vtkProp3DCollection;
34 import vtk.vtkPropCollection;
35 import vtk.vtkPropPicker;
36 import vtk.vtkScenePicker;
38 public class InteractiveVtkPanel extends vtkPanel {
40 protected vtkGenericRenderWindowInteractor iren;
43 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
46 private static final long serialVersionUID = 2815073937537950615L;
49 public InteractiveVtkPanel() {
51 iren = new vtkGenericRenderWindowInteractor();
52 iren.SetRenderWindow(rw);
53 iren.TimerEventResetsTimerOff();
54 iren.SetSize(200, 200);
55 iren.ConfigureEvent();
56 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
57 iren.SetInteractorStyle(style);
63 public void mouseClicked(MouseEvent e) {
68 public void mouseMoved(MouseEvent e) {
73 public void mouseEntered(MouseEvent e) {
74 super.mouseEntered(e);
78 public void mouseExited(MouseEvent e) {
83 public void mousePressed(MouseEvent e) {
88 public void mouseDragged(MouseEvent e) {
93 public void mouseReleased(MouseEvent e) {
98 public void keyPressed(KeyEvent e) {
103 public void keyTyped(KeyEvent e) {
107 public void setSize(int x, int y) {
108 if (windowset == 1) {
112 iren.ConfigureEvent();
117 private vtkScenePicker scenePicker;
121 public int getPickType() {
125 public void setPickType(int pickType) {
126 this.pickType = pickType;
129 public vtkProp[] pick(int x, int y) {
132 // vtkPicker picker = new vtkPicker();
133 // vtkAbstractPicker picker = new vtkAbstractPicker();
134 // picker.Pick(x, rw.GetSize()[1] - y, ren);
135 // // see page 60 of VTK user's guide
139 vtkPropPicker picker = new vtkPropPicker();
141 picker.PickProp(x, rw.GetSize()[1] - y, ren);
145 vtkAssemblyPath apath = picker.GetPath();
146 return processPick(picker, apath);
148 } else if (pickType == 1) {
149 if (scenePicker == null) {
150 scenePicker = new vtkScenePicker();
151 scenePicker.SetRenderer(ren);
152 scenePicker.EnableVertexPickingOn();
157 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
158 //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y});
163 apath.InitTraversal();
164 // System.out.println("Pick, actors " + apath.GetNumberOfItems() );
165 // for (int i = 0; i < apath.GetNumberOfItems(); i++) {
166 // vtkAssemblyNode node = apath.GetNextNode();
167 // vtkProp test = (vtkProp) node.GetViewProp();
168 // System.out.println(test.GetClassName());
171 vtkAssemblyNode node = apath.GetLastNode();
172 vtkProp test = (vtkProp) node.GetViewProp();
175 return new vtkProp[]{test};
179 } else if (pickType == 2) {
180 vtkPointPicker picker = new vtkPointPicker();
181 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
182 picker.SetTolerance(0.00001);
184 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
187 vtkAssemblyPath apath = picker.GetPath();
188 return processPick(picker, apath);
189 } else if (pickType == 3) {
190 vtkAreaPicker picker = new vtkAreaPicker();
192 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
193 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
195 vtkAssemblyPath apath = picker.GetPath();
196 return processPick(picker, apath);
197 } else if (pickType == 4) {
198 vtkCellPicker picker = new vtkCellPicker();
199 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
200 picker.SetTolerance(0.00001);
202 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
204 vtkAssemblyPath apath = picker.GetPath();
205 return processPick(picker, apath);
211 public vtkProp[] pick2(int x, int y) {
214 // vtkPicker picker = new vtkPicker();
215 // vtkAbstractPicker picker = new vtkAbstractPicker();
216 // picker.Pick(x, rw.GetSize()[1] - y, ren);
217 // // see page 60 of VTK user's guide
221 vtkPropPicker picker = new vtkPropPicker();
223 picker.PickProp(x, rw.GetSize()[1] - y, ren);
226 vtkPropCollection coll = picker.GetPickList();
227 return processPick(picker, coll);
229 } else if (pickType == 1) {
230 if (scenePicker == null) {
231 scenePicker = new vtkScenePicker();
232 scenePicker.SetRenderer(ren);
233 scenePicker.EnableVertexPickingOn();
239 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
244 apath.InitTraversal();
247 vtkAssemblyNode node = apath.GetLastNode();
248 vtkProp test = (vtkProp) node.GetViewProp();
251 return new vtkProp[]{test};
255 } else if (pickType == 2) {
256 vtkPointPicker picker = new vtkPointPicker();
257 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
259 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
261 vtkProp3DCollection coll = picker.GetProp3Ds();
262 return processPick(picker, coll);
263 } else if (pickType == 3) {
264 vtkAreaPicker picker = new vtkAreaPicker();
266 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
267 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
269 vtkProp3DCollection coll = picker.GetProp3Ds();
270 return processPick(picker, coll);
271 } else if (pickType == 4) {
272 vtkCellPicker picker = new vtkCellPicker();
273 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
275 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
277 vtkProp3DCollection coll = picker.GetProp3Ds();
278 return processPick(picker, coll);
284 private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
285 // double[] pickPos = picker.GetPickPosition();
288 apath.InitTraversal();
289 vtkProp result[] = new vtkProp[apath.GetNumberOfItems()];
290 for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) {
291 vtkAssemblyNode node = apath.GetNextNode();
292 vtkProp test = (vtkProp) node.GetViewProp();
293 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
304 private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
305 // double[] pickPos = picker.GetPickPosition();
308 coll.InitTraversal();
309 vtkProp result[] = new vtkProp[coll.GetNumberOfItems()];
310 for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) {
311 vtkProp test = coll.GetNextProp();
313 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
325 private vtkAction defaultAction;
326 private vtkAction currentAction;
328 public void setActiveAction(vtkAction action) {
329 if (action.equals(currentAction))
331 if (currentAction != null)
332 currentAction.deattach();
333 currentAction = action;
338 public void setDefaultAction(vtkAction defaultAction) {
339 this.defaultAction = defaultAction;
342 public void useDefaultAction() {
343 setActiveAction(defaultAction);
346 public vtkAction getDefaultAction() {
347 return defaultAction;
352 public synchronized void Render() {
353 //System.out.println("Render " + rendering);
363 public void addListener(RenderListener l) {
367 public void removeListener(RenderListener l) {
371 private List<RenderListener> listeners = new ArrayList<RenderListener>();
373 List<RenderListener> list = new ArrayList<RenderListener>();
375 private void firePreRender() {
376 if (listeners.size() > 0) {
377 list.addAll(listeners);
378 for (RenderListener l : list)
384 private void firePostRender() {
385 if (listeners.size() > 0) {
386 list.addAll(listeners);
387 for (RenderListener l : list)
394 private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
396 public void addDeletable(vtkObjectBase o) {
400 public void removeDeletable (vtkObjectBase o) {
405 public void Delete() {
406 for (vtkObjectBase o : deletable) {
407 if (o.GetVTKId() != 0) {