File: vtkPanel.java

package info (click to toggle)
paraview 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 124,600 kB
  • ctags: 133,728
  • sloc: cpp: 958,817; ansic: 509,658; tcl: 45,787; xml: 23,401; python: 19,574; perl: 3,112; yacc: 1,787; java: 1,517; sh: 665; asm: 471; lex: 400; makefile: 168; objc: 28
file content (460 lines) | stat: -rw-r--r-- 11,249 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package vtk;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.SwingUtilities;

public class vtkPanel extends Canvas implements 
  MouseListener, 
  MouseMotionListener, 
  KeyListener
{
  protected vtkRenderWindow rw = new vtkRenderWindow();     
  protected vtkRenderer ren = new vtkRenderer();
  protected vtkCamera cam = null;
  protected vtkLight lgt = new vtkLight();
  protected int lastX;
  protected int lastY;
  protected int windowset = 0;
  protected int lightingset = 0;
  protected int LightFollowCamera = 1;
  protected int InteractionMode = 1;
  protected boolean rendering = false;
  protected WindowObservable windowSetObservable = new WindowObservable();

  static { 
    System.loadLibrary("vtkCommonJava"); 
    System.loadLibrary("vtkFilteringJava"); 
    System.loadLibrary("vtkIOJava"); 
    System.loadLibrary("vtkImagingJava"); 
    System.loadLibrary("vtkGraphicsJava"); 
    System.loadLibrary("vtkRenderingJava"); 
    try {
      System.loadLibrary("vtkHybridJava");
    } catch (Throwable e) {
      System.out.println("cannot load vtkHybrid, skipping...");
    }
    try {
      System.loadLibrary("vtkVolumeRenderingJava");
    } catch (Throwable e) {
      System.out.println("cannot load vtkVolumeRendering, skipping...");
    }
  }

  protected native int RenderCreate(vtkRenderWindow id0);
  protected native int Lock();
  protected native int UnLock();

  public vtkPanel()
  {
    rw.AddRenderer(ren);
    addMouseListener(this);
    addMouseMotionListener(this);
    addKeyListener(this);
    super.setSize(200,200);
    rw.SetSize(200,200);
    addWindowSetObserver(new WindowSetObserver());
  }

  public void Report() {

    // must be performed on awt event thread
    Runnable updateAComponent = new Runnable() {
        public void run() {
          Lock();
          System.out.println("direct rendering = " + (rw.IsDirect()==1));
          System.out.println("opengl supported = " + (rw.SupportsOpenGL()==1));
          System.out.println("report = " + rw.ReportCapabilities());
          UnLock();
        }
      };

    SwingUtilities.invokeLater(updateAComponent);

  }

  public vtkRenderer GetRenderer()
  {
    return ren;
  }
  
  public vtkRenderWindow GetRenderWindow()
  {
    return rw;
  }

  public void addWindowSetObserver(Observer obs) {
    windowSetObservable.addObserver(obs);
  }

  public void removeWindowSetObserver(Observer obs) {
    windowSetObservable.deleteObserver(obs);
  }
  
  public void setSize(int x, int y)
  {
    super.setSize(x,y);
    if (windowset == 1) 
      {
        Lock();
        rw.SetSize(x,y);
        UnLock();
      }
  }

  public void addNotify() 
  {
    super.addNotify();
    windowset = 0;
    rw.SetForceMakeCurrent();
    rendering = false;
  }

  public void removeNotify() 
  {
    rendering = true;
    super.removeNotify();
  }
  
  public synchronized void Render() 
  {
    if (!rendering)
      {
        rendering = true;
        if (ren.VisibleActorCount() == 0) {rendering = false; return;}
        if (rw != null)
          {
            if (windowset == 0)
              {
                // set the window id and the active camera
                cam = ren.GetActiveCamera();
                if (lightingset == 0) {
                  ren.AddLight(lgt);
                  lgt.SetPosition(cam.GetPosition());
                  lgt.SetFocalPoint(cam.GetFocalPoint());
                  lightingset = 1;
                }
                RenderCreate(rw);
                Lock();
                rw.SetSize(getWidth(), getHeight());
                UnLock();
                windowset = 1;
                // notify observers that we have a renderwindow created
                windowSetObservable.notifyObservers();
              }
            Lock();
            rw.Render();
            UnLock();
            rendering = false;
          }
      }
  }

  public boolean isWindowSet() 
  {
    return (this.windowset==1);
  }
  
  public void paint(Graphics g)
  {
    this.Render();
  }

  public void update(Graphics g) {
    paint(g);
  }
  
  public void LightFollowCameraOn()
  {
    this.LightFollowCamera = 1;
  }
  
  public void LightFollowCameraOff()
  {
    this.LightFollowCamera = 0;
  }
  
  public void InteractionModeRotate()
  {
    this.InteractionMode = 1;
  }
  
  public void InteractionModeTranslate()
  {
    this.InteractionMode = 2;
  }
  
  public void InteractionModeZoom()
  {
    this.InteractionMode = 3;
  }
  
  public void UpdateLight()
  {
    lgt.SetPosition(cam.GetPosition());
    lgt.SetFocalPoint(cam.GetFocalPoint());
  }

  public void resetCameraClippingRange() {
    Lock();
    ren.ResetCameraClippingRange();
    UnLock();
  }

  public void resetCamera() {
    Lock();
    ren.ResetCamera();
    UnLock();
  }
  
  public void mouseClicked(MouseEvent e) {
    
  }
  
  public void mousePressed(MouseEvent e)
  {
      
    if (ren.VisibleActorCount() == 0) return;
    rw.SetDesiredUpdateRate(5.0);
    lastX = e.getX();
    lastY = e.getY();
    if ((e.getModifiers()==InputEvent.BUTTON2_MASK) ||
        (e.getModifiers()==(InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK)))
      {
        InteractionModeTranslate();
      }
    else if (e.getModifiers()==InputEvent.BUTTON3_MASK)
      {
        InteractionModeZoom();
      }
    else 
      {
        InteractionModeRotate();
      }
  }
  
  public void mouseReleased(MouseEvent e)
  {
    rw.SetDesiredUpdateRate(0.01);
  }
  
  public void mouseEntered(MouseEvent e) 
  {
    this.requestFocus();
  }
  
  public void mouseExited(MouseEvent e) {}
  
  public void mouseMoved(MouseEvent e) 
  {
    lastX = e.getX();
    lastY = e.getY();
  }
  
  
  public void mouseDragged(MouseEvent e)
  {
    if (ren.VisibleActorCount() == 0) return;
    int x = e.getX();
    int y = e.getY();
    // rotate
    if (this.InteractionMode == 1)
      {
        cam.Azimuth(lastX - x);
        cam.Elevation(y - lastY);
        cam.OrthogonalizeViewUp();
        resetCameraClippingRange();
        if (this.LightFollowCamera == 1)
          {
            lgt.SetPosition(cam.GetPosition());
            lgt.SetFocalPoint(cam.GetFocalPoint());
          }
      }
    // translate
    if (this.InteractionMode == 2)
      {
        double  FPoint[];
        double  PPoint[];
        double  APoint[] = new double[3];
        double  RPoint[];
        double focalDepth;
        
        // get the current focal point and position
        FPoint = cam.GetFocalPoint();
        PPoint = cam.GetPosition();
        
        // calculate the focal depth since we'll be using it a lot
        ren.SetWorldPoint(FPoint[0],FPoint[1],FPoint[2],1.0);
        ren.WorldToDisplay();
        focalDepth = ren.GetDisplayPoint()[2];
        
        APoint[0] = rw.GetSize()[0]/2.0 + (x - lastX);
        APoint[1] = rw.GetSize()[1]/2.0 - (y - lastY);
        APoint[2] = focalDepth;
        ren.SetDisplayPoint(APoint);
        ren.DisplayToWorld();
        RPoint = ren.GetWorldPoint();
        if (RPoint[3] != 0.0)
          {
            RPoint[0] = RPoint[0]/RPoint[3];
            RPoint[1] = RPoint[1]/RPoint[3];
            RPoint[2] = RPoint[2]/RPoint[3];
          }
        
        /*
         * Compute a translation vector, moving everything 1/2 
         * the distance to the cursor. (Arbitrary scale factor)
         */
        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]);
        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]);
        resetCameraClippingRange();
      }
    // zoom
    if (this.InteractionMode == 3)
      {
        double zoomFactor;
        double clippingRange[];
        
        zoomFactor = Math.pow(1.02,(y - lastY));
        if (cam.GetParallelProjection() == 1)
          {
            cam.SetParallelScale(cam.GetParallelScale()/zoomFactor);
          }
        else
          {
            cam.Dolly(zoomFactor);
            resetCameraClippingRange();
          }
      }
    lastX = x;
    lastY = y;
    this.Render();
  }
  
  public void keyTyped(KeyEvent e) {}
  
  public void keyPressed(KeyEvent e)
  {
    if (ren.VisibleActorCount() == 0) return;
    char keyChar = e.getKeyChar();
      
    if ('r' == keyChar)
      {
        resetCamera();
        this.Render();
      }
    if ('u' == keyChar)
      {
        pickActor(lastX, lastY);
      }
    if ('w' == keyChar)
      {
        vtkActorCollection ac;
        vtkActor anActor;
        vtkActor aPart;
        int i, j;
        
        ac = ren.GetActors();
        ac.InitTraversal();
        for (i = 0; i < ac.GetNumberOfItems(); i++)
          {
            anActor = ac.GetNextActor();
            anActor.InitPartTraversal();
            for (j = 0; j < anActor.GetNumberOfParts(); j++)
              { 
                aPart = anActor.GetNextPart();
                aPart.GetProperty().SetRepresentationToWireframe();
              }
          }
        this.Render();
      }
    if ('s' == keyChar)
      {
        vtkActorCollection ac;
        vtkActor anActor;
        vtkActor aPart;
        int i, j;
        
        ac = ren.GetActors();
        ac.InitTraversal();
        for (i = 0; i < ac.GetNumberOfItems(); i++)
          {
            anActor = ac.GetNextActor();
            anActor.InitPartTraversal();
            for (j = 0; j < anActor.GetNumberOfParts(); j++)
              { 
                aPart = anActor.GetNextPart();
                aPart.GetProperty().SetRepresentationToSurface();
              }
          }
        this.Render();
      }
  }

  public void HardCopy(String filename, int mag) { 

    Lock();

    vtkWindowToImageFilter w2if = new vtkWindowToImageFilter();
    w2if.SetInput(rw);

    w2if.SetMagnification(mag);
    w2if.Update();

    vtkTIFFWriter writer = new vtkTIFFWriter();
    writer.SetInput(w2if.GetOutput());
    writer.SetFileName(filename);
    writer.Write();

    UnLock();
  }

  public void pickActor(int x, int y) {

    vtkPropPicker picker = new vtkPropPicker();

    Lock();
    picker.PickProp(x, rw.GetSize()[1] - y , ren);
    UnLock();

    if (picker.GetActor() != null)
      System.out.println(picker.GetActor().GetClassName());
  }
  
  public void keyReleased(KeyEvent e) {}

  private class WindowObservable extends Observable {

    public void notifyObservers() {
      this.setChanged();
      super.notifyObservers();
    }
     
    public void notifyObservers(Object message) {
      this.setChanged();
      super.notifyObservers(message);
    }
    
    public boolean hasObservers() {
      return 0 < super.countObservers();
    }
  }

  private class WindowSetObserver implements Observer {
    
    public void update(Observable o, Object arg) {
      // we know the window is set, so changes to the render window size
      // will actually take place
//       if (getWidth() > 0 && getHeight() > 0)
//         rw.SetSize(getWidth(), getHeight());
    }
    
  }

}