]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/InteractiveVtkComposite.java
Only disable warning display when not in development mode
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / swt / InteractiveVtkComposite.java
1 package org.simantics.g3d.vtk.swt;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.core.runtime.Platform;
7 import org.eclipse.swt.widgets.Composite;
8 import org.simantics.g3d.scenegraph.RenderListener;
9 import org.simantics.g3d.vtk.action.vtkAction;
10 import org.simantics.g3d.vtk.common.VtkView;
11 import org.simantics.utils.threads.IThreadWorkQueue;
12 import org.simantics.utils.threads.SWTThread;
13 import org.simantics.utils.threads.ThreadUtils;
14
15 import vtk.vtkAbstractPicker;
16 import vtk.vtkActor2D;
17 import vtk.vtkActor2DCollection;
18 import vtk.vtkAreaPicker;
19 import vtk.vtkAssemblyNode;
20 import vtk.vtkAssemblyPath;
21 import vtk.vtkCellPicker;
22 import vtk.vtkObject;
23 import vtk.vtkObjectBase;
24 import vtk.vtkPointPicker;
25 import vtk.vtkProp;
26 import vtk.vtkProp3DCollection;
27 import vtk.vtkPropCollection;
28 import vtk.vtkPropPicker;
29 import vtk.vtkRenderWindow;
30 import vtk.vtkRenderer;
31 import vtk.vtkScenePicker;
32 import vtk.rendering.swt.vtkSwtComponent;
33
34 public class InteractiveVtkComposite extends vtkSwtComponent implements VtkView{
35         
36         Composite parentComposite;
37         public InteractiveVtkComposite(Composite parentComposite) {
38                 super(parentComposite);
39                 this.parentComposite = parentComposite;
40
41                 if (!Platform.inDevelopmentMode()) {
42                         // This is actually a static method in C++
43                         new vtkObject().GlobalWarningDisplayOff();
44                 }
45         }
46         
47         private boolean updating = false;
48         private Object mutex = new Object();
49         
50         @Override
51         public void refresh() {
52 //              if (Thread.currentThread() == getThreadQueue().getThread())
53 //                      update();
54 //              else {
55                         synchronized (mutex) {
56                                 if (!updating) {
57                                         updating = true;
58                                         ThreadUtils.asyncExec(getThreadQueue(), new Runnable() {
59                                                 
60                                                 @Override
61                                                 public void run() {
62                                                         if (parentComposite.isDisposed())
63                                                                 return;
64                                                         synchronized (mutex) {
65                                                                 updating = false;
66                                                         }
67                                                         update();
68                                                 }
69                                         });
70                                         
71                                 }
72                         }
73 //              }
74         }
75         
76         boolean update = false;
77         @Override
78         public void update() {
79                 if (update)
80                         return;
81                 update = true;
82                 super.update();
83                 update = false;
84         }
85         
86         @Override
87         public IThreadWorkQueue getThreadQueue() {
88                 return SWTThread.getThreadAccess();
89         }
90         
91
92         boolean render = false;
93         @Override
94         public void Render() {
95                 if (render)
96                         return;
97 //              System.out.println(getClass().getSimpleName() +" render");
98                 render = true;
99                 firePreRender();
100                 super.Render();
101                 firePostRender();
102                 render = false;
103         }
104
105         public void addListener(RenderListener l) {
106                 listeners.add(l);
107         }
108         
109         public void removeListener(RenderListener l) {
110                 listeners.remove(l);
111         }
112         
113         private List<RenderListener> listeners = new ArrayList<RenderListener>();
114         
115         List<RenderListener> list = new ArrayList<RenderListener>();
116         
117         private void firePreRender() {
118                 if (listeners.size() > 0) {
119                         list.addAll(listeners);
120                         for (RenderListener l : list)
121                                 l.preRender();
122                         list.clear();
123                 }
124         }
125         
126         private void firePostRender() {
127                 if (listeners.size() > 0) {
128                         list.addAll(listeners);
129                         for (RenderListener l : list)
130                                 l.postRender();
131                         list.clear();
132                 }
133         }
134
135         
136         private List<vtkObjectBase> deletable = new ArrayList<vtkObjectBase>();
137         
138         public void addDeletable(vtkObjectBase o) {
139                 deletable.add(o);
140         }
141         
142         public void removeDeletable (vtkObjectBase o) {
143                 deletable.remove(o);
144         }
145         
146         @Override
147         public void Delete() {
148                 for (vtkObjectBase o : deletable) {
149                         if (o.GetVTKId() != 0) {
150                                 o.Delete();
151                         }
152                 }
153                 deletable.clear();
154                 
155                 super.Delete();
156         }
157         
158         private vtkSwtAction defaultAction;
159         private vtkSwtAction currentAction;
160         
161         public void setActiveAction(vtkAction action) {
162                 if (action.equals(currentAction))
163                         return;
164                 if (currentAction != null)
165                         currentAction.deattach();
166                 currentAction = (vtkSwtAction)action;
167                 if (action != null)
168                         action.attach();
169         }
170         
171         public void setDefaultAction(vtkAction defaultAction) {
172                 this.defaultAction = (vtkSwtAction)defaultAction;
173         }
174         
175         public void useDefaultAction() {
176                 setActiveAction(defaultAction);
177         }
178         
179         public vtkAction getDefaultAction() {
180                 return defaultAction;
181         }
182         
183         public void lock() {
184                 getVTKLock().lock();
185                 if (getComponent().getContext() != null && !getComponent().getContext().isCurrent()) {
186                         @SuppressWarnings("unused")
187                         int ret = getComponent().getContext().makeCurrent();
188 //                      System.out.println("Make current2 " + ret);
189                 }
190         }
191         
192         public void unlock() {
193                 getVTKLock().unlock();
194         }
195         
196         private vtkScenePicker scenePicker;
197         
198         int pickType = 4;
199         
200         public int getPickType() {
201                 return pickType;
202         }
203         
204         public void setPickType(int pickType) {
205                 this.pickType = pickType;
206         }
207         
208         public vtkProp[] pick(int x, int y) {
209                 
210                 
211                 vtkRenderWindow rw = getRenderWindow();
212                 vtkRenderer ren = getRenderer();
213                 
214                 int ax = x;
215                 int ay = rw.GetSize()[1] - y;
216
217                 if (pickType == 0) {
218                         throw new IllegalStateException("SWT Binding does not work with hw picking");
219 //                      lock();
220 //                      ren.BreakOnError();
221 //                      ren.DrawOff();
222 //                      vtkPropPicker picker = new vtkPropPicker();
223 //                      picker.DebugOn();
224 //                      //picker.PickProp(x, rw.GetSize()[1] - y, ren);
225 //                      picker.Pick(x, rw.GetSize()[1] - y,0, ren);
226 //                      ren.DrawOn();
227 //                      unlock();
228 //      
229 //                      vtkAssemblyPath apath = picker.GetPath();
230 //                      return processPick(picker, apath);
231 //                      
232                 } else if (pickType == 1) {
233                         throw new IllegalStateException("SWT Binding does not work with hw picking");
234 //                      if (scenePicker == null) {
235 //                              scenePicker = new vtkScenePicker();
236 //                              scenePicker.SetRenderer(ren);
237 //                              scenePicker.EnableVertexPickingOn();
238 //                              
239 //                      }
240 //                      lock();
241 //
242 //                      vtkAssemblyPath apath = ren.PickProp(ax, ay);
243 //                      
244 //                      unlock();
245 //                      
246 //                      if (apath != null) {
247 //                              apath.InitTraversal();
248 //                              vtkAssemblyNode node = apath.GetLastNode();
249 //                              vtkProp test = (vtkProp) node.GetViewProp();
250 //                              apath.Delete();
251 //                              node.Delete();
252 //                              return new vtkProp[]{test};
253 //      
254 //                      }
255                         
256                 } else if (pickType == 2) {
257                         vtkPointPicker picker = new vtkPointPicker();
258                         //picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
259                         picker.SetTolerance(0.00001);
260                         lock();
261                         picker.Pick(new double[]{ax, ay,0}, ren);
262                         unlock();
263                         
264                         vtkAssemblyPath apath = picker.GetPath();
265                         return processPick(picker, apath);
266                 } else if (pickType == 3) {
267                         vtkAreaPicker picker = new vtkAreaPicker();
268                         lock();
269                         picker.Pick(new double[]{ax, ay,0}, ren);
270                         unlock();
271                         vtkAssemblyPath apath = picker.GetPath();
272                         return processPick(picker, apath);
273                 } else if (pickType == 4) {
274                         vtkCellPicker picker = new vtkCellPicker();
275                         picker.SetTolerance(0.00001);
276                         lock();
277                         picker.Pick(new double[]{ax, ay,0}, ren);
278                         unlock();       
279                         vtkAssemblyPath apath = picker.GetPath();
280                         return processPick(picker, apath);
281                 } else if (pickType == 5) {
282                         vtkActor2DCollection coll = ren.GetActors2D(); 
283                         coll.InitTraversal();
284                         vtkActor2D a;
285                         
286                         List<vtkActor2D> picked = new ArrayList<vtkActor2D>();
287                         while ((a = coll.GetNextItem()) != null) {
288                                 double pos[] = a.GetPosition();
289                                 // TODO : width and height do not seem to affect the perceived size of Actor2D.
290                                 //        actual size should be fetched from mapper. 
291                                 double w = a.GetWidth();
292                                 double h = a.GetHeight();
293                                 int minx = (int)(pos[0]-w*0.5);
294                                 int miny = (int)(pos[1]-h*0.5);
295                                 int maxx = (int)(pos[0]+w*0.5);
296                                 int maxy = (int)(pos[1]+h*0.5);
297                                 if (minx <= ax && maxx >= ax &&
298                                         miny <= ay && maxy >= ay) {
299                                         picked.add(a);
300                                 }
301                         }
302                         return picked.toArray(new vtkProp[picked.size()]);
303                 }
304
305                 return null;
306         }
307         
308         public vtkProp[] pick2(int x, int y) {
309                 vtkRenderWindow rw = getRenderWindow();
310                 vtkRenderer ren = getRenderer();
311                 
312 //              vtkPicker picker = new vtkPicker();
313 //              vtkAbstractPicker picker = new vtkAbstractPicker();
314 //              picker.Pick(x, rw.GetSize()[1] - y, ren);
315 //              // see page 60 of VTK user's guide
316 //
317                 if (pickType == 0) {
318         
319                         vtkPropPicker picker = new vtkPropPicker();
320                         lock();
321                         picker.PickProp(x, rw.GetSize()[1] - y, ren);
322         
323                         unlock();
324                         vtkPropCollection coll = picker.GetPickList();
325                         return processPick(picker, coll);
326                         
327                 } else if (pickType == 1) {
328                         if (scenePicker == null) {
329                                 scenePicker = new vtkScenePicker();
330                                 scenePicker.SetRenderer(ren);
331                                 scenePicker.EnableVertexPickingOn();
332                                 scenePicker.DebugOn();
333                                 
334                         }
335                         lock();
336
337                         
338                         vtkAssemblyPath apath = ren.PickProp(x, rw.GetSize()[1] - y);
339                         
340                         unlock();
341                         
342                         if (apath != null) {
343                                 apath.InitTraversal();
344
345                                 
346                                 vtkAssemblyNode node = apath.GetLastNode();
347                                 vtkProp test = (vtkProp) node.GetViewProp();
348                                 apath.Delete();
349                                 node.Delete();
350                                 return new vtkProp[]{test};
351         
352                         }
353                         
354                 } else if (pickType == 2) {
355                         vtkPointPicker picker = new vtkPointPicker();
356                         picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
357                         lock();
358                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
359                         unlock();
360                         vtkProp3DCollection coll = picker.GetProp3Ds();
361                         return processPick(picker, coll);
362                 } else if (pickType == 3) {
363                         vtkAreaPicker picker = new vtkAreaPicker();
364                         lock();
365                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
366                         //picker.AreaPick(x-1, rw.GetSize()[1] - y-1,x+1,rw.GetSize()[1] - y+1, ren);
367                         unlock();
368                         vtkProp3DCollection coll = picker.GetProp3Ds();
369                         return processPick(picker, coll);
370                 } else if (pickType == 4) {
371                         vtkCellPicker picker = new vtkCellPicker();
372                         picker.SetTolerance(2.0/(double)rw.GetSize()[0]);
373                         lock();
374                         picker.Pick(new double[]{x, rw.GetSize()[1] - y,0}, ren);
375                         unlock();       
376                         vtkProp3DCollection coll = picker.GetProp3Ds();
377                         return processPick(picker, coll);
378                 }
379
380                 return null;
381         }
382         
383         private vtkProp[] processPick(vtkAbstractPicker picker, vtkAssemblyPath apath) {
384 //              double[] pickPos = picker.GetPickPosition();
385                 picker.Delete();
386                 if (apath != null) {
387                         apath.InitTraversal();
388                         vtkProp result[] = new vtkProp[apath.GetNumberOfItems()];
389                         for (int i = apath.GetNumberOfItems()-1; i >= 0; i--) {
390                                 vtkAssemblyNode node = apath.GetNextNode();
391                                 vtkProp test = (vtkProp) node.GetViewProp();
392 //                              System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
393                                 result[i] = test;
394                                 node.Delete();
395                         }
396                         apath.Delete();
397                         return result;
398
399                 }
400                 return null;
401         }
402         
403         private vtkProp[] processPick(vtkAbstractPicker picker, vtkPropCollection coll) {
404 //              double[] pickPos = picker.GetPickPosition();
405                 picker.Delete();
406                 if (coll != null) {
407                         coll.InitTraversal();
408                         vtkProp result[] = new vtkProp[coll.GetNumberOfItems()];
409                         for (int i = coll.GetNumberOfItems()-1; i >= 0; i--) {
410                                 vtkProp test = coll.GetNextProp();
411                                 
412 //                              System.out.println("Picked: " + test.GetClassName() + " " + test.GetVTKId());
413                                 result[i] = test;
414                                 
415                         }
416                         coll.Delete();
417                         return result;
418
419                 }
420                 return null;
421         }
422         
423         
424         
425 }