]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk.rendering/src/vtk/rendering/vtkAbstractComponent.java
Compiler warning elimination
[simantics/3d.git] / vtk.rendering / src / vtk / rendering / vtkAbstractComponent.java
1 package vtk.rendering;
2
3 import java.util.concurrent.locks.ReentrantLock;
4
5 import org.eclipse.swt.internal.DPIUtil;
6
7 import vtk.vtkAxesActor;
8 import vtk.vtkCamera;
9 import vtk.vtkGenericRenderWindowInteractor;
10 import vtk.vtkInteractorStyle;
11 import vtk.vtkInteractorStyleTrackballCamera;
12 import vtk.vtkOrientationMarkerWidget;
13 import vtk.vtkRenderWindow;
14 import vtk.vtkRenderer;
15
16 /**
17  * Abstract class that bring most of the VTK logic to any rendering component
18  * regardless its origin. (awt, swt, sing, ...)
19  *
20  * @param <T>
21  *            The concrete type of the graphical component that will contains
22  *            the vtkRenderWindow.
23  *
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.
29  */
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;
39
40   public vtkAbstractComponent() {
41     this(new vtkRenderWindow());
42   }
43
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();
50
51     // Init interactor
52     this.windowInteractor.SetRenderWindow(this.renderWindow);
53     this.windowInteractor.TimerEventResetsTimerOff();
54
55     this.windowInteractor.SetSize(200, 200);
56     this.windowInteractor.ConfigureEvent();
57
58     // Update style
59     vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
60     this.windowInteractor.SetInteractorStyle(style);
61
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");
66
67     // Link renderWindow with renderer
68     this.renderWindow.AddRenderer(this.renderer);
69
70     // Keep camera around to prevent its creation/deletion in Java world
71     this.camera = this.renderer.GetActiveCamera();
72   }
73
74   public ReentrantLock getVTKLock() {
75     return this.lock;
76   }
77
78   public void resetCamera() {
79     if (renderer == null) {
80       return; // Nothing to do we are deleted...
81     }
82
83     try {
84       lock.lockInterruptibly();
85       renderer.ResetCamera();
86     } catch (InterruptedException e) {
87       // Nothing that we can do
88     } finally {
89       this.lock.unlock();
90     }
91   }
92
93   public void resetCameraClippingRange() {
94     if (renderWindow == null) {
95       return; // Nothing to do we are deleted...
96     }
97
98     try {
99       this.lock.lockInterruptibly();
100       renderer.ResetCameraClippingRange();
101     } catch (InterruptedException e) {
102       // Nothing that we can do
103     } finally {
104       this.lock.unlock();
105     }
106   }
107
108   public vtkCamera getActiveCamera() {
109     return this.camera;
110   }
111
112   public vtkRenderer getRenderer() {
113     return this.renderer;
114   }
115
116   public vtkRenderWindow getRenderWindow() {
117     return this.renderWindow;
118   }
119
120   public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
121     return this.windowInteractor;
122   }
123
124   public void setInteractorStyle(vtkInteractorStyle style) {
125     if (this.windowInteractor != null) {
126       this.lock.lock();
127       this.windowInteractor.SetInteractorStyle(style);
128       this.lock.unlock();
129     }
130   }
131
132   public void setSize(int w, int h) {
133     if (renderWindow == null || windowInteractor == null) {
134       return; // Nothing to do we are deleted...
135     }
136     w = DPIUtil.autoScaleUp(w);
137     h = DPIUtil.autoScaleUp(h);
138     try {
139       lock.lockInterruptibly();
140       renderWindow.SetSize(w, h);
141       windowInteractor.SetSize(w, h);
142     } catch (InterruptedException e) {
143       // Nothing that we can do
144     } finally {
145       this.lock.unlock();
146     }
147   }
148
149   public void Delete() {
150     this.lock.lock();
151     this.renderer.DrawOff();
152     this.renderer.Delete();
153     this.renderer = null;
154     this.camera.Delete();
155     this.camera = null;
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.
161     this.lock.unlock();
162   }
163
164   public vtkInteractorForwarder getInteractorForwarder() {
165     return this.eventForwarder;
166   }
167
168   public abstract T getComponent();
169
170   /**
171    * Generic helper method used to attach orientation axes to a vtkComponent
172    *
173    * @param vtkComponent<?>
174    */
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();
180
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();
188   }
189 }