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 <vtkCubeAxesActor.h> |
---|
13 | #ifdef USE_CUSTOM_AXES |
---|
14 | #include <vtkRpCubeAxesActor2D.h> |
---|
15 | #else |
---|
16 | #include <vtkCubeAxesActor2D.h> |
---|
17 | #endif |
---|
18 | #include <vtkScalarBarActor.h> |
---|
19 | #include <vtkRenderer.h> |
---|
20 | #include <vtkRenderWindow.h> |
---|
21 | #include <vtkUnsignedCharArray.h> |
---|
22 | |
---|
23 | #include <string> |
---|
24 | #include <vector> |
---|
25 | #include <tr1/unordered_map> |
---|
26 | |
---|
27 | #include "ColorMap.h" |
---|
28 | #include "RpVtkDataSet.h" |
---|
29 | #include "RpContour2D.h" |
---|
30 | #include "RpGlyphs.h" |
---|
31 | #include "RpHeightMap.h" |
---|
32 | #include "RpPolyData.h" |
---|
33 | #include "RpPseudoColor.h" |
---|
34 | #include "RpVolume.h" |
---|
35 | #include "Trace.h" |
---|
36 | |
---|
37 | // Controls if TGA format is sent to client |
---|
38 | //#define RENDER_TARGA |
---|
39 | #define TARGA_BYTES_PER_PIXEL 3 |
---|
40 | |
---|
41 | namespace Rappture { |
---|
42 | namespace VtkVis { |
---|
43 | |
---|
44 | /** |
---|
45 | * \brief VTK Renderer |
---|
46 | */ |
---|
47 | class Renderer |
---|
48 | { |
---|
49 | public: |
---|
50 | Renderer(); |
---|
51 | virtual ~Renderer(); |
---|
52 | |
---|
53 | enum Axis { |
---|
54 | X_AXIS, |
---|
55 | Y_AXIS, |
---|
56 | Z_AXIS |
---|
57 | }; |
---|
58 | |
---|
59 | enum AxesFlyMode { |
---|
60 | FLY_OUTER_EDGES = 0, |
---|
61 | FLY_CLOSEST_TRIAD, |
---|
62 | FLY_FURTHEST_TRIAD, |
---|
63 | FLY_STATIC_EDGES, |
---|
64 | FLY_STATIC_TRIAD |
---|
65 | }; |
---|
66 | |
---|
67 | enum CameraMode { |
---|
68 | PERSPECTIVE, |
---|
69 | ORTHO, |
---|
70 | IMAGE |
---|
71 | }; |
---|
72 | |
---|
73 | typedef std::string DataSetId; |
---|
74 | typedef std::string ColorMapId; |
---|
75 | typedef std::tr1::unordered_map<DataSetId, DataSet *> DataSetHashmap; |
---|
76 | typedef std::tr1::unordered_map<ColorMapId, ColorMap *> ColorMapHashmap; |
---|
77 | typedef std::tr1::unordered_map<DataSetId, Contour2D *> Contour2DHashmap; |
---|
78 | typedef std::tr1::unordered_map<DataSetId, Glyphs *> GlyphsHashmap; |
---|
79 | typedef std::tr1::unordered_map<DataSetId, HeightMap *> HeightMapHashmap; |
---|
80 | typedef std::tr1::unordered_map<DataSetId, PolyData *> PolyDataHashmap; |
---|
81 | typedef std::tr1::unordered_map<DataSetId, PseudoColor *> PseudoColorHashmap; |
---|
82 | typedef std::tr1::unordered_map<DataSetId, Volume *> VolumeHashmap; |
---|
83 | |
---|
84 | // Data sets |
---|
85 | |
---|
86 | void addDataSet(const DataSetId& id); |
---|
87 | |
---|
88 | void deleteDataSet(const DataSetId& id); |
---|
89 | |
---|
90 | DataSet *getDataSet(const DataSetId& id); |
---|
91 | |
---|
92 | bool setData(const DataSetId& id, char *data, int nbytes); |
---|
93 | |
---|
94 | bool setDataFile(const DataSetId& id, const char *filename); |
---|
95 | |
---|
96 | double getDataValueAtPixel(const DataSetId& id, int x, int y); |
---|
97 | |
---|
98 | double getDataValue(const DataSetId& id, double x, double y, double z); |
---|
99 | |
---|
100 | void setOpacity(const DataSetId& id, double opacity); |
---|
101 | |
---|
102 | void setVisibility(const DataSetId& id, bool state); |
---|
103 | |
---|
104 | void setUseCumulativeDataRange(bool state, bool onlyVisible = false); |
---|
105 | |
---|
106 | // Render window |
---|
107 | |
---|
108 | void setWindowSize(int width, int height); |
---|
109 | |
---|
110 | int getWindowWidth() const; |
---|
111 | |
---|
112 | int getWindowHeight() const; |
---|
113 | |
---|
114 | // Camera controls |
---|
115 | |
---|
116 | void setCameraMode(CameraMode mode); |
---|
117 | |
---|
118 | CameraMode getCameraMode() const; |
---|
119 | |
---|
120 | void resetCamera(bool resetOrientation = true); |
---|
121 | |
---|
122 | void setCameraZoomRegion(double x, double y, double width, double height); |
---|
123 | |
---|
124 | void getCameraZoomRegion(double xywh[4]) const; |
---|
125 | |
---|
126 | void getScreenWorldCoords(double xywh[4]) const; |
---|
127 | |
---|
128 | void rotateCamera(double yaw, double pitch, double roll); |
---|
129 | |
---|
130 | void setCameraOrientation(double quat[4]); |
---|
131 | |
---|
132 | void setCameraOrientationAndPosition(double position[3], |
---|
133 | double focalPoint[3], |
---|
134 | double viewUp[3]); |
---|
135 | |
---|
136 | void getCameraOrientationAndPosition(double position[3], |
---|
137 | double focalPoint[3], |
---|
138 | double viewUp[3]); |
---|
139 | |
---|
140 | void panCamera(double x, double y, bool absolute = true); |
---|
141 | |
---|
142 | void zoomCamera(double z, bool absolute = true); |
---|
143 | |
---|
144 | // Rendering an image |
---|
145 | |
---|
146 | void setBackgroundColor(float color[3]); |
---|
147 | |
---|
148 | bool render(); |
---|
149 | |
---|
150 | void getRenderedFrame(vtkUnsignedCharArray *imgData); |
---|
151 | |
---|
152 | // Axes |
---|
153 | |
---|
154 | void setAxesFlyMode(AxesFlyMode mode); |
---|
155 | |
---|
156 | void setAxesGridVisibility(bool state); |
---|
157 | |
---|
158 | void setAxesVisibility(bool state); |
---|
159 | |
---|
160 | void setAxesColor(double color[3]); |
---|
161 | |
---|
162 | void setAxisGridVisibility(Axis axis, bool state); |
---|
163 | |
---|
164 | void setAxisVisibility(Axis axis, bool state); |
---|
165 | |
---|
166 | void setAxisTitle(Axis axis, const char *title); |
---|
167 | |
---|
168 | void setAxisUnits(Axis axis, const char *units); |
---|
169 | |
---|
170 | // Colormaps |
---|
171 | |
---|
172 | void addColorMap(const ColorMapId& id, ColorMap *colorMap); |
---|
173 | |
---|
174 | void deleteColorMap(const ColorMapId& id); |
---|
175 | |
---|
176 | ColorMap *getColorMap(const ColorMapId& id); |
---|
177 | |
---|
178 | bool renderColorMap(const ColorMapId& id, |
---|
179 | const DataSetId& dataSetID, |
---|
180 | const char *title, |
---|
181 | int width, int height, |
---|
182 | vtkUnsignedCharArray *imgData); |
---|
183 | |
---|
184 | // Contour plots |
---|
185 | |
---|
186 | void addContour2D(const DataSetId& id); |
---|
187 | |
---|
188 | void deleteContour2D(const DataSetId& id); |
---|
189 | |
---|
190 | Contour2D *getContour2D(const DataSetId& id); |
---|
191 | |
---|
192 | void setContours(const DataSetId& id, int numContours); |
---|
193 | |
---|
194 | void setContourList(const DataSetId& id, const std::vector<double>& contours); |
---|
195 | |
---|
196 | void setContourOpacity(const DataSetId& id, double opacity); |
---|
197 | |
---|
198 | void setContourVisibility(const DataSetId& id, bool state); |
---|
199 | |
---|
200 | void setContourEdgeColor(const DataSetId& id, float color[3]); |
---|
201 | |
---|
202 | void setContourEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
203 | |
---|
204 | void setContourLighting(const DataSetId& id, bool state); |
---|
205 | |
---|
206 | // Glyphs |
---|
207 | |
---|
208 | void addGlyphs(const DataSetId& id); |
---|
209 | |
---|
210 | void deleteGlyphs(const DataSetId& id); |
---|
211 | |
---|
212 | Glyphs *getGlyphs(const DataSetId& id); |
---|
213 | |
---|
214 | void setGlyphsColorMap(const DataSetId& id, const ColorMapId& colorMapId); |
---|
215 | |
---|
216 | void setGlyphsShape(const DataSetId& id, Glyphs::GlyphShape shape); |
---|
217 | |
---|
218 | void setGlyphsScaleFactor(const DataSetId& id, double scale); |
---|
219 | |
---|
220 | void setGlyphsOpacity(const DataSetId& id, double opacity); |
---|
221 | |
---|
222 | void setGlyphsVisibility(const DataSetId& id, bool state); |
---|
223 | |
---|
224 | void setGlyphsLighting(const DataSetId& id, bool state); |
---|
225 | |
---|
226 | // Height maps |
---|
227 | |
---|
228 | void addHeightMap(const DataSetId& id); |
---|
229 | |
---|
230 | void deleteHeightMap(const DataSetId& id); |
---|
231 | |
---|
232 | HeightMap *getHeightMap(const DataSetId& id); |
---|
233 | |
---|
234 | void setHeightMapVolumeSlice(const DataSetId& id, HeightMap::Axis axis, double ratio); |
---|
235 | |
---|
236 | void setHeightMapHeightScale(const DataSetId& id, double scale); |
---|
237 | |
---|
238 | void setHeightMapColorMap(const DataSetId& id, const ColorMapId& colorMapId); |
---|
239 | |
---|
240 | void setHeightMapContours(const DataSetId& id, int numContours); |
---|
241 | |
---|
242 | void setHeightMapContourList(const DataSetId& id, const std::vector<double>& contours); |
---|
243 | |
---|
244 | void setHeightMapOpacity(const DataSetId& id, double opacity); |
---|
245 | |
---|
246 | void setHeightMapVisibility(const DataSetId& id, bool state); |
---|
247 | |
---|
248 | void setHeightMapEdgeVisibility(const DataSetId& id, bool state); |
---|
249 | |
---|
250 | void setHeightMapEdgeColor(const DataSetId& id, float color[3]); |
---|
251 | |
---|
252 | void setHeightMapEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
253 | |
---|
254 | void setHeightMapContourVisibility(const DataSetId& id, bool state); |
---|
255 | |
---|
256 | void setHeightMapContourEdgeColor(const DataSetId& id, float color[3]); |
---|
257 | |
---|
258 | void setHeightMapContourEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
259 | |
---|
260 | void setHeightMapLighting(const DataSetId& id, bool state); |
---|
261 | |
---|
262 | // Meshes |
---|
263 | |
---|
264 | void addPolyData(const DataSetId& id); |
---|
265 | |
---|
266 | void deletePolyData(const DataSetId& id); |
---|
267 | |
---|
268 | PolyData *getPolyData(const DataSetId& id); |
---|
269 | |
---|
270 | void setPolyDataOpacity(const DataSetId& id, double opacity); |
---|
271 | |
---|
272 | void setPolyDataVisibility(const DataSetId& id, bool state); |
---|
273 | |
---|
274 | void setPolyDataColor(const DataSetId& id, float color[3]); |
---|
275 | |
---|
276 | void setPolyDataEdgeVisibility(const DataSetId& id, bool state); |
---|
277 | |
---|
278 | void setPolyDataEdgeColor(const DataSetId& id, float color[3]); |
---|
279 | |
---|
280 | void setPolyDataEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
281 | |
---|
282 | void setPolyDataWireframe(const DataSetId& id, bool state); |
---|
283 | |
---|
284 | void setPolyDataLighting(const DataSetId& id, bool state); |
---|
285 | |
---|
286 | // Color-mapped surfaces |
---|
287 | |
---|
288 | void addPseudoColor(const DataSetId& id); |
---|
289 | |
---|
290 | void deletePseudoColor(const DataSetId& id); |
---|
291 | |
---|
292 | PseudoColor *getPseudoColor(const DataSetId& id); |
---|
293 | |
---|
294 | void setPseudoColorColorMap(const DataSetId& id, const ColorMapId& colorMapId); |
---|
295 | |
---|
296 | void setPseudoColorOpacity(const DataSetId& id, double opacity); |
---|
297 | |
---|
298 | void setPseudoColorVisibility(const DataSetId& id, bool state); |
---|
299 | |
---|
300 | void setPseudoColorEdgeVisibility(const DataSetId& id, bool state); |
---|
301 | |
---|
302 | void setPseudoColorEdgeColor(const DataSetId& id, float color[3]); |
---|
303 | |
---|
304 | void setPseudoColorEdgeWidth(const DataSetId& id, float edgeWidth); |
---|
305 | |
---|
306 | void setPseudoColorLighting(const DataSetId& id, bool state); |
---|
307 | |
---|
308 | // Volumes |
---|
309 | |
---|
310 | void addVolume(const DataSetId& id); |
---|
311 | |
---|
312 | void deleteVolume(const DataSetId& id); |
---|
313 | |
---|
314 | Volume *getVolume(const DataSetId& id); |
---|
315 | |
---|
316 | void setVolumeColorMap(const DataSetId& id, const ColorMapId& colorMapId); |
---|
317 | |
---|
318 | void setVolumeOpacity(const DataSetId& id, double opacity); |
---|
319 | |
---|
320 | void setVolumeVisibility(const DataSetId& id, bool state); |
---|
321 | |
---|
322 | void setVolumeAmbient(const DataSetId& id, double coeff); |
---|
323 | |
---|
324 | void setVolumeDiffuse(const DataSetId& id, double coeff); |
---|
325 | |
---|
326 | void setVolumeSpecular(const DataSetId& id, double coeff, double power); |
---|
327 | |
---|
328 | void setVolumeLighting(const DataSetId& id, bool state); |
---|
329 | |
---|
330 | private: |
---|
331 | static void printCameraInfo(vtkCamera *camera); |
---|
332 | static inline double min2(double a, double b) |
---|
333 | { |
---|
334 | return ((a < b) ? a : b); |
---|
335 | } |
---|
336 | static inline double max2(double a, double b) |
---|
337 | { |
---|
338 | return ((a > b) ? a : b); |
---|
339 | } |
---|
340 | static void mergeBounds(double *boundsDest, const double *bounds1, const double *bounds2); |
---|
341 | |
---|
342 | void collectBounds(double *bounds, bool onlyVisible); |
---|
343 | |
---|
344 | void collectDataRanges(double *range, bool onlyVisible); |
---|
345 | |
---|
346 | void updateRanges(bool useCumulative); |
---|
347 | |
---|
348 | void computeDisplayToWorld(double x, double y, double z, double worldPt[4]); |
---|
349 | |
---|
350 | void computeWorldToDisplay(double x, double y, double z, double displayPt[3]); |
---|
351 | |
---|
352 | void computeScreenWorldCoords(); |
---|
353 | |
---|
354 | void storeCameraOrientation(); |
---|
355 | void restoreCameraOrientation(); |
---|
356 | void initCamera(); |
---|
357 | void initAxes(); |
---|
358 | void resetAxes(); |
---|
359 | |
---|
360 | bool _needsRedraw; |
---|
361 | int _windowWidth, _windowHeight; |
---|
362 | double _imgWorldOrigin[2]; |
---|
363 | double _imgWorldDims[2]; |
---|
364 | double _screenWorldCoords[4]; |
---|
365 | double _cameraPos[3]; |
---|
366 | double _cameraFocalPoint[3]; |
---|
367 | double _cameraUp[3]; |
---|
368 | double _cameraZoomRatio; |
---|
369 | double _cameraPan[2]; |
---|
370 | float _bgColor[3]; |
---|
371 | bool _useCumulativeRange; |
---|
372 | bool _cumulativeRangeOnlyVisible; |
---|
373 | double _cumulativeDataRange[2]; |
---|
374 | |
---|
375 | ColorMapHashmap _colorMaps; |
---|
376 | DataSetHashmap _dataSets; |
---|
377 | Contour2DHashmap _contours; |
---|
378 | GlyphsHashmap _glyphs; |
---|
379 | HeightMapHashmap _heightMaps; |
---|
380 | PolyDataHashmap _polyDatas; |
---|
381 | PseudoColorHashmap _pseudoColors; |
---|
382 | VolumeHashmap _volumes; |
---|
383 | |
---|
384 | CameraMode _cameraMode; |
---|
385 | |
---|
386 | vtkSmartPointer<vtkPlaneCollection> _clippingPlanes; |
---|
387 | vtkSmartPointer<vtkCubeAxesActor> _cubeAxesActor; // For 3D view |
---|
388 | #ifdef USE_CUSTOM_AXES |
---|
389 | vtkSmartPointer<vtkRpCubeAxesActor2D> _cubeAxesActor2D; // For 2D view |
---|
390 | #else |
---|
391 | vtkSmartPointer<vtkCubeAxesActor2D> _cubeAxesActor2D; // For 2D view |
---|
392 | #endif |
---|
393 | vtkSmartPointer<vtkScalarBarActor> _scalarBarActor; |
---|
394 | vtkSmartPointer<vtkRenderer> _renderer; |
---|
395 | vtkSmartPointer<vtkRenderer> _legendRenderer; |
---|
396 | vtkSmartPointer<vtkRenderWindow> _renderWindow; |
---|
397 | vtkSmartPointer<vtkRenderWindow> _legendRenderWindow; |
---|
398 | }; |
---|
399 | |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | #endif |
---|