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