3 import java.util.concurrent.locks.ReentrantLock;
5 import vtk.vtkAxesActor;
7 import vtk.vtkGenericRenderWindowInteractor;
8 import vtk.vtkInteractorStyle;
9 import vtk.vtkInteractorStyleTrackballCamera;
10 import vtk.vtkOrientationMarkerWidget;
11 import vtk.vtkRenderWindow;
12 import vtk.vtkRenderer;
15 * Abstract class that bring most of the VTK logic to any rendering component
16 * regardless its origin. (awt, swt, sing, ...)
19 * The concrete type of the graphical component that will contains
20 * the vtkRenderWindow.
22 * @authors Sebastien Jourdain - sebastien.jourdain@kitware.com, Kitware Inc 2012
23 * Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
24 * @copyright This work was supported by CEA/CESTA
25 * Commissariat a l'Energie Atomique et aux Energies Alternatives,
26 * 15 avenue des Sablieres, CS 60001, 33116 Le Barp, France.
28 public abstract class vtkAbstractComponent<T> implements vtkComponent<T> {
29 protected vtkRenderWindow renderWindow;
30 protected vtkRenderer renderer;
31 protected vtkCamera camera;
32 protected vtkGenericRenderWindowInteractor windowInteractor;
33 protected vtkInteractorForwarder eventForwarder;
34 protected ReentrantLock lock;
35 protected boolean inRenderCall;
37 public vtkAbstractComponent() {
38 this(new vtkRenderWindow());
41 public vtkAbstractComponent(vtkRenderWindow renderWindowToUse) {
42 this.inRenderCall = false;
43 this.renderWindow = renderWindowToUse;
44 this.renderer = new vtkRenderer();
45 this.windowInteractor = new vtkGenericRenderWindowInteractor();
46 this.lock = new ReentrantLock();
49 this.windowInteractor.SetRenderWindow(this.renderWindow);
50 this.windowInteractor.TimerEventResetsTimerOff();
52 this.windowInteractor.SetSize(200, 200);
53 this.windowInteractor.ConfigureEvent();
56 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
57 this.windowInteractor.SetInteractorStyle(style);
59 // Setup event forwarder
60 this.eventForwarder = new vtkInteractorForwarder(this);
61 this.windowInteractor.AddObserver("CreateTimerEvent", this.eventForwarder, "StartTimer");
62 this.windowInteractor.AddObserver("DestroyTimerEvent", this.eventForwarder, "DestroyTimer");
64 // Link renderWindow with renderer
65 this.renderWindow.AddRenderer(this.renderer);
67 // Keep camera around to prevent its creation/deletion in Java world
68 this.camera = this.renderer.GetActiveCamera();
71 public ReentrantLock getVTKLock() {
75 public void resetCamera() {
76 if (renderer == null) {
77 return; // Nothing to do we are deleted...
81 lock.lockInterruptibly();
82 renderer.ResetCamera();
83 } catch (InterruptedException e) {
84 // Nothing that we can do
90 public void resetCameraClippingRange() {
91 if (renderWindow == null) {
92 return; // Nothing to do we are deleted...
96 this.lock.lockInterruptibly();
97 renderer.ResetCameraClippingRange();
98 } catch (InterruptedException e) {
99 // Nothing that we can do
105 public vtkCamera getActiveCamera() {
109 public vtkRenderer getRenderer() {
110 return this.renderer;
113 public vtkRenderWindow getRenderWindow() {
114 return this.renderWindow;
117 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
118 return this.windowInteractor;
121 public void setInteractorStyle(vtkInteractorStyle style) {
122 if (this.windowInteractor != null) {
124 this.windowInteractor.SetInteractorStyle(style);
129 public void setSize(int w, int h) {
130 if (renderWindow == null || windowInteractor == null) {
131 return; // Nothing to do we are deleted...
135 lock.lockInterruptibly();
136 renderWindow.SetSize(w, h);
137 windowInteractor.SetSize(w, h);
138 } catch (InterruptedException e) {
139 // Nothing that we can do
145 public void Delete() {
147 this.renderer = null;
149 this.windowInteractor = null;
150 // removing the renderWindow is let to the superclass
151 // because in the very special case of an AWT component
152 // under Linux, destroying renderWindow crashes.
156 public vtkInteractorForwarder getInteractorForwarder() {
157 return this.eventForwarder;
160 public abstract T getComponent();
163 * Generic helper method used to attach orientation axes to a vtkComponent
165 * @param vtkComponent<?>
167 public static void attachOrientationAxes(vtkComponent<?> component) {
168 // only build this once, because it creates its own renderer.
169 // Extra renderers causes issues with resetting.
170 vtkAxesActor axes = new vtkAxesActor();
171 vtkOrientationMarkerWidget axesWidget = new vtkOrientationMarkerWidget();
173 axesWidget.SetOutlineColor(0.9300, 0.5700, 0.1300);
174 axesWidget.SetOrientationMarker(axes);
175 axesWidget.SetInteractor(component.getRenderWindowInteractor());
176 axesWidget.SetDefaultRenderer(component.getRenderer());
177 axesWidget.SetViewport(0.0, 0.0, .2, .2);
178 axesWidget.EnabledOn();
179 axesWidget.InteractiveOff();