File: vtkAbstractComponent.java

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (181 lines) | stat: -rw-r--r-- 5,474 bytes parent folder | download | duplicates (12)
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
package vtk.rendering;

import java.util.concurrent.locks.ReentrantLock;

import vtk.vtkAxesActor;
import vtk.vtkCamera;
import vtk.vtkGenericRenderWindowInteractor;
import vtk.vtkInteractorStyle;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkOrientationMarkerWidget;
import vtk.vtkRenderWindow;
import vtk.vtkRenderer;

/**
 * Abstract class that bring most of the VTK logic to any rendering component
 * regardless its origin. (awt, swt, sing, ...)
 *
 * @param <T>
 *            The concrete type of the graphical component that will contains
 *            the vtkRenderWindow.
 *
 * @authors Sebastien Jourdain - sebastien.jourdain@kitware.com, Kitware Inc 2012
 *          Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
 * @copyright This work was supported by CEA/CESTA
 *            Commissariat a l'Energie Atomique et aux Energies Alternatives,
 *            15 avenue des Sablieres, CS 60001, 33116 Le Barp, France.
 */
public abstract class vtkAbstractComponent<T> implements vtkComponent<T> {
  protected vtkRenderWindow renderWindow;
  protected vtkRenderer renderer;
  protected vtkCamera camera;
  protected vtkGenericRenderWindowInteractor windowInteractor;
  protected vtkInteractorForwarder eventForwarder;
  protected ReentrantLock lock;
  protected boolean inRenderCall;

  public vtkAbstractComponent() {
    this(new vtkRenderWindow());
  }

  public vtkAbstractComponent(vtkRenderWindow renderWindowToUse) {
    this.inRenderCall = false;
    this.renderWindow = renderWindowToUse;
    this.renderer = new vtkRenderer();
    this.windowInteractor = new vtkGenericRenderWindowInteractor();
    this.lock = new ReentrantLock();

    // Init interactor
    this.windowInteractor.SetRenderWindow(this.renderWindow);
    this.windowInteractor.TimerEventResetsTimerOff();

    this.windowInteractor.SetSize(200, 200);
    this.windowInteractor.ConfigureEvent();

    // Update style
    vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
    this.windowInteractor.SetInteractorStyle(style);

    // Setup event forwarder
    this.eventForwarder = new vtkInteractorForwarder(this);
    this.windowInteractor.AddObserver("CreateTimerEvent", this.eventForwarder, "StartTimer");
    this.windowInteractor.AddObserver("DestroyTimerEvent", this.eventForwarder, "DestroyTimer");

    // Link renderWindow with renderer
    this.renderWindow.AddRenderer(this.renderer);

    // Keep camera around to prevent its creation/deletion in Java world
    this.camera = this.renderer.GetActiveCamera();
  }

  public ReentrantLock getVTKLock() {
    return this.lock;
  }

  public void resetCamera() {
    if (renderer == null) {
      return; // Nothing to do we are deleted...
    }

    try {
      lock.lockInterruptibly();
      renderer.ResetCamera();
    } catch (InterruptedException e) {
      // Nothing that we can do
    } finally {
      this.lock.unlock();
    }
  }

  public void resetCameraClippingRange() {
    if (renderWindow == null) {
      return; // Nothing to do we are deleted...
    }

    try {
      this.lock.lockInterruptibly();
      renderer.ResetCameraClippingRange();
    } catch (InterruptedException e) {
      // Nothing that we can do
    } finally {
      this.lock.unlock();
    }
  }

  public vtkCamera getActiveCamera() {
    return this.camera;
  }

  public vtkRenderer getRenderer() {
    return this.renderer;
  }

  public vtkRenderWindow getRenderWindow() {
    return this.renderWindow;
  }

  public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
    return this.windowInteractor;
  }

  public void setInteractorStyle(vtkInteractorStyle style) {
    if (this.windowInteractor != null) {
      this.lock.lock();
      this.windowInteractor.SetInteractorStyle(style);
      this.lock.unlock();
    }
  }

  public void setSize(int w, int h) {
    if (renderWindow == null || windowInteractor == null) {
      return; // Nothing to do we are deleted...
    }

    try {
      lock.lockInterruptibly();
      renderWindow.SetSize(w, h);
      windowInteractor.SetSize(w, h);
    } catch (InterruptedException e) {
      // Nothing that we can do
    } finally {
      this.lock.unlock();
    }
  }

  public void Delete() {
    this.lock.lock();
    this.renderer = null;
    this.camera = null;
    this.windowInteractor = null;
    // removing the renderWindow is let to the superclass
    // because in the very special case of an AWT component
    // under Linux, destroying renderWindow crashes.
    this.lock.unlock();
  }

  public vtkInteractorForwarder getInteractorForwarder() {
    return this.eventForwarder;
  }

  public abstract T getComponent();

  /**
   * Generic helper method used to attach orientation axes to a vtkComponent
   *
   * @param vtkComponent<?>
   */
  public static void attachOrientationAxes(vtkComponent<?> component) {
    // only build this once, because it creates its own renderer.
    // Extra renderers causes issues with resetting.
    vtkAxesActor axes = new vtkAxesActor();
    vtkOrientationMarkerWidget axesWidget = new vtkOrientationMarkerWidget();

    axesWidget.SetOutlineColor(0.9300, 0.5700, 0.1300);
    axesWidget.SetOrientationMarker(axes);
    axesWidget.SetInteractor(component.getRenderWindowInteractor());
    axesWidget.SetDefaultRenderer(component.getRenderer());
    axesWidget.SetViewport(0.0, 0.0, .2, .2);
    axesWidget.EnabledOn();
    axesWidget.InteractiveOff();
  }
}