]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk/src/vtk/vtkPanel.java
Revised native library loading for VTK
[simantics/3d.git] / vtk / src / vtk / vtkPanel.java
1 package vtk;
2
3 import java.awt.Canvas;
4 import java.awt.Graphics;
5 import java.awt.event.InputEvent;
6 import java.awt.event.KeyEvent;
7 import java.awt.event.KeyListener;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.awt.event.MouseMotionListener;
11 import java.awt.event.MouseWheelEvent;
12 import java.awt.event.MouseWheelListener;
13
14 import javax.swing.SwingUtilities;
15
16 /**
17  * Java AWT component that encapsulate vtkRenderWindow, vtkRenderer, vtkCamera,
18  * vtkLight.
19  *
20  * If a vtkInteractor is needed, use vtkCanvas instead. This is necessary when
21  * Widget and Picker are used.
22  *
23  * @author Kitware
24  */
25 public class vtkPanel extends Canvas implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {
26   private static final long serialVersionUID = 1L;
27   protected vtkRenderWindow rw = new vtkRenderWindow();
28   protected vtkRenderer ren = new vtkRenderer();
29   protected vtkCamera cam = null;
30   protected vtkLight lgt = new vtkLight();
31   protected int lastX;
32   protected int lastY;
33   protected int windowset = 0;
34   protected int lightingset = 0;
35   protected int LightFollowCamera = 1;
36   protected int InteractionMode = 1;
37   protected boolean rendering = false;
38   
39   static {
40          vtkJavaDep.load();
41   }
42
43   // Allow access to display lock() and unlock().
44   // Call these whenever you call something that causes
45   // a Render().
46   // e.g.
47   // panel.lock();
48   // // Code that causes a render
49   // panel.unlock();
50   public void lock() {
51     Lock();
52   }
53
54   public void unlock() {
55     UnLock();
56   }
57
58   public void Delete() {
59     if(rendering) {
60       return;
61     }
62     rendering = true;
63     // We prevent any further rendering
64
65     if (this.getParent() != null) {
66       this.getParent().remove(this);
67     }
68     // Free internal VTK objects
69     ren = null;
70     cam = null;
71     lgt = null;
72     // On linux we prefer to have a memory leak instead of a crash
73     if(!rw.GetClassName().equals("vtkXOpenGLRenderWindow")) {
74       rw = null;
75     } else {
76       System.out.println("The renderwindow has been kept around to prevent a crash");
77     }
78   }
79
80   protected native int RenderCreate(vtkRenderWindow id0);
81
82   protected native int Lock();
83
84   protected native int UnLock();
85
86   public vtkPanel() {
87         
88     rw.AddRenderer(ren);
89     addMouseListener(this);
90     addMouseMotionListener(this);
91     addMouseWheelListener(this);
92     addKeyListener(this);
93     super.setSize(200, 200);
94     rw.SetSize(200, 200);
95   }
96
97   public vtkPanel(vtkRenderWindow renwin) {
98     rw = renwin;
99     rw.AddRenderer(ren);
100     addMouseListener(this);
101     addMouseMotionListener(this);
102     addKeyListener(this);
103     super.setSize(200, 200);
104     rw.SetSize(200, 200);
105   }
106
107   public void Report() {
108
109     // must be performed on awt event thread
110     Runnable updateAComponent = new Runnable() {
111       public void run() {
112         Lock();
113         System.out.println("direct rendering = " + (rw.IsDirect() == 1));
114         System.out.println("opengl supported = " + (rw.SupportsOpenGL() == 1));
115         System.out.println("report = " + rw.ReportCapabilities());
116         UnLock();
117       }
118     };
119
120     SwingUtilities.invokeLater(updateAComponent);
121
122   }
123
124   public vtkRenderer GetRenderer() {
125     return ren;
126   }
127
128   public vtkRenderWindow GetRenderWindow() {
129     return rw;
130   }
131
132   public void setSize(int x, int y) {
133     super.setSize(x, y);
134     if (windowset == 1) {
135       Lock();
136       rw.SetSize(x, y);
137       UnLock();
138     }
139   }
140
141   public void addNotify() {
142     super.addNotify();
143     windowset = 0;
144     rw.SetForceMakeCurrent();
145     rendering = false;
146   }
147
148   public void removeNotify() {
149     rendering = true;
150     super.removeNotify();
151   }
152
153   public synchronized void Render() {
154     if (!rendering) {
155       rendering = true;
156       if (ren.VisibleActorCount() == 0) {
157         rendering = false;
158         return;
159       }
160       if (rw != null) {
161         if (windowset == 0) {
162           // set the window id and the active camera
163           cam = ren.GetActiveCamera();
164           if (lightingset == 0) {
165             ren.AddLight(lgt);
166             lgt.SetPosition(cam.GetPosition());
167             lgt.SetFocalPoint(cam.GetFocalPoint());
168             lightingset = 1;
169           }
170           RenderCreate(rw);
171           Lock();
172           rw.SetSize(getWidth(), getHeight());
173           UnLock();
174           windowset = 1;
175           this.setSize(getWidth(), getHeight());
176         }
177         Lock();
178         rw.Render();
179         UnLock();
180         rendering = false;
181       }
182     }
183   }
184
185   public boolean isWindowSet() {
186     return (this.windowset == 1);
187   }
188
189   public void paint(Graphics g) {
190     this.Render();
191   }
192
193   public void update(Graphics g) {
194     paint(g);
195   }
196
197   public void LightFollowCameraOn() {
198     this.LightFollowCamera = 1;
199   }
200
201   public void LightFollowCameraOff() {
202     this.LightFollowCamera = 0;
203   }
204
205   public void InteractionModeRotate() {
206     this.InteractionMode = 1;
207   }
208
209   public void InteractionModeTranslate() {
210     this.InteractionMode = 2;
211   }
212
213   public void InteractionModeZoom() {
214     this.InteractionMode = 3;
215   }
216
217   public void UpdateLight() {
218     lgt.SetPosition(cam.GetPosition());
219     lgt.SetFocalPoint(cam.GetFocalPoint());
220   }
221
222   public void resetCameraClippingRange() {
223     Lock();
224     ren.ResetCameraClippingRange();
225     UnLock();
226   }
227
228   public void resetCamera() {
229     Lock();
230     ren.ResetCamera();
231     UnLock();
232   }
233
234   public void mouseClicked(MouseEvent e) {
235
236   }
237
238   public void mousePressed(MouseEvent e) {
239
240     if (ren.VisibleActorCount() == 0)
241       return;
242     rw.SetDesiredUpdateRate(5.0);
243     lastX = e.getX();
244     lastY = e.getY();
245     if ((e.getModifiers() == InputEvent.BUTTON2_MASK) || (e.getModifiers() == (InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK))) {
246       InteractionModeTranslate();
247     } else if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
248       InteractionModeZoom();
249     } else {
250       InteractionModeRotate();
251     }
252   }
253
254   public void mouseReleased(MouseEvent e) {
255     rw.SetDesiredUpdateRate(0.01);
256   }
257
258   public void mouseEntered(MouseEvent e) {
259     this.requestFocus();
260   }
261
262   public void mouseExited(MouseEvent e) {
263   }
264
265   public void mouseMoved(MouseEvent e) {
266     lastX = e.getX();
267     lastY = e.getY();
268   }
269
270   public void mouseDragged(MouseEvent e) {
271     if (ren.VisibleActorCount() == 0)
272       return;
273     int x = e.getX();
274     int y = e.getY();
275     // rotate
276     if (this.InteractionMode == 1) {
277       cam.Azimuth(lastX - x);
278       cam.Elevation(y - lastY);
279       cam.OrthogonalizeViewUp();
280       resetCameraClippingRange();
281       if (this.LightFollowCamera == 1) {
282         lgt.SetPosition(cam.GetPosition());
283         lgt.SetFocalPoint(cam.GetFocalPoint());
284       }
285     }
286     // translate
287     if (this.InteractionMode == 2) {
288       double FPoint[];
289       double PPoint[];
290       double APoint[] = new double[3];
291       double RPoint[];
292       double focalDepth;
293
294       // get the current focal point and position
295       FPoint = cam.GetFocalPoint();
296       PPoint = cam.GetPosition();
297
298       // calculate the focal depth since we'll be using it a lot
299       ren.SetWorldPoint(FPoint[0], FPoint[1], FPoint[2], 1.0);
300       ren.WorldToDisplay();
301       focalDepth = ren.GetDisplayPoint()[2];
302
303       APoint[0] = rw.GetSize()[0] / 2.0 + (x - lastX);
304       APoint[1] = rw.GetSize()[1] / 2.0 - (y - lastY);
305       APoint[2] = focalDepth;
306       ren.SetDisplayPoint(APoint);
307       ren.DisplayToWorld();
308       RPoint = ren.GetWorldPoint();
309       if (RPoint[3] != 0.0) {
310         RPoint[0] = RPoint[0] / RPoint[3];
311         RPoint[1] = RPoint[1] / RPoint[3];
312         RPoint[2] = RPoint[2] / RPoint[3];
313       }
314
315       /*
316        * Compute a translation vector, moving everything 1/2 the distance
317        * to the cursor. (Arbitrary scale factor)
318        */
319       cam.SetFocalPoint((FPoint[0] - RPoint[0]) / 2.0 + FPoint[0], (FPoint[1] - RPoint[1]) / 2.0 + FPoint[1], (FPoint[2] - RPoint[2]) / 2.0 + FPoint[2]);
320       cam.SetPosition((FPoint[0] - RPoint[0]) / 2.0 + PPoint[0], (FPoint[1] - RPoint[1]) / 2.0 + PPoint[1], (FPoint[2] - RPoint[2]) / 2.0 + PPoint[2]);
321       resetCameraClippingRange();
322     }
323     // zoom
324     if (this.InteractionMode == 3) {
325       double zoomFactor;
326       zoomFactor = Math.pow(1.02, (y - lastY));
327       if (cam.GetParallelProjection() == 1) {
328         cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
329       } else {
330         cam.Dolly(zoomFactor);
331         resetCameraClippingRange();
332       }
333     }
334     lastX = x;
335     lastY = y;
336     this.Render();
337   }
338
339   public void mouseWheelMoved(MouseWheelEvent e) {
340     if (ren.VisibleActorCount() == 0 || e.getWheelRotation() == 0)
341       return;
342     int exponent;
343     exponent = -10 * e.getWheelRotation() / Math.abs(e.getWheelRotation());
344     double zoomFactor;
345     zoomFactor = Math.pow(1.02, exponent);
346     if (cam.GetParallelProjection() == 1) {
347       cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
348     } else {
349       cam.Dolly(zoomFactor);
350       resetCameraClippingRange();
351     }
352     this.Render();
353   }
354
355   public void keyTyped(KeyEvent e) {
356   }
357
358   public void keyPressed(KeyEvent e) {
359     if (ren.VisibleActorCount() == 0)
360       return;
361     char keyChar = e.getKeyChar();
362
363     if ('r' == keyChar) {
364       resetCamera();
365       this.Render();
366     }
367     if ('u' == keyChar) {
368       pickActor(lastX, lastY);
369     }
370     if ('w' == keyChar) {
371       vtkActorCollection ac;
372       vtkActor anActor;
373       int i;
374
375       ac = ren.GetActors();
376       ac.InitTraversal();
377       for (i = 0; i < ac.GetNumberOfItems(); i++) {
378         anActor = ac.GetNextActor();
379         anActor.GetProperty().SetRepresentationToWireframe();
380       }
381       this.Render();
382     }
383     if ('s' == keyChar) {
384       vtkActorCollection ac;
385       vtkActor anActor;
386       int i;
387
388       ac = ren.GetActors();
389       ac.InitTraversal();
390       for (i = 0; i < ac.GetNumberOfItems(); i++) {
391         anActor = ac.GetNextActor();
392         anActor.GetProperty().SetRepresentationToSurface();
393       }
394       this.Render();
395     }
396   }
397
398   public void HardCopy(String filename, int mag) {
399
400     Lock();
401
402     vtkWindowToImageFilter w2if = new vtkWindowToImageFilter();
403     w2if.SetInput(rw);
404
405     w2if.SetScale(mag);
406     w2if.Update();
407
408     vtkTIFFWriter writer = new vtkTIFFWriter();
409     writer.SetInputConnection(w2if.GetOutputPort());
410     writer.SetFileName(filename);
411     writer.Write();
412
413     UnLock();
414   }
415
416   public void pickActor(int x, int y) {
417
418     vtkPropPicker picker = new vtkPropPicker();
419
420     Lock();
421     picker.PickProp(x, rw.GetSize()[1] - y, ren);
422     UnLock();
423
424     if (picker.GetActor() != null)
425       System.out.println(picker.GetActor().GetClassName());
426   }
427
428   public void keyReleased(KeyEvent e) {
429   }
430
431 }