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