3 import java.util.concurrent.locks.ReentrantLock;
5 import org.eclipse.swt.internal.DPIUtil;
7 import vtk.vtkAxesActor;
9 import vtk.vtkGenericRenderWindowInteractor;
10 import vtk.vtkInteractorStyle;
11 import vtk.vtkInteractorStyleTrackballCamera;
12 import vtk.vtkOrientationMarkerWidget;
13 import vtk.vtkRenderWindow;
14 import vtk.vtkRenderer;
17 * Abstract class that bring most of the VTK logic to any rendering component
18 * regardless its origin. (awt, swt, sing, ...)
21 * The concrete type of the graphical component that will contains
22 * the vtkRenderWindow.
24 * @authors Sebastien Jourdain - sebastien.jourdain@kitware.com, Kitware Inc 2012
25 * Joachim Pouderoux - joachim.pouderoux@kitware.com, Kitware SAS 2012
26 * @copyright This work was supported by CEA/CESTA
27 * Commissariat a l'Energie Atomique et aux Energies Alternatives,
28 * 15 avenue des Sablieres, CS 60001, 33116 Le Barp, France.
30 public abstract class vtkAbstractComponent<T> implements vtkComponent<T> {
31 protected vtkRenderWindow renderWindow;
32 protected vtkRenderer renderer;
33 protected vtkCamera camera;
34 protected vtkGenericRenderWindowInteractor windowInteractor;
35 protected vtkInteractorForwarder eventForwarder;
36 protected ReentrantLock lock;
37 protected boolean inRenderCall;
39 public vtkAbstractComponent() {
40 this(new vtkRenderWindow());
43 public vtkAbstractComponent(vtkRenderWindow renderWindowToUse) {
44 this.inRenderCall = false;
45 this.renderWindow = renderWindowToUse;
46 this.renderer = new vtkRenderer();
47 this.windowInteractor = new vtkGenericRenderWindowInteractor();
48 this.lock = new ReentrantLock();
51 this.windowInteractor.SetRenderWindow(this.renderWindow);
52 this.windowInteractor.TimerEventResetsTimerOff();
54 this.windowInteractor.SetSize(200, 200);
55 this.windowInteractor.ConfigureEvent();
58 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
59 this.windowInteractor.SetInteractorStyle(style);
61 // Setup event forwarder
62 this.eventForwarder = new vtkInteractorForwarder(this);
63 this.windowInteractor.AddObserver("CreateTimerEvent", this.eventForwarder, "StartTimer");
64 this.windowInteractor.AddObserver("DestroyTimerEvent", this.eventForwarder, "DestroyTimer");
66 // Link renderWindow with renderer
67 this.renderWindow.AddRenderer(this.renderer);
69 // Keep camera around to prevent its creation/deletion in Java world
70 this.camera = this.renderer.GetActiveCamera();
73 public ReentrantLock getVTKLock() {
77 public void resetCamera() {
78 if (renderer == null) {
79 return; // Nothing to do we are deleted...
83 lock.lockInterruptibly();
84 renderer.ResetCamera();
85 } catch (InterruptedException e) {
86 // Nothing that we can do
92 public void resetCameraClippingRange() {
93 if (renderWindow == null) {
94 return; // Nothing to do we are deleted...
98 this.lock.lockInterruptibly();
99 renderer.ResetCameraClippingRange();
100 } catch (InterruptedException e) {
101 // Nothing that we can do
107 public vtkCamera getActiveCamera() {
111 public vtkRenderer getRenderer() {
112 return this.renderer;
115 public vtkRenderWindow getRenderWindow() {
116 return this.renderWindow;
119 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
120 return this.windowInteractor;
123 public void setInteractorStyle(vtkInteractorStyle style) {
124 if (this.windowInteractor != null) {
126 this.windowInteractor.SetInteractorStyle(style);
131 public void setSize(int w, int h) {
132 if (renderWindow == null || windowInteractor == null) {
133 return; // Nothing to do we are deleted...
135 w = DPIUtil.autoScaleUp(w);
136 h = DPIUtil.autoScaleUp(h);
138 lock.lockInterruptibly();
139 renderWindow.SetSize(w, h);
140 windowInteractor.SetSize(w, h);
141 } catch (InterruptedException e) {
142 // Nothing that we can do
148 public void Delete() {
150 this.renderer.DrawOff();
151 this.renderer.Delete();
152 this.renderer = null;
153 this.camera.Delete();
155 this.windowInteractor.Delete();
156 this.windowInteractor = null;
157 // removing the renderWindow is let to the superclass
158 // because in the very special case of an AWT component
159 // under Linux, destroying renderWindow crashes.
163 public vtkInteractorForwarder getInteractorForwarder() {
164 return this.eventForwarder;
167 public abstract T getComponent();
170 * Generic helper method used to attach orientation axes to a vtkComponent
172 * @param vtkComponent<?>
174 public static void attachOrientationAxes(vtkComponent<?> component) {
175 // only build this once, because it creates its own renderer.
176 // Extra renderers causes issues with resetting.
177 vtkAxesActor axes = new vtkAxesActor();
178 vtkOrientationMarkerWidget axesWidget = new vtkOrientationMarkerWidget();
180 axesWidget.SetOutlineColor(0.9300, 0.5700, 0.1300);
181 axesWidget.SetOrientationMarker(axes);
182 axesWidget.SetInteractor(component.getRenderWindowInteractor());
183 axesWidget.SetDefaultRenderer(component.getRenderer());
184 axesWidget.SetViewport(0.0, 0.0, .2, .2);
185 axesWidget.EnabledOn();
186 axesWidget.InteractiveOff();