]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/awt/InteractiveVtkPanel.java
Improve cameraAndSelectorAction api
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / awt / InteractiveVtkPanel.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.vtk.awt;
13
14 import java.awt.event.KeyEvent;
15 import java.awt.event.MouseEvent;
16 import java.util.ArrayList;
17 import java.util.List;
18
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;
25
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;
33 import vtk.vtkObject;
34 import vtk.vtkObjectBase;
35 import vtk.vtkPanel;
36 import vtk.vtkPointPicker;
37 import vtk.vtkProp;
38 import vtk.vtkProp3DCollection;
39 import vtk.vtkPropCollection;
40 import vtk.vtkPropPicker;
41 import vtk.vtkRenderer;
42 import vtk.vtkScenePicker;
43
44 public class InteractiveVtkPanel extends vtkPanel implements VtkView {
45         
46         protected vtkGenericRenderWindowInteractor iren;
47         
48
49         public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
50                 return this.iren;
51         }
52         private static final long serialVersionUID = 2815073937537950615L;
53         
54         
55         public InteractiveVtkPanel() {
56                 super();
57                 
58                 if (!Platform.inDevelopmentMode()) {
59                         // This is actually a static method in C++
60                         new vtkObject().GlobalWarningDisplayOff();
61                 }
62                 
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);
70                 addDeletable(style);
71                 addDeletable(iren);
72         }
73         
74         @Override
75         public IThreadWorkQueue getThreadQueue() {
76                 return AWTThread.getThreadAccess();
77         }
78         
79         @Override
80         public vtkRenderer getRenderer() {
81                 return GetRenderer();
82         }
83         
84         @Override
85         public void mouseClicked(MouseEvent e) {
86
87         }
88         
89         @Override
90         public void mouseMoved(MouseEvent e) {
91
92         }
93         
94         @Override
95         public void mouseEntered(MouseEvent e) {
96                 super.mouseEntered(e);
97         }
98         
99         @Override
100         public void mouseExited(MouseEvent e) {
101         
102         }
103         
104         @Override
105         public void mousePressed(MouseEvent e) {
106                 
107         }
108         
109         @Override
110         public void mouseDragged(MouseEvent e) {
111                 
112         }
113         
114         @Override
115         public void mouseReleased(MouseEvent e) {
116         
117         }
118         
119         @Override
120         public void keyPressed(KeyEvent e) {
121
122         }
123         
124         @Override
125         public void keyTyped(KeyEvent e) {
126                 
127         }
128         
129         public void setSize(int x, int y) {
130                 if (windowset == 1) {
131                         Lock();
132                         rw.SetSize(x, y);
133                         iren.SetSize(x, y);
134                         iren.ConfigureEvent();
135                         UnLock();
136                 }
137         }
138
139         private vtkScenePicker scenePicker;
140         
141         int pickType = 4;
142         
143         public int getPickType() {
144                 return pickType;
145         }
146         
147         public void setPickType(int pickType) {
148                 this.pickType = pickType;
149         }
150         
151         public vtkProp[] pick(int x, int y) {
152                 
153                 
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
158 //
159                 if (pickType == 0) {
160         
161                         vtkPropPicker picker = new vtkPropPicker();
162                         Lock();
163                         picker.PickProp(x, rw.GetSize()[1] - y, ren);
164         
165                         UnLock();
166         
167                         vtkAssemblyPath apath = picker.GetPath();
168                         return processPick(picker, apath);
169                         
170                 } else if (pickType == 1) {
171                         if (scenePicker == null) {
172                                 scenePicker = new vtkScenePicker();
173                                 scenePicker.SetRenderer(ren);
174                                 scenePicker.EnableVertexPickingOn();
175                                 
176                         }
177                         Lock();
178
179                         vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
180                         //int vertexId = scenePicker.GetVertexId(new int[]{x, rw.GetSize()[1] - y});
181                         
182                         UnLock();
183                         
184                         if (apath != null) {
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());
191 //                              }
192                                 
193                                 vtkAssemblyNode node = apath.GetLastNode();
194                                 vtkProp test = (vtkProp) node.GetViewProp();
195                                 apath.Delete();
196                                 node.Delete();
197                                 return new vtkProp[]{test};
198         
199                         }
200                         
201                 } else if (pickType == 2) {
202                         vtkPointPicker picker = new vtkPointPicker();
203                         //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
204                         picker.SetTolerance(0.00001);
205                         Lock();
206                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
207                         UnLock();
208                         
209                         vtkAssemblyPath apath = picker.GetPath();
210                         return processPick(picker, apath);
211                 } else if (pickType == 3) {
212                         vtkAreaPicker picker = new vtkAreaPicker();
213                         Lock();
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);
216                         UnLock();
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);
223                         Lock();
224                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
225                         UnLock();       
226                         vtkAssemblyPath apath = picker.GetPath();
227                         return processPick(picker, apath);
228                 }
229
230                 return null;
231         }
232         
233         public vtkProp[] pick2(int x, int y) {
234                 
235                 
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
240 //
241                 if (pickType == 0) {
242         
243                         vtkPropPicker picker = new vtkPropPicker();
244                         Lock();
245                         picker.PickProp(x, rw.GetSize()[1] - y, ren);
246         
247                         UnLock();
248                         vtkPropCollection coll = picker.GetPickList();
249                         return processPick(picker, coll);
250                         
251                 } else if (pickType == 1) {
252                         if (scenePicker == null) {
253                                 scenePicker = new vtkScenePicker();
254                                 scenePicker.SetRenderer(ren);
255                                 scenePicker.EnableVertexPickingOn();
256                                 
257                         }
258                         Lock();
259
260                         
261                         vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
262                         
263                         UnLock();
264                         
265                         if (apath != null) {
266                                 apath.InitTraversal();
267
268                                 
269                                 vtkAssemblyNode node = apath.GetLastNode();
270                                 vtkProp test = (vtkProp) node.GetViewProp();
271                                 apath.Delete();
272                                 node.Delete();
273                                 return new vtkProp[]{test};
274         
275                         }
276                         
277                 } else if (pickType == 2) {
278                         vtkPointPicker picker = new vtkPointPicker();
279                         picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
280                         Lock();
281                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
282                         UnLock();
283                         vtkProp3DCollection coll = picker.GetProp3Ds();
284                         return processPick(picker, coll);
285                 } else if (pickType == 3) {
286                         vtkAreaPicker picker = new vtkAreaPicker();
287                         Lock();
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);
290                         UnLock();
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]);
296                         Lock();
297                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
298                         UnLock();       
299                         vtkProp3DCollection coll = picker.GetProp3Ds();
300                         return processPick(picker, coll);
301                 }
302
303                 return null;
304         }
305         
306         private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
307 //              double[] pickPos = picker.GetPickPosition();
308                 picker.Delete();
309                 if (apath != null) {
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());
316                                 result[i] = test;
317                                 node.Delete();
318                         }
319                         apath.Delete();
320                         return result;
321
322                 }
323                 return null;
324         }
325         
326         private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
327 //              double[] pickPos = picker.GetPickPosition();
328                 picker.Delete();
329                 if (coll != null) {
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();
334                                 
335 //                              System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
336                                 result[i] = test;
337                                 
338                         }
339                         coll.Delete();
340                         return result;
341
342                 }
343                 return null;
344         }
345         
346         
347         private vtkAwtAction defaultAction;
348         private vtkAwtAction currentAction;
349         
350         public void setActiveAction(vtkAction action) {
351                 if (action.equals(currentAction))
352                         return;
353                 if (currentAction != null)
354                         currentAction.deattach();
355                 currentAction = (vtkAwtAction)action;
356                 if (action != null)
357                         action.attach();
358         }
359         
360         public void setDefaultAction(vtkAction defaultAction) {
361                 this.defaultAction = (vtkAwtAction)defaultAction;
362         }
363         
364         public void useDefaultAction() {
365                 setActiveAction(defaultAction);
366         }
367         
368         public vtkAction getDefaultAction() {
369                 return defaultAction;
370         }
371         
372         
373         @Override
374         public synchronized void Render() {
375                 //System.out.println("Render " + rendering);
376                 if (rendering)
377                         return;
378                 
379                 firePreRender();
380                 super.Render();
381                 firePostRender();
382         }
383         
384         
385         public void addListener(RenderListener l) {
386                 listeners.add(l);
387         }
388         
389         public void removeListener(RenderListener l) {
390                 listeners.remove(l);
391         }
392         
393         private List<RenderListener> listeners = new ArrayList<RenderListener>();
394         
395         List<RenderListener> list = new ArrayList<RenderListener>();
396         
397         private void firePreRender() {
398                 if (listeners.size() > 0) {
399                         list.addAll(listeners);
400                         for (RenderListener l : list)
401                                 l.preRender();
402                         list.clear();
403                 }
404         }
405         
406         private void firePostRender() {
407                 if (listeners.size() > 0) {
408                         list.addAll(listeners);
409                         for (RenderListener l : list)
410                                 l.postRender();
411                         list.clear();
412                 }
413         }
414
415         
416         private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
417         
418         public void addDeletable(vtkObjectBase o) {
419                 deletable.add(o);
420         }
421         
422         public void removeDeletable (vtkObjectBase o) {
423                 deletable.remove(o);
424         }
425         
426         @Override
427         public void Delete() {
428                 for (vtkObjectBase o : deletable) {
429                         if (o.GetVTKId() != 0) {
430                                 o.Delete();
431                         }
432                 }
433                 deletable.clear();
434                 
435                 super.Delete();
436         }
437         
438         @Override
439         public void refresh() {
440                 repaint();
441         }
442         
443         
444         
445 }