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.eclipse.core.runtime.Platform;
20 import org.simantics.g3d.scenegraph.RenderListener;
21 import org.simantics.g3d.vtk.action.vtkAction;
22 import org.simantics.g3d.vtk.common.VtkView;
23 import org.simantics.utils.threads.AWTThread;
24 import org.simantics.utils.threads.IThreadWorkQueue;
26 import vtk.vtkAbstractPicker;
27 import vtk.vtkAreaPicker;
28 import vtk.vtkAssemblyNode;
29 import vtk.vtkAssemblyPath;
30 import vtk.vtkCellPicker;
31 import vtk.vtkGenericRenderWindowInteractor;
32 import vtk.vtkInteractorStyleTrackballCamera;
34 import vtk.vtkObjectBase;
36 import vtk.vtkPointPicker;
38 import vtk.vtkProp3DCollection;
39 import vtk.vtkPropCollection;
40 import vtk.vtkPropPicker;
41 import vtk.vtkRenderer;
42 import vtk.vtkScenePicker;
44 public class InteractiveVtkPanel extends vtkPanel implements VtkView {
46 protected vtkGenericRenderWindowInteractor iren;
49 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
52 private static final long serialVersionUID = 2815073937537950615L;
55 public InteractiveVtkPanel() {
58 if (!Platform.inDevelopmentMode()) {
59 // This is actually a static method in C++
60 new vtkObject().GlobalWarningDisplayOff();
63 iren = new vtkGenericRenderWindowInteractor();
64 iren.SetRenderWindow(rw);
65 iren.TimerEventResetsTimerOff();
66 iren.SetSize(200, 200);
67 iren.ConfigureEvent();
68 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
69 iren.SetInteractorStyle(style);
75 public IThreadWorkQueue getThreadQueue() {
76 return AWTThread.getThreadAccess();
80 public vtkRenderer getRenderer() {
85 public void mouseClicked(MouseEvent e) {
90 public void mouseMoved(MouseEvent e) {
95 public void mouseEntered(MouseEvent e) {
96 super.mouseEntered(e);
100 public void mouseExited(MouseEvent e) {
105 public void mousePressed(MouseEvent e) {
110 public void mouseDragged(MouseEvent e) {
115 public void mouseReleased(MouseEvent e) {
120 public void keyPressed(KeyEvent e) {
125 public void keyTyped(KeyEvent e) {
129 public void setSize(int x, int y) {
130 if (windowset == 1) {
134 iren.ConfigureEvent();
139 private vtkScenePicker scenePicker;
143 public int getPickType() {
147 public void setPickType(int pickType) {
148 this.pickType = pickType;
151 public vtkProp[] pick(int x, int y) {
154 // vtkPicker picker = new vtkPicker();
155 // vtkAbstractPicker picker = new vtkAbstractPicker();
156 // picker.Pick(x, rw.GetSize()[1] - y, ren);
157 // // see page 60 of VTK user's guide
161 vtkPropPicker picker = new vtkPropPicker();
163 picker.PickProp(x, rw.GetSize()[1] - y, ren);
167 vtkAssemblyPath apath = picker.GetPath();
168 return processPick(picker, apath);
170 } else if (pickType == 1) {
171 if (scenePicker == null) {
172 scenePicker = new vtkScenePicker();
173 scenePicker.SetRenderer(ren);
174 scenePicker.EnableVertexPickingOn();
179 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
180 //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y});
185 apath.InitTraversal();
186 // System.out.println("Pick, actors " + apath.GetNumberOfItems() );
187 // for (int i = 0; i < apath.GetNumberOfItems(); i++) {
188 // vtkAssemblyNode node = apath.GetNextNode();
189 // vtkProp test = (vtkProp) node.GetViewProp();
190 // System.out.println(test.GetClassName());
193 vtkAssemblyNode node = apath.GetLastNode();
194 vtkProp test = (vtkProp) node.GetViewProp();
197 return new vtkProp[]{test};
201 } else if (pickType == 2) {
202 vtkPointPicker picker = new vtkPointPicker();
203 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
204 picker.SetTolerance(0.00001);
206 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
209 vtkAssemblyPath apath = picker.GetPath();
210 return processPick(picker, apath);
211 } else if (pickType == 3) {
212 vtkAreaPicker picker = new vtkAreaPicker();
214 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
215 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
217 vtkAssemblyPath apath = picker.GetPath();
218 return processPick(picker, apath);
219 } else if (pickType == 4) {
220 vtkCellPicker picker = new vtkCellPicker();
221 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
222 picker.SetTolerance(0.00001);
224 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
226 vtkAssemblyPath apath = picker.GetPath();
227 return processPick(picker, apath);
233 public vtkProp[] pick2(int x, int y) {
236 // vtkPicker picker = new vtkPicker();
237 // vtkAbstractPicker picker = new vtkAbstractPicker();
238 // picker.Pick(x, rw.GetSize()[1] - y, ren);
239 // // see page 60 of VTK user's guide
243 vtkPropPicker picker = new vtkPropPicker();
245 picker.PickProp(x, rw.GetSize()[1] - y, ren);
248 vtkPropCollection coll = picker.GetPickList();
249 return processPick(picker, coll);
251 } else if (pickType == 1) {
252 if (scenePicker == null) {
253 scenePicker = new vtkScenePicker();
254 scenePicker.SetRenderer(ren);
255 scenePicker.EnableVertexPickingOn();
261 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
266 apath.InitTraversal();
269 vtkAssemblyNode node = apath.GetLastNode();
270 vtkProp test = (vtkProp) node.GetViewProp();
273 return new vtkProp[]{test};
277 } else if (pickType == 2) {
278 vtkPointPicker picker = new vtkPointPicker();
279 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
281 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
283 vtkProp3DCollection coll = picker.GetProp3Ds();
284 return processPick(picker, coll);
285 } else if (pickType == 3) {
286 vtkAreaPicker picker = new vtkAreaPicker();
288 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
289 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
291 vtkProp3DCollection coll = picker.GetProp3Ds();
292 return processPick(picker, coll);
293 } else if (pickType == 4) {
294 vtkCellPicker picker = new vtkCellPicker();
295 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
297 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
299 vtkProp3DCollection coll = picker.GetProp3Ds();
300 return processPick(picker, coll);
306 private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
307 // double[] pickPos = picker.GetPickPosition();
310 apath.InitTraversal();
311 vtkProp result[] = new vtkProp[apath.GetNumberOfItems()];
312 for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) {
313 vtkAssemblyNode node = apath.GetNextNode();
314 vtkProp test = (vtkProp) node.GetViewProp();
315 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
326 private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
327 // double[] pickPos = picker.GetPickPosition();
330 coll.InitTraversal();
331 vtkProp result[] = new vtkProp[coll.GetNumberOfItems()];
332 for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) {
333 vtkProp test = coll.GetNextProp();
335 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
347 private vtkAwtAction defaultAction;
348 private vtkAwtAction currentAction;
350 public void setActiveAction(vtkAction action) {
351 if (action.equals(currentAction))
353 if (currentAction != null)
354 currentAction.deattach();
355 currentAction = (vtkAwtAction)action;
360 public void setDefaultAction(vtkAction defaultAction) {
361 this.defaultAction = (vtkAwtAction)defaultAction;
364 public void useDefaultAction() {
365 setActiveAction(defaultAction);
368 public vtkAction getDefaultAction() {
369 return defaultAction;
374 public synchronized void Render() {
375 //System.out.println("Render " + rendering);
385 public void addListener(RenderListener l) {
389 public void removeListener(RenderListener l) {
393 private List<RenderListener> listeners = new ArrayList<RenderListener>();
395 List<RenderListener> list = new ArrayList<RenderListener>();
397 private void firePreRender() {
398 if (listeners.size() > 0) {
399 list.addAll(listeners);
400 for (RenderListener l : list)
406 private void firePostRender() {
407 if (listeners.size() > 0) {
408 list.addAll(listeners);
409 for (RenderListener l : list)
416 private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
418 public void addDeletable(vtkObjectBase o) {
422 public void removeDeletable (vtkObjectBase o) {
427 public void Delete() {
428 for (vtkObjectBase o : deletable) {
429 if (o.GetVTKId() != 0) {
439 public void refresh() {