]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk.rendering/src/vtk/rendering/vtkAbstractComponent.java
c905a72f3020a614d6fdce63f13ca87af3376478
[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 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;
38
39   public vtkAbstractComponent() {
40     this(new vtkRenderWindow());
41   }
42
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();
49
50     // Init interactor
51     this.windowInteractor.SetRenderWindow(this.renderWindow);
52     this.windowInteractor.TimerEventResetsTimerOff();
53
54     this.windowInteractor.SetSize(200, 200);
55     this.windowInteractor.ConfigureEvent();
56
57     // Update style
58     vtkInteractorStyleTrackballCamera style = new vtkInteractorStyleTrackballCamera();
59     this.windowInteractor.SetInteractorStyle(style);
60
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");
65
66     // Link renderWindow with renderer
67     this.renderWindow.AddRenderer(this.renderer);
68
69     // Keep camera around to prevent its creation/deletion in Java world
70     this.camera = this.renderer.GetActiveCamera();
71   }
72
73   public ReentrantLock getVTKLock() {
74     return this.lock;
75   }
76
77   public void resetCamera() {
78     if (renderer == null) {
79       return; // Nothing to do we are deleted...
80     }
81
82     try {
83       lock.lockInterruptibly();
84       renderer.ResetCamera();
85     } catch (InterruptedException e) {
86       // Nothing that we can do
87     } finally {
88       this.lock.unlock();
89     }
90   }
91
92   public void resetCameraClippingRange() {
93     if (renderWindow == null) {
94       return; // Nothing to do we are deleted...
95     }
96
97     try {
98       this.lock.lockInterruptibly();
99       renderer.ResetCameraClippingRange();
100     } catch (InterruptedException e) {
101       // Nothing that we can do
102     } finally {
103       this.lock.unlock();
104     }
105   }
106
107   public vtkCamera getActiveCamera() {
108     return this.camera;
109   }
110
111   public vtkRenderer getRenderer() {
112     return this.renderer;
113   }
114
115   public vtkRenderWindow getRenderWindow() {
116     return this.renderWindow;
117   }
118
119   public vtkGenericRenderWindowInteractor getRenderWindowInteractor() {
120     return this.windowInteractor;
121   }
122
123   public void setInteractorStyle(vtkInteractorStyle style) {
124     if (this.windowInteractor != null) {
125       this.lock.lock();
126       this.windowInteractor.SetInteractorStyle(style);
127       this.lock.unlock();
128     }
129   }
130
131   public void setSize(int w, int h) {
132     if (renderWindow == null || windowInteractor == null) {
133       return; // Nothing to do we are deleted...
134     }
135     w = DPIUtil.autoScaleUp(w);
136     h = DPIUtil.autoScaleUp(h);
137     try {
138       lock.lockInterruptibly();
139       renderWindow.SetSize(w, h);
140       windowInteractor.SetSize(w, h);
141     } catch (InterruptedException e) {
142       // Nothing that we can do
143     } finally {
144       this.lock.unlock();
145     }
146   }
147
148   public void Delete() {
149     this.lock.lock();
150     this.renderer.DrawOff();
151     this.renderer.Delete();
152     this.renderer = null;
153     this.camera.Delete();
154     this.camera = null;
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.
160     this.lock.unlock();
161   }
162
163   public vtkInteractorForwarder getInteractorForwarder() {
164     return this.eventForwarder;
165   }
166
167   public abstract T getComponent();
168
169   /**
170    * Generic helper method used to attach orientation axes to a vtkComponent
171    *
172    * @param vtkComponent<?>
173    */
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();
179
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();
187   }
188 }