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