1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2011, Purdue Research Foundation |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef __RAPPTURE_VTKVIS_RENDERER_H__ |
---|
9 | #define __RAPPTURE_VTKVIS_RENDERER_H__ |
---|
10 | |
---|
11 | #include <vtkSmartPointer.h> |
---|
12 | #include <vtkLookupTable.h> |
---|
13 | #include <vtkCubeAxesActor.h> |
---|
14 | #ifdef USE_CUSTOM_AXES |
---|
15 | #include <vtkRpCubeAxesActor2D.h> |
---|
16 | #else |
---|
17 | #include <vtkCubeAxesActor2D.h> |
---|
18 | #endif |
---|
19 | #include <vtkScalarBarActor.h> |
---|
20 | #include <vtkRenderer.h> |
---|
21 | #include <vtkRenderWindow.h> |
---|
22 | #include <vtkUnsignedCharArray.h> |
---|
23 | |
---|
24 | #include <string> |
---|
25 | #include <vector> |
---|
26 | #include <tr1/unordered_map> |
---|
27 | |
---|
28 | #include "ColorMap.h" |
---|
29 | #include "RpVtkDataSet.h" |
---|
30 | #include "RpPseudoColor.h" |
---|
31 | #include "RpContour2D.h" |
---|
32 | #include "RpPolyData.h" |
---|
33 | |
---|
34 | namespace Rappture { |
---|
35 | namespace VtkVis { |
---|
36 | |
---|
37 | /** |
---|
38 | * \brief VTK Renderer |
---|
39 | */ |
---|
40 | class Renderer |
---|
41 | { |
---|
42 | public: |
---|
43 | Renderer(); |
---|
44 | virtual ~Renderer(); |
---|
45 | |
---|
46 | enum Axis { |
---|
47 | X_AXIS, |
---|
48 | Y_AXIS, |
---|
49 | Z_AXIS |
---|
50 | }; |
---|
51 | |
---|
52 | enum AxesFlyMode { |
---|
53 | FLY_OUTER_EDGES = 0, |
---|
54 | FLY_CLOSEST_TRIAD, |
---|
55 | FLY_FURTHEST_TRIAD, |
---|
56 | FLY_STATIC_EDGES, |
---|
57 | FLY_STATIC_TRIAD |
---|
58 | }; |
---|
59 | |
---|
60 | enum CameraMode { |
---|
61 | PERSPECTIVE, |
---|
62 | ORTHO, |
---|
63 | IMAGE |
---|
64 | }; |
---|
65 | |
---|
66 | typedef std::string DataSetId; |
---|
67 | typedef std::string ColorMapId; |
---|
68 | typedef std::tr1::unordered_map<DataSetId, DataSet *> DataSetHashmap; |
---|
69 | typedef std::tr1::unordered_map<ColorMapId, ColorMap *> ColorMapHashmap; |
---|
70 | typedef std::tr1::unordered_map<DataSetId, PseudoColor *> PseudoColorHashmap; |
---|
71 | typedef std::tr1::unordered_map<DataSetId, Contour2D *> Contour2DHashmap; |
---|
72 | typedef std::tr1::unordered_map<DataSetId, PolyData *> PolyDataHashmap; |
---|
73 | |
---|
74 | // Data sets |
---|
75 | |
---|
76 | void addDataSet(const DataSetId& id); |
---|
77 | |
---|
78 | void deleteDataSet(const DataSetId& id); |
---|
79 | |
---|
80 | DataSet *getDataSet(const DataSetId& id); |
---|
81 | |
---|
82 | bool setData(const DataSetId& id, char *data, int nbytes); |
---|
83 | |
---|
84 | bool setDataFile(const DataSetId& id, const char *filename); |
---|
85 | |
---|
86 | double getDataValueAtPixel(const DataSetId& id, int x, int y); |
---|
87 | |
---|
88 | double getDataValue(const DataSetId& id, double x, double y, double z); |
---|
89 | |
---|
90 | void setOpacity(const DataSetId& id, double opacity); |
---|
91 | |
---|
92 | void setVisibility(const DataSetId& id, bool state); |
---|
93 | |
---|
94 | void setUseCumulativeDataRange(bool state, bool onlyVisible = false); |
---|
95 | |
---|
96 | // Render window |
---|
97 | |
---|
98 | void setWindowSize(int width, int height); |
---|
99 | |
---|
100 | int getWindowWidth() const; |
---|
101 | |
---|
102 | int getWindowHeight() const; |
---|
103 | |
---|
104 | // Camera controls |
---|
105 | |
---|
106 | void setCameraMode(CameraMode mode); |
---|
107 | |
---|
108 | CameraMode getCameraMode() const; |
---|
109 | |
---|
110 | void resetCamera(bool resetOrientation = true); |
---|
111 | |
---|
112 | void setCameraZoomRegion(double x, double y, double width, double height); |
---|
113 | |
---|
114 | void getCameraZoomRegion(double xywh[4]) const; |
---|
115 | |
---|
116 | void getScreenWorldCoords(double xywh[4]) const; |
---|
117 | |
---|
118 | void rotateCamera(double yaw, double pitch, double roll); |
---|
119 | |
---|
120 | void setCameraOrientation(double quat[4]); |
---|
121 | |
---|
122 | void setCameraOrientationAndPosition(double position[3], |
---|
123 | double focalPoint[3], |
---|
124 | double viewUp[3]); |
---|
125 | |
---|
126 | void getCameraOrientationAndPosition(double position[3], |
---|
127 | double focalPoint[3], |
---|
128 | double viewUp[3]); |
---|
129 | |
---|
130 | void panCamera(double x, double y, bool absolute = true); |
---|
131 | |
---|
132 | void zoomCamera(double z, bool absolute = true); |
---|
133 | |
---|
134 | // Rendering an image |
---|
135 | |
---|
136 | void setBackgroundColor(float color[3]); |
---|
137 | |
---|
138 | bool render(); |
---|
139 | |
---|
140 | void getRenderedFrame(vtkUnsignedCharArray *imgData); |
---|
141 | |
---|
142 | // Axes |
---|
143 | |
---|
144 | void setAxesFlyMode(AxesFlyMode mode); |
---|
145 | |
---|
146 | void setAxesGridVisibility(bool state); |
---|
147 | |
---|
148 | void setAxesVisibility(bool state); |
---|
149 | |
---|
150 | void setAxesColor(double color[3]); |
---|
151 | |
---|
152 | void setAxisGridVisibility(Axis axis, bool state); |
---|
153 | |
---|
154 | void setAxisVisibility(Axis axis, bool state); |
---|
155 | |
---|
156 | void setAxisTitle(Axis axis, const char *title); |
---|
157 | |
---|
158 | void setAxisUnits(Axis axis, const char *units); |
---|
159 | |
---|
160 | // Colormaps |
---|
161 | |
---|
162 | void addColorMap(const ColorMapId& id, ColorMap *colorMap); |
---|
163 | |
---|
164 | void deleteColorMap(const ColorMapId& id); |
---|
165 | |
---|
166 | ColorMap *getColorMap(const ColorMapId& id); |
---|
167 | |
---|
168 | bool renderColorMap(const ColorMapId& id, |
---|
169 | const DataSetId& dataSetID, |
---|
170 | const char *title, |
---|
171 | int width, int height, |
---|
172 | vtkUnsignedCharArray *imgData); |
---|
173 | |
---|
174 | // Color-mapped surfaces |
---|
175 | |
---|
176 | void addPseudoColor(const DataSetId& id); |
---|
177 | |
---|
178 | void deletePseudoColor(const DataSetId& id); |
---|
179 | |
---|
180 | PseudoColor *getPseudoColor(const DataSetId& id); |
---|
181 | |
---|
182 | void setPseudoColorColorMap(const DataSetId& id, const ColorMapId& colorMapId); |
---|
183 | |
---|
184 | vtkLookupTable *getPseudoColorColorMap(const DataSetId& id); |
---|
185 | |
---|
186 | void setPseudoColorOpacity(const DataSetId& id, double opacity); |
---|
187 | |
---|
188 | void setPseudoColorVisibility(const DataSetId& id, bool state); |
---|
189 | |
---|
190 | void setPseudoColorEdgeVisibility(const DataSetId& id, bool state); |
---|
191 | |
---|
192 | void setPseudoColorEdgeColor(const DataSetId& id, float color[3]); |
---|
193 | |
---|
194 | void setPseudoColorEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
195 | |
---|
196 | void setPseudoColorLighting(const DataSetId& id, bool state); |
---|
197 | |
---|
198 | // Contour plots |
---|
199 | |
---|
200 | void addContour2D(const DataSetId& id); |
---|
201 | |
---|
202 | void deleteContour2D(const DataSetId& id); |
---|
203 | |
---|
204 | Contour2D *getContour2D(const DataSetId& id); |
---|
205 | |
---|
206 | void setContours(const DataSetId& id, int numContours); |
---|
207 | |
---|
208 | void setContourList(const DataSetId& id, const std::vector<double>& contours); |
---|
209 | |
---|
210 | void setContourOpacity(const DataSetId& id, double opacity); |
---|
211 | |
---|
212 | void setContourVisibility(const DataSetId& id, bool state); |
---|
213 | |
---|
214 | void setContourEdgeColor(const DataSetId& id, float color[3]); |
---|
215 | |
---|
216 | void setContourEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
217 | |
---|
218 | void setContourLighting(const DataSetId& id, bool state); |
---|
219 | |
---|
220 | // Meshes |
---|
221 | |
---|
222 | void addPolyData(const DataSetId& id); |
---|
223 | |
---|
224 | void deletePolyData(const DataSetId& id); |
---|
225 | |
---|
226 | PolyData *getPolyData(const DataSetId& id); |
---|
227 | |
---|
228 | void setPolyDataOpacity(const DataSetId& id, double opacity); |
---|
229 | |
---|
230 | void setPolyDataVisibility(const DataSetId& id, bool state); |
---|
231 | |
---|
232 | void setPolyDataColor(const DataSetId& id, float color[3]); |
---|
233 | |
---|
234 | void setPolyDataEdgeVisibility(const DataSetId& id, bool state); |
---|
235 | |
---|
236 | void setPolyDataEdgeColor(const DataSetId& id, float color[3]); |
---|
237 | |
---|
238 | void setPolyDataEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
239 | |
---|
240 | void setPolyDataWireframe(const DataSetId& id, bool state); |
---|
241 | |
---|
242 | void setPolyDataLighting(const DataSetId& id, bool state); |
---|
243 | |
---|
244 | private: |
---|
245 | static void printCameraInfo(vtkCamera *camera); |
---|
246 | static inline double min2(double a, double b) |
---|
247 | { |
---|
248 | return ((a < b) ? a : b); |
---|
249 | } |
---|
250 | static inline double max2(double a, double b) |
---|
251 | { |
---|
252 | return ((a > b) ? a : b); |
---|
253 | } |
---|
254 | static void mergeBounds(double *boundsDest, const double *bounds1, const double *bounds2); |
---|
255 | |
---|
256 | void collectBounds(double *bounds, bool onlyVisible); |
---|
257 | |
---|
258 | void collectDataRanges(double *range, bool onlyVisible); |
---|
259 | |
---|
260 | void updateRanges(bool useCumulative); |
---|
261 | |
---|
262 | void computeDisplayToWorld(double x, double y, double z, double worldPt[4]); |
---|
263 | |
---|
264 | void computeWorldToDisplay(double x, double y, double z, double displayPt[3]); |
---|
265 | |
---|
266 | void computeScreenWorldCoords(); |
---|
267 | |
---|
268 | void storeCameraOrientation(); |
---|
269 | void restoreCameraOrientation(); |
---|
270 | void initCamera(); |
---|
271 | void initAxes(); |
---|
272 | void resetAxes(); |
---|
273 | |
---|
274 | bool _needsRedraw; |
---|
275 | int _windowWidth, _windowHeight; |
---|
276 | double _imgWorldOrigin[2]; |
---|
277 | double _imgWorldDims[2]; |
---|
278 | double _screenWorldCoords[4]; |
---|
279 | double _cameraPos[3]; |
---|
280 | double _cameraFocalPoint[3]; |
---|
281 | double _cameraUp[3]; |
---|
282 | double _cameraZoomRatio; |
---|
283 | double _cameraPan[2]; |
---|
284 | float _bgColor[3]; |
---|
285 | bool _useCumulativeRange; |
---|
286 | bool _cumulativeRangeOnlyVisible; |
---|
287 | double _cumulativeDataRange[2]; |
---|
288 | |
---|
289 | ColorMapHashmap _colorMaps; |
---|
290 | DataSetHashmap _dataSets; |
---|
291 | PseudoColorHashmap _pseudoColors; |
---|
292 | Contour2DHashmap _contours; |
---|
293 | PolyDataHashmap _polyDatas; |
---|
294 | |
---|
295 | CameraMode _cameraMode; |
---|
296 | |
---|
297 | vtkSmartPointer<vtkPlaneCollection> _clippingPlanes; |
---|
298 | vtkSmartPointer<vtkCubeAxesActor> _cubeAxesActor; // For 3D view |
---|
299 | #ifdef USE_CUSTOM_AXES |
---|
300 | vtkSmartPointer<vtkRpCubeAxesActor2D> _cubeAxesActor2D; // For 2D view |
---|
301 | #else |
---|
302 | vtkSmartPointer<vtkCubeAxesActor2D> _cubeAxesActor2D; // For 2D view |
---|
303 | #endif |
---|
304 | vtkSmartPointer<vtkScalarBarActor> _scalarBarActor; |
---|
305 | vtkSmartPointer<vtkRenderer> _renderer; |
---|
306 | vtkSmartPointer<vtkRenderer> _legendRenderer; |
---|
307 | vtkSmartPointer<vtkRenderWindow> _renderWindow; |
---|
308 | vtkSmartPointer<vtkRenderWindow> _legendRenderWindow; |
---|
309 | }; |
---|
310 | |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | #endif |
---|