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.awt;
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;
21 import org.simantics.g3d.vtk.common.VtkView;
22 import org.simantics.utils.threads.AWTThread;
23 import org.simantics.utils.threads.IThreadWorkQueue;
25 import vtk.vtkAbstractPicker;
26 import vtk.vtkAreaPicker;
27 import vtk.vtkAssemblyNode;
28 import vtk.vtkAssemblyPath;
29 import vtk.vtkCellPicker;
30 import vtk.vtkGenericRenderWindowInteractor;
31 import vtk.vtkInteractorStyleTrackballCamera;
33 import vtk.vtkObjectBase;
35 import vtk.vtkPointPicker;
37 import vtk.vtkProp3DCollection;
38 import vtk.vtkPropCollection;
39 import vtk.vtkPropPicker;
40 import vtk.vtkRenderer;
41 import vtk.vtkScenePicker;
43 public class InteractiveVtkPanel extends vtkPanel implements VtkView {
45 protected vtkGenericRenderWindowInteractor iren;
48 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
51 private static final long serialVersionUID = 2815073937537950615L;
54 public InteractiveVtkPanel() {
57 // This is actually a static method in C++
58 new vtkObject().GlobalWarningDisplayOff();
60 iren = new vtkGenericRenderWindowInteractor();
61 iren.SetRenderWindow(rw);
62 iren.TimerEventResetsTimerOff();
63 iren.SetSize(200, 200);
64 iren.ConfigureEvent();
65 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
66 iren.SetInteractorStyle(style);
72 public IThreadWorkQueue getThreadQueue() {
73 return AWTThread.getThreadAccess();
77 public vtkRenderer getRenderer() {
82 public void mouseClicked(MouseEvent e) {
87 public void mouseMoved(MouseEvent e) {
92 public void mouseEntered(MouseEvent e) {
93 super.mouseEntered(e);
97 public void mouseExited(MouseEvent e) {
102 public void mousePressed(MouseEvent e) {
107 public void mouseDragged(MouseEvent e) {
112 public void mouseReleased(MouseEvent e) {
117 public void keyPressed(KeyEvent e) {
122 public void keyTyped(KeyEvent e) {
126 public void setSize(int x, int y) {
127 if (windowset == 1) {
131 iren.ConfigureEvent();
136 private vtkScenePicker scenePicker;
140 public int getPickType() {
144 public void setPickType(int pickType) {
145 this.pickType = pickType;
148 public vtkProp[] pick(int x, int y) {
151 // vtkPicker picker = new vtkPicker();
152 // vtkAbstractPicker picker = new vtkAbstractPicker();
153 // picker.Pick(x, rw.GetSize()[1] - y, ren);
154 // // see page 60 of VTK user's guide
158 vtkPropPicker picker = new vtkPropPicker();
160 picker.PickProp(x, rw.GetSize()[1] - y, ren);
164 vtkAssemblyPath apath = picker.GetPath();
165 return processPick(picker, apath);
167 } else if (pickType == 1) {
168 if (scenePicker == null) {
169 scenePicker = new vtkScenePicker();
170 scenePicker.SetRenderer(ren);
171 scenePicker.EnableVertexPickingOn();
176 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
177 //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y});
182 apath.InitTraversal();
183 // System.out.println("Pick, actors " + apath.GetNumberOfItems() );
184 // for (int i = 0; i < apath.GetNumberOfItems(); i++) {
185 // vtkAssemblyNode node = apath.GetNextNode();
186 // vtkProp test = (vtkProp) node.GetViewProp();
187 // System.out.println(test.GetClassName());
190 vtkAssemblyNode node = apath.GetLastNode();
191 vtkProp test = (vtkProp) node.GetViewProp();
194 return new vtkProp[]{test};
198 } else if (pickType == 2) {
199 vtkPointPicker picker = new vtkPointPicker();
200 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
201 picker.SetTolerance(0.00001);
203 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
206 vtkAssemblyPath apath = picker.GetPath();
207 return processPick(picker, apath);
208 } else if (pickType == 3) {
209 vtkAreaPicker picker = new vtkAreaPicker();
211 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
212 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
214 vtkAssemblyPath apath = picker.GetPath();
215 return processPick(picker, apath);
216 } else if (pickType == 4) {
217 vtkCellPicker picker = new vtkCellPicker();
218 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
219 picker.SetTolerance(0.00001);
221 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
223 vtkAssemblyPath apath = picker.GetPath();
224 return processPick(picker, apath);
230 public vtkProp[] pick2(int x, int y) {
233 // vtkPicker picker = new vtkPicker();
234 // vtkAbstractPicker picker = new vtkAbstractPicker();
235 // picker.Pick(x, rw.GetSize()[1] - y, ren);
236 // // see page 60 of VTK user's guide
240 vtkPropPicker picker = new vtkPropPicker();
242 picker.PickProp(x, rw.GetSize()[1] - y, ren);
245 vtkPropCollection coll = picker.GetPickList();
246 return processPick(picker, coll);
248 } else if (pickType == 1) {
249 if (scenePicker == null) {
250 scenePicker = new vtkScenePicker();
251 scenePicker.SetRenderer(ren);
252 scenePicker.EnableVertexPickingOn();
258 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
263 apath.InitTraversal();
266 vtkAssemblyNode node = apath.GetLastNode();
267 vtkProp test = (vtkProp) node.GetViewProp();
270 return new vtkProp[]{test};
274 } else if (pickType == 2) {
275 vtkPointPicker picker = new vtkPointPicker();
276 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
278 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
280 vtkProp3DCollection coll = picker.GetProp3Ds();
281 return processPick(picker, coll);
282 } else if (pickType == 3) {
283 vtkAreaPicker picker = new vtkAreaPicker();
285 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
286 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
288 vtkProp3DCollection coll = picker.GetProp3Ds();
289 return processPick(picker, coll);
290 } else if (pickType == 4) {
291 vtkCellPicker picker = new vtkCellPicker();
292 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
294 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
296 vtkProp3DCollection coll = picker.GetProp3Ds();
297 return processPick(picker, coll);
303 private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
304 // double[] pickPos = picker.GetPickPosition();
307 apath.InitTraversal();
308 vtkProp result[] = new vtkProp[apath.GetNumberOfItems()];
309 for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) {
310 vtkAssemblyNode node = apath.GetNextNode();
311 vtkProp test = (vtkProp) node.GetViewProp();
312 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
323 private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
324 // double[] pickPos = picker.GetPickPosition();
327 coll.InitTraversal();
328 vtkProp result[] = new vtkProp[coll.GetNumberOfItems()];
329 for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) {
330 vtkProp test = coll.GetNextProp();
332 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
344 private vtkAwtAction defaultAction;
345 private vtkAwtAction currentAction;
347 public void setActiveAction(vtkAction action) {
348 if (action.equals(currentAction))
350 if (currentAction != null)
351 currentAction.deattach();
352 currentAction = (vtkAwtAction)action;
357 public void setDefaultAction(vtkAction defaultAction) {
358 this.defaultAction = (vtkAwtAction)defaultAction;
361 public void useDefaultAction() {
362 setActiveAction(defaultAction);
365 public vtkAction getDefaultAction() {
366 return defaultAction;
371 public synchronized void Render() {
372 //System.out.println("Render " + rendering);
382 public void addListener(RenderListener l) {
386 public void removeListener(RenderListener l) {
390 private List<RenderListener> listeners = new ArrayList<RenderListener>();
392 List<RenderListener> list = new ArrayList<RenderListener>();
394 private void firePreRender() {
395 if (listeners.size() > 0) {
396 list.addAll(listeners);
397 for (RenderListener l : list)
403 private void firePostRender() {
404 if (listeners.size() > 0) {
405 list.addAll(listeners);
406 for (RenderListener l : list)
413 private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
415 public void addDeletable(vtkObjectBase o) {
419 public void removeDeletable (vtkObjectBase o) {
424 public void Delete() {
425 for (vtkObjectBase o : deletable) {
426 if (o.GetVTKId() != 0) {
436 public void refresh() {