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