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 @SuppressWarnings("restriction")
31 public abstract class vtkAbstractComponent<T> implements vtkComponent<T> {
32 protected vtkRenderWindow renderWindow;
33 protected vtkRenderer renderer;
34 protected vtkCamera camera;
35 protected vtkGenericRenderWindowInteractor windowInteractor;
36 protected vtkInteractorForwarder eventForwarder;
37 protected ReentrantLock lock;
38 protected boolean inRenderCall;
40 public vtkAbstractComponent() {
41 this(new vtkRenderWindow());
44 public vtkAbstractComponent(vtkRenderWindow renderWindowToUse) {
45 this.inRenderCall = false;
46 this.renderWindow = renderWindowToUse;
47 this.renderer = new vtkRenderer();
48 this.windowInteractor = new vtkGenericRenderWindowInteractor();
49 this.lock = new ReentrantLock();
52 this.windowInteractor.SetRenderWindow(this.renderWindow);
53 this.windowInteractor.TimerEventResetsTimerOff();
55 this.windowInteractor.SetSize(200, 200);
56 this.windowInteractor.ConfigureEvent();
59 vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
60 this.windowInteractor.SetInteractorStyle(style);
62 // Setup event forwarder
63 this.eventForwarder = new vtkInteractorForwarder(this);
64 this.windowInteractor.AddObserver("CreateTimerEvent", this.eventForwarder, "StartTimer");
65 this.windowInteractor.AddObserver("DestroyTimerEvent", this.eventForwarder, "DestroyTimer");
67 // Link renderWindow with renderer
68 this.renderWindow.AddRenderer(this.renderer);
70 // Keep camera around to prevent its creation/deletion in Java world
71 this.camera = this.renderer.GetActiveCamera();
74 public ReentrantLock getVTKLock() {
78 public void resetCamera() {
79 if (renderer == null) {
80 return; // Nothing to do we are deleted...
84 lock.lockInterruptibly();
85 renderer.ResetCamera();
86 } catch (InterruptedException e) {
87 // Nothing that we can do
93 public void resetCameraClippingRange() {
94 if (renderWindow == null) {
95 return; // Nothing to do we are deleted...
99 this.lock.lockInterruptibly();
100 renderer.ResetCameraClippingRange();
101 } catch (InterruptedException e) {
102 // Nothing that we can do
108 public vtkCamera getActiveCamera() {
112 public vtkRenderer getRenderer() {
113 return this.renderer;
116 public vtkRenderWindow getRenderWindow() {
117 return this.renderWindow;
120 public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
121 return this.windowInteractor;
124 public void setInteractorStyle(vtkInteractorStyle style) {
125 if (this.windowInteractor != null) {
127 this.windowInteractor.SetInteractorStyle(style);
132 public void setSize(int w, int h) {
133 if (renderWindow == null || windowInteractor == null) {
134 return; // Nothing to do we are deleted...
136 w = DPIUtil.autoScaleUp(w);
137 h = DPIUtil.autoScaleUp(h);
139 lock.lockInterruptibly();
140 renderWindow.SetSize(w, h);
141 windowInteractor.SetSize(w, h);
142 } catch (InterruptedException e) {
143 // Nothing that we can do
149 public void Delete() {
151 this.renderer.DrawOff();
152 this.renderer.Delete();
153 this.renderer = null;
154 this.camera.Delete();
156 this.windowInteractor.Delete();
157 this.windowInteractor = null;
158 // removing the renderWindow is let to the superclass
159 // because in the very special case of an AWT component
160 // under Linux, destroying renderWindow crashes.
164 public vtkInteractorForwarder getInteractorForwarder() {
165 return this.eventForwarder;
168 public abstract T getComponent();
171 * Generic helper method used to attach orientation axes to a vtkComponent
173 * @param vtkComponent<?>
175 public static void attachOrientationAxes(vtkComponent<?> component) {
176 // only build this once, because it creates its own renderer.
177 // Extra renderers causes issues with resetting.
178 vtkAxesActor axes = new vtkAxesActor();
179 vtkOrientationMarkerWidget axesWidget = new vtkOrientationMarkerWidget();
181 axesWidget.SetOutlineColor(0.9300, 0.5700, 0.1300);
182 axesWidget.SetOrientationMarker(axes);
183 axesWidget.SetInteractor(component.getRenderWindowInteractor());
184 axesWidget.SetDefaultRenderer(component.getRenderer());
185 axesWidget.SetViewport(0.0, 0.0, .2, .2);
186 axesWidget.EnabledOn();
187 axesWidget.InteractiveOff();