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;
32 import vtk.vtkObjectBase;
34 import vtk.vtkPointPicker;
36 import vtk.vtkProp3DCollection;
37 import vtk.vtkPropCollection;
38 import vtk.vtkPropPicker;
39 import vtk.vtkRenderer;
40 import vtk.vtkScenePicker;
42 public class InteractiveVtkPanel extends vtkPanel implements VtkView {
44 protected vtkGenericRenderWindowInteractor iren;
47 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
50 private static final long serialVersionUID = 2815073937537950615L;
53 public InteractiveVtkPanel() {
55 iren = new vtkGenericRenderWindowInteractor();
56 iren.SetRenderWindow(rw);
57 iren.TimerEventResetsTimerOff();
58 iren.SetSize(200, 200);
59 iren.ConfigureEvent();
60 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
61 iren.SetInteractorStyle(style);
67 public IThreadWorkQueue getThreadQueue() {
68 return AWTThread.getThreadAccess();
72 public vtkRenderer getRenderer() {
77 public void mouseClicked(MouseEvent e) {
82 public void mouseMoved(MouseEvent e) {
87 public void mouseEntered(MouseEvent e) {
88 super.mouseEntered(e);
92 public void mouseExited(MouseEvent e) {
97 public void mousePressed(MouseEvent e) {
102 public void mouseDragged(MouseEvent e) {
107 public void mouseReleased(MouseEvent e) {
112 public void keyPressed(KeyEvent e) {
117 public void keyTyped(KeyEvent e) {
121 public void setSize(int x, int y) {
122 if (windowset == 1) {
126 iren.ConfigureEvent();
131 private vtkScenePicker scenePicker;
135 public int getPickType() {
139 public void setPickType(int pickType) {
140 this.pickType = pickType;
143 public vtkProp[] pick(int x, int y) {
146 // vtkPicker picker = new vtkPicker();
147 // vtkAbstractPicker picker = new vtkAbstractPicker();
148 // picker.Pick(x, rw.GetSize()[1] - y, ren);
149 // // see page 60 of VTK user's guide
153 vtkPropPicker picker = new vtkPropPicker();
155 picker.PickProp(x, rw.GetSize()[1] - y, ren);
159 vtkAssemblyPath apath = picker.GetPath();
160 return processPick(picker, apath);
162 } else if (pickType == 1) {
163 if (scenePicker == null) {
164 scenePicker = new vtkScenePicker();
165 scenePicker.SetRenderer(ren);
166 scenePicker.EnableVertexPickingOn();
171 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
172 //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y});
177 apath.InitTraversal();
178 // System.out.println("Pick, actors " + apath.GetNumberOfItems() );
179 // for (int i = 0; i < apath.GetNumberOfItems(); i++) {
180 // vtkAssemblyNode node = apath.GetNextNode();
181 // vtkProp test = (vtkProp) node.GetViewProp();
182 // System.out.println(test.GetClassName());
185 vtkAssemblyNode node = apath.GetLastNode();
186 vtkProp test = (vtkProp) node.GetViewProp();
189 return new vtkProp[]{test};
193 } else if (pickType == 2) {
194 vtkPointPicker picker = new vtkPointPicker();
195 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
196 picker.SetTolerance(0.00001);
198 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
201 vtkAssemblyPath apath = picker.GetPath();
202 return processPick(picker, apath);
203 } else if (pickType == 3) {
204 vtkAreaPicker picker = new vtkAreaPicker();
206 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
207 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
209 vtkAssemblyPath apath = picker.GetPath();
210 return processPick(picker, apath);
211 } else if (pickType == 4) {
212 vtkCellPicker picker = new vtkCellPicker();
213 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
214 picker.SetTolerance(0.00001);
216 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
218 vtkAssemblyPath apath = picker.GetPath();
219 return processPick(picker, apath);
225 public vtkProp[] pick2(int x, int y) {
228 // vtkPicker picker = new vtkPicker();
229 // vtkAbstractPicker picker = new vtkAbstractPicker();
230 // picker.Pick(x, rw.GetSize()[1] - y, ren);
231 // // see page 60 of VTK user's guide
235 vtkPropPicker picker = new vtkPropPicker();
237 picker.PickProp(x, rw.GetSize()[1] - y, ren);
240 vtkPropCollection coll = picker.GetPickList();
241 return processPick(picker, coll);
243 } else if (pickType == 1) {
244 if (scenePicker == null) {
245 scenePicker = new vtkScenePicker();
246 scenePicker.SetRenderer(ren);
247 scenePicker.EnableVertexPickingOn();
253 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
258 apath.InitTraversal();
261 vtkAssemblyNode node = apath.GetLastNode();
262 vtkProp test = (vtkProp) node.GetViewProp();
265 return new vtkProp[]{test};
269 } else if (pickType == 2) {
270 vtkPointPicker picker = new vtkPointPicker();
271 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
273 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
275 vtkProp3DCollection coll = picker.GetProp3Ds();
276 return processPick(picker, coll);
277 } else if (pickType == 3) {
278 vtkAreaPicker picker = new vtkAreaPicker();
280 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
281 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
283 vtkProp3DCollection coll = picker.GetProp3Ds();
284 return processPick(picker, coll);
285 } else if (pickType == 4) {
286 vtkCellPicker picker = new vtkCellPicker();
287 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
289 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
291 vtkProp3DCollection coll = picker.GetProp3Ds();
292 return processPick(picker, coll);
298 private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
299 // double[] pickPos = picker.GetPickPosition();
302 apath.InitTraversal();
303 vtkProp result[] = new vtkProp[apath.GetNumberOfItems()];
304 for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) {
305 vtkAssemblyNode node = apath.GetNextNode();
306 vtkProp test = (vtkProp) node.GetViewProp();
307 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
318 private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
319 // double[] pickPos = picker.GetPickPosition();
322 coll.InitTraversal();
323 vtkProp result[] = new vtkProp[coll.GetNumberOfItems()];
324 for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) {
325 vtkProp test = coll.GetNextProp();
327 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
339 private vtkAwtAction defaultAction;
340 private vtkAwtAction currentAction;
342 public void setActiveAction(vtkAction action) {
343 if (action.equals(currentAction))
345 if (currentAction != null)
346 currentAction.deattach();
347 currentAction = (vtkAwtAction)action;
352 public void setDefaultAction(vtkAction defaultAction) {
353 this.defaultAction = (vtkAwtAction)defaultAction;
356 public void useDefaultAction() {
357 setActiveAction(defaultAction);
360 public vtkAction getDefaultAction() {
361 return defaultAction;
366 public synchronized void Render() {
367 //System.out.println("Render " + rendering);
377 public void addListener(RenderListener l) {
381 public void removeListener(RenderListener l) {
385 private List<RenderListener> listeners = new ArrayList<RenderListener>();
387 List<RenderListener> list = new ArrayList<RenderListener>();
389 private void firePreRender() {
390 if (listeners.size() > 0) {
391 list.addAll(listeners);
392 for (RenderListener l : list)
398 private void firePostRender() {
399 if (listeners.size() > 0) {
400 list.addAll(listeners);
401 for (RenderListener l : list)
408 private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
410 public void addDeletable(vtkObjectBase o) {
414 public void removeDeletable (vtkObjectBase o) {
419 public void Delete() {
420 for (vtkObjectBase o : deletable) {
421 if (o.GetVTKId() != 0) {
431 public void refresh() {