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.scenegraph.utils.DPIUtil;
24 import org.simantics.utils.threads.AWTThread;
25 import org.simantics.utils.threads.IThreadWorkQueue;
27 import vtk.vtkAbstractPicker;
28 import vtk.vtkAreaPicker;
29 import vtk.vtkAssemblyNode;
30 import vtk.vtkAssemblyPath;
31 import vtk.vtkCellPicker;
32 import vtk.vtkGenericRenderWindowInteractor;
33 import vtk.vtkInteractorStyleTrackballCamera;
35 import vtk.vtkObjectBase;
37 import vtk.vtkPointPicker;
39 import vtk.vtkProp3DCollection;
40 import vtk.vtkPropCollection;
41 import vtk.vtkPropPicker;
42 import vtk.vtkRenderer;
43 import vtk.vtkScenePicker;
45 public class InteractiveVtkPanel extends vtkPanel implements VtkView {
47 protected vtkGenericRenderWindowInteractor iren;
50 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
53 private static final long serialVersionUID = 2815073937537950615L;
56 public InteractiveVtkPanel() {
59 if (!Platform.inDevelopmentMode()) {
60 // This is actually a static method in C++
61 new vtkObject().GlobalWarningDisplayOff();
64 iren = new vtkGenericRenderWindowInteractor();
65 iren.SetRenderWindow(rw);
66 iren.TimerEventResetsTimerOff();
67 iren.SetSize(200, 200);
68 iren.ConfigureEvent();
69 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
70 iren.SetInteractorStyle(style);
76 public IThreadWorkQueue getThreadQueue() {
77 return AWTThread.getThreadAccess();
81 public vtkRenderer getRenderer() {
86 public void mouseClicked(MouseEvent e) {
91 public void mouseMoved(MouseEvent e) {
96 public void mouseEntered(MouseEvent e) {
97 super.mouseEntered(e);
101 public void mouseExited(MouseEvent e) {
106 public void mousePressed(MouseEvent e) {
111 public void mouseDragged(MouseEvent e) {
116 public void mouseReleased(MouseEvent e) {
121 public void keyPressed(KeyEvent e) {
126 public void keyTyped(KeyEvent e) {
130 public void setSize(int x, int y) {
134 public void _setSize(int x, int y) {
135 System.out.println(this.getClass().getSimpleName() + " " + windowset + " setSize " +x + " " + y);
136 if (windowset == 1) {
141 iren.ConfigureEvent();
145 System.out.println();
150 public void setBounds(int x, int y, int width, int height) {
151 super.setBounds(x, y, width, height);
152 _setSize(width, height);
155 private vtkScenePicker scenePicker;
159 public int getPickType() {
163 public void setPickType(int pickType) {
164 this.pickType = pickType;
167 public vtkProp[] pick(int x, int y) {
170 // vtkPicker picker = new vtkPicker();
171 // vtkAbstractPicker picker = new vtkAbstractPicker();
172 // picker.Pick(x, rw.GetSize()[1] - y, ren);
173 // // see page 60 of VTK user's guide
177 vtkPropPicker picker = new vtkPropPicker();
179 picker.PickProp(x, rw.GetSize()[1] - y, ren);
183 vtkAssemblyPath apath = picker.GetPath();
184 return processPick(picker, apath);
186 } else if (pickType == 1) {
187 if (scenePicker == null) {
188 scenePicker = new vtkScenePicker();
189 scenePicker.SetRenderer(ren);
190 scenePicker.EnableVertexPickingOn();
195 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
196 //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y});
201 apath.InitTraversal();
202 // System.out.println("Pick, actors " + apath.GetNumberOfItems() );
203 // for (int i = 0; i < apath.GetNumberOfItems(); i++) {
204 // vtkAssemblyNode node = apath.GetNextNode();
205 // vtkProp test = (vtkProp) node.GetViewProp();
206 // System.out.println(test.GetClassName());
209 vtkAssemblyNode node = apath.GetLastNode();
210 vtkProp test = (vtkProp) node.GetViewProp();
213 return new vtkProp[]{test};
217 } else if (pickType == 2) {
218 vtkPointPicker picker = new vtkPointPicker();
219 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
220 picker.SetTolerance(0.00001);
222 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
225 vtkAssemblyPath apath = picker.GetPath();
226 return processPick(picker, apath);
227 } else if (pickType == 3) {
228 vtkAreaPicker picker = new vtkAreaPicker();
230 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
231 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
233 vtkAssemblyPath apath = picker.GetPath();
234 return processPick(picker, apath);
235 } else if (pickType == 4) {
236 vtkCellPicker picker = new vtkCellPicker();
237 //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
238 picker.SetTolerance(0.00001);
240 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
242 vtkAssemblyPath apath = picker.GetPath();
243 return processPick(picker, apath);
249 public vtkProp[] pick2(int x, int y) {
252 // vtkPicker picker = new vtkPicker();
253 // vtkAbstractPicker picker = new vtkAbstractPicker();
254 // picker.Pick(x, rw.GetSize()[1] - y, ren);
255 // // see page 60 of VTK user's guide
259 vtkPropPicker picker = new vtkPropPicker();
261 picker.PickProp(x, rw.GetSize()[1] - y, ren);
264 vtkPropCollection coll = picker.GetPickList();
265 return processPick(picker, coll);
267 } else if (pickType == 1) {
268 if (scenePicker == null) {
269 scenePicker = new vtkScenePicker();
270 scenePicker.SetRenderer(ren);
271 scenePicker.EnableVertexPickingOn();
277 vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
282 apath.InitTraversal();
285 vtkAssemblyNode node = apath.GetLastNode();
286 vtkProp test = (vtkProp) node.GetViewProp();
289 return new vtkProp[]{test};
293 } else if (pickType == 2) {
294 vtkPointPicker picker = new vtkPointPicker();
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);
301 } else if (pickType == 3) {
302 vtkAreaPicker picker = new vtkAreaPicker();
304 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
305 //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
307 vtkProp3DCollection coll = picker.GetProp3Ds();
308 return processPick(picker, coll);
309 } else if (pickType == 4) {
310 vtkCellPicker picker = new vtkCellPicker();
311 picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
313 picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
315 vtkProp3DCollection coll = picker.GetProp3Ds();
316 return processPick(picker, coll);
322 private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
323 // double[] pickPos = picker.GetPickPosition();
326 apath.InitTraversal();
327 vtkProp result[] = new vtkProp[apath.GetNumberOfItems()];
328 for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) {
329 vtkAssemblyNode node = apath.GetNextNode();
330 vtkProp test = (vtkProp) node.GetViewProp();
331 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
342 private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
343 // double[] pickPos = picker.GetPickPosition();
346 coll.InitTraversal();
347 vtkProp result[] = new vtkProp[coll.GetNumberOfItems()];
348 for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) {
349 vtkProp test = coll.GetNextProp();
351 // System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
363 private vtkAwtAction defaultAction;
364 private vtkAwtAction currentAction;
366 public void setActiveAction(vtkAction action) {
367 if (action.equals(currentAction))
369 if (currentAction != null)
370 currentAction.deattach();
371 currentAction = (vtkAwtAction)action;
376 public void setDefaultAction(vtkAction defaultAction) {
377 this.defaultAction = (vtkAwtAction)defaultAction;
380 public void useDefaultAction() {
381 setActiveAction(defaultAction);
384 public vtkAction getDefaultAction() {
385 return defaultAction;
390 public synchronized void Render() {
391 //System.out.println("Render " + rendering);
401 public void addListener(RenderListener l) {
405 public void removeListener(RenderListener l) {
409 private List<RenderListener> listeners = new ArrayList<RenderListener>();
411 List<RenderListener> list = new ArrayList<RenderListener>();
413 private void firePreRender() {
414 if (listeners.size() > 0) {
415 list.addAll(listeners);
416 for (RenderListener l : list)
422 private void firePostRender() {
423 if (listeners.size() > 0) {
424 list.addAll(listeners);
425 for (RenderListener l : list)
432 private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
434 public void addDeletable(vtkObjectBase o) {
438 public void removeDeletable (vtkObjectBase o) {
443 public void Delete() {
444 for (vtkObjectBase o : deletable) {
445 if (o.GetVTKId() != 0) {
455 public void refresh() {
460 public double upscale(double d) {
461 return DPIUtil.upscale(d);
465 public int upscale(int i) {
466 return DPIUtil.upscale(i);