1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2012 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <cfloat> |
---|
9 | #include <cstring> |
---|
10 | #include <cassert> |
---|
11 | #include <cmath> |
---|
12 | |
---|
13 | #include <GL/gl.h> |
---|
14 | |
---|
15 | #ifdef WANT_TRACE |
---|
16 | #include <sys/time.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <vtkMath.h> |
---|
20 | #include <vtkCamera.h> |
---|
21 | #include <vtkLight.h> |
---|
22 | #include <vtkCoordinate.h> |
---|
23 | #include <vtkTransform.h> |
---|
24 | #include <vtkCharArray.h> |
---|
25 | #ifdef USE_CUSTOM_AXES |
---|
26 | #include "vtkRpCubeAxesActor.h" |
---|
27 | #else |
---|
28 | #include <vtkCubeAxesActor.h> |
---|
29 | #endif |
---|
30 | #include <vtkDataSetReader.h> |
---|
31 | #include <vtkDataSetMapper.h> |
---|
32 | #include <vtkContourFilter.h> |
---|
33 | #include <vtkPolyDataMapper.h> |
---|
34 | #include <vtkProperty.h> |
---|
35 | #include <vtkProperty2D.h> |
---|
36 | #include <vtkPointData.h> |
---|
37 | #include <vtkLookupTable.h> |
---|
38 | #include <vtkTextProperty.h> |
---|
39 | #include <vtkOpenGLRenderWindow.h> |
---|
40 | #include <vtkVersion.h> |
---|
41 | |
---|
42 | #include "VtkRenderer.h" |
---|
43 | #include "VtkRendererGraphicsObjs.h" |
---|
44 | #include "Math.h" |
---|
45 | #include "ColorMap.h" |
---|
46 | #include "Trace.h" |
---|
47 | |
---|
48 | #define MSECS_ELAPSED(t1, t2) \ |
---|
49 | ((t1).tv_sec == (t2).tv_sec ? (((t2).tv_usec - (t1).tv_usec)/1.0e+3) : \ |
---|
50 | (((t2).tv_sec - (t1).tv_sec))*1.0e+3 + (double)((t2).tv_usec - (t1).tv_usec)/1.0e+3) |
---|
51 | |
---|
52 | #define printCameraInfo(camera) \ |
---|
53 | do { \ |
---|
54 | TRACE("pscale: %g, angle: %g, d: %g pos: %g %g %g, fpt: %g %g %g, vup: %g %g %g, clip: %g %g", \ |
---|
55 | (camera)->GetParallelScale(), \ |
---|
56 | (camera)->GetViewAngle(), \ |
---|
57 | (camera)->GetDistance(), \ |
---|
58 | (camera)->GetPosition()[0], \ |
---|
59 | (camera)->GetPosition()[1], \ |
---|
60 | (camera)->GetPosition()[2], \ |
---|
61 | (camera)->GetFocalPoint()[0], \ |
---|
62 | (camera)->GetFocalPoint()[1], \ |
---|
63 | (camera)->GetFocalPoint()[2], \ |
---|
64 | (camera)->GetViewUp()[0], \ |
---|
65 | (camera)->GetViewUp()[1], \ |
---|
66 | (camera)->GetViewUp()[2], \ |
---|
67 | (camera)->GetClippingRange()[0], \ |
---|
68 | (camera)->GetClippingRange()[1]); \ |
---|
69 | } while(0) |
---|
70 | |
---|
71 | using namespace VtkVis; |
---|
72 | |
---|
73 | Renderer::Renderer() : |
---|
74 | _needsRedraw(true), |
---|
75 | _needsAxesReset(false), |
---|
76 | _needsCameraClippingRangeReset(false), |
---|
77 | _needsCameraReset(false), |
---|
78 | _windowWidth(500), |
---|
79 | _windowHeight(500), |
---|
80 | _cameraMode(PERSPECTIVE), |
---|
81 | _cameraAspect(ASPECT_NATIVE), |
---|
82 | _imgCameraPlane(PLANE_XY), |
---|
83 | _imgCameraOffset(0), |
---|
84 | _cameraZoomRatio(1), |
---|
85 | _useCumulativeRange(true), |
---|
86 | _cumulativeRangeOnlyVisible(false) |
---|
87 | { |
---|
88 | _bgColor[0] = 0; |
---|
89 | _bgColor[1] = 0; |
---|
90 | _bgColor[2] = 0; |
---|
91 | _cameraPan[0] = 0; |
---|
92 | _cameraPan[1] = 0; |
---|
93 | _cameraOrientation[0] = 1.0; |
---|
94 | _cameraOrientation[1] = 0.0; |
---|
95 | _cameraOrientation[2] = 0.0; |
---|
96 | _cameraOrientation[3] = 0.0; |
---|
97 | // clipping planes to prevent overdrawing axes |
---|
98 | _activeClipPlanes = vtkSmartPointer<vtkPlaneCollection>::New(); |
---|
99 | // bottom |
---|
100 | _cameraClipPlanes[0] = vtkSmartPointer<vtkPlane>::New(); |
---|
101 | _cameraClipPlanes[0]->SetNormal(0, 1, 0); |
---|
102 | _cameraClipPlanes[0]->SetOrigin(0, 0, 0); |
---|
103 | if (_cameraMode == IMAGE) |
---|
104 | _activeClipPlanes->AddItem(_cameraClipPlanes[0]); |
---|
105 | // left |
---|
106 | _cameraClipPlanes[1] = vtkSmartPointer<vtkPlane>::New(); |
---|
107 | _cameraClipPlanes[1]->SetNormal(1, 0, 0); |
---|
108 | _cameraClipPlanes[1]->SetOrigin(0, 0, 0); |
---|
109 | if (_cameraMode == IMAGE) |
---|
110 | _activeClipPlanes->AddItem(_cameraClipPlanes[1]); |
---|
111 | // top |
---|
112 | _cameraClipPlanes[2] = vtkSmartPointer<vtkPlane>::New(); |
---|
113 | _cameraClipPlanes[2]->SetNormal(0, -1, 0); |
---|
114 | _cameraClipPlanes[2]->SetOrigin(0, 1, 0); |
---|
115 | if (_cameraMode == IMAGE) |
---|
116 | _activeClipPlanes->AddItem(_cameraClipPlanes[2]); |
---|
117 | // right |
---|
118 | _cameraClipPlanes[3] = vtkSmartPointer<vtkPlane>::New(); |
---|
119 | _cameraClipPlanes[3]->SetNormal(-1, 0, 0); |
---|
120 | _cameraClipPlanes[3]->SetOrigin(1, 0, 0); |
---|
121 | if (_cameraMode == IMAGE) |
---|
122 | _activeClipPlanes->AddItem(_cameraClipPlanes[3]); |
---|
123 | _renderer = vtkSmartPointer<vtkRenderer>::New(); |
---|
124 | |
---|
125 | // Global ambient (defaults to 1,1,1) |
---|
126 | //_renderer->SetAmbient(.2, .2, .2); |
---|
127 | |
---|
128 | vtkSmartPointer<vtkLight> headlight = vtkSmartPointer<vtkLight>::New(); |
---|
129 | headlight->SetLightTypeToHeadlight(); |
---|
130 | headlight->PositionalOff(); |
---|
131 | // Light ambient color defaults to 0,0,0 |
---|
132 | //headlight->SetAmbientColor(1, 1, 1); |
---|
133 | _renderer->AddLight(headlight); |
---|
134 | vtkSmartPointer<vtkLight> skylight = vtkSmartPointer<vtkLight>::New(); |
---|
135 | skylight->SetLightTypeToCameraLight(); |
---|
136 | skylight->SetPosition(0, 1, 0); |
---|
137 | skylight->SetFocalPoint(0, 0, 0); |
---|
138 | skylight->PositionalOff(); |
---|
139 | // Light ambient color defaults to 0,0,0 |
---|
140 | //skylight->SetAmbientColor(1, 1, 1); |
---|
141 | _renderer->AddLight(skylight); |
---|
142 | _renderer->LightFollowCameraOn(); |
---|
143 | _renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); |
---|
144 | #ifdef USE_OFFSCREEN_RENDERING |
---|
145 | _renderWindow->DoubleBufferOff(); |
---|
146 | _renderWindow->OffScreenRenderingOn(); |
---|
147 | #else |
---|
148 | _renderWindow->SwapBuffersOff(); |
---|
149 | #endif |
---|
150 | _renderWindow->SetSize(_windowWidth, _windowHeight); |
---|
151 | // Next 2 options needed to support depth peeling |
---|
152 | _renderWindow->SetAlphaBitPlanes(1); |
---|
153 | _renderWindow->SetMultiSamples(0); |
---|
154 | _renderer->SetMaximumNumberOfPeels(100); |
---|
155 | _renderer->SetUseDepthPeeling(1); |
---|
156 | _renderer->SetTwoSidedLighting(1); |
---|
157 | _renderWindow->AddRenderer(_renderer); |
---|
158 | setViewAngle(_windowHeight); |
---|
159 | initAxes(); |
---|
160 | initCamera(); |
---|
161 | addColorMap("default", ColorMap::getDefault()); |
---|
162 | addColorMap("grayDefault", ColorMap::getGrayDefault()); |
---|
163 | addColorMap("volumeDefault", ColorMap::getVolumeDefault()); |
---|
164 | addColorMap("elementDefault", ColorMap::getElementDefault()); |
---|
165 | } |
---|
166 | |
---|
167 | Renderer::~Renderer() |
---|
168 | { |
---|
169 | TRACE("Enter"); |
---|
170 | |
---|
171 | deleteAllGraphicsObjects<Arc>(); |
---|
172 | deleteAllGraphicsObjects<Arrow>(); |
---|
173 | deleteAllGraphicsObjects<Box>(); |
---|
174 | deleteAllGraphicsObjects<Cone>(); |
---|
175 | deleteAllGraphicsObjects<Contour2D>(); |
---|
176 | deleteAllGraphicsObjects<Contour3D>(); |
---|
177 | deleteAllGraphicsObjects<Cutplane>(); |
---|
178 | deleteAllGraphicsObjects<Cylinder>(); |
---|
179 | deleteAllGraphicsObjects<Disk>(); |
---|
180 | deleteAllGraphicsObjects<Glyphs>(); |
---|
181 | deleteAllGraphicsObjects<Group>(); |
---|
182 | deleteAllGraphicsObjects<HeightMap>(); |
---|
183 | deleteAllGraphicsObjects<LIC>(); |
---|
184 | deleteAllGraphicsObjects<Line>(); |
---|
185 | deleteAllGraphicsObjects<Molecule>(); |
---|
186 | deleteAllGraphicsObjects<Outline>(); |
---|
187 | deleteAllGraphicsObjects<PolyData>(); |
---|
188 | deleteAllGraphicsObjects<Polygon>(); |
---|
189 | deleteAllGraphicsObjects<PseudoColor>(); |
---|
190 | deleteAllGraphicsObjects<Sphere>(); |
---|
191 | deleteAllGraphicsObjects<Streamlines>(); |
---|
192 | deleteAllGraphicsObjects<Volume>(); |
---|
193 | deleteAllGraphicsObjects<Warp>(); |
---|
194 | |
---|
195 | TRACE("Deleting ColorMaps"); |
---|
196 | // Delete color maps and data sets last in case references still |
---|
197 | // exist |
---|
198 | for (ColorMapHashmap::iterator itr = _colorMaps.begin(); |
---|
199 | itr != _colorMaps.end(); ++itr) { |
---|
200 | delete itr->second; |
---|
201 | } |
---|
202 | _colorMaps.clear(); |
---|
203 | TRACE("Deleting DataSets"); |
---|
204 | for (DataSetHashmap::iterator itr = _dataSets.begin(); |
---|
205 | itr != _dataSets.end(); ++itr) { |
---|
206 | delete itr->second; |
---|
207 | } |
---|
208 | _dataSets.clear(); |
---|
209 | |
---|
210 | clearFieldRanges(); |
---|
211 | clearUserFieldRanges(); |
---|
212 | |
---|
213 | TRACE("Leave"); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * \brief Add a DataSet to this Renderer |
---|
218 | * |
---|
219 | * This just adds the DataSet to the Renderer's list of data sets. |
---|
220 | * In order to render the data, a graphics object using the data |
---|
221 | * set must be added to the Renderer. |
---|
222 | */ |
---|
223 | void Renderer::addDataSet(const DataSetId& id) |
---|
224 | { |
---|
225 | if (getDataSet(id) != NULL) { |
---|
226 | WARN("Replacing existing DataSet %s", id.c_str()); |
---|
227 | deleteDataSet(id); |
---|
228 | } |
---|
229 | _dataSets[id] = new DataSet(id); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * \brief Remove the specified DataSet and associated rendering objects |
---|
234 | * |
---|
235 | * The underlying DataSet and any associated graphics |
---|
236 | * objects are deleted, freeing the memory used. |
---|
237 | */ |
---|
238 | void Renderer::deleteDataSet(const DataSetId& id) |
---|
239 | { |
---|
240 | DataSetHashmap::iterator itr; |
---|
241 | |
---|
242 | bool doAll = false; |
---|
243 | |
---|
244 | if (id.compare("all") == 0) { |
---|
245 | itr = _dataSets.begin(); |
---|
246 | doAll = true; |
---|
247 | } else { |
---|
248 | itr = _dataSets.find(id); |
---|
249 | } |
---|
250 | if (itr == _dataSets.end()) { |
---|
251 | ERROR("Unknown dataset %s", id.c_str()); |
---|
252 | return; |
---|
253 | } |
---|
254 | |
---|
255 | do { |
---|
256 | TRACE("Deleting dataset %s", itr->second->getName().c_str()); |
---|
257 | |
---|
258 | deleteGraphicsObject<Contour2D>(itr->second->getName()); |
---|
259 | deleteGraphicsObject<Contour3D>(itr->second->getName()); |
---|
260 | deleteGraphicsObject<Cutplane>(itr->second->getName()); |
---|
261 | deleteGraphicsObject<Glyphs>(itr->second->getName()); |
---|
262 | deleteGraphicsObject<HeightMap>(itr->second->getName()); |
---|
263 | deleteGraphicsObject<LIC>(itr->second->getName()); |
---|
264 | deleteGraphicsObject<Molecule>(itr->second->getName()); |
---|
265 | deleteGraphicsObject<Outline>(itr->second->getName()); |
---|
266 | deleteGraphicsObject<PolyData>(itr->second->getName()); |
---|
267 | deleteGraphicsObject<PseudoColor>(itr->second->getName()); |
---|
268 | deleteGraphicsObject<Streamlines>(itr->second->getName()); |
---|
269 | deleteGraphicsObject<Volume>(itr->second->getName()); |
---|
270 | deleteGraphicsObject<Warp>(itr->second->getName()); |
---|
271 | |
---|
272 | TRACE("After deleting graphics objects"); |
---|
273 | |
---|
274 | delete itr->second; |
---|
275 | itr = _dataSets.erase(itr); |
---|
276 | } while (doAll && itr != _dataSets.end()); |
---|
277 | |
---|
278 | // Update cumulative data range |
---|
279 | initFieldRanges(); |
---|
280 | updateFieldRanges(); |
---|
281 | |
---|
282 | sceneBoundsChanged(); |
---|
283 | _needsRedraw = true; |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * \brief Get a list of DataSets this Renderer knows about |
---|
288 | */ |
---|
289 | void Renderer::getDataSetNames(std::vector<std::string>& names) |
---|
290 | { |
---|
291 | names.clear(); |
---|
292 | for (DataSetHashmap::iterator itr = _dataSets.begin(); |
---|
293 | itr != _dataSets.end(); ++itr) { |
---|
294 | names.push_back(itr->second->getName()); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * \brief Find the DataSet for the given DataSetId key |
---|
300 | * |
---|
301 | * \return A pointer to the DataSet, or NULL if not found |
---|
302 | */ |
---|
303 | DataSet *Renderer::getDataSet(const DataSetId& id) |
---|
304 | { |
---|
305 | DataSetHashmap::iterator itr = _dataSets.find(id); |
---|
306 | if (itr == _dataSets.end()) { |
---|
307 | #ifdef DEBUG |
---|
308 | TRACE("DataSet not found: %s", id.c_str()); |
---|
309 | #endif |
---|
310 | return NULL; |
---|
311 | } else |
---|
312 | return itr->second; |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * \brief (Re-)load the data for the specified DataSet key from a file |
---|
317 | */ |
---|
318 | bool Renderer::setDataFile(const DataSetId& id, const char *filename) |
---|
319 | { |
---|
320 | DataSet *ds = getDataSet(id); |
---|
321 | if (ds) { |
---|
322 | bool ret = ds->setDataFile(filename); |
---|
323 | initFieldRanges(); |
---|
324 | updateFieldRanges(); |
---|
325 | _needsRedraw = true; |
---|
326 | return ret; |
---|
327 | } else |
---|
328 | return false; |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * \brief (Re-)load the data for the specified DataSet key from a memory buffer |
---|
333 | */ |
---|
334 | bool Renderer::setData(const DataSetId& id, char *data, int nbytes) |
---|
335 | { |
---|
336 | DataSet *ds = getDataSet(id); |
---|
337 | if (ds) { |
---|
338 | bool ret = ds->setData(data, nbytes); |
---|
339 | initFieldRanges(); |
---|
340 | updateFieldRanges(); |
---|
341 | _needsRedraw = true; |
---|
342 | return ret; |
---|
343 | } else |
---|
344 | return false; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * \brief Set the active scalar field array by name for a DataSet |
---|
349 | */ |
---|
350 | bool Renderer::setDataSetActiveScalars(const DataSetId& id, const char *scalarName) |
---|
351 | { |
---|
352 | DataSetHashmap::iterator itr; |
---|
353 | |
---|
354 | bool doAll = false; |
---|
355 | |
---|
356 | if (id.compare("all") == 0) { |
---|
357 | itr = _dataSets.begin(); |
---|
358 | doAll = true; |
---|
359 | } else { |
---|
360 | itr = _dataSets.find(id); |
---|
361 | } |
---|
362 | if (itr == _dataSets.end()) { |
---|
363 | ERROR("DataSet not found: %s", id.c_str()); |
---|
364 | return false; |
---|
365 | } |
---|
366 | |
---|
367 | bool ret = true; |
---|
368 | bool needRangeUpdate = false; |
---|
369 | do { |
---|
370 | if (strcmp(itr->second->getActiveScalarsName(), scalarName) != 0) { |
---|
371 | if (!itr->second->setActiveScalars(scalarName)) { |
---|
372 | ret = false; |
---|
373 | } else { |
---|
374 | needRangeUpdate = true; |
---|
375 | } |
---|
376 | } |
---|
377 | } while (doAll && ++itr != _dataSets.end()); |
---|
378 | |
---|
379 | if (needRangeUpdate) { |
---|
380 | updateFieldRanges(); |
---|
381 | _needsRedraw = true; |
---|
382 | } |
---|
383 | |
---|
384 | return ret; |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | * \brief Set the active vector field array by name for a DataSet |
---|
389 | */ |
---|
390 | bool Renderer::setDataSetActiveVectors(const DataSetId& id, const char *vectorName) |
---|
391 | { |
---|
392 | DataSetHashmap::iterator itr; |
---|
393 | |
---|
394 | bool doAll = false; |
---|
395 | |
---|
396 | if (id.compare("all") == 0) { |
---|
397 | itr = _dataSets.begin(); |
---|
398 | doAll = true; |
---|
399 | } else { |
---|
400 | itr = _dataSets.find(id); |
---|
401 | } |
---|
402 | if (itr == _dataSets.end()) { |
---|
403 | ERROR("DataSet not found: %s", id.c_str()); |
---|
404 | return false; |
---|
405 | } |
---|
406 | |
---|
407 | bool ret = true; |
---|
408 | bool needRangeUpdate = false; |
---|
409 | do { |
---|
410 | if (strcmp(itr->second->getActiveVectorsName(), vectorName) != 0) { |
---|
411 | if (!itr->second->setActiveVectors(vectorName)) { |
---|
412 | ret = false; |
---|
413 | } else { |
---|
414 | needRangeUpdate = true; |
---|
415 | } |
---|
416 | } |
---|
417 | } while (doAll && ++itr != _dataSets.end()); |
---|
418 | |
---|
419 | if (needRangeUpdate) { |
---|
420 | updateFieldRanges(); |
---|
421 | _needsRedraw = true; |
---|
422 | } |
---|
423 | |
---|
424 | return ret; |
---|
425 | } |
---|
426 | |
---|
427 | /** |
---|
428 | * \brief Control whether the cumulative data range of datasets is used for |
---|
429 | * colormapping and contours |
---|
430 | * |
---|
431 | * NOTE: If using explicit range(s), setting cumulative ranges on will |
---|
432 | * remove explicit ranges on all fields |
---|
433 | */ |
---|
434 | void Renderer::setUseCumulativeDataRange(bool state, bool onlyVisible) |
---|
435 | { |
---|
436 | TRACE("use cumulative: %d, only visible: %d", (state ? 1 : 0), (onlyVisible ? 1 : 0)); |
---|
437 | _useCumulativeRange = state; |
---|
438 | _cumulativeRangeOnlyVisible = onlyVisible; |
---|
439 | clearUserFieldRanges(); |
---|
440 | updateFieldRanges(); |
---|
441 | resetAxes(); |
---|
442 | _needsRedraw = true; |
---|
443 | } |
---|
444 | |
---|
445 | void Renderer::resetAxes(double bounds[6]) |
---|
446 | { |
---|
447 | TRACE("Resetting axes"); |
---|
448 | |
---|
449 | if (_cubeAxesActor == NULL) { |
---|
450 | initAxes(); |
---|
451 | } |
---|
452 | |
---|
453 | if (_cameraMode == IMAGE) { |
---|
454 | _cubeAxesActor->SetUse2DMode(1); |
---|
455 | } else { |
---|
456 | _cubeAxesActor->SetUse2DMode(0); |
---|
457 | } |
---|
458 | |
---|
459 | if (_cameraMode == IMAGE) { |
---|
460 | return; |
---|
461 | } |
---|
462 | |
---|
463 | setAxesBounds(bounds); |
---|
464 | setAxesRanges(); |
---|
465 | } |
---|
466 | |
---|
467 | /** |
---|
468 | * \brief Explicitly set world coordinate bounds of axes |
---|
469 | * |
---|
470 | * This determines the region in world coordinates around which |
---|
471 | * the axes are drawn |
---|
472 | */ |
---|
473 | void Renderer::setAxesBounds(double min, double max) |
---|
474 | { |
---|
475 | setAxisBounds(X_AXIS, min, max); |
---|
476 | setAxisBounds(Y_AXIS, min, max); |
---|
477 | setAxisBounds(Z_AXIS, min, max); |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * \brief Set axes bounds based on auto/explicit settings |
---|
482 | */ |
---|
483 | void Renderer::setAxesBounds(double boundsIn[6]) |
---|
484 | { |
---|
485 | double newBounds[6]; |
---|
486 | if (boundsIn != NULL) { |
---|
487 | memcpy(&newBounds[0], &boundsIn[0], sizeof(double)*6); |
---|
488 | } else { |
---|
489 | if (_axesAutoBounds[X_AXIS] || |
---|
490 | _axesAutoBounds[Y_AXIS] || |
---|
491 | _axesAutoBounds[Z_AXIS]) { |
---|
492 | collectBounds(newBounds, false); |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | double bounds[6]; |
---|
497 | if (_axesAutoBounds[X_AXIS]) { |
---|
498 | memcpy(&bounds[0], &newBounds[0], sizeof(double)*2); |
---|
499 | } else { |
---|
500 | memcpy(&bounds[0], &_axesUserBounds[0], sizeof(double)*2); |
---|
501 | } |
---|
502 | if (_axesAutoBounds[Y_AXIS]) { |
---|
503 | memcpy(&bounds[2], &newBounds[2], sizeof(double)*2); |
---|
504 | } else { |
---|
505 | memcpy(&bounds[2], &_axesUserBounds[2], sizeof(double)*2); |
---|
506 | } |
---|
507 | if (_axesAutoBounds[Z_AXIS]) { |
---|
508 | memcpy(&bounds[4], &newBounds[4], sizeof(double)*2); |
---|
509 | } else { |
---|
510 | memcpy(&bounds[4], &_axesUserBounds[4], sizeof(double)*2); |
---|
511 | } |
---|
512 | |
---|
513 | _cubeAxesActor->SetBounds(bounds); |
---|
514 | |
---|
515 | TRACE("Axis bounds: %g %g %g %g %g %g", |
---|
516 | bounds[0], |
---|
517 | bounds[1], |
---|
518 | bounds[2], |
---|
519 | bounds[3], |
---|
520 | bounds[4], |
---|
521 | bounds[5]); |
---|
522 | |
---|
523 | _needsRedraw = true; |
---|
524 | } |
---|
525 | |
---|
526 | /** |
---|
527 | * \brief Toggle automatic vs. explicit range setting on all axes |
---|
528 | */ |
---|
529 | void Renderer::setAxesAutoBounds(bool state) |
---|
530 | { |
---|
531 | _axesAutoBounds[X_AXIS] = state; |
---|
532 | _axesAutoBounds[Y_AXIS] = state; |
---|
533 | _axesAutoBounds[Z_AXIS] = state; |
---|
534 | |
---|
535 | TRACE("Set axes bounds to %s", (state ? "auto" : "explicit")); |
---|
536 | |
---|
537 | setAxesBounds(); |
---|
538 | |
---|
539 | _needsRedraw = true; |
---|
540 | } |
---|
541 | |
---|
542 | /** |
---|
543 | * \brief Toggle automatic vs. explicit range setting on all axes |
---|
544 | */ |
---|
545 | void Renderer::setAxisAutoBounds(Axis axis, bool state) |
---|
546 | { |
---|
547 | _axesAutoBounds[axis] = state; |
---|
548 | |
---|
549 | TRACE("Set axis %d bounds to %s", axis, (state ? "auto" : "explicit")); |
---|
550 | |
---|
551 | setAxesBounds(); |
---|
552 | |
---|
553 | _needsRedraw = true; |
---|
554 | } |
---|
555 | |
---|
556 | /** |
---|
557 | * \brief Toggle automatic vs. explicit range setting on all axes |
---|
558 | */ |
---|
559 | void Renderer::setAxesAutoRange(bool state) |
---|
560 | { |
---|
561 | _axesRangeMode[X_AXIS] = state ? RANGE_AUTO : RANGE_EXPLICIT; |
---|
562 | _axesRangeMode[Y_AXIS] = state ? RANGE_AUTO : RANGE_EXPLICIT; |
---|
563 | _axesRangeMode[Z_AXIS] = state ? RANGE_AUTO : RANGE_EXPLICIT; |
---|
564 | |
---|
565 | TRACE("Set axes range to %s", (state ? "auto" : "explicit")); |
---|
566 | |
---|
567 | setAxesRanges(); |
---|
568 | |
---|
569 | _needsRedraw = true; |
---|
570 | } |
---|
571 | |
---|
572 | /** |
---|
573 | * \brief Explicitly set world coordinate bounds of axis |
---|
574 | * |
---|
575 | * This determines the region in world coordinates around which |
---|
576 | * the axes are drawn |
---|
577 | */ |
---|
578 | void Renderer::setAxisBounds(Axis axis, double min, double max) |
---|
579 | { |
---|
580 | double bounds[6]; |
---|
581 | _cubeAxesActor->GetBounds(bounds); |
---|
582 | |
---|
583 | bounds[axis*2 ] = _axesUserBounds[axis*2 ] = min; |
---|
584 | bounds[axis*2+1] = _axesUserBounds[axis*2+1] = max; |
---|
585 | |
---|
586 | _cubeAxesActor->SetBounds(bounds); |
---|
587 | |
---|
588 | _axesAutoBounds[axis] = false; |
---|
589 | |
---|
590 | TRACE("Axis bounds: %g %g %g %g %g %g", |
---|
591 | bounds[0], |
---|
592 | bounds[1], |
---|
593 | bounds[2], |
---|
594 | bounds[3], |
---|
595 | bounds[4], |
---|
596 | bounds[5]); |
---|
597 | |
---|
598 | _needsRedraw = true; |
---|
599 | } |
---|
600 | |
---|
601 | /** |
---|
602 | * \brief Toggle automatic vs. explicit range setting on specific axis |
---|
603 | */ |
---|
604 | void Renderer::setAxisAutoRange(Axis axis, bool state) |
---|
605 | { |
---|
606 | _axesRangeMode[axis] = state ? RANGE_AUTO : RANGE_EXPLICIT; |
---|
607 | |
---|
608 | TRACE("Set axis %d range to %s", axis, (state ? "auto" : "explicit")); |
---|
609 | |
---|
610 | setAxesRanges(); |
---|
611 | |
---|
612 | _needsRedraw = true; |
---|
613 | } |
---|
614 | |
---|
615 | /** |
---|
616 | * \brief Set explicit range on axes and disable auto range |
---|
617 | */ |
---|
618 | void Renderer::setAxesRange(double min, double max) |
---|
619 | { |
---|
620 | setAxisRange(X_AXIS, min, max); |
---|
621 | setAxisRange(Y_AXIS, min, max); |
---|
622 | setAxisRange(Z_AXIS, min, max); |
---|
623 | } |
---|
624 | |
---|
625 | /** |
---|
626 | * \brief Explicitly set power multiplier for axes labels |
---|
627 | * |
---|
628 | * The exponent will appear in the axis title and be omitted from labels |
---|
629 | */ |
---|
630 | void Renderer::setAxesLabelPowerScaling(int xPow, int yPow, int zPow, bool useCustom) |
---|
631 | { |
---|
632 | _cubeAxesActor->SetLabelScaling(!useCustom, xPow, yPow, zPow); |
---|
633 | |
---|
634 | TRACE("Set axis label scaling: custom: %d, powers: %d,%d,%d", (int)useCustom, xPow, yPow, zPow); |
---|
635 | |
---|
636 | _needsRedraw = true; |
---|
637 | } |
---|
638 | |
---|
639 | /** |
---|
640 | * \brief Set explicit range on axis and disable auto range |
---|
641 | */ |
---|
642 | void Renderer::setAxisRange(Axis axis, double min, double max) |
---|
643 | { |
---|
644 | switch (axis) { |
---|
645 | case X_AXIS: |
---|
646 | _axesRangeMode[X_AXIS] = RANGE_EXPLICIT; |
---|
647 | _cubeAxesActor->SetXAxisRange(min, max); |
---|
648 | break; |
---|
649 | case Y_AXIS: |
---|
650 | _axesRangeMode[Y_AXIS] = RANGE_EXPLICIT; |
---|
651 | _cubeAxesActor->SetYAxisRange(min, max); |
---|
652 | break; |
---|
653 | case Z_AXIS: |
---|
654 | _axesRangeMode[Z_AXIS] = RANGE_EXPLICIT; |
---|
655 | _cubeAxesActor->SetZAxisRange(min, max); |
---|
656 | break; |
---|
657 | default: |
---|
658 | break; |
---|
659 | } |
---|
660 | |
---|
661 | TRACE("Set axis %d range to: %g, %g", axis, min, max); |
---|
662 | |
---|
663 | _needsRedraw = true; |
---|
664 | } |
---|
665 | |
---|
666 | /** |
---|
667 | * \brief Set axes ranges based on bounds and range modes |
---|
668 | */ |
---|
669 | void Renderer::setAxesRanges() |
---|
670 | { |
---|
671 | computeAxesScale(); |
---|
672 | |
---|
673 | double bounds[6]; |
---|
674 | _cubeAxesActor->GetBounds(bounds); |
---|
675 | |
---|
676 | double ranges[6]; |
---|
677 | if (_axesRangeMode[X_AXIS] != RANGE_EXPLICIT) { |
---|
678 | ranges[0] = bounds[0] * _axesScale[X_AXIS]; |
---|
679 | ranges[1] = bounds[1] * _axesScale[X_AXIS]; |
---|
680 | _cubeAxesActor->SetXAxisRange(&ranges[0]); |
---|
681 | } else { |
---|
682 | _cubeAxesActor->GetXAxisRange(&ranges[0]); |
---|
683 | } |
---|
684 | |
---|
685 | if (_axesRangeMode[Y_AXIS] != RANGE_EXPLICIT) { |
---|
686 | ranges[2] = bounds[2] * _axesScale[Y_AXIS]; |
---|
687 | ranges[3] = bounds[3] * _axesScale[Y_AXIS]; |
---|
688 | _cubeAxesActor->SetYAxisRange(&ranges[2]); |
---|
689 | } else { |
---|
690 | _cubeAxesActor->GetYAxisRange(&ranges[2]); |
---|
691 | } |
---|
692 | |
---|
693 | if (_axesRangeMode[Z_AXIS] != RANGE_EXPLICIT) { |
---|
694 | ranges[4] = bounds[4] * _axesScale[Z_AXIS]; |
---|
695 | ranges[5] = bounds[5] * _axesScale[Z_AXIS]; |
---|
696 | _cubeAxesActor->SetZAxisRange(&ranges[4]); |
---|
697 | } else { |
---|
698 | _cubeAxesActor->GetZAxisRange(&ranges[4]); |
---|
699 | } |
---|
700 | |
---|
701 | TRACE("Axis ranges: %g %g %g %g %g %g", |
---|
702 | ranges[0], |
---|
703 | ranges[1], |
---|
704 | ranges[2], |
---|
705 | ranges[3], |
---|
706 | ranges[4], |
---|
707 | ranges[5]); |
---|
708 | |
---|
709 | _needsRedraw = true; |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | * \brief Compute scaling from axes bounds to ranges |
---|
714 | * |
---|
715 | * Uses actor scaling to determine if world coordinates |
---|
716 | * need to be scaled to undo actor scaling in range labels |
---|
717 | */ |
---|
718 | void Renderer::computeAxesScale() |
---|
719 | { |
---|
720 | if (_axesRangeMode[X_AXIS] != RANGE_AUTO && |
---|
721 | _axesRangeMode[Y_AXIS] != RANGE_AUTO && |
---|
722 | _axesRangeMode[Z_AXIS] != RANGE_AUTO) |
---|
723 | return; |
---|
724 | |
---|
725 | double bounds[6]; |
---|
726 | if (_cameraMode == IMAGE) { |
---|
727 | collectBounds(bounds, false); |
---|
728 | } else |
---|
729 | _cubeAxesActor->GetBounds(bounds); |
---|
730 | |
---|
731 | double unscaledBounds[6]; |
---|
732 | collectUnscaledBounds(unscaledBounds, false); |
---|
733 | |
---|
734 | if (_axesRangeMode[X_AXIS] == RANGE_AUTO) { |
---|
735 | double sx = (bounds[1] == bounds[0]) ? 1.0 : (unscaledBounds[1] - unscaledBounds[0]) / (bounds[1] - bounds[0]); |
---|
736 | _axesScale[X_AXIS] = sx; |
---|
737 | TRACE("Setting X axis scale to: %g", sx); |
---|
738 | } |
---|
739 | if (_axesRangeMode[Y_AXIS] == RANGE_AUTO) { |
---|
740 | double sy = (bounds[3] == bounds[2]) ? 1.0 : (unscaledBounds[3] - unscaledBounds[2]) / (bounds[3] - bounds[2]); |
---|
741 | _axesScale[Y_AXIS] = sy; |
---|
742 | TRACE("Setting Y axis scale to: %g", sy); |
---|
743 | } |
---|
744 | if (_axesRangeMode[Y_AXIS] == RANGE_AUTO) { |
---|
745 | double sz = (bounds[5] == bounds[4]) ? 1.0 : (unscaledBounds[5] - unscaledBounds[4]) / (bounds[5] - bounds[4]); |
---|
746 | _axesScale[Z_AXIS] = sz; |
---|
747 | TRACE("Setting Z axis scale to: %g", sz); |
---|
748 | } |
---|
749 | } |
---|
750 | |
---|
751 | /** |
---|
752 | * \brief Set scaling factor to convert world coordinates to axis |
---|
753 | * label values in X, Y, Z |
---|
754 | */ |
---|
755 | void Renderer::setAxesScale(double scale) |
---|
756 | { |
---|
757 | _axesRangeMode[X_AXIS] = RANGE_SCALE_BOUNDS; |
---|
758 | _axesRangeMode[Y_AXIS] = RANGE_SCALE_BOUNDS; |
---|
759 | _axesRangeMode[Z_AXIS] = RANGE_SCALE_BOUNDS; |
---|
760 | |
---|
761 | _axesScale[X_AXIS] = scale; |
---|
762 | _axesScale[Y_AXIS] = scale; |
---|
763 | _axesScale[Z_AXIS] = scale; |
---|
764 | |
---|
765 | TRACE("Setting axes scale to: %g", scale); |
---|
766 | |
---|
767 | setAxesRanges(); |
---|
768 | } |
---|
769 | |
---|
770 | /** |
---|
771 | * \brief Set scaling factor to convert world coordinates to axis label values |
---|
772 | */ |
---|
773 | void Renderer::setAxisScale(Axis axis, double scale) |
---|
774 | { |
---|
775 | _axesRangeMode[axis] = RANGE_SCALE_BOUNDS; |
---|
776 | _axesScale[axis] = scale; |
---|
777 | |
---|
778 | TRACE("Setting axis %d scale to: %g", axis, scale); |
---|
779 | |
---|
780 | setAxesRanges(); |
---|
781 | } |
---|
782 | |
---|
783 | /** |
---|
784 | * \brief Set an origin point within the axes bounds where the axes will intersect |
---|
785 | * |
---|
786 | * \param x X coordinate of origin |
---|
787 | * \param y Y coordinate of origin |
---|
788 | * \param z Z coordinate of origin |
---|
789 | * \param useCustom Flag indicating if custom origin is to be used/enabled |
---|
790 | */ |
---|
791 | void Renderer::setAxesOrigin(double x, double y, double z, bool useCustom) |
---|
792 | { |
---|
793 | _cubeAxesActor->SetAxisOrigin(x, y, z); |
---|
794 | _cubeAxesActor->SetUseAxisOrigin((useCustom ? 1 : 0)); |
---|
795 | |
---|
796 | _needsRedraw = true; |
---|
797 | } |
---|
798 | |
---|
799 | /** |
---|
800 | * \brief Set inital properties on the 2D Axes |
---|
801 | */ |
---|
802 | void Renderer::initAxes() |
---|
803 | { |
---|
804 | TRACE("Initializing axes"); |
---|
805 | #ifdef USE_CUSTOM_AXES |
---|
806 | if (_cubeAxesActor == NULL) |
---|
807 | _cubeAxesActor = vtkSmartPointer<vtkRpCubeAxesActor>::New(); |
---|
808 | #else |
---|
809 | if (_cubeAxesActor == NULL) |
---|
810 | _cubeAxesActor = vtkSmartPointer<vtkCubeAxesActor>::New(); |
---|
811 | #endif |
---|
812 | _cubeAxesActor->SetCamera(_renderer->GetActiveCamera()); |
---|
813 | _cubeAxesActor->GetProperty()->LightingOff(); |
---|
814 | // Don't offset labels at origin |
---|
815 | _cubeAxesActor->SetCornerOffset(0); |
---|
816 | _cubeAxesActor->SetFlyModeToStaticTriad(); |
---|
817 | |
---|
818 | _cubeAxesActor->SetGridLineLocation(VTK_GRID_LINES_ALL); |
---|
819 | _cubeAxesActor->SetDrawXInnerGridlines(0); |
---|
820 | _cubeAxesActor->SetDrawYInnerGridlines(0); |
---|
821 | _cubeAxesActor->SetDrawZInnerGridlines(0); |
---|
822 | _cubeAxesActor->SetDrawXGridpolys(0); |
---|
823 | _cubeAxesActor->SetDrawYGridpolys(0); |
---|
824 | _cubeAxesActor->SetDrawZGridpolys(0); |
---|
825 | _cubeAxesActor->SetDistanceLODThreshold(1); |
---|
826 | _cubeAxesActor->SetEnableDistanceLOD(0); |
---|
827 | _cubeAxesActor->SetViewAngleLODThreshold(0); |
---|
828 | _cubeAxesActor->SetEnableViewAngleLOD(0); |
---|
829 | // Attempt to set font sizes for 2D (this currently doesn't work) |
---|
830 | for (int i = 0; i < 3; i++) { |
---|
831 | _cubeAxesActor->GetTitleTextProperty(i)->SetFontSize(14); |
---|
832 | _cubeAxesActor->GetLabelTextProperty(i)->SetFontSize(12); |
---|
833 | } |
---|
834 | // Set font pixel size for 3D |
---|
835 | _cubeAxesActor->SetScreenSize(8.0); |
---|
836 | |
---|
837 | _cubeAxesActor->SetBounds(0, 1, 0, 1, 0, 1); |
---|
838 | _cubeAxesActor->SetXAxisRange(0, 1); |
---|
839 | _cubeAxesActor->SetYAxisRange(0, 1); |
---|
840 | _cubeAxesActor->SetZAxisRange(0, 1); |
---|
841 | for (int axis = 0; axis < 3; axis++) { |
---|
842 | _axesAutoBounds[axis] = true; |
---|
843 | _axesRangeMode[axis] = RANGE_AUTO; |
---|
844 | _axesScale[axis] = 1.0; |
---|
845 | } |
---|
846 | |
---|
847 | double axesColor[] = {1,1,1}; |
---|
848 | setAxesColor(axesColor); |
---|
849 | |
---|
850 | if (!_renderer->HasViewProp(_cubeAxesActor)) |
---|
851 | _renderer->AddViewProp(_cubeAxesActor); |
---|
852 | |
---|
853 | if (_cameraMode == IMAGE) { |
---|
854 | _cubeAxesActor->SetUse2DMode(1); |
---|
855 | } else { |
---|
856 | _cubeAxesActor->SetUse2DMode(0); |
---|
857 | } |
---|
858 | } |
---|
859 | |
---|
860 | /** |
---|
861 | * \brief Set Fly mode of axes |
---|
862 | */ |
---|
863 | void Renderer::setAxesFlyMode(AxesFlyMode mode) |
---|
864 | { |
---|
865 | if (_cubeAxesActor == NULL) |
---|
866 | return; |
---|
867 | |
---|
868 | switch (mode) { |
---|
869 | case FLY_STATIC_EDGES: |
---|
870 | _cubeAxesActor->SetFlyModeToStaticEdges(); |
---|
871 | _cubeAxesActor->SetGridLineLocation(VTK_GRID_LINES_ALL); |
---|
872 | break; |
---|
873 | case FLY_STATIC_TRIAD: |
---|
874 | _cubeAxesActor->SetFlyModeToStaticTriad(); |
---|
875 | _cubeAxesActor->SetGridLineLocation(VTK_GRID_LINES_ALL); |
---|
876 | break; |
---|
877 | case FLY_OUTER_EDGES: |
---|
878 | _cubeAxesActor->SetFlyModeToOuterEdges(); |
---|
879 | _cubeAxesActor->SetGridLineLocation(VTK_GRID_LINES_ALL); |
---|
880 | break; |
---|
881 | case FLY_FURTHEST_TRIAD: |
---|
882 | _cubeAxesActor->SetFlyModeToFurthestTriad(); |
---|
883 | _cubeAxesActor->SetGridLineLocation(VTK_GRID_LINES_FURTHEST); |
---|
884 | break; |
---|
885 | case FLY_CLOSEST_TRIAD: |
---|
886 | default: |
---|
887 | _cubeAxesActor->SetFlyModeToClosestTriad(); |
---|
888 | _cubeAxesActor->SetGridLineLocation(VTK_GRID_LINES_CLOSEST); |
---|
889 | break; |
---|
890 | } |
---|
891 | _needsRedraw = true; |
---|
892 | } |
---|
893 | |
---|
894 | /** |
---|
895 | * \brief Set color of axes, ticks, labels, titles |
---|
896 | */ |
---|
897 | void Renderer::setAxesColor(double color[3], double opacity) |
---|
898 | { |
---|
899 | setAxesTitleColor(color, opacity); |
---|
900 | setAxesLabelColor(color, opacity); |
---|
901 | setAxesLinesColor(color, opacity); |
---|
902 | setAxesGridlinesColor(color, opacity); |
---|
903 | setAxesInnerGridlinesColor(color, opacity); |
---|
904 | setAxesGridpolysColor(color, opacity); |
---|
905 | } |
---|
906 | |
---|
907 | /** |
---|
908 | * \brief Set color of axes title text |
---|
909 | */ |
---|
910 | void Renderer::setAxesTitleColor(double color[3], double opacity) |
---|
911 | { |
---|
912 | setAxisTitleColor(X_AXIS, color, opacity); |
---|
913 | setAxisTitleColor(Y_AXIS, color, opacity); |
---|
914 | setAxisTitleColor(Z_AXIS, color, opacity); |
---|
915 | } |
---|
916 | |
---|
917 | /** |
---|
918 | * \brief Set color of axes label text |
---|
919 | */ |
---|
920 | void Renderer::setAxesLabelColor(double color[3], double opacity) |
---|
921 | { |
---|
922 | setAxisLabelColor(X_AXIS, color, opacity); |
---|
923 | setAxisLabelColor(Y_AXIS, color, opacity); |
---|
924 | setAxisLabelColor(Z_AXIS, color, opacity); |
---|
925 | } |
---|
926 | |
---|
927 | /** |
---|
928 | * \brief Set color of main axes lines |
---|
929 | */ |
---|
930 | void Renderer::setAxesLinesColor(double color[3], double opacity) |
---|
931 | { |
---|
932 | setAxisLinesColor(X_AXIS, color, opacity); |
---|
933 | setAxisLinesColor(Y_AXIS, color, opacity); |
---|
934 | setAxisLinesColor(Z_AXIS, color, opacity); |
---|
935 | } |
---|
936 | |
---|
937 | /** |
---|
938 | * \brief Set color of axis gridlines |
---|
939 | */ |
---|
940 | void Renderer::setAxesGridlinesColor(double color[3], double opacity) |
---|
941 | { |
---|
942 | setAxisGridlinesColor(X_AXIS, color, opacity); |
---|
943 | setAxisGridlinesColor(Y_AXIS, color, opacity); |
---|
944 | setAxisGridlinesColor(Z_AXIS, color, opacity); |
---|
945 | } |
---|
946 | |
---|
947 | /** |
---|
948 | * \brief Set color of axes inner gridlines |
---|
949 | */ |
---|
950 | void Renderer::setAxesInnerGridlinesColor(double color[3], double opacity) |
---|
951 | { |
---|
952 | setAxisInnerGridlinesColor(X_AXIS, color, opacity); |
---|
953 | setAxisInnerGridlinesColor(Y_AXIS, color, opacity); |
---|
954 | setAxisInnerGridlinesColor(Z_AXIS, color, opacity); |
---|
955 | } |
---|
956 | |
---|
957 | /** |
---|
958 | * \brief Set color of axes inner grid polygons |
---|
959 | */ |
---|
960 | void Renderer::setAxesGridpolysColor(double color[3], double opacity) |
---|
961 | { |
---|
962 | setAxisGridpolysColor(X_AXIS, color, opacity); |
---|
963 | setAxisGridpolysColor(Y_AXIS, color, opacity); |
---|
964 | setAxisGridpolysColor(Z_AXIS, color, opacity); |
---|
965 | } |
---|
966 | |
---|
967 | /** |
---|
968 | * \brief Turn on/off rendering of all enabled axes |
---|
969 | */ |
---|
970 | void Renderer::setAxesVisibility(bool state) |
---|
971 | { |
---|
972 | if (_cubeAxesActor != NULL) { |
---|
973 | _cubeAxesActor->SetVisibility((state ? 1 : 0)); |
---|
974 | _needsRedraw = true; |
---|
975 | } |
---|
976 | } |
---|
977 | |
---|
978 | /** |
---|
979 | * \brief Turn on/off rendering of all axes gridlines |
---|
980 | */ |
---|
981 | void Renderer::setAxesGridVisibility(bool state) |
---|
982 | { |
---|
983 | setAxisGridVisibility(X_AXIS, state); |
---|
984 | setAxisGridVisibility(Y_AXIS, state); |
---|
985 | setAxisGridVisibility(Z_AXIS, state); |
---|
986 | } |
---|
987 | |
---|
988 | /** |
---|
989 | * \brief Turn on/off rendering of all axes inner gridlines |
---|
990 | */ |
---|
991 | void Renderer::setAxesInnerGridVisibility(bool state) |
---|
992 | { |
---|
993 | setAxisInnerGridVisibility(X_AXIS, state); |
---|
994 | setAxisInnerGridVisibility(Y_AXIS, state); |
---|
995 | setAxisInnerGridVisibility(Z_AXIS, state); |
---|
996 | } |
---|
997 | |
---|
998 | /** |
---|
999 | * \brief Turn on/off rendering of all axes inner grid polygons |
---|
1000 | */ |
---|
1001 | void Renderer::setAxesGridpolysVisibility(bool state) |
---|
1002 | { |
---|
1003 | setAxisGridpolysVisibility(X_AXIS, state); |
---|
1004 | setAxisGridpolysVisibility(Y_AXIS, state); |
---|
1005 | setAxisGridpolysVisibility(Z_AXIS, state); |
---|
1006 | } |
---|
1007 | |
---|
1008 | /** |
---|
1009 | * \brief Turn on/off rendering of all axis labels |
---|
1010 | */ |
---|
1011 | void Renderer::setAxesLabelVisibility(bool state) |
---|
1012 | { |
---|
1013 | setAxisLabelVisibility(X_AXIS, state); |
---|
1014 | setAxisLabelVisibility(Y_AXIS, state); |
---|
1015 | setAxisLabelVisibility(Z_AXIS, state); |
---|
1016 | } |
---|
1017 | |
---|
1018 | /** |
---|
1019 | * \brief Turn on/off rendering of all axis ticks |
---|
1020 | */ |
---|
1021 | void Renderer::setAxesTickVisibility(bool state) |
---|
1022 | { |
---|
1023 | setAxisTickVisibility(X_AXIS, state); |
---|
1024 | setAxisTickVisibility(Y_AXIS, state); |
---|
1025 | setAxisTickVisibility(Z_AXIS, state); |
---|
1026 | } |
---|
1027 | |
---|
1028 | /** |
---|
1029 | * \brief Turn on/off rendering of all axis ticks |
---|
1030 | */ |
---|
1031 | void Renderer::setAxesMinorTickVisibility(bool state) |
---|
1032 | { |
---|
1033 | setAxisMinorTickVisibility(X_AXIS, state); |
---|
1034 | setAxisMinorTickVisibility(Y_AXIS, state); |
---|
1035 | setAxisMinorTickVisibility(Z_AXIS, state); |
---|
1036 | } |
---|
1037 | |
---|
1038 | /** |
---|
1039 | * \brief Control position of ticks on 3D axes |
---|
1040 | */ |
---|
1041 | void Renderer::setAxesTickPosition(AxesTickPosition pos) |
---|
1042 | { |
---|
1043 | if (_cubeAxesActor == NULL) |
---|
1044 | return; |
---|
1045 | |
---|
1046 | switch (pos) { |
---|
1047 | case TICKS_BOTH: |
---|
1048 | _cubeAxesActor->SetTickLocationToBoth(); |
---|
1049 | break; |
---|
1050 | case TICKS_OUTSIDE: |
---|
1051 | _cubeAxesActor->SetTickLocationToOutside(); |
---|
1052 | break; |
---|
1053 | case TICKS_INSIDE: |
---|
1054 | default: |
---|
1055 | _cubeAxesActor->SetTickLocationToInside(); |
---|
1056 | break; |
---|
1057 | } |
---|
1058 | _needsRedraw = true; |
---|
1059 | } |
---|
1060 | |
---|
1061 | /** |
---|
1062 | * \brief Controls label scaling by power of 10 |
---|
1063 | */ |
---|
1064 | void Renderer::setAxesLabelScaling(bool autoScale, int xpow, int ypow, int zpow) |
---|
1065 | { |
---|
1066 | if (_cubeAxesActor != NULL) { |
---|
1067 | _cubeAxesActor->SetLabelScaling(autoScale, xpow, ypow, zpow); |
---|
1068 | _needsRedraw = true; |
---|
1069 | } |
---|
1070 | } |
---|
1071 | |
---|
1072 | /** |
---|
1073 | * \brief Set axes title/label font size in pixels |
---|
1074 | */ |
---|
1075 | void Renderer::setAxesPixelFontSize(double screenSize) |
---|
1076 | { |
---|
1077 | if (_cubeAxesActor != NULL) { |
---|
1078 | TRACE("Setting axes font size to: %g px", screenSize); |
---|
1079 | _cubeAxesActor->SetScreenSize(screenSize); |
---|
1080 | _needsRedraw = true; |
---|
1081 | } |
---|
1082 | } |
---|
1083 | |
---|
1084 | /** |
---|
1085 | * \brief Set axis title font family |
---|
1086 | */ |
---|
1087 | void Renderer::setAxesTitleFont(const char *fontName) |
---|
1088 | { |
---|
1089 | setAxisTitleFont(X_AXIS, fontName); |
---|
1090 | setAxisTitleFont(Y_AXIS, fontName); |
---|
1091 | setAxisTitleFont(Z_AXIS, fontName); |
---|
1092 | } |
---|
1093 | |
---|
1094 | /** |
---|
1095 | * \brief Set axis title font size |
---|
1096 | */ |
---|
1097 | void Renderer::setAxesTitleFontSize(int sz) |
---|
1098 | { |
---|
1099 | setAxisTitleFontSize(X_AXIS, sz); |
---|
1100 | setAxisTitleFontSize(Y_AXIS, sz); |
---|
1101 | setAxisTitleFontSize(Z_AXIS, sz); |
---|
1102 | } |
---|
1103 | |
---|
1104 | /** |
---|
1105 | * \brief Set orientation (as rotation in degrees) of axis title |
---|
1106 | */ |
---|
1107 | void Renderer::setAxesTitleOrientation(double orientation) |
---|
1108 | { |
---|
1109 | setAxisTitleOrientation(X_AXIS, orientation); |
---|
1110 | setAxisTitleOrientation(Y_AXIS, orientation); |
---|
1111 | setAxisTitleOrientation(Z_AXIS, orientation); |
---|
1112 | } |
---|
1113 | |
---|
1114 | /** |
---|
1115 | * \brief Set axis label font family |
---|
1116 | */ |
---|
1117 | void Renderer::setAxesLabelFont(const char *fontName) |
---|
1118 | { |
---|
1119 | setAxisLabelFont(X_AXIS, fontName); |
---|
1120 | setAxisLabelFont(Y_AXIS, fontName); |
---|
1121 | setAxisLabelFont(Z_AXIS, fontName); |
---|
1122 | } |
---|
1123 | |
---|
1124 | /** |
---|
1125 | * \brief Set axis label font size |
---|
1126 | */ |
---|
1127 | void Renderer::setAxesLabelFontSize(int sz) |
---|
1128 | { |
---|
1129 | setAxisLabelFontSize(X_AXIS, sz); |
---|
1130 | setAxisLabelFontSize(Y_AXIS, sz); |
---|
1131 | setAxisLabelFontSize(Z_AXIS, sz); |
---|
1132 | } |
---|
1133 | |
---|
1134 | /** |
---|
1135 | * \brief Set orientation (as rotation in degrees) of axis labels |
---|
1136 | */ |
---|
1137 | void Renderer::setAxesLabelOrientation(double orientation) |
---|
1138 | { |
---|
1139 | setAxisLabelOrientation(X_AXIS, orientation); |
---|
1140 | setAxisLabelOrientation(Y_AXIS, orientation); |
---|
1141 | setAxisLabelOrientation(Z_AXIS, orientation); |
---|
1142 | } |
---|
1143 | |
---|
1144 | /** |
---|
1145 | * \brief Set printf style label format string |
---|
1146 | */ |
---|
1147 | void Renderer::setAxesLabelFormat(const char *format) |
---|
1148 | { |
---|
1149 | setAxisLabelFormat(X_AXIS, format); |
---|
1150 | setAxisLabelFormat(Y_AXIS, format); |
---|
1151 | setAxisLabelFormat(Z_AXIS, format); |
---|
1152 | } |
---|
1153 | |
---|
1154 | /** |
---|
1155 | * \brief Turn on/off rendering of the specified axis |
---|
1156 | */ |
---|
1157 | void Renderer::setAxisVisibility(Axis axis, bool state) |
---|
1158 | { |
---|
1159 | if (_cubeAxesActor != NULL) { |
---|
1160 | if (axis == X_AXIS) { |
---|
1161 | _cubeAxesActor->SetXAxisVisibility((state ? 1 : 0)); |
---|
1162 | } else if (axis == Y_AXIS) { |
---|
1163 | _cubeAxesActor->SetYAxisVisibility((state ? 1 : 0)); |
---|
1164 | } else if (axis == Z_AXIS) { |
---|
1165 | _cubeAxesActor->SetZAxisVisibility((state ? 1 : 0)); |
---|
1166 | } |
---|
1167 | _needsRedraw = true; |
---|
1168 | } |
---|
1169 | } |
---|
1170 | |
---|
1171 | /** |
---|
1172 | * \brief Turn on/off rendering of single axis gridlines |
---|
1173 | */ |
---|
1174 | void Renderer::setAxisGridVisibility(Axis axis, bool state) |
---|
1175 | { |
---|
1176 | if (_cubeAxesActor != NULL) { |
---|
1177 | if (axis == X_AXIS) { |
---|
1178 | _cubeAxesActor->SetDrawXGridlines((state ? 1 : 0)); |
---|
1179 | } else if (axis == Y_AXIS) { |
---|
1180 | _cubeAxesActor->SetDrawYGridlines((state ? 1 : 0)); |
---|
1181 | } else if (axis == Z_AXIS) { |
---|
1182 | _cubeAxesActor->SetDrawZGridlines((state ? 1 : 0)); |
---|
1183 | } |
---|
1184 | _needsRedraw = true; |
---|
1185 | } |
---|
1186 | } |
---|
1187 | |
---|
1188 | /** |
---|
1189 | * \brief Turn on/off rendering of single axis gridlines |
---|
1190 | */ |
---|
1191 | void Renderer::setAxisInnerGridVisibility(Axis axis, bool state) |
---|
1192 | { |
---|
1193 | if (_cubeAxesActor != NULL) { |
---|
1194 | if (axis == X_AXIS) { |
---|
1195 | _cubeAxesActor->SetDrawXInnerGridlines((state ? 1 : 0)); |
---|
1196 | } else if (axis == Y_AXIS) { |
---|
1197 | _cubeAxesActor->SetDrawYInnerGridlines((state ? 1 : 0)); |
---|
1198 | } else if (axis == Z_AXIS) { |
---|
1199 | _cubeAxesActor->SetDrawZInnerGridlines((state ? 1 : 0)); |
---|
1200 | } |
---|
1201 | _needsRedraw = true; |
---|
1202 | } |
---|
1203 | } |
---|
1204 | |
---|
1205 | /** |
---|
1206 | * \brief Turn on/off rendering of single axis gridlines |
---|
1207 | */ |
---|
1208 | void Renderer::setAxisGridpolysVisibility(Axis axis, bool state) |
---|
1209 | { |
---|
1210 | if (_cubeAxesActor != NULL) { |
---|
1211 | if (axis == X_AXIS) { |
---|
1212 | _cubeAxesActor->SetDrawXGridpolys((state ? 1 : 0)); |
---|
1213 | } else if (axis == Y_AXIS) { |
---|
1214 | _cubeAxesActor->SetDrawYGridpolys((state ? 1 : 0)); |
---|
1215 | } else if (axis == Z_AXIS) { |
---|
1216 | _cubeAxesActor->SetDrawZGridpolys((state ? 1 : 0)); |
---|
1217 | } |
---|
1218 | _needsRedraw = true; |
---|
1219 | } |
---|
1220 | } |
---|
1221 | |
---|
1222 | /** |
---|
1223 | * \brief Toggle label visibility for the specified axis |
---|
1224 | */ |
---|
1225 | void Renderer::setAxisLabelVisibility(Axis axis, bool state) |
---|
1226 | { |
---|
1227 | if (_cubeAxesActor != NULL) { |
---|
1228 | if (axis == X_AXIS) { |
---|
1229 | _cubeAxesActor->SetXAxisLabelVisibility((state ? 1 : 0)); |
---|
1230 | } else if (axis == Y_AXIS) { |
---|
1231 | _cubeAxesActor->SetYAxisLabelVisibility((state ? 1 : 0)); |
---|
1232 | } else if (axis == Z_AXIS) { |
---|
1233 | _cubeAxesActor->SetZAxisLabelVisibility((state ? 1 : 0)); |
---|
1234 | } |
---|
1235 | _needsRedraw = true; |
---|
1236 | } |
---|
1237 | } |
---|
1238 | |
---|
1239 | /** |
---|
1240 | * \brief Toggle tick visibility for the specified axis |
---|
1241 | */ |
---|
1242 | void Renderer::setAxisTickVisibility(Axis axis, bool state) |
---|
1243 | { |
---|
1244 | if (_cubeAxesActor != NULL) { |
---|
1245 | if (axis == X_AXIS) { |
---|
1246 | _cubeAxesActor->SetXAxisTickVisibility((state ? 1 : 0)); |
---|
1247 | } else if (axis == Y_AXIS) { |
---|
1248 | _cubeAxesActor->SetYAxisTickVisibility((state ? 1 : 0)); |
---|
1249 | } else if (axis == Z_AXIS) { |
---|
1250 | _cubeAxesActor->SetZAxisTickVisibility((state ? 1 : 0)); |
---|
1251 | } |
---|
1252 | _needsRedraw = true; |
---|
1253 | } |
---|
1254 | } |
---|
1255 | |
---|
1256 | /** |
---|
1257 | * \brief Toggle tick visibility for the specified axis |
---|
1258 | */ |
---|
1259 | void Renderer::setAxisMinorTickVisibility(Axis axis, bool state) |
---|
1260 | { |
---|
1261 | if (_cubeAxesActor != NULL) { |
---|
1262 | if (axis == X_AXIS) { |
---|
1263 | _cubeAxesActor->SetXAxisMinorTickVisibility((state ? 1 : 0)); |
---|
1264 | } else if (axis == Y_AXIS) { |
---|
1265 | _cubeAxesActor->SetYAxisMinorTickVisibility((state ? 1 : 0)); |
---|
1266 | } else if (axis == Z_AXIS) { |
---|
1267 | _cubeAxesActor->SetZAxisMinorTickVisibility((state ? 1 : 0)); |
---|
1268 | } |
---|
1269 | _needsRedraw = true; |
---|
1270 | } |
---|
1271 | } |
---|
1272 | |
---|
1273 | /** |
---|
1274 | * \brief Set color of axis lines, ticks, labels, titles |
---|
1275 | */ |
---|
1276 | void Renderer::setAxisColor(Axis axis, double color[3], double opacity) |
---|
1277 | { |
---|
1278 | setAxisTitleColor(axis, color, opacity); |
---|
1279 | setAxisLabelColor(axis, color, opacity); |
---|
1280 | setAxisLinesColor(axis, color, opacity); |
---|
1281 | setAxisGridlinesColor(axis, color, opacity); |
---|
1282 | setAxisInnerGridlinesColor(axis, color, opacity); |
---|
1283 | setAxisGridpolysColor(axis, color, opacity); |
---|
1284 | } |
---|
1285 | |
---|
1286 | /** |
---|
1287 | * \brief Set color of axis title text |
---|
1288 | */ |
---|
1289 | void Renderer::setAxisTitleColor(Axis axis, double color[3], double opacity) |
---|
1290 | { |
---|
1291 | if (_cubeAxesActor != NULL) { |
---|
1292 | _cubeAxesActor->GetTitleTextProperty(axis)->SetColor(color); |
---|
1293 | _cubeAxesActor->GetTitleTextProperty(axis)->SetOpacity(opacity); |
---|
1294 | _needsRedraw = true; |
---|
1295 | } |
---|
1296 | } |
---|
1297 | |
---|
1298 | /** |
---|
1299 | * \brief Set color of axis label text |
---|
1300 | */ |
---|
1301 | void Renderer::setAxisLabelColor(Axis axis, double color[3], double opacity) |
---|
1302 | { |
---|
1303 | if (_cubeAxesActor != NULL) { |
---|
1304 | _cubeAxesActor->GetLabelTextProperty(axis)->SetColor(color); |
---|
1305 | _cubeAxesActor->GetLabelTextProperty(axis)->SetOpacity(opacity); |
---|
1306 | _needsRedraw = true; |
---|
1307 | } |
---|
1308 | } |
---|
1309 | |
---|
1310 | /** |
---|
1311 | * \brief Set color of main axis line |
---|
1312 | */ |
---|
1313 | void Renderer::setAxisLinesColor(Axis axis, double color[3], double opacity) |
---|
1314 | { |
---|
1315 | if (_cubeAxesActor != NULL) { |
---|
1316 | switch (axis) { |
---|
1317 | case X_AXIS: |
---|
1318 | _cubeAxesActor->GetXAxesLinesProperty()->SetColor(color); |
---|
1319 | _cubeAxesActor->GetXAxesLinesProperty()->SetOpacity(opacity); |
---|
1320 | break; |
---|
1321 | case Y_AXIS: |
---|
1322 | _cubeAxesActor->GetYAxesLinesProperty()->SetColor(color); |
---|
1323 | _cubeAxesActor->GetYAxesLinesProperty()->SetOpacity(opacity); |
---|
1324 | break; |
---|
1325 | case Z_AXIS: |
---|
1326 | _cubeAxesActor->GetZAxesLinesProperty()->SetColor(color); |
---|
1327 | _cubeAxesActor->GetZAxesLinesProperty()->SetOpacity(opacity); |
---|
1328 | break; |
---|
1329 | default: |
---|
1330 | ; |
---|
1331 | } |
---|
1332 | _needsRedraw = true; |
---|
1333 | } |
---|
1334 | } |
---|
1335 | |
---|
1336 | /** |
---|
1337 | * \brief Set color of axis gridlines |
---|
1338 | */ |
---|
1339 | void Renderer::setAxisGridlinesColor(Axis axis, double color[3], double opacity) |
---|
1340 | { |
---|
1341 | if (_cubeAxesActor != NULL) { |
---|
1342 | switch (axis) { |
---|
1343 | case X_AXIS: |
---|
1344 | _cubeAxesActor->GetXAxesGridlinesProperty()->SetColor(color); |
---|
1345 | _cubeAxesActor->GetXAxesGridlinesProperty()->SetOpacity(opacity); |
---|
1346 | break; |
---|
1347 | case Y_AXIS: |
---|
1348 | _cubeAxesActor->GetYAxesGridlinesProperty()->SetColor(color); |
---|
1349 | _cubeAxesActor->GetYAxesGridlinesProperty()->SetOpacity(opacity); |
---|
1350 | break; |
---|
1351 | case Z_AXIS: |
---|
1352 | _cubeAxesActor->GetZAxesGridlinesProperty()->SetColor(color); |
---|
1353 | _cubeAxesActor->GetZAxesGridlinesProperty()->SetOpacity(opacity); |
---|
1354 | break; |
---|
1355 | default: |
---|
1356 | ; |
---|
1357 | } |
---|
1358 | _needsRedraw = true; |
---|
1359 | } |
---|
1360 | } |
---|
1361 | |
---|
1362 | /** |
---|
1363 | * \brief Set color of axis inner gridlines |
---|
1364 | */ |
---|
1365 | void Renderer::setAxisInnerGridlinesColor(Axis axis, double color[3], double opacity) |
---|
1366 | { |
---|
1367 | if (_cubeAxesActor != NULL) { |
---|
1368 | switch (axis) { |
---|
1369 | case X_AXIS: |
---|
1370 | _cubeAxesActor->GetXAxesInnerGridlinesProperty()->SetColor(color); |
---|
1371 | _cubeAxesActor->GetXAxesInnerGridlinesProperty()->SetOpacity(opacity); |
---|
1372 | break; |
---|
1373 | case Y_AXIS: |
---|
1374 | _cubeAxesActor->GetYAxesInnerGridlinesProperty()->SetColor(color); |
---|
1375 | _cubeAxesActor->GetYAxesInnerGridlinesProperty()->SetOpacity(opacity); |
---|
1376 | break; |
---|
1377 | case Z_AXIS: |
---|
1378 | _cubeAxesActor->GetZAxesInnerGridlinesProperty()->SetColor(color); |
---|
1379 | _cubeAxesActor->GetZAxesInnerGridlinesProperty()->SetOpacity(opacity); |
---|
1380 | break; |
---|
1381 | default: |
---|
1382 | ; |
---|
1383 | } |
---|
1384 | _needsRedraw = true; |
---|
1385 | } |
---|
1386 | } |
---|
1387 | |
---|
1388 | /** |
---|
1389 | * \brief Set color of axis inner grid polygons |
---|
1390 | * |
---|
1391 | * Default opacity is 0.6 |
---|
1392 | */ |
---|
1393 | void Renderer::setAxisGridpolysColor(Axis axis, double color[3], double opacity) |
---|
1394 | { |
---|
1395 | if (_cubeAxesActor != NULL) { |
---|
1396 | switch (axis) { |
---|
1397 | case X_AXIS: |
---|
1398 | _cubeAxesActor->GetXAxesGridpolysProperty()->SetColor(color); |
---|
1399 | _cubeAxesActor->GetXAxesGridpolysProperty()->SetOpacity(opacity); |
---|
1400 | break; |
---|
1401 | case Y_AXIS: |
---|
1402 | _cubeAxesActor->GetYAxesGridpolysProperty()->SetColor(color); |
---|
1403 | _cubeAxesActor->GetYAxesGridpolysProperty()->SetOpacity(opacity); |
---|
1404 | break; |
---|
1405 | case Z_AXIS: |
---|
1406 | _cubeAxesActor->GetZAxesGridpolysProperty()->SetColor(color); |
---|
1407 | _cubeAxesActor->GetZAxesGridpolysProperty()->SetOpacity(opacity); |
---|
1408 | break; |
---|
1409 | default: |
---|
1410 | ; |
---|
1411 | } |
---|
1412 | _needsRedraw = true; |
---|
1413 | } |
---|
1414 | } |
---|
1415 | |
---|
1416 | /** |
---|
1417 | * \brief Set title of the specified axis |
---|
1418 | */ |
---|
1419 | void Renderer::setAxisTitle(Axis axis, const char *title) |
---|
1420 | { |
---|
1421 | if (_cubeAxesActor != NULL) { |
---|
1422 | if (axis == X_AXIS) { |
---|
1423 | _cubeAxesActor->SetXTitle(title); |
---|
1424 | } else if (axis == Y_AXIS) { |
---|
1425 | _cubeAxesActor->SetYTitle(title); |
---|
1426 | } else if (axis == Z_AXIS) { |
---|
1427 | _cubeAxesActor->SetZTitle(title); |
---|
1428 | } |
---|
1429 | _needsRedraw = true; |
---|
1430 | } |
---|
1431 | } |
---|
1432 | |
---|
1433 | /** |
---|
1434 | * \brief Set units of the specified axis |
---|
1435 | */ |
---|
1436 | void Renderer::setAxisUnits(Axis axis, const char *units) |
---|
1437 | { |
---|
1438 | if (_cubeAxesActor != NULL) { |
---|
1439 | if (axis == X_AXIS) { |
---|
1440 | _cubeAxesActor->SetXUnits(units); |
---|
1441 | } else if (axis == Y_AXIS) { |
---|
1442 | _cubeAxesActor->SetYUnits(units); |
---|
1443 | } else if (axis == Z_AXIS) { |
---|
1444 | _cubeAxesActor->SetZUnits(units); |
---|
1445 | } |
---|
1446 | _needsRedraw = true; |
---|
1447 | } |
---|
1448 | } |
---|
1449 | |
---|
1450 | /** |
---|
1451 | * \brief Set axis title font family |
---|
1452 | */ |
---|
1453 | void Renderer::setAxisTitleFont(Axis axis, const char *fontName) |
---|
1454 | { |
---|
1455 | if (_cubeAxesActor != NULL) { |
---|
1456 | TRACE("Setting axis %d title font to: '%s'", axis, fontName); |
---|
1457 | _cubeAxesActor->GetTitleTextProperty(axis)->SetFontFamilyAsString(fontName); |
---|
1458 | _needsRedraw = true; |
---|
1459 | } |
---|
1460 | } |
---|
1461 | |
---|
1462 | /** |
---|
1463 | * \brief Set axis title font size (and optionally pixel size) |
---|
1464 | */ |
---|
1465 | void Renderer::setAxisTitleFontSize(Axis axis, int sz) |
---|
1466 | { |
---|
1467 | if (_cubeAxesActor != NULL) { |
---|
1468 | TRACE("Setting axis %d title font size to: %d", axis, sz); |
---|
1469 | _cubeAxesActor->GetTitleTextProperty(axis)->SetFontSize(sz); |
---|
1470 | _needsRedraw = true; |
---|
1471 | } |
---|
1472 | } |
---|
1473 | |
---|
1474 | /** |
---|
1475 | * \brief Set orientation (as rotation in degrees) of axis titles |
---|
1476 | */ |
---|
1477 | void Renderer::setAxisTitleOrientation(Axis axis, double orientation) |
---|
1478 | { |
---|
1479 | if (_cubeAxesActor != NULL) { |
---|
1480 | TRACE("Setting axis %d title orientation to: %g", axis, orientation); |
---|
1481 | _cubeAxesActor->GetTitleTextProperty(axis)->SetOrientation(orientation); |
---|
1482 | _needsRedraw = true; |
---|
1483 | } |
---|
1484 | } |
---|
1485 | |
---|
1486 | /** |
---|
1487 | * \brief Set axis label font family |
---|
1488 | */ |
---|
1489 | void Renderer::setAxisLabelFont(Axis axis, const char *fontName) |
---|
1490 | { |
---|
1491 | if (_cubeAxesActor != NULL) { |
---|
1492 | TRACE("Setting axis %d label font to: '%s'", axis, fontName); |
---|
1493 | _cubeAxesActor->GetLabelTextProperty(axis)->SetFontFamilyAsString(fontName); |
---|
1494 | _needsRedraw = true; |
---|
1495 | } |
---|
1496 | } |
---|
1497 | |
---|
1498 | /** |
---|
1499 | * \brief Set axis label font size (and optionally pixel size) |
---|
1500 | */ |
---|
1501 | void Renderer::setAxisLabelFontSize(Axis axis, int sz) |
---|
1502 | { |
---|
1503 | if (_cubeAxesActor != NULL) { |
---|
1504 | TRACE("Setting axis %d label font size to: %d", axis, sz); |
---|
1505 | _cubeAxesActor->GetLabelTextProperty(axis)->SetFontSize(sz); |
---|
1506 | _needsRedraw = true; |
---|
1507 | } |
---|
1508 | } |
---|
1509 | |
---|
1510 | /** |
---|
1511 | * \brief Set orientation (as rotation in degrees) of axis labels |
---|
1512 | */ |
---|
1513 | void Renderer::setAxisLabelOrientation(Axis axis, double orientation) |
---|
1514 | { |
---|
1515 | if (_cubeAxesActor != NULL) { |
---|
1516 | TRACE("Setting axis %d label orientation to: %g", axis, orientation); |
---|
1517 | _cubeAxesActor->GetLabelTextProperty(axis)->SetOrientation(orientation); |
---|
1518 | _needsRedraw = true; |
---|
1519 | } |
---|
1520 | } |
---|
1521 | |
---|
1522 | /** |
---|
1523 | * \brief Set printf style label format string |
---|
1524 | */ |
---|
1525 | void Renderer::setAxisLabelFormat(Axis axis, const char *format) |
---|
1526 | { |
---|
1527 | if (_cubeAxesActor != NULL) { |
---|
1528 | TRACE("Setting axis %d label format to: '%s'", axis, format); |
---|
1529 | if (axis == X_AXIS) { |
---|
1530 | _cubeAxesActor->SetXLabelFormat(format); |
---|
1531 | _cubeAxesActor->XAutoLabelFormatOff(); |
---|
1532 | } else if (axis == Y_AXIS) { |
---|
1533 | _cubeAxesActor->SetYLabelFormat(format); |
---|
1534 | _cubeAxesActor->YAutoLabelFormatOff(); |
---|
1535 | } else if (axis == Z_AXIS) { |
---|
1536 | _cubeAxesActor->SetZLabelFormat(format); |
---|
1537 | _cubeAxesActor->ZAutoLabelFormatOff(); |
---|
1538 | } |
---|
1539 | _needsRedraw = true; |
---|
1540 | } |
---|
1541 | } |
---|
1542 | |
---|
1543 | /** |
---|
1544 | * \brief Notify graphics objects that color map has changed |
---|
1545 | */ |
---|
1546 | void Renderer::updateColorMap(ColorMap *cmap) |
---|
1547 | { |
---|
1548 | TRACE("%s", cmap->getName().c_str()); |
---|
1549 | updateGraphicsObjectColorMap<Contour2D>(cmap); |
---|
1550 | updateGraphicsObjectColorMap<Contour3D>(cmap); |
---|
1551 | updateGraphicsObjectColorMap<Cutplane>(cmap); |
---|
1552 | updateGraphicsObjectColorMap<Glyphs>(cmap); |
---|
1553 | updateGraphicsObjectColorMap<HeightMap>(cmap); |
---|
1554 | updateGraphicsObjectColorMap<LIC>(cmap); |
---|
1555 | updateGraphicsObjectColorMap<Molecule>(cmap); |
---|
1556 | updateGraphicsObjectColorMap<PseudoColor>(cmap); |
---|
1557 | updateGraphicsObjectColorMap<Streamlines>(cmap); |
---|
1558 | updateGraphicsObjectColorMap<Volume>(cmap); |
---|
1559 | updateGraphicsObjectColorMap<Warp>(cmap); |
---|
1560 | TRACE("Leave"); |
---|
1561 | } |
---|
1562 | |
---|
1563 | /** |
---|
1564 | * \brief Check if a ColorMap is in use by graphics objects |
---|
1565 | */ |
---|
1566 | bool Renderer::colorMapUsed(ColorMap *cmap) |
---|
1567 | { |
---|
1568 | if (graphicsObjectColorMapUsed<Contour2D>(cmap)) |
---|
1569 | return true; |
---|
1570 | if (graphicsObjectColorMapUsed<Contour3D>(cmap)) |
---|
1571 | return true; |
---|
1572 | if (graphicsObjectColorMapUsed<Cutplane>(cmap)) |
---|
1573 | return true; |
---|
1574 | if (graphicsObjectColorMapUsed<Glyphs>(cmap)) |
---|
1575 | return true; |
---|
1576 | if (graphicsObjectColorMapUsed<HeightMap>(cmap)) |
---|
1577 | return true; |
---|
1578 | if (graphicsObjectColorMapUsed<LIC>(cmap)) |
---|
1579 | return true; |
---|
1580 | if (graphicsObjectColorMapUsed<Molecule>(cmap)) |
---|
1581 | return true; |
---|
1582 | if (graphicsObjectColorMapUsed<PseudoColor>(cmap)) |
---|
1583 | return true; |
---|
1584 | if (graphicsObjectColorMapUsed<Streamlines>(cmap)) |
---|
1585 | return true; |
---|
1586 | if (graphicsObjectColorMapUsed<Volume>(cmap)) |
---|
1587 | return true; |
---|
1588 | if (graphicsObjectColorMapUsed<Warp>(cmap)) |
---|
1589 | return true; |
---|
1590 | |
---|
1591 | return false; |
---|
1592 | } |
---|
1593 | |
---|
1594 | /** |
---|
1595 | * \brief Add/replace a ColorMap for use in the Renderer |
---|
1596 | */ |
---|
1597 | void Renderer::addColorMap(const ColorMapId& id, ColorMap *colorMap) |
---|
1598 | { |
---|
1599 | if (colorMap != NULL) { |
---|
1600 | colorMap->build(); |
---|
1601 | if (getColorMap(id) != NULL) { |
---|
1602 | TRACE("Replacing existing ColorMap %s", id.c_str()); |
---|
1603 | // Copy to current colormap to avoid invalidating |
---|
1604 | // pointers in graphics objects using the color map |
---|
1605 | *_colorMaps[id] = *colorMap; |
---|
1606 | delete colorMap; |
---|
1607 | // Notify graphics objects of change |
---|
1608 | updateColorMap(_colorMaps[id]); |
---|
1609 | } else |
---|
1610 | _colorMaps[id] = colorMap; |
---|
1611 | } else { |
---|
1612 | ERROR("NULL ColorMap"); |
---|
1613 | } |
---|
1614 | } |
---|
1615 | |
---|
1616 | /** |
---|
1617 | * \brief Return the ColorMap associated with the colormap key given |
---|
1618 | */ |
---|
1619 | ColorMap *Renderer::getColorMap(const ColorMapId& id) |
---|
1620 | { |
---|
1621 | ColorMapHashmap::iterator itr = _colorMaps.find(id); |
---|
1622 | |
---|
1623 | if (itr == _colorMaps.end()) |
---|
1624 | return NULL; |
---|
1625 | else |
---|
1626 | return itr->second; |
---|
1627 | } |
---|
1628 | |
---|
1629 | /** |
---|
1630 | * \brief Remove the colormap associated with the key given |
---|
1631 | * |
---|
1632 | * The underlying vtkLookupTable will be deleted if it is not referenced |
---|
1633 | * by any other objects |
---|
1634 | */ |
---|
1635 | void Renderer::deleteColorMap(const ColorMapId& id) |
---|
1636 | { |
---|
1637 | ColorMapHashmap::iterator itr; |
---|
1638 | |
---|
1639 | bool doAll = false; |
---|
1640 | |
---|
1641 | if (id.compare("all") == 0) { |
---|
1642 | itr = _colorMaps.begin(); |
---|
1643 | doAll = true; |
---|
1644 | } else { |
---|
1645 | itr = _colorMaps.find(id); |
---|
1646 | } |
---|
1647 | |
---|
1648 | if (itr == _colorMaps.end()) { |
---|
1649 | ERROR("Unknown ColorMap %s", id.c_str()); |
---|
1650 | return; |
---|
1651 | } |
---|
1652 | |
---|
1653 | do { |
---|
1654 | if (itr->second->getName().compare("default") == 0 || |
---|
1655 | itr->second->getName().compare("grayDefault") == 0 || |
---|
1656 | itr->second->getName().compare("volumeDefault") == 0 || |
---|
1657 | itr->second->getName().compare("elementDefault") == 0) { |
---|
1658 | if (id.compare("all") != 0) { |
---|
1659 | WARN("Cannot delete a default color map"); |
---|
1660 | } |
---|
1661 | continue; |
---|
1662 | } else if (colorMapUsed(itr->second)) { |
---|
1663 | WARN("Cannot delete color map '%s', it is in use", itr->second->getName().c_str()); |
---|
1664 | continue; |
---|
1665 | } |
---|
1666 | |
---|
1667 | TRACE("Deleting ColorMap %s", itr->second->getName().c_str()); |
---|
1668 | |
---|
1669 | delete itr->second; |
---|
1670 | itr = _colorMaps.erase(itr); |
---|
1671 | } while (doAll && itr != _colorMaps.end()); |
---|
1672 | } |
---|
1673 | |
---|
1674 | /** |
---|
1675 | * \brief Set the number of discrete colors used in the colormap's lookup table |
---|
1676 | * |
---|
1677 | * Note that the number of table entries is independent of the number of |
---|
1678 | * control points in the color/alpha ramp |
---|
1679 | */ |
---|
1680 | void Renderer::setColorMapNumberOfTableEntries(const ColorMapId& id, int numEntries) |
---|
1681 | { |
---|
1682 | ColorMapHashmap::iterator itr; |
---|
1683 | |
---|
1684 | bool doAll = false; |
---|
1685 | |
---|
1686 | if (id.compare("all") == 0) { |
---|
1687 | itr = _colorMaps.begin(); |
---|
1688 | doAll = true; |
---|
1689 | } else { |
---|
1690 | itr = _colorMaps.find(id); |
---|
1691 | } |
---|
1692 | |
---|
1693 | if (itr == _colorMaps.end()) { |
---|
1694 | ERROR("Unknown ColorMap %s", id.c_str()); |
---|
1695 | return; |
---|
1696 | } |
---|
1697 | |
---|
1698 | if (numEntries < 0) { |
---|
1699 | numEntries = 256; |
---|
1700 | TRACE("Setting numEntries to default value of %d", numEntries); |
---|
1701 | } |
---|
1702 | |
---|
1703 | do { |
---|
1704 | if (itr->second->getName() == "elementDefault") { |
---|
1705 | TRACE("Can't change number of table entries for default element color map"); |
---|
1706 | } else { |
---|
1707 | itr->second->setNumberOfTableEntries(numEntries); |
---|
1708 | updateColorMap(itr->second); |
---|
1709 | } |
---|
1710 | } while (doAll && ++itr != _colorMaps.end()); |
---|
1711 | |
---|
1712 | _needsRedraw = true; |
---|
1713 | } |
---|
1714 | |
---|
1715 | /** |
---|
1716 | * \brief Render a labelled legend image for the given colormap |
---|
1717 | * |
---|
1718 | * The field is assumed to be the active scalar or vector field |
---|
1719 | * based on the legendType. |
---|
1720 | * |
---|
1721 | * \param[in] id ColorMap name |
---|
1722 | * \param[in] dataSetID DataSet name |
---|
1723 | * \param[in] legendType scalar or vector field legend |
---|
1724 | * \param[in,out] title If supplied, draw title ("#auto" means to |
---|
1725 | * fill in field name and draw). If blank, do not draw title. |
---|
1726 | * If title was blank or "#auto", will be filled with field name on |
---|
1727 | * return |
---|
1728 | * \param[in,out] range Data range to use in legend. Set min > max to have |
---|
1729 | * range computed, will be filled with valid min and max values |
---|
1730 | * \param[in] width Pixel width of legend (aspect controls orientation) |
---|
1731 | * \param[in] height Pixel height of legend (aspect controls orientation) |
---|
1732 | * \param[in] opaque Flag to control if legend is rendered opaque or translucent |
---|
1733 | * \param[in] numLabels Number of labels to render (includes min/max) |
---|
1734 | * \param[in,out] imgData Pointer to array to fill with image bytes. Array |
---|
1735 | * will be resized if needed. |
---|
1736 | * \return The image is rendered into the supplied array, false is |
---|
1737 | * returned if the color map is not found |
---|
1738 | */ |
---|
1739 | bool Renderer::renderColorMap(const ColorMapId& id, |
---|
1740 | const DataSetId& dataSetID, |
---|
1741 | Renderer::LegendType legendType, |
---|
1742 | std::string& title, |
---|
1743 | double range[2], |
---|
1744 | int width, int height, |
---|
1745 | bool opaque, |
---|
1746 | int numLabels, |
---|
1747 | vtkUnsignedCharArray *imgData) |
---|
1748 | { |
---|
1749 | DataSet *dataSet = NULL; |
---|
1750 | if (dataSetID.compare("all") == 0) { |
---|
1751 | if (_dataSets.empty()) { |
---|
1752 | WARN("No DataSets exist, can't fill title or range"); |
---|
1753 | return renderColorMap(id, dataSetID, legendType, |
---|
1754 | NULL, |
---|
1755 | DataSet::POINT_DATA, |
---|
1756 | title, range, width, height, opaque, numLabels, imgData); |
---|
1757 | } else { |
---|
1758 | dataSet = _dataSets.begin()->second; |
---|
1759 | } |
---|
1760 | } else { |
---|
1761 | dataSet = getDataSet(dataSetID); |
---|
1762 | if (dataSet == NULL) { |
---|
1763 | ERROR("DataSet '%s' not found", dataSetID.c_str()); |
---|
1764 | return false; |
---|
1765 | } |
---|
1766 | } |
---|
1767 | |
---|
1768 | if (legendType == LEGEND_SCALAR) { |
---|
1769 | return renderColorMap(id, dataSetID, legendType, |
---|
1770 | dataSet->getActiveScalarsName(), |
---|
1771 | dataSet->getActiveScalarsType(), |
---|
1772 | title, range, width, height, opaque, numLabels, imgData); |
---|
1773 | } else { |
---|
1774 | return renderColorMap(id, dataSetID, legendType, |
---|
1775 | dataSet->getActiveVectorsName(), |
---|
1776 | dataSet->getActiveVectorsType(), |
---|
1777 | title, range, width, height, opaque, numLabels, imgData); |
---|
1778 | } |
---|
1779 | } |
---|
1780 | |
---|
1781 | /** |
---|
1782 | * \brief Render a labelled legend image for the given colormap |
---|
1783 | * |
---|
1784 | * The field is assumed to be point data, if the field is not found |
---|
1785 | * as point data, cell data is used. |
---|
1786 | * |
---|
1787 | * \param[in] id ColorMap name |
---|
1788 | * \param[in] dataSetID DataSet name |
---|
1789 | * \param[in] legendType scalar or vector field legend |
---|
1790 | * \param[in] fieldName Name of the field array this legend is for |
---|
1791 | * \param[in,out] title If supplied, draw title ("#auto" means to |
---|
1792 | * fill in field name and draw). If blank, do not draw title. |
---|
1793 | * If title was blank or "#auto", will be filled with field name on |
---|
1794 | * return |
---|
1795 | * \param[in,out] range Data range to use in legend. Set min > max to have |
---|
1796 | * range computed, will be filled with valid min and max values |
---|
1797 | * \param[in] width Pixel width of legend (aspect controls orientation) |
---|
1798 | * \param[in] height Pixel height of legend (aspect controls orientation) |
---|
1799 | * \param[in] opaque Flag to control if legend is rendered opaque or translucent |
---|
1800 | * \param[in] numLabels Number of labels to render (includes min/max) |
---|
1801 | * \param[in,out] imgData Pointer to array to fill with image bytes. Array |
---|
1802 | * will be resized if needed. |
---|
1803 | * \return The image is rendered into the supplied array, false is |
---|
1804 | * returned if the color map is not found |
---|
1805 | */ |
---|
1806 | bool Renderer::renderColorMap(const ColorMapId& id, |
---|
1807 | const DataSetId& dataSetID, |
---|
1808 | Renderer::LegendType legendType, |
---|
1809 | const char *fieldName, |
---|
1810 | std::string& title, |
---|
1811 | double range[2], |
---|
1812 | int width, int height, |
---|
1813 | bool opaque, |
---|
1814 | int numLabels, |
---|
1815 | vtkUnsignedCharArray *imgData) |
---|
1816 | { |
---|
1817 | DataSet *dataSet = NULL; |
---|
1818 | if (dataSetID.compare("all") == 0) { |
---|
1819 | if (_dataSets.empty()) { |
---|
1820 | WARN("No DataSets exist, can't fill title or range"); |
---|
1821 | return renderColorMap(id, dataSetID, legendType, |
---|
1822 | NULL, |
---|
1823 | DataSet::POINT_DATA, |
---|
1824 | title, range, width, height, opaque, numLabels, imgData); |
---|
1825 | } else { |
---|
1826 | dataSet = _dataSets.begin()->second; |
---|
1827 | } |
---|
1828 | } else { |
---|
1829 | dataSet = getDataSet(dataSetID); |
---|
1830 | if (dataSet == NULL) { |
---|
1831 | ERROR("DataSet '%s' not found", dataSetID.c_str()); |
---|
1832 | return false; |
---|
1833 | } |
---|
1834 | } |
---|
1835 | |
---|
1836 | DataSet::DataAttributeType attrType; |
---|
1837 | int numComponents; |
---|
1838 | |
---|
1839 | dataSet->getFieldInfo(fieldName, &attrType, &numComponents); |
---|
1840 | |
---|
1841 | return renderColorMap(id, dataSetID, legendType, |
---|
1842 | fieldName, |
---|
1843 | attrType, |
---|
1844 | title, range, width, height, opaque, numLabels, imgData); |
---|
1845 | } |
---|
1846 | |
---|
1847 | /** |
---|
1848 | * \brief Render a labelled legend image for the given colormap |
---|
1849 | * |
---|
1850 | * \param[in] id ColorMap name |
---|
1851 | * \param[in] dataSetID DataSet name |
---|
1852 | * \param[in] legendType scalar or vector field legend |
---|
1853 | * \param[in] fieldName Name of the field array this legend is for |
---|
1854 | * \param[in] type DataAttributeType of the field |
---|
1855 | * \param[in,out] title If supplied, draw title ("#auto" means to |
---|
1856 | * fill in field name and draw). If blank, do not draw title. |
---|
1857 | * If title was blank or "#auto", will be filled with field name on |
---|
1858 | * return |
---|
1859 | * \param[in,out] range Data range to use in legend. Set min > max to have |
---|
1860 | * range computed, will be filled with valid min and max values |
---|
1861 | * \param[in] width Pixel width of legend (aspect controls orientation) |
---|
1862 | * \param[in] height Pixel height of legend (aspect controls orientation) |
---|
1863 | * \param[in] opaque Flag to control if legend is rendered opaque or translucent |
---|
1864 | * \param[in] numLabels Number of labels to render (includes min/max) |
---|
1865 | * \param[in,out] imgData Pointer to array to fill with image bytes. Array |
---|
1866 | * will be resized if needed. |
---|
1867 | * \return The image is rendered into the supplied array, false is |
---|
1868 | * returned if the color map is not found |
---|
1869 | */ |
---|
1870 | bool Renderer::renderColorMap(const ColorMapId& id, |
---|
1871 | const DataSetId& dataSetID, |
---|
1872 | Renderer::LegendType legendType, |
---|
1873 | const char *fieldName, |
---|
1874 | DataSet::DataAttributeType type, |
---|
1875 | std::string& title, |
---|
1876 | double range[2], |
---|
1877 | int width, int height, |
---|
1878 | bool opaque, |
---|
1879 | int numLabels, |
---|
1880 | vtkUnsignedCharArray *imgData) |
---|
1881 | { |
---|
1882 | TRACE("Enter"); |
---|
1883 | ColorMap *colorMap = getColorMap(id); |
---|
1884 | if (colorMap == NULL) |
---|
1885 | return false; |
---|
1886 | |
---|
1887 | if (_legendRenderWindow == NULL) { |
---|
1888 | _legendRenderWindow = vtkSmartPointer<vtkRenderWindow>::New(); |
---|
1889 | _legendRenderWindow->SetMultiSamples(0); |
---|
1890 | #ifdef USE_OFFSCREEN_RENDERING |
---|
1891 | _legendRenderWindow->DoubleBufferOff(); |
---|
1892 | _legendRenderWindow->OffScreenRenderingOn(); |
---|
1893 | #else |
---|
1894 | _legendRenderWindow->DoubleBufferOn(); |
---|
1895 | _legendRenderWindow->SwapBuffersOff(); |
---|
1896 | #endif |
---|
1897 | } |
---|
1898 | |
---|
1899 | _legendRenderWindow->SetSize(width, height); |
---|
1900 | |
---|
1901 | if (_legendRenderer == NULL) { |
---|
1902 | _legendRenderer = vtkSmartPointer<vtkRenderer>::New(); |
---|
1903 | _legendRenderWindow->AddRenderer(_legendRenderer); |
---|
1904 | } |
---|
1905 | _legendRenderer->SetBackground(_bgColor[0], _bgColor[1], _bgColor[2]); |
---|
1906 | |
---|
1907 | if (_scalarBarActor == NULL) { |
---|
1908 | _scalarBarActor = vtkSmartPointer<vtkScalarBarActor>::New(); |
---|
1909 | _scalarBarActor->DrawFrameOff(); |
---|
1910 | _scalarBarActor->DrawBackgroundOff(); |
---|
1911 | _legendRenderer->AddViewProp(_scalarBarActor); |
---|
1912 | } |
---|
1913 | |
---|
1914 | if (opaque) { |
---|
1915 | _scalarBarActor->UseOpacityOff(); |
---|
1916 | } else { |
---|
1917 | _scalarBarActor->UseOpacityOn(); |
---|
1918 | } |
---|
1919 | |
---|
1920 | if (width > height) { |
---|
1921 | _scalarBarActor->SetOrientationToHorizontal(); |
---|
1922 | } else { |
---|
1923 | _scalarBarActor->SetOrientationToVertical(); |
---|
1924 | } |
---|
1925 | |
---|
1926 | // Set viewport-relative width/height/pos |
---|
1927 | if (title.empty() && numLabels == 0) { |
---|
1928 | _scalarBarActor->SetPosition(0, 0); |
---|
1929 | if (width > height) { |
---|
1930 | // horizontal |
---|
1931 | _scalarBarActor->SetHeight((((double)height+1.5)/((double)height))/0.4); // VTK: floor(actorHeight * .4) |
---|
1932 | _scalarBarActor->SetWidth(1); // VTK: actorWidth |
---|
1933 | } else { |
---|
1934 | // vertical |
---|
1935 | _scalarBarActor->SetHeight((((double)height+1.5)/((double)height))/0.86); // VTK: floor(actorHeight * .86) |
---|
1936 | _scalarBarActor->SetWidth(((double)(width+5))/((double)width)); // VTK: actorWidth - 4 pixels |
---|
1937 | } |
---|
1938 | } else { |
---|
1939 | if (width > height) { |
---|
1940 | // horizontal |
---|
1941 | _scalarBarActor->SetPosition(.075, .1); |
---|
1942 | _scalarBarActor->SetHeight(0.8); |
---|
1943 | _scalarBarActor->SetWidth(0.85); |
---|
1944 | } else { |
---|
1945 | // vertical |
---|
1946 | _scalarBarActor->SetPosition(.1, .05); |
---|
1947 | _scalarBarActor->SetHeight(0.9); |
---|
1948 | _scalarBarActor->SetWidth(0.8); |
---|
1949 | } |
---|
1950 | } |
---|
1951 | |
---|
1952 | vtkSmartPointer<vtkLookupTable> lut = colorMap->getLookupTable(); |
---|
1953 | DataSet *dataSet = NULL; |
---|
1954 | bool cumulative = _useCumulativeRange; |
---|
1955 | if (dataSetID.compare("all") == 0) { |
---|
1956 | if (_dataSets.empty()) { |
---|
1957 | WARN("No DataSets exist, can't fill title or range"); |
---|
1958 | } else { |
---|
1959 | dataSet = _dataSets.begin()->second; |
---|
1960 | } |
---|
1961 | cumulative = true; |
---|
1962 | } else { |
---|
1963 | dataSet = getDataSet(dataSetID); |
---|
1964 | if (dataSet == NULL) { |
---|
1965 | ERROR("DataSet '%s' not found", dataSetID.c_str()); |
---|
1966 | return false; |
---|
1967 | } |
---|
1968 | } |
---|
1969 | |
---|
1970 | bool drawTitle = false; |
---|
1971 | if (!title.empty()) { |
---|
1972 | drawTitle = true; |
---|
1973 | if (title.compare("#auto") == 0) { |
---|
1974 | title.clear(); |
---|
1975 | } |
---|
1976 | } |
---|
1977 | |
---|
1978 | bool needRange = false; |
---|
1979 | if (range[0] > range[1]) { |
---|
1980 | range[0] = 0.0; |
---|
1981 | range[1] = 1.0; |
---|
1982 | needRange = true; |
---|
1983 | } |
---|
1984 | |
---|
1985 | switch (legendType) { |
---|
1986 | case LEGEND_VECTOR_MAGNITUDE: |
---|
1987 | if (needRange) { |
---|
1988 | if (cumulative) { |
---|
1989 | getCumulativeDataRange(range, fieldName, type, 3); |
---|
1990 | } else if (dataSet != NULL) { |
---|
1991 | dataSet->getDataRange(range, fieldName, type); |
---|
1992 | } |
---|
1993 | } |
---|
1994 | |
---|
1995 | lut->SetRange(range); |
---|
1996 | |
---|
1997 | if (title.empty() && dataSet != NULL) { |
---|
1998 | if (fieldName != NULL) { |
---|
1999 | title = fieldName; |
---|
2000 | title.append("(mag)"); |
---|
2001 | } |
---|
2002 | } |
---|
2003 | break; |
---|
2004 | case LEGEND_VECTOR_X: |
---|
2005 | if (needRange) { |
---|
2006 | if (cumulative) { |
---|
2007 | getCumulativeDataRange(range, fieldName, type, 3, 0); |
---|
2008 | } else if (dataSet != NULL) { |
---|
2009 | dataSet->getDataRange(range, fieldName, type, 0); |
---|
2010 | } |
---|
2011 | } |
---|
2012 | |
---|
2013 | lut->SetRange(range); |
---|
2014 | |
---|
2015 | if (title.empty() && dataSet != NULL) { |
---|
2016 | if (fieldName != NULL) { |
---|
2017 | title = fieldName; |
---|
2018 | title.append("(x)"); |
---|
2019 | } |
---|
2020 | } |
---|
2021 | break; |
---|
2022 | case LEGEND_VECTOR_Y: |
---|
2023 | if (needRange) { |
---|
2024 | if (cumulative) { |
---|
2025 | getCumulativeDataRange(range, fieldName, type, 3, 1); |
---|
2026 | } else if (dataSet != NULL) { |
---|
2027 | dataSet->getDataRange(range, fieldName, type, 1); |
---|
2028 | } |
---|
2029 | } |
---|
2030 | |
---|
2031 | lut->SetRange(range); |
---|
2032 | |
---|
2033 | if (title.empty() && dataSet != NULL) { |
---|
2034 | if (fieldName != NULL) { |
---|
2035 | title = fieldName; |
---|
2036 | title.append("(y)"); |
---|
2037 | } |
---|
2038 | } |
---|
2039 | break; |
---|
2040 | case LEGEND_VECTOR_Z: |
---|
2041 | if (needRange) { |
---|
2042 | if (cumulative) { |
---|
2043 | getCumulativeDataRange(range, fieldName, type, 3, 2); |
---|
2044 | } else if (dataSet != NULL) { |
---|
2045 | dataSet->getDataRange(range, fieldName, type, 1); |
---|
2046 | } |
---|
2047 | } |
---|
2048 | |
---|
2049 | lut->SetRange(range); |
---|
2050 | |
---|
2051 | if (title.empty() && dataSet != NULL) { |
---|
2052 | if (fieldName != NULL) { |
---|
2053 | title = fieldName; |
---|
2054 | title.append("(z)"); |
---|
2055 | } |
---|
2056 | } |
---|
2057 | break; |
---|
2058 | case LEGEND_SCALAR: |
---|
2059 | default: |
---|
2060 | if (needRange) { |
---|
2061 | if (cumulative) { |
---|
2062 | getCumulativeDataRange(range, fieldName, type, 1); |
---|
2063 | } else if (dataSet != NULL) { |
---|
2064 | dataSet->getDataRange(range, fieldName, type); |
---|
2065 | } |
---|
2066 | } |
---|
2067 | |
---|
2068 | lut->SetRange(range); |
---|
2069 | |
---|
2070 | if (title.empty() && dataSet != NULL) { |
---|
2071 | if (fieldName != NULL) |
---|
2072 | title = fieldName; |
---|
2073 | } |
---|
2074 | break; |
---|
2075 | } |
---|
2076 | |
---|
2077 | _scalarBarActor->SetLookupTable(lut); |
---|
2078 | _scalarBarActor->SetMaximumNumberOfColors((width > height ? width : height)); |
---|
2079 | |
---|
2080 | if (drawTitle) { |
---|
2081 | _scalarBarActor->SetTitle(title.c_str()); |
---|
2082 | } else { |
---|
2083 | _scalarBarActor->SetTitle(""); |
---|
2084 | } |
---|
2085 | _scalarBarActor->GetTitleTextProperty()->ItalicOff(); |
---|
2086 | _scalarBarActor->SetNumberOfLabels(numLabels); |
---|
2087 | _scalarBarActor->GetLabelTextProperty()->BoldOff(); |
---|
2088 | _scalarBarActor->GetLabelTextProperty()->ItalicOff(); |
---|
2089 | _scalarBarActor->GetLabelTextProperty()->ShadowOff(); |
---|
2090 | |
---|
2091 | _legendRenderWindow->Render(); |
---|
2092 | |
---|
2093 | #ifdef RENDER_TARGA |
---|
2094 | _legendRenderWindow->MakeCurrent(); |
---|
2095 | // Must clear previous errors first. |
---|
2096 | while (glGetError() != GL_NO_ERROR){ |
---|
2097 | ; |
---|
2098 | } |
---|
2099 | int bytesPerPixel = TARGA_BYTES_PER_PIXEL; |
---|
2100 | int size = bytesPerPixel * width * height; |
---|
2101 | |
---|
2102 | if (imgData->GetMaxId() + 1 != size) |
---|
2103 | { |
---|
2104 | imgData->SetNumberOfComponents(bytesPerPixel); |
---|
2105 | imgData->SetNumberOfValues(size); |
---|
2106 | } |
---|
2107 | glDisable(GL_TEXTURE_2D); |
---|
2108 | if (_legendRenderWindow->GetDoubleBuffer()) { |
---|
2109 | glReadBuffer(static_cast<GLenum>(vtkOpenGLRenderWindow::SafeDownCast(_legendRenderWindow)->GetBackLeftBuffer())); |
---|
2110 | } else { |
---|
2111 | glReadBuffer(static_cast<GLenum>(vtkOpenGLRenderWindow::SafeDownCast(_legendRenderWindow)->GetFrontLeftBuffer())); |
---|
2112 | } |
---|
2113 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
---|
2114 | if (bytesPerPixel == 4) { |
---|
2115 | glReadPixels(0, 0, width, height, GL_BGRA, |
---|
2116 | GL_UNSIGNED_BYTE, imgData->GetPointer(0)); |
---|
2117 | } else { |
---|
2118 | glReadPixels(0, 0, width, height, GL_BGR, |
---|
2119 | GL_UNSIGNED_BYTE, imgData->GetPointer(0)); |
---|
2120 | } |
---|
2121 | if (glGetError() != GL_NO_ERROR) { |
---|
2122 | ERROR("glReadPixels"); |
---|
2123 | } |
---|
2124 | #else |
---|
2125 | _legendRenderWindow->GetPixelData(0, 0, width-1, height-1, |
---|
2126 | !_legendRenderWindow->GetDoubleBuffer(), |
---|
2127 | imgData); |
---|
2128 | #endif |
---|
2129 | TRACE("Leave"); |
---|
2130 | return true; |
---|
2131 | } |
---|
2132 | |
---|
2133 | /** |
---|
2134 | * \brief Set camera FOV based on render window height |
---|
2135 | * |
---|
2136 | * Computes a field-of-view angle based on some assumptions about |
---|
2137 | * viewer's distance to screen and pixel density |
---|
2138 | */ |
---|
2139 | void Renderer::setViewAngle(int height) |
---|
2140 | { |
---|
2141 | // Distance of eyes from screen in inches |
---|
2142 | double d = 20.0; |
---|
2143 | // Assume 72 ppi screen |
---|
2144 | double h = (double)height / 72.0; |
---|
2145 | |
---|
2146 | double angle = vtkMath::DegreesFromRadians(2.0 * atan((h/2.0)/d)); |
---|
2147 | _renderer->GetActiveCamera()->SetViewAngle(angle); |
---|
2148 | |
---|
2149 | TRACE("Setting view angle: %g", angle); |
---|
2150 | } |
---|
2151 | |
---|
2152 | /** |
---|
2153 | * \brief Resize the render window (image size for renderings) |
---|
2154 | */ |
---|
2155 | void Renderer::setWindowSize(int width, int height) |
---|
2156 | { |
---|
2157 | if (_windowWidth == width && |
---|
2158 | _windowHeight == height) { |
---|
2159 | TRACE("No change"); |
---|
2160 | return; |
---|
2161 | } |
---|
2162 | |
---|
2163 | TRACE("Setting window size to %dx%d", width, height); |
---|
2164 | |
---|
2165 | //setViewAngle(height); |
---|
2166 | |
---|
2167 | // FIXME: Fix up panning on aspect change |
---|
2168 | #ifdef notdef |
---|
2169 | if (_cameraPan[0] != 0.0) { |
---|
2170 | _cameraPan[0] *= ((double)_windowWidth / width); |
---|
2171 | } |
---|
2172 | if (_cameraPan[1] != 0.0) { |
---|
2173 | _cameraPan[1] *= ((double)_windowHeight / height); |
---|
2174 | } |
---|
2175 | #endif |
---|
2176 | _windowWidth = width; |
---|
2177 | _windowHeight = height; |
---|
2178 | _renderWindow->SetSize(_windowWidth, _windowHeight); |
---|
2179 | |
---|
2180 | if (_cameraMode == IMAGE) { |
---|
2181 | if (_cameraAspect == ASPECT_WINDOW) { |
---|
2182 | double imgWindowAspect = getImageCameraAspect(); |
---|
2183 | TRACE("Setting object aspect to %g", imgWindowAspect); |
---|
2184 | setObjectAspects(imgWindowAspect); |
---|
2185 | initCamera(); |
---|
2186 | } else { |
---|
2187 | if (_userImgWorldDims[0] > 0) { |
---|
2188 | _setCameraZoomRegion(_userImgWorldOrigin[0], |
---|
2189 | _userImgWorldOrigin[1], |
---|
2190 | _userImgWorldDims[0], |
---|
2191 | _userImgWorldDims[1]); |
---|
2192 | } else { |
---|
2193 | if (isCameraMaximized()) { |
---|
2194 | initCamera(); |
---|
2195 | } else { |
---|
2196 | _setCameraZoomRegion(_imgWorldOrigin[0], |
---|
2197 | _imgWorldOrigin[1], |
---|
2198 | _imgWorldDims[0], |
---|
2199 | _imgWorldDims[1]); |
---|
2200 | } |
---|
2201 | } |
---|
2202 | } |
---|
2203 | } |
---|
2204 | _needsRedraw = true; |
---|
2205 | } |
---|
2206 | |
---|
2207 | void Renderer::setObjectAspects(double aspectRatio) |
---|
2208 | { |
---|
2209 | setGraphicsObjectAspect<Arc>(aspectRatio); |
---|
2210 | setGraphicsObjectAspect<Arrow>(aspectRatio); |
---|
2211 | setGraphicsObjectAspect<Box>(aspectRatio); |
---|
2212 | setGraphicsObjectAspect<Cone>(aspectRatio); |
---|
2213 | setGraphicsObjectAspect<Contour2D>(aspectRatio); |
---|
2214 | setGraphicsObjectAspect<Contour3D>(aspectRatio); |
---|
2215 | setGraphicsObjectAspect<Cutplane>(aspectRatio); |
---|
2216 | setGraphicsObjectAspect<Cylinder>(aspectRatio); |
---|
2217 | setGraphicsObjectAspect<Disk>(aspectRatio); |
---|
2218 | setGraphicsObjectAspect<Glyphs>(aspectRatio); |
---|
2219 | setGraphicsObjectAspect<HeightMap>(aspectRatio); |
---|
2220 | setGraphicsObjectAspect<LIC>(aspectRatio); |
---|
2221 | setGraphicsObjectAspect<Line>(aspectRatio); |
---|
2222 | setGraphicsObjectAspect<Molecule>(aspectRatio); |
---|
2223 | setGraphicsObjectAspect<Outline>(aspectRatio); |
---|
2224 | setGraphicsObjectAspect<PolyData>(aspectRatio); |
---|
2225 | setGraphicsObjectAspect<Polygon>(aspectRatio); |
---|
2226 | setGraphicsObjectAspect<PseudoColor>(aspectRatio); |
---|
2227 | setGraphicsObjectAspect<Sphere>(aspectRatio); |
---|
2228 | setGraphicsObjectAspect<Streamlines>(aspectRatio); |
---|
2229 | setGraphicsObjectAspect<Volume>(aspectRatio); |
---|
2230 | setGraphicsObjectAspect<Warp>(aspectRatio); |
---|
2231 | } |
---|
2232 | |
---|
2233 | void Renderer::setCameraAspect(Aspect aspect) |
---|
2234 | { |
---|
2235 | //if (_cameraAspect == aspect) |
---|
2236 | // return; |
---|
2237 | |
---|
2238 | _cameraAspect = aspect; |
---|
2239 | |
---|
2240 | double aspectRatio = 0.0; |
---|
2241 | switch (aspect) { |
---|
2242 | case ASPECT_SQUARE: |
---|
2243 | aspectRatio = 1.0; |
---|
2244 | break; |
---|
2245 | case ASPECT_WINDOW: |
---|
2246 | aspectRatio = 1.0; |
---|
2247 | if (_cameraMode == IMAGE) { |
---|
2248 | aspectRatio = getImageCameraAspect(); |
---|
2249 | } |
---|
2250 | break; |
---|
2251 | case ASPECT_NATIVE: |
---|
2252 | default: |
---|
2253 | aspectRatio = 0.0; |
---|
2254 | } |
---|
2255 | |
---|
2256 | setObjectAspects(aspectRatio); |
---|
2257 | |
---|
2258 | if (_cameraMode == IMAGE) |
---|
2259 | _needsCameraReset = true; |
---|
2260 | _needsRedraw = true; |
---|
2261 | } |
---|
2262 | |
---|
2263 | /** |
---|
2264 | * \brief Change the camera type: perspective, orthographic or image view |
---|
2265 | * |
---|
2266 | * Perspective mode is a normal 3D camera. |
---|
2267 | * |
---|
2268 | * Orthogrphic mode is parallel projection. |
---|
2269 | * |
---|
2270 | * Image mode is an orthographic camera with fixed axes and a clipping region |
---|
2271 | * around the plot area, use _setCameraZoomRegion to control the displayed area |
---|
2272 | * |
---|
2273 | * \param[in] mode Enum specifying camera type |
---|
2274 | */ |
---|
2275 | void Renderer::setCameraMode(CameraMode mode) |
---|
2276 | { |
---|
2277 | if (_cameraMode == mode) return; |
---|
2278 | |
---|
2279 | CameraMode origMode = _cameraMode; |
---|
2280 | _cameraMode = mode; |
---|
2281 | resetAxes(); |
---|
2282 | |
---|
2283 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2284 | switch (mode) { |
---|
2285 | case ORTHO: { |
---|
2286 | TRACE("Set camera to Ortho mode"); |
---|
2287 | camera->ParallelProjectionOn(); |
---|
2288 | if (origMode == IMAGE) { |
---|
2289 | resetCamera(true); |
---|
2290 | } |
---|
2291 | break; |
---|
2292 | } |
---|
2293 | case PERSPECTIVE: { |
---|
2294 | TRACE("Set camera to Perspective mode"); |
---|
2295 | camera->ParallelProjectionOff(); |
---|
2296 | if (origMode == IMAGE) { |
---|
2297 | resetCamera(true); |
---|
2298 | } |
---|
2299 | break; |
---|
2300 | } |
---|
2301 | case IMAGE: { |
---|
2302 | camera->ParallelProjectionOn(); |
---|
2303 | _setCameraZoomRegion(_imgWorldOrigin[0], _imgWorldOrigin[1], |
---|
2304 | _imgWorldDims[0],_imgWorldDims[1]); |
---|
2305 | TRACE("Set camera to Image mode"); |
---|
2306 | break; |
---|
2307 | } |
---|
2308 | default: |
---|
2309 | ERROR("Unkown camera mode: %d", mode); |
---|
2310 | } |
---|
2311 | _needsRedraw = true; |
---|
2312 | } |
---|
2313 | |
---|
2314 | /** |
---|
2315 | * \brief Get the current camera mode |
---|
2316 | */ |
---|
2317 | Renderer::CameraMode Renderer::getCameraMode() const |
---|
2318 | { |
---|
2319 | return _cameraMode; |
---|
2320 | } |
---|
2321 | |
---|
2322 | /** |
---|
2323 | * \brief Set the VTK camera parameters based on a 4x4 view matrix |
---|
2324 | */ |
---|
2325 | void Renderer::setCameraFromMatrix(vtkCamera *camera, vtkMatrix4x4& mat) |
---|
2326 | { |
---|
2327 | double d = camera->GetDistance(); |
---|
2328 | double vu[3]; |
---|
2329 | vu[0] = mat[1][0]; |
---|
2330 | vu[1] = mat[1][1]; |
---|
2331 | vu[2] = mat[1][2]; |
---|
2332 | double trans[3]; |
---|
2333 | trans[0] = mat[0][3]; |
---|
2334 | trans[1] = mat[1][3]; |
---|
2335 | trans[2] = mat[2][3]; |
---|
2336 | mat[0][3] = 0; |
---|
2337 | mat[1][3] = 0; |
---|
2338 | mat[2][3] = 0; |
---|
2339 | double vpn[3] = {mat[2][0], mat[2][1], mat[2][2]}; |
---|
2340 | double pos[3]; |
---|
2341 | // With translation removed, we have an orthogonal matrix, |
---|
2342 | // so the inverse is the transpose |
---|
2343 | mat.Transpose(); |
---|
2344 | mat.MultiplyPoint(trans, pos); |
---|
2345 | pos[0] = -pos[0]; |
---|
2346 | pos[1] = -pos[1]; |
---|
2347 | pos[2] = -pos[2]; |
---|
2348 | double fp[3]; |
---|
2349 | fp[0] = pos[0] - vpn[0] * d; |
---|
2350 | fp[1] = pos[1] - vpn[1] * d; |
---|
2351 | fp[2] = pos[2] - vpn[2] * d; |
---|
2352 | camera->SetPosition(pos); |
---|
2353 | camera->SetFocalPoint(fp); |
---|
2354 | camera->SetViewUp(vu); |
---|
2355 | } |
---|
2356 | |
---|
2357 | /** |
---|
2358 | * \brief Set the orientation of the camera from a quaternion |
---|
2359 | * rotation |
---|
2360 | * |
---|
2361 | * \param[in] quat A quaternion with scalar part first: w,x,y,z |
---|
2362 | * \param[in] absolute Is rotation absolute or relative? |
---|
2363 | */ |
---|
2364 | void Renderer::setCameraOrientation(const double quat[4], bool absolute) |
---|
2365 | { |
---|
2366 | if (_cameraMode == IMAGE) |
---|
2367 | return; |
---|
2368 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2369 | vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); |
---|
2370 | vtkSmartPointer<vtkMatrix4x4> rotMat = vtkSmartPointer<vtkMatrix4x4>::New(); |
---|
2371 | |
---|
2372 | double q[4]; |
---|
2373 | copyQuat(quat, q); |
---|
2374 | |
---|
2375 | if (absolute) { |
---|
2376 | double abs[4]; |
---|
2377 | // Save absolute rotation |
---|
2378 | copyQuat(q, abs); |
---|
2379 | // Compute relative rotation |
---|
2380 | quatMult(quatReciprocal(_cameraOrientation), q, q); |
---|
2381 | // Store absolute rotation |
---|
2382 | copyQuat(abs, _cameraOrientation); |
---|
2383 | } else { |
---|
2384 | // Compute new absolute rotation |
---|
2385 | quatMult(_cameraOrientation, q, _cameraOrientation); |
---|
2386 | } |
---|
2387 | |
---|
2388 | quaternionToTransposeMatrix4x4(q, *rotMat); |
---|
2389 | #ifdef DEBUG |
---|
2390 | TRACE("Rotation matrix:\n %g %g %g\n %g %g %g\n %g %g %g", |
---|
2391 | (*rotMat)[0][0], (*rotMat)[0][1], (*rotMat)[0][2], |
---|
2392 | (*rotMat)[1][0], (*rotMat)[1][1], (*rotMat)[1][2], |
---|
2393 | (*rotMat)[2][0], (*rotMat)[2][1], (*rotMat)[2][2]); |
---|
2394 | vtkSmartPointer<vtkMatrix4x4> camMat = vtkSmartPointer<vtkMatrix4x4>::New(); |
---|
2395 | camMat->DeepCopy(camera->GetViewTransformMatrix()); |
---|
2396 | TRACE("Camera matrix:\n %g %g %g %g\n %g %g %g %g\n %g %g %g %g\n %g %g %g %g", |
---|
2397 | (*camMat)[0][0], (*camMat)[0][1], (*camMat)[0][2], (*camMat)[0][3], |
---|
2398 | (*camMat)[1][0], (*camMat)[1][1], (*camMat)[1][2], (*camMat)[1][3], |
---|
2399 | (*camMat)[2][0], (*camMat)[2][1], (*camMat)[2][2], (*camMat)[2][3], |
---|
2400 | (*camMat)[3][0], (*camMat)[3][1], (*camMat)[3][2], (*camMat)[3][3]); |
---|
2401 | printCameraInfo(camera); |
---|
2402 | #endif |
---|
2403 | trans->Translate(0, 0, -camera->GetDistance()); |
---|
2404 | trans->Concatenate(rotMat); |
---|
2405 | trans->Translate(0, 0, camera->GetDistance()); |
---|
2406 | trans->Concatenate(camera->GetViewTransformMatrix()); |
---|
2407 | setCameraFromMatrix(camera, *trans->GetMatrix()); |
---|
2408 | |
---|
2409 | resetCameraClippingRange(); |
---|
2410 | printCameraInfo(camera); |
---|
2411 | _needsRedraw = true; |
---|
2412 | } |
---|
2413 | |
---|
2414 | /** |
---|
2415 | * \brief Set the position and orientation of the camera |
---|
2416 | * |
---|
2417 | * \param[in] position x,y,z position of camera in world coordinates |
---|
2418 | * \param[in] focalPoint x,y,z look-at point in world coordinates |
---|
2419 | * \param[in] viewUp x,y,z up vector of camera |
---|
2420 | */ |
---|
2421 | void Renderer::setCameraOrientationAndPosition(const double position[3], |
---|
2422 | const double focalPoint[3], |
---|
2423 | const double viewUp[3]) |
---|
2424 | { |
---|
2425 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2426 | camera->SetPosition(position); |
---|
2427 | camera->SetFocalPoint(focalPoint); |
---|
2428 | camera->SetViewUp(viewUp); |
---|
2429 | resetCameraClippingRange(); |
---|
2430 | _needsRedraw = true; |
---|
2431 | } |
---|
2432 | |
---|
2433 | /** |
---|
2434 | * \brief Get the position and orientation of the camera |
---|
2435 | * |
---|
2436 | * \param[out] position x,y,z position of camera in world coordinates |
---|
2437 | * \param[out] focalPoint x,y,z look-at point in world coordinates |
---|
2438 | * \param[out] viewUp x,y,z up vector of camera |
---|
2439 | */ |
---|
2440 | void Renderer::getCameraOrientationAndPosition(double position[3], |
---|
2441 | double focalPoint[3], |
---|
2442 | double viewUp[3]) |
---|
2443 | { |
---|
2444 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2445 | camera->GetPosition(position); |
---|
2446 | camera->GetFocalPoint(focalPoint); |
---|
2447 | camera->GetViewUp(viewUp); |
---|
2448 | } |
---|
2449 | |
---|
2450 | void Renderer::sceneBoundsChanged() |
---|
2451 | { |
---|
2452 | #ifdef RESET_CAMERA_ON_SCENE_CHANGE |
---|
2453 | _needsCameraReset = true; |
---|
2454 | #else |
---|
2455 | _needsAxesReset = true; |
---|
2456 | _needsCameraClippingRangeReset = true; |
---|
2457 | #endif |
---|
2458 | |
---|
2459 | _needsRedraw = true; |
---|
2460 | } |
---|
2461 | |
---|
2462 | /** |
---|
2463 | * \brief Reset pan, zoom, clipping planes and optionally rotation |
---|
2464 | * |
---|
2465 | * \param[in] resetOrientation Reset the camera rotation/orientation also |
---|
2466 | */ |
---|
2467 | void Renderer::resetCamera(bool resetOrientation) |
---|
2468 | { |
---|
2469 | TRACE("Enter: %d", resetOrientation ? 1 : 0); |
---|
2470 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2471 | if (_cameraMode == IMAGE) { |
---|
2472 | initCamera(); |
---|
2473 | } else { |
---|
2474 | if (resetOrientation) { |
---|
2475 | camera->SetPosition(0, 0, 1); |
---|
2476 | camera->SetFocalPoint(0, 0, 0); |
---|
2477 | camera->SetViewUp(0, 1, 0); |
---|
2478 | _cameraOrientation[0] = 1.0; |
---|
2479 | _cameraOrientation[1] = 0.0; |
---|
2480 | _cameraOrientation[2] = 0.0; |
---|
2481 | _cameraOrientation[3] = 0.0; |
---|
2482 | } |
---|
2483 | //setViewAngle(_windowHeight); |
---|
2484 | //double bounds[6]; |
---|
2485 | //collectBounds(bounds, false); |
---|
2486 | //_renderer->ResetCamera(bounds); |
---|
2487 | if (_needsAxesReset) { |
---|
2488 | resetAxes(); |
---|
2489 | _needsAxesReset = false; |
---|
2490 | } |
---|
2491 | resetVtkCamera(); |
---|
2492 | //computeScreenWorldCoords(); |
---|
2493 | } |
---|
2494 | |
---|
2495 | printCameraInfo(camera); |
---|
2496 | |
---|
2497 | _cameraZoomRatio = 1; |
---|
2498 | _cameraPan[0] = 0; |
---|
2499 | _cameraPan[1] = 0; |
---|
2500 | |
---|
2501 | _needsRedraw = true; |
---|
2502 | } |
---|
2503 | |
---|
2504 | void Renderer::resetVtkCamera(double *bounds) |
---|
2505 | { |
---|
2506 | TRACE("Enter: bounds: %p", bounds); |
---|
2507 | if (bounds != NULL) |
---|
2508 | _renderer->ResetCamera(bounds); |
---|
2509 | else |
---|
2510 | _renderer->ResetCamera(); |
---|
2511 | printCameraInfo(_renderer->GetActiveCamera()); |
---|
2512 | } |
---|
2513 | |
---|
2514 | /** |
---|
2515 | * \brief Set the camera near/far clipping range based on current scene bounds |
---|
2516 | */ |
---|
2517 | void Renderer::resetCameraClippingRange() |
---|
2518 | { |
---|
2519 | _renderer->ResetCameraClippingRange(); |
---|
2520 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2521 | //double dist = camera->GetClippingRange()[0] + (camera->GetClippingRange()[1] - camera->GetClippingRange()[0])/2.0; |
---|
2522 | //camera->SetDistance(dist); |
---|
2523 | printCameraInfo(camera); |
---|
2524 | } |
---|
2525 | |
---|
2526 | /** |
---|
2527 | * \brief Perform a relative rotation to current camera orientation |
---|
2528 | * |
---|
2529 | * Angles are in degrees, rotation is about focal point |
---|
2530 | */ |
---|
2531 | void Renderer::rotateCamera(double yaw, double pitch, double roll) |
---|
2532 | { |
---|
2533 | if (_cameraMode == IMAGE) |
---|
2534 | return; |
---|
2535 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2536 | camera->Azimuth(yaw); // Rotate about object |
---|
2537 | //camera->SetYaw(yaw); // Rotate about camera |
---|
2538 | camera->Elevation(pitch); // Rotate about object |
---|
2539 | //camera->SetPitch(pitch); // Rotate about camera |
---|
2540 | camera->Roll(roll); // Roll about camera view axis |
---|
2541 | resetCameraClippingRange(); |
---|
2542 | //computeScreenWorldCoords(); |
---|
2543 | _needsRedraw = true; |
---|
2544 | } |
---|
2545 | |
---|
2546 | /** |
---|
2547 | * \brief Perform a 2D translation of the camera |
---|
2548 | * |
---|
2549 | * x,y pan amount are specified as signed absolute pan amount in viewport |
---|
2550 | * units -- i.e. 0 is no pan, .5 is half the viewport, 2 is twice the viewport, |
---|
2551 | * etc. |
---|
2552 | * |
---|
2553 | * \param[in] x Viewport coordinate horizontal panning (positive number pans |
---|
2554 | * camera left, object right) |
---|
2555 | * \param[in] y Viewport coordinate vertical panning (positive number pans |
---|
2556 | * camera up, object down) |
---|
2557 | * \param[in] absolute Control if pan amount is relative to current or absolute |
---|
2558 | */ |
---|
2559 | void Renderer::panCamera(double x, double y, bool absolute) |
---|
2560 | { |
---|
2561 | TRACE("Enter panCamera: %g %g, current abs: %g %g", |
---|
2562 | x, y, _cameraPan[0], _cameraPan[1]); |
---|
2563 | |
---|
2564 | if (_cameraMode == IMAGE) { |
---|
2565 | _userImgWorldOrigin[0] = 0; |
---|
2566 | _userImgWorldOrigin[1] = 0; |
---|
2567 | _userImgWorldDims[0] = -1; |
---|
2568 | _userImgWorldDims[1] = -1; |
---|
2569 | |
---|
2570 | // Reverse x rather than y, since we are panning the camera, and client |
---|
2571 | // expects to be panning/moving the object |
---|
2572 | x = -x * _screenWorldCoords[2]; |
---|
2573 | y = y * _screenWorldCoords[3]; |
---|
2574 | |
---|
2575 | if (absolute) { |
---|
2576 | double panAbs[2]; |
---|
2577 | panAbs[0] = x; |
---|
2578 | panAbs[1] = y; |
---|
2579 | x -= _cameraPan[0]; |
---|
2580 | y -= _cameraPan[1]; |
---|
2581 | _cameraPan[0] = panAbs[0]; |
---|
2582 | _cameraPan[1] = panAbs[1]; |
---|
2583 | } else { |
---|
2584 | _cameraPan[0] += x; |
---|
2585 | _cameraPan[1] += y; |
---|
2586 | } |
---|
2587 | |
---|
2588 | _imgWorldOrigin[0] += x; |
---|
2589 | _imgWorldOrigin[1] += y; |
---|
2590 | _setCameraZoomRegion(_imgWorldOrigin[0], _imgWorldOrigin[1], |
---|
2591 | _imgWorldDims[0], _imgWorldDims[1]); |
---|
2592 | } else { |
---|
2593 | y = -y; |
---|
2594 | if (absolute) { |
---|
2595 | double panAbs[2]; |
---|
2596 | panAbs[0] = x; |
---|
2597 | panAbs[1] = y; |
---|
2598 | x -= _cameraPan[0]; |
---|
2599 | y -= _cameraPan[1]; |
---|
2600 | _cameraPan[0] = panAbs[0]; |
---|
2601 | _cameraPan[1] = panAbs[1]; |
---|
2602 | } else { |
---|
2603 | _cameraPan[0] += x; |
---|
2604 | _cameraPan[1] += y; |
---|
2605 | } |
---|
2606 | |
---|
2607 | if (x != 0.0 || y != 0.0) { |
---|
2608 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2609 | double viewFocus[4], focalDepth, viewPoint[3]; |
---|
2610 | double newPickPoint[4], oldPickPoint[4], motionVector[3]; |
---|
2611 | |
---|
2612 | camera->GetFocalPoint(viewFocus); |
---|
2613 | computeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], |
---|
2614 | viewFocus); |
---|
2615 | focalDepth = viewFocus[2]; |
---|
2616 | |
---|
2617 | computeDisplayToWorld(( x * 2. + 1.) * _windowWidth / 2.0, |
---|
2618 | ( y * 2. + 1.) * _windowHeight / 2.0, |
---|
2619 | focalDepth, |
---|
2620 | newPickPoint); |
---|
2621 | |
---|
2622 | computeDisplayToWorld(_windowWidth / 2.0, |
---|
2623 | _windowHeight / 2.0, |
---|
2624 | focalDepth, |
---|
2625 | oldPickPoint); |
---|
2626 | |
---|
2627 | // Camera motion is reversed |
---|
2628 | motionVector[0] = oldPickPoint[0] - newPickPoint[0]; |
---|
2629 | motionVector[1] = oldPickPoint[1] - newPickPoint[1]; |
---|
2630 | motionVector[2] = oldPickPoint[2] - newPickPoint[2]; |
---|
2631 | |
---|
2632 | camera->GetFocalPoint(viewFocus); |
---|
2633 | camera->GetPosition(viewPoint); |
---|
2634 | camera->SetFocalPoint(motionVector[0] + viewFocus[0], |
---|
2635 | motionVector[1] + viewFocus[1], |
---|
2636 | motionVector[2] + viewFocus[2]); |
---|
2637 | |
---|
2638 | camera->SetPosition(motionVector[0] + viewPoint[0], |
---|
2639 | motionVector[1] + viewPoint[1], |
---|
2640 | motionVector[2] + viewPoint[2]); |
---|
2641 | |
---|
2642 | resetCameraClippingRange(); |
---|
2643 | //computeScreenWorldCoords(); |
---|
2644 | } |
---|
2645 | } |
---|
2646 | |
---|
2647 | TRACE("Leave panCamera: %g %g, current abs: %g %g", |
---|
2648 | x, y, _cameraPan[0], _cameraPan[1]); |
---|
2649 | |
---|
2650 | _needsRedraw = true; |
---|
2651 | } |
---|
2652 | |
---|
2653 | /** |
---|
2654 | * \brief Dolly camera or set orthographic scaling based on camera type |
---|
2655 | * |
---|
2656 | * \param[in] z Ratio to change zoom (greater than 1 is zoom in, less than 1 is zoom out) |
---|
2657 | * \param[in] absolute Control if zoom factor is relative to current setting or absolute |
---|
2658 | */ |
---|
2659 | void Renderer::zoomCamera(double z, bool absolute) |
---|
2660 | { |
---|
2661 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2662 | TRACE("Enter Zoom: current abs: %g, z: %g, view angle %g", |
---|
2663 | _cameraZoomRatio, z, camera->GetViewAngle()); |
---|
2664 | |
---|
2665 | if (absolute) { |
---|
2666 | assert(_cameraZoomRatio > 0.0); |
---|
2667 | double zAbs = z; |
---|
2668 | z *= 1.0/_cameraZoomRatio; |
---|
2669 | _cameraZoomRatio = zAbs; |
---|
2670 | } else { |
---|
2671 | _cameraZoomRatio *= z; |
---|
2672 | } |
---|
2673 | |
---|
2674 | if (_cameraMode == IMAGE) { |
---|
2675 | _userImgWorldOrigin[0] = 0; |
---|
2676 | _userImgWorldOrigin[1] = 0; |
---|
2677 | _userImgWorldDims[0] = -1; |
---|
2678 | _userImgWorldDims[1] = -1; |
---|
2679 | |
---|
2680 | double dx = _imgWorldDims[0]; |
---|
2681 | double dy = _imgWorldDims[1]; |
---|
2682 | _imgWorldDims[0] /= z; |
---|
2683 | _imgWorldDims[1] /= z; |
---|
2684 | dx -= _imgWorldDims[0]; |
---|
2685 | dy -= _imgWorldDims[1]; |
---|
2686 | _imgWorldOrigin[0] += dx/2.0; |
---|
2687 | _imgWorldOrigin[1] += dy/2.0; |
---|
2688 | _setCameraZoomRegion(_imgWorldOrigin[0], _imgWorldOrigin[1], |
---|
2689 | _imgWorldDims[0], _imgWorldDims[1]); |
---|
2690 | } else { |
---|
2691 | // Keep ortho and perspective modes in sync |
---|
2692 | // Move camera forward/back for perspective camera |
---|
2693 | camera->Dolly(z); |
---|
2694 | // Change ortho parallel scale |
---|
2695 | camera->SetParallelScale(camera->GetParallelScale()/z); |
---|
2696 | resetCameraClippingRange(); |
---|
2697 | //computeScreenWorldCoords(); |
---|
2698 | } |
---|
2699 | |
---|
2700 | TRACE("Leave Zoom: rel %g, new abs: %g, view angle %g", |
---|
2701 | z, _cameraZoomRatio, camera->GetViewAngle()); |
---|
2702 | |
---|
2703 | _needsRedraw = true; |
---|
2704 | } |
---|
2705 | |
---|
2706 | bool Renderer::setCameraZoomRegionPixels(int x, int y, int width, int height) |
---|
2707 | { |
---|
2708 | if (_cameraMode != IMAGE) |
---|
2709 | return false; |
---|
2710 | |
---|
2711 | double wx, wy, ww, wh; |
---|
2712 | |
---|
2713 | y = _windowHeight - y; |
---|
2714 | double pxToWorldX = _screenWorldCoords[2] / (double)_windowWidth; |
---|
2715 | double pxToWorldY = _screenWorldCoords[3] / (double)_windowHeight; |
---|
2716 | |
---|
2717 | wx = _screenWorldCoords[0] + x * pxToWorldX; |
---|
2718 | wy = _screenWorldCoords[1] + y * pxToWorldY; |
---|
2719 | ww = abs(width) * pxToWorldX; |
---|
2720 | wh = abs(height) * pxToWorldY; |
---|
2721 | setCameraZoomRegion(wx, wy, ww, wh); |
---|
2722 | |
---|
2723 | TRACE("\npx: %d %d %d %d\nworld: %g %g %g %g", |
---|
2724 | x, y, width, height, |
---|
2725 | wx, wy, ww, wh); |
---|
2726 | |
---|
2727 | return true; |
---|
2728 | } |
---|
2729 | |
---|
2730 | bool Renderer::setCameraZoomRegion(double x, double y, double width, double height) |
---|
2731 | { |
---|
2732 | if (_cameraMode != IMAGE) |
---|
2733 | return false; |
---|
2734 | |
---|
2735 | _userImgWorldOrigin[0] = x; |
---|
2736 | _userImgWorldOrigin[1] = y; |
---|
2737 | _userImgWorldDims[0] = width; |
---|
2738 | _userImgWorldDims[1] = height; |
---|
2739 | _setCameraZoomRegion(x, y, width, height); |
---|
2740 | |
---|
2741 | return true; |
---|
2742 | } |
---|
2743 | |
---|
2744 | /** |
---|
2745 | * \brief Set the pan/zoom using a corner and dimensions in pixel coordinates |
---|
2746 | * |
---|
2747 | * \param[in] x left pixel coordinate |
---|
2748 | * \param[in] y bottom pixel coordinate (with y=0 at top of window) |
---|
2749 | * \param[in] width Width of zoom region in pixel coordinates |
---|
2750 | * \param[in] height Height of zoom region in pixel coordinates |
---|
2751 | */ |
---|
2752 | void Renderer::_setCameraZoomRegionPixels(int x, int y, int width, int height) |
---|
2753 | { |
---|
2754 | if (_cameraMode != IMAGE) { |
---|
2755 | ERROR("Called while camera mode is not image"); |
---|
2756 | return; |
---|
2757 | } |
---|
2758 | |
---|
2759 | double wx, wy, ww, wh; |
---|
2760 | |
---|
2761 | y = _windowHeight - y; |
---|
2762 | double pxToWorldX = _screenWorldCoords[2] / (double)_windowWidth; |
---|
2763 | double pxToWorldY = _screenWorldCoords[3] / (double)_windowHeight; |
---|
2764 | |
---|
2765 | wx = _screenWorldCoords[0] + x * pxToWorldX; |
---|
2766 | wy = _screenWorldCoords[1] + y * pxToWorldY; |
---|
2767 | ww = abs(width) * pxToWorldX; |
---|
2768 | wh = abs(height) * pxToWorldY; |
---|
2769 | _setCameraZoomRegion(wx, wy, ww, wh); |
---|
2770 | |
---|
2771 | TRACE("\npx: %d %d %d %d\nworld: %g %g %g %g", |
---|
2772 | x, y, width, height, |
---|
2773 | wx, wy, ww, wh); |
---|
2774 | } |
---|
2775 | |
---|
2776 | void Renderer::getImageCameraSizes(int *imgWidthPx, int *imgHeightPx, |
---|
2777 | int *_pxOffsetX, int *_pxOffsetY) |
---|
2778 | { |
---|
2779 | int pxOffsetX, pxOffsetY; |
---|
2780 | pxOffsetX = (int)(0.17 * (double)_windowWidth); |
---|
2781 | pxOffsetX = (pxOffsetX > 100 ? 100 : pxOffsetX); |
---|
2782 | pxOffsetY = (int)(0.15 * (double)_windowHeight); |
---|
2783 | pxOffsetY = (pxOffsetY > 75 ? 75 : pxOffsetY); |
---|
2784 | |
---|
2785 | int outerGutter = (int)(0.03 * (double)_windowWidth); |
---|
2786 | outerGutter = (outerGutter > 15 ? 15 : outerGutter); |
---|
2787 | |
---|
2788 | *imgWidthPx = _windowWidth - pxOffsetX - outerGutter; |
---|
2789 | *imgHeightPx = _windowHeight - pxOffsetY - outerGutter; |
---|
2790 | if (_pxOffsetX != NULL) *_pxOffsetX = pxOffsetX; |
---|
2791 | if (_pxOffsetY != NULL) *_pxOffsetY = pxOffsetY; |
---|
2792 | } |
---|
2793 | |
---|
2794 | double Renderer::getImageCameraAspect() |
---|
2795 | { |
---|
2796 | int imgWidthPx, imgHeightPx; |
---|
2797 | getImageCameraSizes(&imgWidthPx, &imgHeightPx); |
---|
2798 | return ((double)imgWidthPx / (double)imgHeightPx); |
---|
2799 | } |
---|
2800 | |
---|
2801 | /** |
---|
2802 | * \brief Set the pan/zoom using a corner and dimensions in world coordinates |
---|
2803 | * |
---|
2804 | * \param[in] x left world coordinate |
---|
2805 | * \param[in] y bottom world coordinate |
---|
2806 | * \param[in] width Width of zoom region in world coordinates |
---|
2807 | * \param[in] height Height of zoom region in world coordinates |
---|
2808 | */ |
---|
2809 | void Renderer::_setCameraZoomRegion(double x, double y, double width, double height) |
---|
2810 | { |
---|
2811 | if (_cameraMode != IMAGE) { |
---|
2812 | ERROR("Called while camera mode is not image"); |
---|
2813 | return; |
---|
2814 | } |
---|
2815 | |
---|
2816 | int imgWidthPx, imgHeightPx; |
---|
2817 | int pxOffsetX, pxOffsetY; |
---|
2818 | |
---|
2819 | getImageCameraSizes(&imgWidthPx, &imgHeightPx, &pxOffsetX, &pxOffsetY); |
---|
2820 | |
---|
2821 | double imgWindowAspect = (double)imgWidthPx / (double)imgHeightPx; |
---|
2822 | |
---|
2823 | double pxToWorld; |
---|
2824 | double imgWidthWorld; |
---|
2825 | double imgHeightWorld; |
---|
2826 | |
---|
2827 | double requestedAspect = width / height; |
---|
2828 | |
---|
2829 | if (requestedAspect >= imgWindowAspect) { |
---|
2830 | pxToWorld = width / (double)imgWidthPx; |
---|
2831 | imgWidthWorld = width; |
---|
2832 | imgHeightWorld = (double)imgHeightPx * pxToWorld; |
---|
2833 | } else { |
---|
2834 | pxToWorld = height / (double)imgHeightPx; |
---|
2835 | imgWidthWorld = (double)imgWidthPx * pxToWorld; |
---|
2836 | imgHeightWorld = height; |
---|
2837 | } |
---|
2838 | |
---|
2839 | double offsetX = pxOffsetX * pxToWorld; |
---|
2840 | double offsetY = pxOffsetY * pxToWorld; |
---|
2841 | double winWidthWorld = _windowWidth * pxToWorld; |
---|
2842 | double winHeightWorld = _windowHeight * pxToWorld; |
---|
2843 | |
---|
2844 | TRACE("Window: %d %d", _windowWidth, _windowHeight); |
---|
2845 | TRACE("ZoomRegion: %g %g %g %g", x, y, width, height); |
---|
2846 | TRACE("pxToWorld: %g", pxToWorld); |
---|
2847 | TRACE("offset: %g %g", offsetX, offsetY); |
---|
2848 | |
---|
2849 | _imgWorldOrigin[0] = x; |
---|
2850 | _imgWorldOrigin[1] = y; |
---|
2851 | _imgWorldDims[0] = width; |
---|
2852 | _imgWorldDims[1] = height; |
---|
2853 | _imgWindowWorldDims[0] = imgWidthWorld; |
---|
2854 | _imgWindowWorldDims[1] = imgHeightWorld; |
---|
2855 | |
---|
2856 | double camPos[2]; |
---|
2857 | camPos[0] = _imgWorldOrigin[0] - offsetX + winWidthWorld / 2.0; |
---|
2858 | camPos[1] = _imgWorldOrigin[1] - offsetY + winHeightWorld / 2.0; |
---|
2859 | |
---|
2860 | vtkSmartPointer<vtkCamera> camera = _renderer->GetActiveCamera(); |
---|
2861 | camera->ParallelProjectionOn(); |
---|
2862 | camera->SetClippingRange(1, 2); |
---|
2863 | // Half of world coordinate height of viewport (Documentation is wrong) |
---|
2864 | camera->SetParallelScale(winHeightWorld / 2.0); |
---|
2865 | |
---|
2866 | if (_imgCameraPlane == PLANE_XY) { |
---|
2867 | // XY plane |
---|
2868 | camera->SetPosition(camPos[0], camPos[1], _imgCameraOffset + 1.); |
---|
2869 | camera->SetFocalPoint(camPos[0], camPos[1], _imgCameraOffset); |
---|
2870 | camera->SetViewUp(0, 1, 0); |
---|
2871 | // bottom |
---|
2872 | _cameraClipPlanes[0]->SetOrigin(0, _imgWorldOrigin[1], 0); |
---|
2873 | _cameraClipPlanes[0]->SetNormal(0, 1, 0); |
---|
2874 | // left |
---|
2875 | _cameraClipPlanes[1]->SetOrigin(_imgWorldOrigin[0], 0, 0); |
---|
2876 | _cameraClipPlanes[1]->SetNormal(1, 0, 0); |
---|
2877 | // top |
---|
2878 | _cameraClipPlanes[2]->SetOrigin(0, _imgWorldOrigin[1] + _imgWindowWorldDims[1], 0); |
---|
2879 | _cameraClipPlanes[2]->SetNormal(0, -1, 0); |
---|
2880 | // right |
---|
2881 | _cameraClipPlanes[3]->SetOrigin(_imgWorldOrigin[0] + _imgWindowWorldDims[0], 0, 0); |
---|
2882 | _cameraClipPlanes[3]->SetNormal(-1, 0, 0); |
---|
2883 | |
---|
2884 | _cubeAxesActor->SetBounds(_imgWorldOrigin[0], _imgWorldOrigin[0] + _imgWindowWorldDims[0], |
---|
2885 | _imgWorldOrigin[1], _imgWorldOrigin[1] + _imgWindowWorldDims[1], |
---|
2886 | _imgCameraOffset, _imgCameraOffset); |
---|
2887 | _cubeAxesActor->XAxisVisibilityOn(); |
---|
2888 | _cubeAxesActor->YAxisVisibilityOn(); |
---|
2889 | _cubeAxesActor->ZAxisVisibilityOff(); |
---|
2890 | } else if (_imgCameraPlane == PLANE_ZY) { |
---|
2891 | // ZY plane |
---|
2892 | camera->SetPosition(_imgCameraOffset - 1., camPos[1], camPos[0]); |
---|
2893 | camera->SetFocalPoint(_imgCameraOffset, camPos[1], camPos[0]); |
---|
2894 | camera->SetViewUp(0, 1, 0); |
---|
2895 | // bottom |
---|
2896 | _cameraClipPlanes[0]->SetOrigin(0, _imgWorldOrigin[1], 0); |
---|
2897 | _cameraClipPlanes[0]->SetNormal(0, 1, 0); |
---|
2898 | // left |
---|
2899 | _cameraClipPlanes[1]->SetOrigin(0, 0, _imgWorldOrigin[0]); |
---|
2900 | _cameraClipPlanes[1]->SetNormal(0, 0, 1); |
---|
2901 | // top |
---|
2902 | _cameraClipPlanes[2]->SetOrigin(0, _imgWorldOrigin[1] + _imgWindowWorldDims[1], 0); |
---|
2903 | _cameraClipPlanes[2]->SetNormal(0, -1, 0); |
---|
2904 | // right |
---|
2905 | _cameraClipPlanes[3]->SetOrigin(0, 0, _imgWorldOrigin[0] + _imgWindowWorldDims[0]); |
---|
2906 | _cameraClipPlanes[3]->SetNormal(0, 0, -1); |
---|
2907 | |
---|
2908 | _cubeAxesActor->SetBounds(_imgCameraOffset, _imgCameraOffset, |
---|
2909 | _imgWorldOrigin[1], _imgWorldOrigin[1] + _imgWindowWorldDims[1], |
---|
2910 | _imgWorldOrigin[0], _imgWorldOrigin[0] + _imgWindowWorldDims[0]); |
---|
2911 | _cubeAxesActor->XAxisVisibilityOff(); |
---|
2912 | _cubeAxesActor->YAxisVisibilityOn(); |
---|
2913 | _cubeAxesActor->ZAxisVisibilityOn(); |
---|
2914 | } else { |
---|
2915 | // XZ plane |
---|
2916 | camera->SetPosition(camPos[0], _imgCameraOffset - 1., camPos[1]); |
---|
2917 | camera->SetFocalPoint(camPos[0], _imgCameraOffset, camPos[1]); |
---|
2918 | camera->SetViewUp(0, 0, 1); |
---|
2919 | // bottom |
---|
2920 | _cameraClipPlanes[0]->SetOrigin(0, 0, _imgWorldOrigin[1]); |
---|
2921 | _cameraClipPlanes[0]->SetNormal(0, 0, 1); |
---|
2922 | // left |
---|
2923 | _cameraClipPlanes[1]->SetOrigin(_imgWorldOrigin[0], 0, 0); |
---|
2924 | _cameraClipPlanes[1]->SetNormal(1, 0, 0); |
---|
2925 | // top |
---|
2926 | _cameraClipPlanes[2]->SetOrigin(0, 0, _imgWorldOrigin[1] + _imgWorldDims[1]); |
---|
2927 | _cameraClipPlanes[2]->SetNormal(0, 0, -1); |
---|
2928 | // right |
---|
2929 | _cameraClipPlanes[3]->SetOrigin(_imgWorldOrigin[0] + _imgWorldDims[0], 0, 0); |
---|
2930 | _cameraClipPlanes[3]->SetNormal(-1, 0, 0); |
---|
2931 | |
---|
2932 | _cubeAxesActor->SetBounds(_imgWorldOrigin[0], _imgWorldOrigin[0] + _imgWindowWorldDims[0], |
---|
2933 | _imgCameraOffset, _imgCameraOffset, |
---|
2934 | _imgWorldOrigin[1], _imgWorldOrigin[1] + _imgWindowWorldDims[1]); |
---|
2935 | _cubeAxesActor->XAxisVisibilityOn(); |
---|
2936 | _cubeAxesActor->YAxisVisibilityOff(); |
---|
2937 | _cubeAxesActor->ZAxisVisibilityOn(); |
---|
2938 | } |
---|
2939 | |
---|
2940 | // Fix up axis ranges based on new bounds |
---|
2941 | setAxesRanges(); |
---|
2942 | |
---|
2943 | // Compute screen world coordinates |
---|
2944 | computeScreenWorldCoords(); |
---|
2945 | |
---|
2946 | #ifdef DEBUG |
---|
2947 | printCameraInfo(camera); |
---|
2948 | #endif |
---|
2949 | |
---|
2950 | _needsRedraw = true; |
---|
2951 | } |
---|
2952 | |
---|
2953 | /** |
---|
2954 | * \brief Convert pixel/display coordinates to world coordinates based on current camera |
---|
2955 | */ |
---|
2956 | void Renderer::computeDisplayToWorld(double x, double y, double z, double worldPt[4]) |
---|
2957 | { |
---|
2958 | _renderer->SetDisplayPoint(x, y, z); |
---|
2959 | _renderer->DisplayToWorld(); |
---|
2960 | _renderer->GetWorldPoint(worldPt); |
---|
2961 | if (worldPt[3]) { |
---|
2962 | worldPt[0] /= worldPt[3]; |
---|
2963 | worldPt[1] /= worldPt[3]; |
---|
2964 | worldPt[2] /= worldPt[3]; |
---|
2965 | worldPt[3] = 1.0; |
---|
2966 | } |
---|
2967 | } |
---|
2968 | |
---|
2969 | /** |
---|
2970 | * \brief Convert world coordinates to pixel/display coordinates based on current camera |
---|
2971 | */ |
---|
2972 | void Renderer::computeWorldToDisplay(double x, double y, double z, double displayPt[3]) |
---|
2973 | { |
---|
2974 | _renderer->SetWorldPoint(x, y, z, 1.0); |
---|
2975 | _renderer->WorldToDisplay(); |
---|
2976 | _renderer->GetDisplayPoint(displayPt); |
---|
2977 | } |
---|
2978 | |
---|
2979 | /** |
---|
2980 | * \brief Compute the world coordinate bounds of the display rectangle |
---|
2981 | */ |
---|
2982 | void Renderer::computeScreenWorldCoords() |
---|
2983 | { |
---|
2984 | // Start with viewport coords [-1,1] |
---|
2985 | double x0 = -1; |
---|
2986 | double y0 = -1; |
---|
2987 | double z0 = -1; |
---|
2988 | double x1 = 1; |
---|
2989 | double y1 = 1; |
---|
2990 | double z1 = -1; |
---|
2991 | |
---|
2992 | vtkMatrix4x4 *mat = vtkMatrix4x4::New(); |
---|
2993 | double result[4]; |
---|
2994 | |
---|
2995 | // get the perspective transformation from the active camera |
---|
2996 | mat->DeepCopy(_renderer->GetActiveCamera()-> |
---|
2997 | GetCompositeProjectionTransformMatrix(_renderer->GetTiledAspectRatio(),0,1)); |
---|
2998 | |
---|
2999 | // use the inverse matrix |
---|
3000 | mat->Invert(); |
---|
3001 | |
---|
3002 | // Transform point to world coordinates |
---|
3003 | result[0] = x0; |
---|
3004 | result[1] = y0; |
---|
3005 | result[2] = z0; |
---|
3006 | result[3] = 1.0; |
---|
3007 | |
---|
3008 | mat->MultiplyPoint(result, result); |
---|
3009 | |
---|
3010 | // Get the transformed vector & set WorldPoint |
---|
3011 | // while we are at it try to keep w at one |
---|
3012 | if (result[3]) { |
---|
3013 | x0 = result[0] / result[3]; |
---|
3014 | y0 = result[1] / result[3]; |
---|
3015 | z0 = result[2] / result[3]; |
---|
3016 | } |
---|
3017 | |
---|
3018 | result[0] = x1; |
---|
3019 | result[1] = y1; |
---|
3020 | result[2] = z1; |
---|
3021 | result[3] = 1.0; |
---|
3022 | |
---|
3023 | mat->MultiplyPoint(result, result); |
---|
3024 | |
---|
3025 | if (result[3]) { |
---|
3026 | x1 = result[0] / result[3]; |
---|
3027 | y1 = result[1] / result[3]; |
---|
3028 | z1 = result[2] / result[3]; |
---|
3029 | } |
---|
3030 | |
---|
3031 | mat->Delete(); |
---|
3032 | |
---|
3033 | if (_imgCameraPlane == PLANE_XZ) { |
---|
3034 | _screenWorldCoords[0] = x0; |
---|
3035 | _screenWorldCoords[1] = z0; |
---|
3036 | _screenWorldCoords[2] = x1 - x0; |
---|
3037 | _screenWorldCoords[3] = z1 - z0; |
---|
3038 | } else if (_imgCameraPlane == PLANE_ZY) { |
---|
3039 | _screenWorldCoords[0] = z0; |
---|
3040 | _screenWorldCoords[1] = y0; |
---|
3041 | _screenWorldCoords[2] = z1 - z0; |
---|
3042 | _screenWorldCoords[3] = y1 - y0; |
---|
3043 | } else { |
---|
3044 | // XY |
---|
3045 | _screenWorldCoords[0] = x0; |
---|
3046 | _screenWorldCoords[1] = y0; |
---|
3047 | _screenWorldCoords[2] = x1 - x0; |
---|
3048 | _screenWorldCoords[3] = y1 - y0; |
---|
3049 | } |
---|
3050 | } |
---|
3051 | |
---|
3052 | /** |
---|
3053 | * \brief Get the world coordinates of the image camera plot area |
---|
3054 | * |
---|
3055 | * \param[out] xywh Array to hold x,y,width,height world coordinates |
---|
3056 | */ |
---|
3057 | void Renderer::getCameraZoomRegion(double xywh[4]) const |
---|
3058 | { |
---|
3059 | xywh[0] = _imgWorldOrigin[0]; |
---|
3060 | xywh[1] = _imgWorldOrigin[1]; |
---|
3061 | xywh[2] = _imgWorldDims[0]; |
---|
3062 | xywh[3] = _imgWorldDims[1]; |
---|
3063 | } |
---|
3064 | |
---|
3065 | /** |
---|
3066 | * \brief Get the world origin and dimensions of the screen |
---|
3067 | * |
---|
3068 | * \param[out] xywh Array to hold x,y,width,height world coordinates |
---|
3069 | */ |
---|
3070 | void Renderer::getScreenWorldCoords(double xywh[4]) const |
---|
3071 | { |
---|
3072 | memcpy(xywh, _screenWorldCoords, sizeof(double)*4); |
---|
3073 | } |
---|
3074 | |
---|
3075 | /** |
---|
3076 | * \brief Compute bounding box containing the two input bounding boxes |
---|
3077 | * |
---|
3078 | * \param[out] boundsDest Union of the two input bounding boxes |
---|
3079 | * \param[in] bounds1 Input bounding box |
---|
3080 | * \param[in] bounds2 Input bounding box |
---|
3081 | */ |
---|
3082 | void Renderer::mergeBounds(double *boundsDest, |
---|
3083 | const double *bounds1, const double *bounds2) |
---|
3084 | { |
---|
3085 | assert(boundsDest != NULL); |
---|
3086 | assert(bounds1 != NULL); |
---|
3087 | if (bounds2 == NULL) { |
---|
3088 | WARN("NULL bounds2 array"); |
---|
3089 | return; |
---|
3090 | } |
---|
3091 | bool b1empty = (bounds1[0] > bounds1[1]); |
---|
3092 | bool b2empty = (bounds2[0] > bounds2[1]); |
---|
3093 | |
---|
3094 | if (b1empty && b2empty) |
---|
3095 | return; |
---|
3096 | if (b1empty) { |
---|
3097 | memcpy(boundsDest, bounds2, sizeof(double) * 6); |
---|
3098 | return; |
---|
3099 | } else if (b2empty) { |
---|
3100 | memcpy(boundsDest, bounds1, sizeof(double) * 6); |
---|
3101 | return; |
---|
3102 | } |
---|
3103 | |
---|
3104 | for (int i = 0; i < 6; i++) { |
---|
3105 | if (i % 2 == 0) |
---|
3106 | boundsDest[i] = min2(bounds1[i], bounds2[i]); |
---|
3107 | else |
---|
3108 | boundsDest[i] = max2(bounds1[i], bounds2[i]); |
---|
3109 | } |
---|
3110 | } |
---|
3111 | |
---|
3112 | /** |
---|
3113 | * \brief Collect bounds of all graphics objects |
---|
3114 | * |
---|
3115 | * \param[out] bounds Bounds of all scene objects |
---|
3116 | * \param[in] onlyVisible Only collect bounds of visible objects |
---|
3117 | */ |
---|
3118 | void Renderer::collectBounds(double *bounds, bool onlyVisible) |
---|
3119 | { |
---|
3120 | bounds[0] = DBL_MAX; |
---|
3121 | bounds[1] = -DBL_MAX; |
---|
3122 | bounds[2] = DBL_MAX; |
---|
3123 | bounds[3] = -DBL_MAX; |
---|
3124 | bounds[4] = DBL_MAX; |
---|
3125 | bounds[5] = -DBL_MAX; |
---|
3126 | |
---|
3127 | mergeGraphicsObjectBounds<Arc>(bounds, onlyVisible); |
---|
3128 | mergeGraphicsObjectBounds<Arrow>(bounds, onlyVisible); |
---|
3129 | mergeGraphicsObjectBounds<Box>(bounds, onlyVisible); |
---|
3130 | mergeGraphicsObjectBounds<Cone>(bounds, onlyVisible); |
---|
3131 | mergeGraphicsObjectBounds<Contour2D>(bounds, onlyVisible); |
---|
3132 | mergeGraphicsObjectBounds<Contour3D>(bounds, onlyVisible); |
---|
3133 | mergeGraphicsObjectBounds<Cutplane>(bounds, onlyVisible); |
---|
3134 | mergeGraphicsObjectBounds<Cylinder>(bounds, onlyVisible); |
---|
3135 | mergeGraphicsObjectBounds<Disk>(bounds, onlyVisible); |
---|
3136 | mergeGraphicsObjectBounds<Glyphs>(bounds, onlyVisible); |
---|
3137 | mergeGraphicsObjectBounds<HeightMap>(bounds, onlyVisible); |
---|
3138 | mergeGraphicsObjectBounds<LIC>(bounds, onlyVisible); |
---|
3139 | mergeGraphicsObjectBounds<Line>(bounds, onlyVisible); |
---|
3140 | mergeGraphicsObjectBounds<Molecule>(bounds, onlyVisible); |
---|
3141 | mergeGraphicsObjectBounds<Outline>(bounds, onlyVisible); |
---|
3142 | mergeGraphicsObjectBounds<PolyData>(bounds, onlyVisible); |
---|
3143 | mergeGraphicsObjectBounds<Polygon>(bounds, onlyVisible); |
---|
3144 | mergeGraphicsObjectBounds<PseudoColor>(bounds, onlyVisible); |
---|
3145 | mergeGraphicsObjectBounds<Sphere>(bounds, onlyVisible); |
---|
3146 | mergeGraphicsObjectBounds<Streamlines>(bounds, onlyVisible); |
---|
3147 | mergeGraphicsObjectBounds<Volume>(bounds, onlyVisible); |
---|
3148 | mergeGraphicsObjectBounds<Warp>(bounds, onlyVisible); |
---|
3149 | |
---|
3150 | for (int i = 0; i < 6; i += 2) { |
---|
3151 | if (bounds[i+1] < bounds[i]) { |
---|
3152 | bounds[i] = -0.5; |
---|
3153 | bounds[i+1] = 0.5; |
---|
3154 | } |
---|
3155 | } |
---|
3156 | |
---|
3157 | int numDims = 0; |
---|
3158 | if (bounds[0] != bounds[1]) |
---|
3159 | numDims++; |
---|
3160 | if (bounds[2] != bounds[3]) |
---|
3161 | numDims++; |
---|
3162 | if (bounds[4] != bounds[5]) |
---|
3163 | numDims++; |
---|
3164 | |
---|
3165 | if (numDims == 0) { |
---|
3166 | bounds[0] -= .5; |
---|
3167 | bounds[1] += .5; |
---|
3168 | bounds[2] -= .5; |
---|
3169 | bounds[3] += .5; |
---|
3170 | } |
---|
3171 | |
---|
3172 | TRACE("Bounds: %g %g %g %g %g %g", |
---|
3173 | bounds[0], |
---|
3174 | bounds[1], |
---|
3175 | bounds[2], |
---|
3176 | bounds[3], |
---|
3177 | bounds[4], |
---|
3178 | bounds[5]); |
---|
3179 | } |
---|
3180 | |
---|
3181 | /** |
---|
3182 | * \brief Collect bounds of all graphics objects |
---|
3183 | * |
---|
3184 | * \param[out] bounds Bounds of all scene objects |
---|
3185 | * \param[in] onlyVisible Only collect bounds of visible objects |
---|
3186 | */ |
---|
3187 | void Renderer::collectUnscaledBounds(double *bounds, bool onlyVisible) |
---|
3188 | { |
---|
3189 | bounds[0] = DBL_MAX; |
---|
3190 | bounds[1] = -DBL_MAX; |
---|
3191 | bounds[2] = DBL_MAX; |
---|
3192 | bounds[3] = -DBL_MAX; |
---|
3193 | bounds[4] = DBL_MAX; |
---|
3194 | bounds[5] = -DBL_MAX; |
---|
3195 | |
---|
3196 | mergeGraphicsObjectUnscaledBounds<Arc>(bounds, onlyVisible); |
---|
3197 | mergeGraphicsObjectUnscaledBounds<Arrow>(bounds, onlyVisible); |
---|
3198 | mergeGraphicsObjectUnscaledBounds<Box>(bounds, onlyVisible); |
---|
3199 | mergeGraphicsObjectUnscaledBounds<Cone>(bounds, onlyVisible); |
---|
3200 | mergeGraphicsObjectUnscaledBounds<Contour2D>(bounds, onlyVisible); |
---|
3201 | mergeGraphicsObjectUnscaledBounds<Contour3D>(bounds, onlyVisible); |
---|
3202 | mergeGraphicsObjectUnscaledBounds<Cutplane>(bounds, onlyVisible); |
---|
3203 | mergeGraphicsObjectUnscaledBounds<Cylinder>(bounds, onlyVisible); |
---|
3204 | mergeGraphicsObjectUnscaledBounds<Disk>(bounds, onlyVisible); |
---|
3205 | mergeGraphicsObjectUnscaledBounds<Glyphs>(bounds, onlyVisible); |
---|
3206 | mergeGraphicsObjectUnscaledBounds<HeightMap>(bounds, onlyVisible); |
---|
3207 | mergeGraphicsObjectUnscaledBounds<LIC>(bounds, onlyVisible); |
---|
3208 | mergeGraphicsObjectUnscaledBounds<Line>(bounds, onlyVisible); |
---|
3209 | mergeGraphicsObjectUnscaledBounds<Molecule>(bounds, onlyVisible); |
---|
3210 | mergeGraphicsObjectUnscaledBounds<Outline>(bounds, onlyVisible); |
---|
3211 | mergeGraphicsObjectUnscaledBounds<PolyData>(bounds, onlyVisible); |
---|
3212 | mergeGraphicsObjectUnscaledBounds<Polygon>(bounds, onlyVisible); |
---|
3213 | mergeGraphicsObjectUnscaledBounds<PseudoColor>(bounds, onlyVisible); |
---|
3214 | mergeGraphicsObjectUnscaledBounds<Sphere>(bounds, onlyVisible); |
---|
3215 | mergeGraphicsObjectUnscaledBounds<Streamlines>(bounds, onlyVisible); |
---|
3216 | mergeGraphicsObjectUnscaledBounds<Volume>(bounds, onlyVisible); |
---|
3217 | mergeGraphicsObjectUnscaledBounds<Warp>(bounds, onlyVisible); |
---|
3218 | |
---|
3219 | for (int i = 0; i < 6; i += 2) { |
---|
3220 | if (bounds[i+1] < bounds[i]) { |
---|
3221 | bounds[i] = -0.5; |
---|
3222 | bounds[i+1] = 0.5; |
---|
3223 | } |
---|
3224 | } |
---|
3225 | |
---|
3226 | int numDims = 0; |
---|
3227 | if (bounds[0] != bounds[1]) |
---|
3228 | numDims++; |
---|
3229 | if (bounds[2] != bounds[3]) |
---|
3230 | numDims++; |
---|
3231 | if (bounds[4] != bounds[5]) |
---|
3232 | numDims++; |
---|
3233 | |
---|
3234 | if (numDims == 0) { |
---|
3235 | bounds[0] -= .5; |
---|
3236 | bounds[1] += .5; |
---|
3237 | bounds[2] -= .5; |
---|
3238 | bounds[3] += .5; |
---|
3239 | } |
---|
3240 | |
---|
3241 | TRACE("Bounds: %g %g %g %g %g %g", |
---|
3242 | bounds[0], |
---|
3243 | bounds[1], |
---|
3244 | bounds[2], |
---|
3245 | bounds[3], |
---|
3246 | bounds[4], |
---|
3247 | bounds[5]); |
---|
3248 | } |
---|
3249 | |
---|
3250 | /** |
---|
3251 | * \brief Update data ranges for color-mapping and contours |
---|
3252 | */ |
---|
3253 | void Renderer::updateFieldRanges() |
---|
3254 | { |
---|
3255 | collectDataRanges(); |
---|
3256 | |
---|
3257 | updateGraphicsObjectFieldRanges<Contour2D>(); |
---|
3258 | updateGraphicsObjectFieldRanges<Contour3D>(); |
---|
3259 | updateGraphicsObjectFieldRanges<Cutplane>(); |
---|
3260 | updateGraphicsObjectFieldRanges<Glyphs>(); |
---|
3261 | updateGraphicsObjectFieldRanges<HeightMap>(); |
---|
3262 | updateGraphicsObjectFieldRanges<LIC>(); |
---|
3263 | updateGraphicsObjectFieldRanges<Molecule>(); |
---|
3264 | updateGraphicsObjectFieldRanges<PseudoColor>(); |
---|
3265 | updateGraphicsObjectFieldRanges<Streamlines>(); |
---|
3266 | updateGraphicsObjectFieldRanges<Volume>(); |
---|
3267 | updateGraphicsObjectFieldRanges<Warp>(); |
---|
3268 | } |
---|
3269 | |
---|
3270 | /** |
---|
3271 | * \brief Collect cumulative data range of all DataSets |
---|
3272 | * |
---|
3273 | * \param[out] range Data range of all DataSets |
---|
3274 | * \param[in] name Field name |
---|
3275 | * \param[in] type Attribute type: e.g. POINT_DATA, CELL_DATA |
---|
3276 | * \param[in] component Array component or -1 for magnitude |
---|
3277 | * \param[in] onlyVisible Only collect range of visible DataSets |
---|
3278 | */ |
---|
3279 | void Renderer::collectDataRanges(double *range, const char *name, |
---|
3280 | DataSet::DataAttributeType type, |
---|
3281 | int component, bool onlyVisible) |
---|
3282 | { |
---|
3283 | range[0] = DBL_MAX; |
---|
3284 | range[1] = -DBL_MAX; |
---|
3285 | |
---|
3286 | for (DataSetHashmap::iterator itr = _dataSets.begin(); |
---|
3287 | itr != _dataSets.end(); ++itr) { |
---|
3288 | if (!onlyVisible || itr->second->getVisibility()) { |
---|
3289 | double r[2]; |
---|
3290 | r[0] = DBL_MAX; |
---|
3291 | r[1] = -DBL_MAX; |
---|
3292 | itr->second->getDataRange(r, name, type, component); |
---|
3293 | range[0] = min2(range[0], r[0]); |
---|
3294 | range[1] = max2(range[1], r[1]); |
---|
3295 | } |
---|
3296 | } |
---|
3297 | if (range[0] == DBL_MAX) |
---|
3298 | range[0] = 0; |
---|
3299 | if (range[1] == -DBL_MAX) |
---|
3300 | range[1] = 1; |
---|
3301 | |
---|
3302 | TRACE("n:'%s' t:%d c:%d [%g,%g]", name, type, component, |
---|
3303 | range[0], range[1]); |
---|
3304 | } |
---|
3305 | |
---|
3306 | /** |
---|
3307 | * \brief Clear field range hashtables and free memory |
---|
3308 | */ |
---|
3309 | void Renderer::clearFieldRanges() |
---|
3310 | { |
---|
3311 | TRACE("Deleting Field Ranges"); |
---|
3312 | for (FieldRangeHashmap::iterator itr = _scalarPointDataRange.begin(); |
---|
3313 | itr != _scalarPointDataRange.end(); ++itr) { |
---|
3314 | delete [] itr->second; |
---|
3315 | } |
---|
3316 | _scalarPointDataRange.clear(); |
---|
3317 | for (FieldRangeHashmap::iterator itr = _scalarCellDataRange.begin(); |
---|
3318 | itr != _scalarCellDataRange.end(); ++itr) { |
---|
3319 | delete [] itr->second; |
---|
3320 | } |
---|
3321 | _scalarCellDataRange.clear(); |
---|
3322 | for (FieldRangeHashmap::iterator itr = _vectorPointDataRange.begin(); |
---|
3323 | itr != _vectorPointDataRange.end(); ++itr) { |
---|
3324 | delete [] itr->second; |
---|
3325 | } |
---|
3326 | _vectorPointDataRange.clear(); |
---|
3327 | for (int i = 0; i < 3; i++) { |
---|
3328 | for (FieldRangeHashmap::iterator itr = _vectorCompPointDataRange[i].begin(); |
---|
3329 | itr != _vectorCompPointDataRange[i].end(); ++itr) { |
---|
3330 | delete [] itr->second; |
---|
3331 | } |
---|
3332 | _vectorCompPointDataRange[i].clear(); |
---|
3333 | } |
---|
3334 | for (FieldRangeHashmap::iterator itr = _vectorCellDataRange.begin(); |
---|
3335 | itr != _vectorCellDataRange.end(); ++itr) { |
---|
3336 | delete [] itr->second; |
---|
3337 | } |
---|
3338 | _vectorCellDataRange.clear(); |
---|
3339 | for (int i = 0; i < 3; i++) { |
---|
3340 | for (FieldRangeHashmap::iterator itr = _vectorCompCellDataRange[i].begin(); |
---|
3341 | itr != _vectorCompCellDataRange[i].end(); ++itr) { |
---|
3342 | delete [] itr->second; |
---|
3343 | } |
---|
3344 | _vectorCompCellDataRange[i].clear(); |
---|
3345 | } |
---|
3346 | } |
---|
3347 | |
---|
3348 | /** |
---|
3349 | * \brief Clear user-defined field range hashtables and free memory |
---|
3350 | */ |
---|
3351 | void Renderer::clearUserFieldRanges() |
---|
3352 | { |
---|
3353 | TRACE("Deleting User Field Ranges"); |
---|
3354 | for (FieldRangeHashmap::iterator itr = _userScalarPointDataRange.begin(); |
---|
3355 | itr != _userScalarPointDataRange.end(); ++itr) { |
---|
3356 | delete [] itr->second; |
---|
3357 | } |
---|
3358 | _userScalarPointDataRange.clear(); |
---|
3359 | for (FieldRangeHashmap::iterator itr = _userScalarCellDataRange.begin(); |
---|
3360 | itr != _userScalarCellDataRange.end(); ++itr) { |
---|
3361 | delete [] itr->second; |
---|
3362 | } |
---|
3363 | _userScalarCellDataRange.clear(); |
---|
3364 | for (FieldRangeHashmap::iterator itr = _userVectorPointDataRange.begin(); |
---|
3365 | itr != _userVectorPointDataRange.end(); ++itr) { |
---|
3366 | delete [] itr->second; |
---|
3367 | } |
---|
3368 | _userVectorPointDataRange.clear(); |
---|
3369 | for (int i = 0; i < 3; i++) { |
---|
3370 | for (FieldRangeHashmap::iterator itr = _userVectorCompPointDataRange[i].begin(); |
---|
3371 | itr != _userVectorCompPointDataRange[i].end(); ++itr) { |
---|
3372 | delete [] itr->second; |
---|
3373 | } |
---|
3374 | _userVectorCompPointDataRange[i].clear(); |
---|
3375 | } |
---|
3376 | for (FieldRangeHashmap::iterator itr = _userVectorCellDataRange.begin(); |
---|
3377 | itr != _userVectorCellDataRange.end(); ++itr) { |
---|
3378 | delete [] itr->second; |
---|
3379 | } |
---|
3380 | _userVectorCellDataRange.clear(); |
---|
3381 | for (int i = 0; i < 3; i++) { |
---|
3382 | for (FieldRangeHashmap::iterator itr = _userVectorCompCellDataRange[i].begin(); |
---|
3383 | itr != _userVectorCompCellDataRange[i].end(); ++itr) { |
---|
3384 | delete [] itr->second; |
---|
3385 | } |
---|
3386 | _userVectorCompCellDataRange[i].clear(); |
---|
3387 | } |
---|
3388 | } |
---|
3389 | |
---|
3390 | /** |
---|
3391 | * \brief Set up hashtables for min/max values of all fields from loaded |
---|
3392 | * datasets |
---|
3393 | * |
---|
3394 | * Note that this method does not set the ranges, it just creates the table |
---|
3395 | * entries |
---|
3396 | */ |
---|
3397 | void Renderer::initFieldRanges() |
---|
3398 | { |
---|
3399 | clearFieldRanges(); |
---|
3400 | |
---|
3401 | for (DataSetHashmap::iterator itr = _dataSets.begin(); |
---|
3402 | itr != _dataSets.end(); ++itr) { |
---|
3403 | DataSet *ds = itr->second; |
---|
3404 | std::vector<std::string> names; |
---|
3405 | ds->getFieldNames(names, DataSet::POINT_DATA, 1); |
---|
3406 | for (std::vector<std::string>::iterator itr = names.begin(); |
---|
3407 | itr != names.end(); ++itr) { |
---|
3408 | FieldRangeHashmap::iterator fritr = |
---|
3409 | _scalarPointDataRange.find(*itr); |
---|
3410 | if (fritr == _scalarPointDataRange.end()) { |
---|
3411 | _scalarPointDataRange[*itr] = new double[2]; |
---|
3412 | _scalarPointDataRange[*itr][0] = 0; |
---|
3413 | _scalarPointDataRange[*itr][1] = 1; |
---|
3414 | } |
---|
3415 | } |
---|
3416 | names.clear(); |
---|
3417 | ds->getFieldNames(names, DataSet::CELL_DATA, 1); |
---|
3418 | for (std::vector<std::string>::iterator itr = names.begin(); |
---|
3419 | itr != names.end(); ++itr) { |
---|
3420 | FieldRangeHashmap::iterator fritr = |
---|
3421 | _scalarCellDataRange.find(*itr); |
---|
3422 | if (fritr == _scalarCellDataRange.end()) { |
---|
3423 | _scalarCellDataRange[*itr] = new double[2]; |
---|
3424 | _scalarCellDataRange[*itr][0] = 0; |
---|
3425 | _scalarCellDataRange[*itr][1] = 1; |
---|
3426 | } |
---|
3427 | } |
---|
3428 | names.clear(); |
---|
3429 | ds->getFieldNames(names, DataSet::POINT_DATA, 3); |
---|
3430 | for (std::vector<std::string>::iterator itr = names.begin(); |
---|
3431 | itr != names.end(); ++itr) { |
---|
3432 | FieldRangeHashmap::iterator fritr = |
---|
3433 | _vectorPointDataRange.find(*itr); |
---|
3434 | if (fritr == _vectorPointDataRange.end()) { |
---|
3435 | _vectorPointDataRange[*itr] = new double[2]; |
---|
3436 | _vectorPointDataRange[*itr][0] = 0; |
---|
3437 | _vectorPointDataRange[*itr][1] = 1; |
---|
3438 | } |
---|
3439 | for (int i = 0; i < 3; i++) { |
---|
3440 | fritr = _vectorCompPointDataRange[i].find(*itr); |
---|
3441 | if (fritr == _vectorCompPointDataRange[i].end()) { |
---|
3442 | _vectorCompPointDataRange[i][*itr] = new double[2]; |
---|
3443 | _vectorCompPointDataRange[i][*itr][0] = 0; |
---|
3444 | _vectorCompPointDataRange[i][*itr][1] = 1; |
---|
3445 | } |
---|
3446 | } |
---|
3447 | } |
---|
3448 | names.clear(); |
---|
3449 | ds->getFieldNames(names, DataSet::CELL_DATA, 3); |
---|
3450 | for (std::vector<std::string>::iterator itr = names.begin(); |
---|
3451 | itr != names.end(); ++itr) { |
---|
3452 | FieldRangeHashmap::iterator fritr = |
---|
3453 | _vectorCellDataRange.find(*itr); |
---|
3454 | if (fritr == _vectorCellDataRange.end()) { |
---|
3455 | _vectorCellDataRange[*itr] = new double[2]; |
---|
3456 | _vectorCellDataRange[*itr][0] = 0; |
---|
3457 | _vectorCellDataRange[*itr][1] = 1; |
---|
3458 | } |
---|
3459 | for (int i = 0; i < 3; i++) { |
---|
3460 | fritr = _vectorCompCellDataRange[i].find(*itr); |
---|
3461 | if (fritr == _vectorCompCellDataRange[i].end()) { |
---|
3462 | _vectorCompCellDataRange[i][*itr] = new double[2]; |
---|
3463 | _vectorCompCellDataRange[i][*itr][0] = 0; |
---|
3464 | _vectorCompCellDataRange[i][*itr][1] = 1; |
---|
3465 | } |
---|
3466 | } |
---|
3467 | } |
---|
3468 | } |
---|
3469 | } |
---|
3470 | |
---|
3471 | /** |
---|
3472 | * \brief Returns boolean flag indicating if cumulative data ranges |
---|
3473 | * should be used |
---|
3474 | */ |
---|
3475 | bool Renderer::getUseCumulativeRange() |
---|
3476 | { |
---|
3477 | return _useCumulativeRange; |
---|
3478 | } |
---|
3479 | |
---|
3480 | /** |
---|
3481 | * \brief Set explicit range to use for mapping fields |
---|
3482 | */ |
---|
3483 | bool Renderer::setCumulativeDataRange(double *range, const char *name, |
---|
3484 | DataSet::DataAttributeType type, |
---|
3485 | int numComponents, |
---|
3486 | int component) |
---|
3487 | { |
---|
3488 | if (range == NULL || name == NULL) |
---|
3489 | return false; |
---|
3490 | |
---|
3491 | _useCumulativeRange = true; |
---|
3492 | bool found = false; |
---|
3493 | |
---|
3494 | switch (type) { |
---|
3495 | case DataSet::POINT_DATA: { |
---|
3496 | FieldRangeHashmap::iterator itr; |
---|
3497 | if (numComponents == 1) { |
---|
3498 | itr = _userScalarPointDataRange.find(name); |
---|
3499 | if (itr == _userScalarPointDataRange.end()) { |
---|
3500 | _userScalarPointDataRange[name] = new double[2]; |
---|
3501 | memcpy(_userScalarPointDataRange[name], range, sizeof(double)*2); |
---|
3502 | } else { |
---|
3503 | found = true; |
---|
3504 | memcpy(itr->second, range, sizeof(double)*2); |
---|
3505 | } |
---|
3506 | } else if (numComponents == 3) { |
---|
3507 | if (component == -1) { |
---|
3508 | itr = _userVectorPointDataRange.find(name); |
---|
3509 | if (itr == _userVectorPointDataRange.end()) { |
---|
3510 | _userVectorPointDataRange[name] = new double[2]; |
---|
3511 | memcpy(itr->second, _userVectorPointDataRange[name], sizeof(double)*2); |
---|
3512 | } else { |
---|
3513 | found = true; |
---|
3514 | memcpy(itr->second, range, sizeof(double)*2); |
---|
3515 | } |
---|
3516 | return found; |
---|
3517 | } else if (component >= 0 && component <= 3) { |
---|
3518 | itr = _userVectorCompPointDataRange[component].find(name); |
---|
3519 | if (itr == _userVectorCompPointDataRange[component].end()) { |
---|
3520 | _userVectorCompPointDataRange[component][name] = new double[2]; |
---|
3521 | memcpy(_userVectorCompPointDataRange[component][name], range, sizeof(double)*2); |
---|
3522 | } else { |
---|
3523 | found = true; |
---|
3524 | memcpy(itr->second, range, sizeof(double)*2); |
---|
3525 | } |
---|
3526 | } |
---|
3527 | } |
---|
3528 | } |
---|
3529 | break; |
---|
3530 | case DataSet::CELL_DATA: { |
---|
3531 | FieldRangeHashmap::iterator itr; |
---|
3532 | if (numComponents == 1) { |
---|
3533 | itr = _userScalarCellDataRange.find(name); |
---|
3534 | if (itr == _userScalarCellDataRange.end()) { |
---|
3535 | _userScalarCellDataRange[name] = new double[2]; |
---|
3536 | memcpy(_userScalarCellDataRange[name], range, sizeof(double)*2); |
---|
3537 | } else { |
---|
3538 | found = true; |
---|
3539 | memcpy(itr->second, range, sizeof(double)*2); |
---|
3540 | } |
---|
3541 | } else if (numComponents == 3) { |
---|
3542 | if (component == -1) { |
---|
3543 | itr = _userVectorCellDataRange.find(name); |
---|
3544 | if (itr == _userVectorCellDataRange.end()) { |
---|
3545 | _userVectorCellDataRange[name] = new double[2]; |
---|
3546 | memcpy(_userVectorCellDataRange[name], range, sizeof(double)*2); |
---|
3547 | } else { |
---|
3548 | found = true; |
---|
3549 | memcpy(itr->second, range, sizeof(double)*2); |
---|
3550 | } |
---|
3551 | } else if (component >= 0 && component <= 3) { |
---|
3552 | itr = _userVectorCompCellDataRange[component].find(name); |
---|
3553 | if (itr == _userVectorCompCellDataRange[component].end()) { |
---|
3554 | _userVectorCompCellDataRange[component][name] = new double[2]; |
---|
3555 | memcpy(_userVectorCompCellDataRange[component][name], range, sizeof(double)*2); |
---|
3556 | } else { |
---|
3557 | found = true; |
---|
3558 | memcpy(itr->second, range, sizeof(double)*2); |
---|
3559 | } |
---|
3560 | } |
---|
3561 | } |
---|
3562 | } |
---|
3563 | break; |
---|
3564 | default: |
---|
3565 | ERROR("Bad Field Type"); |
---|
3566 | } |
---|
3567 | |
---|
3568 | // Notify graphics objects of new ranges |
---|
3569 | updateFieldRanges(); |
---|
3570 | // Bounds may have changed |
---|
3571 | sceneBoundsChanged(); |
---|
3572 | _needsRedraw = true; |
---|
3573 | |
---|
3574 | TRACE("Field: %s found: %d range: %g %g", name, (found ? 1 : 0), range[0], range[1]); |
---|
3575 | return found; |
---|
3576 | } |
---|
3577 | |
---|
3578 | /** |
---|
3579 | * \brief Get the cumulative range across all DataSets for a point |
---|
3580 | * data field if it exists, otherwise a cell data field if it exists |
---|
3581 | * |
---|
3582 | * \param[out] range Pointer to an array of 2 doubles |
---|
3583 | * \param[in] name Field name |
---|
3584 | * \param[in] numComponents Number of components in field |
---|
3585 | * \param[in] component Index of component or -1 for magnitude/scalar |
---|
3586 | * \return boolean indicating if field was found |
---|
3587 | */ |
---|
3588 | bool Renderer::getCumulativeDataRange(double *range, const char *name, |
---|
3589 | int numComponents, |
---|
3590 | int component) |
---|
3591 | { |
---|
3592 | bool ret; |
---|
3593 | if (!(ret = getCumulativeDataRange(range, name, DataSet::POINT_DATA, |
---|
3594 | numComponents, component))) |
---|
3595 | ret = getCumulativeDataRange(range, name, DataSet::CELL_DATA, |
---|
3596 | numComponents, component); |
---|
3597 | return ret; |
---|
3598 | } |
---|
3599 | |
---|
3600 | /** |
---|
3601 | * \brief Get the cumulative range across all DataSets for a field |
---|
3602 | * |
---|
3603 | * \param[out] range Pointer to an array of 2 doubles |
---|
3604 | * \param[in] name Field name |
---|
3605 | * \param[in] type DataAttributeType of field |
---|
3606 | * \param[in] numComponents Number of components in field |
---|
3607 | * \param[in] component Index of component or -1 for magnitude/scalar |
---|
3608 | * \return boolean indicating if field was found |
---|
3609 | */ |
---|
3610 | bool Renderer::getCumulativeDataRange(double *range, const char *name, |
---|
3611 | DataSet::DataAttributeType type, |
---|
3612 | int numComponents, |
---|
3613 | int component) |
---|
3614 | { |
---|
3615 | if (range == NULL || name == NULL) |
---|
3616 | return false; |
---|
3617 | |
---|
3618 | switch (type) { |
---|
3619 | case DataSet::POINT_DATA: { |
---|
3620 | FieldRangeHashmap::iterator itr; |
---|
3621 | if (numComponents == 1) { |
---|
3622 | itr = _userScalarPointDataRange.find(name); |
---|
3623 | if (itr == _userScalarPointDataRange.end()) { |
---|
3624 | itr = _scalarPointDataRange.find(name); |
---|
3625 | if (itr == _scalarPointDataRange.end()) { |
---|
3626 | return false; |
---|
3627 | } |
---|
3628 | } |
---|
3629 | memcpy(range, itr->second, sizeof(double)*2); |
---|
3630 | return true; |
---|
3631 | } else if (numComponents == 3) { |
---|
3632 | if (component == -1) { |
---|
3633 | itr = _userVectorPointDataRange.find(name); |
---|
3634 | if (itr == _userVectorPointDataRange.end()) { |
---|
3635 | itr = _vectorPointDataRange.find(name); |
---|
3636 | if (itr == _vectorPointDataRange.end()) { |
---|
3637 | return false; |
---|
3638 | } |
---|
3639 | } |
---|
3640 | memcpy(range, itr->second, sizeof(double)*2); |
---|
3641 | return true; |
---|
3642 | } else if (component >= 0 && component <= 3) { |
---|
3643 | itr = _userVectorCompPointDataRange[component].find(name); |
---|
3644 | if (itr == _userVectorCompPointDataRange[component].end()) { |
---|
3645 | itr = _vectorCompPointDataRange[component].find(name); |
---|
3646 | if (itr == _vectorCompPointDataRange[component].end()) { |
---|
3647 | return false; |
---|
3648 | } |
---|
3649 | } |
---|
3650 | memcpy(range, itr->second, sizeof(double)*2); |
---|
3651 | return true; |
---|
3652 | } |
---|
3653 | } |
---|
3654 | } |
---|
3655 | break; |
---|
3656 | case DataSet::CELL_DATA: { |
---|
3657 | FieldRangeHashmap::iterator itr; |
---|
3658 | if (numComponents == 1) { |
---|
3659 | itr = _userScalarCellDataRange.find(name); |
---|
3660 | if (itr == _userScalarCellDataRange.end()) { |
---|
3661 | itr = _scalarCellDataRange.find(name); |
---|
3662 | if (itr == _scalarCellDataRange.end()) { |
---|
3663 | return false; |
---|
3664 | } |
---|
3665 | } |
---|
3666 | memcpy(range, itr->second, sizeof(double)*2); |
---|
3667 | return true; |
---|
3668 | } else if (numComponents == 3) { |
---|
3669 | if (component == -1) { |
---|
3670 | itr = _userVectorCellDataRange.find(name); |
---|
3671 | if (itr == _userVectorCellDataRange.end()) { |
---|
3672 | itr = _vectorCellDataRange.find(name); |
---|
3673 | if (itr == _vectorCellDataRange.end()) { |
---|
3674 | return false; |
---|
3675 | } |
---|
3676 | } |
---|
3677 | memcpy(range, itr->second, sizeof(double)*2); |
---|
3678 | return true; |
---|
3679 | } else if (component >= 0 && component <= 3) { |
---|
3680 | itr = _userVectorCompCellDataRange[component].find(name); |
---|
3681 | if (itr == _userVectorCompCellDataRange[component].end()) { |
---|
3682 | itr = _vectorCompCellDataRange[component].find(name); |
---|
3683 | if (itr == _vectorCompCellDataRange[component].end()) { |
---|
3684 | return false; |
---|
3685 | } |
---|
3686 | } |
---|
3687 | memcpy(range, itr->second, sizeof(double)*2); |
---|
3688 | return true; |
---|
3689 | } |
---|
3690 | } |
---|
3691 | } |
---|
3692 | break; |
---|
3693 | default: |
---|
3694 | break; |
---|
3695 | } |
---|
3696 | return false; |
---|
3697 | } |
---|
3698 | |
---|
3699 | void Renderer::collectDataRanges() |
---|
3700 | { |
---|
3701 | for (FieldRangeHashmap::iterator itr = _scalarPointDataRange.begin(); |
---|
3702 | itr != _scalarPointDataRange.end(); ++itr) { |
---|
3703 | collectDataRanges(itr->second, itr->first.c_str(), |
---|
3704 | DataSet::POINT_DATA, -1, |
---|
3705 | _cumulativeRangeOnlyVisible); |
---|
3706 | } |
---|
3707 | for (FieldRangeHashmap::iterator itr = _scalarCellDataRange.begin(); |
---|
3708 | itr != _scalarCellDataRange.end(); ++itr) { |
---|
3709 | collectDataRanges(itr->second, itr->first.c_str(), |
---|
3710 | DataSet::CELL_DATA, -1, |
---|
3711 | _cumulativeRangeOnlyVisible); |
---|
3712 | } |
---|
3713 | for (FieldRangeHashmap::iterator itr = _vectorPointDataRange.begin(); |
---|
3714 | itr != _vectorPointDataRange.end(); ++itr) { |
---|
3715 | collectDataRanges(itr->second, itr->first.c_str(), |
---|
3716 | DataSet::POINT_DATA, -1, |
---|
3717 | _cumulativeRangeOnlyVisible); |
---|
3718 | } |
---|
3719 | for (int i = 0; i < 3; i++) { |
---|
3720 | for (FieldRangeHashmap::iterator itr = _vectorCompPointDataRange[i].begin(); |
---|
3721 | itr != _vectorCompPointDataRange[i].end(); ++itr) { |
---|
3722 | collectDataRanges(itr->second, itr->first.c_str(), |
---|
3723 | DataSet::POINT_DATA, i, |
---|
3724 | _cumulativeRangeOnlyVisible); |
---|
3725 | } |
---|
3726 | } |
---|
3727 | for (FieldRangeHashmap::iterator itr = _vectorCellDataRange.begin(); |
---|
3728 | itr != _vectorCellDataRange.end(); ++itr) { |
---|
3729 | collectDataRanges(itr->second, itr->first.c_str(), |
---|
3730 | DataSet::CELL_DATA, -1, |
---|
3731 | _cumulativeRangeOnlyVisible); |
---|
3732 | } |
---|
3733 | for (int i = 0; i < 3; i++) { |
---|
3734 | for (FieldRangeHashmap::iterator itr = _vectorCompCellDataRange[i].begin(); |
---|
3735 | itr != _vectorCompCellDataRange[i].end(); ++itr) { |
---|
3736 | collectDataRanges(itr->second, itr->first.c_str(), |
---|
3737 | DataSet::CELL_DATA, i, |
---|
3738 | _cumulativeRangeOnlyVisible); |
---|
3739 | } |
---|
3740 | } |
---|
3741 | } |
---|
3742 | |
---|
3743 | /** |
---|
3744 | * \brief Determines if AABB lies in a principal axis plane |
---|
3745 | * and if so, returns the plane normal |
---|
3746 | */ |
---|
3747 | bool Renderer::is2D(const double bounds[6], |
---|
3748 | PrincipalPlane *plane, |
---|
3749 | double *offset) const |
---|
3750 | { |
---|
3751 | if (bounds[4] == bounds[5]) { |
---|
3752 | // Z = 0, XY plane |
---|
3753 | if (plane) |
---|
3754 | *plane = PLANE_XY; |
---|
3755 | if (offset) |
---|
3756 | *offset = bounds[4]; |
---|
3757 | return true; |
---|
3758 | } else if (bounds[0] == bounds[1]) { |
---|
3759 | // X = 0, ZY plane |
---|
3760 | if (plane) |
---|
3761 | *plane = PLANE_ZY; |
---|
3762 | if (offset) |
---|
3763 | *offset = bounds[0]; |
---|
3764 | return true; |
---|
3765 | } else if (bounds[2] == bounds[3]) { |
---|
3766 | // Y = 0, XZ plane |
---|
3767 | if (plane) |
---|
3768 | *plane = PLANE_XZ; |
---|
3769 | if (offset) |
---|
3770 | *offset = bounds[2]; |
---|
3771 | return true; |
---|
3772 | } |
---|
3773 | *plane = PLANE_XY; |
---|
3774 | *offset = 0; |
---|
3775 | return false; |
---|
3776 | } |
---|
3777 | |
---|
3778 | /** |
---|
3779 | * \brief Initialize the camera zoom region to include the bounding volume given |
---|
3780 | */ |
---|
3781 | void Renderer::initCamera(bool initCameraMode) |
---|
3782 | { |
---|
3783 | #ifdef WANT_TRACE |
---|
3784 | switch (_cameraMode) { |
---|
3785 | case IMAGE: |
---|
3786 | TRACE("Image camera"); |
---|
3787 | break; |
---|
3788 | case ORTHO: |
---|
3789 | TRACE("Ortho camera"); |
---|
3790 | break; |
---|
3791 | case PERSPECTIVE: |
---|
3792 | TRACE("Perspective camera"); |
---|
3793 | break; |
---|
3794 | default: |
---|
3795 | TRACE("Unknown camera mode"); |
---|
3796 | } |
---|
3797 | #endif |
---|
3798 | // Clear user requested zoom region |
---|
3799 | _userImgWorldOrigin[0] = 0; |
---|
3800 | _userImgWorldOrigin[1] = 0; |
---|
3801 | _userImgWorldDims[0] = -1; |
---|
3802 | _userImgWorldDims[1] = -1; |
---|
3803 | |
---|
3804 | double bounds[6]; |
---|
3805 | collectBounds(bounds, false); |
---|
3806 | bool twod = is2D(bounds, &_imgCameraPlane, &_imgCameraOffset); |
---|
3807 | if (twod) { |
---|
3808 | if (initCameraMode) { |
---|
3809 | TRACE("Changing camera mode to image"); |
---|
3810 | _cameraMode = IMAGE; |
---|
3811 | } |
---|
3812 | if (_imgCameraPlane == PLANE_ZY) { |
---|
3813 | _imgWorldOrigin[0] = bounds[4]; |
---|
3814 | _imgWorldOrigin[1] = bounds[2]; |
---|
3815 | _imgWorldDims[0] = bounds[5] - bounds[4]; |
---|
3816 | _imgWorldDims[1] = bounds[3] - bounds[2]; |
---|
3817 | } else if (_imgCameraPlane == PLANE_XZ) { |
---|
3818 | _imgWorldOrigin[0] = bounds[0]; |
---|
3819 | _imgWorldOrigin[1] = bounds[4]; |
---|
3820 | _imgWorldDims[0] = bounds[1] - bounds[0]; |
---|
3821 | _imgWorldDims[1] = bounds[5] - bounds[4]; |
---|
3822 | } else { |
---|
3823 | _imgWorldOrigin[0] = bounds[0]; |
---|
3824 | _imgWorldOrigin[1] = bounds[2]; |
---|
3825 | _imgWorldDims[0] = bounds[1] - bounds[0]; |
---|
3826 | _imgWorldDims[1] = bounds[3] - bounds[2]; |
---|
3827 | } |
---|
3828 | } else { |
---|
3829 | _imgWorldOrigin[0] = bounds[0]; |
---|
3830 | _imgWorldOrigin[1] = bounds[2]; |
---|
3831 | _imgWorldDims[0] = bounds[1] - bounds[0]; |
---|
3832 | _imgWorldDims[1] = bounds[3] - bounds[2]; |
---|
3833 | } |
---|
3834 | |
---|
3835 | _cameraPan[0] = 0; |
---|
3836 | _cameraPan[1] = 0; |
---|
3837 | _cameraZoomRatio = 1; |
---|
3838 | |
---|
3839 | switch (_cameraMode) { |
---|
3840 | case IMAGE: |
---|
3841 | //_renderer->ResetCamera(bounds); |
---|
3842 | _setCameraZoomRegion(_imgWorldOrigin[0], _imgWorldOrigin[1], |
---|
3843 | _imgWorldDims[0], _imgWorldDims[1]); |
---|
3844 | resetAxes(bounds); |
---|
3845 | break; |
---|
3846 | case ORTHO: |
---|
3847 | _renderer->GetActiveCamera()->ParallelProjectionOn(); |
---|
3848 | resetAxes(bounds); |
---|
3849 | //_renderer->ResetCamera(bounds); |
---|
3850 | resetVtkCamera(); |
---|
3851 | //computeScreenWorldCoords(); |
---|
3852 | break; |
---|
3853 | case PERSPECTIVE: |
---|
3854 | _renderer->GetActiveCamera()->ParallelProjectionOff(); |
---|
3855 | resetAxes(bounds); |
---|
3856 | //_renderer->ResetCamera(bounds); |
---|
3857 | resetVtkCamera(); |
---|
3858 | //computeScreenWorldCoords(); |
---|
3859 | break; |
---|
3860 | default: |
---|
3861 | ERROR("Unknown camera mode"); |
---|
3862 | } |
---|
3863 | |
---|
3864 | #ifdef WANT_TRACE |
---|
3865 | printCameraInfo(_renderer->GetActiveCamera()); |
---|
3866 | #endif |
---|
3867 | } |
---|
3868 | |
---|
3869 | #if 0 |
---|
3870 | /** |
---|
3871 | * \brief Print debugging info about a vtkCamera |
---|
3872 | */ |
---|
3873 | void Renderer::printCameraInfo(vtkCamera *camera) |
---|
3874 | { |
---|
3875 | TRACE("pscale: %g, angle: %g, d: %g pos: %g %g %g, fpt: %g %g %g, vup: %g %g %g, clip: %g %g", |
---|
3876 | camera->GetParallelScale(), |
---|
3877 | camera->GetViewAngle(), |
---|
3878 | camera->GetDistance(), |
---|
3879 | camera->GetPosition()[0], |
---|
3880 | camera->GetPosition()[1], |
---|
3881 | camera->GetPosition()[2], |
---|
3882 | camera->GetFocalPoint()[0], |
---|
3883 | camera->GetFocalPoint()[1], |
---|
3884 | camera->GetFocalPoint()[2], |
---|
3885 | camera->GetViewUp()[0], |
---|
3886 | camera->GetViewUp()[1], |
---|
3887 | camera->GetViewUp()[2], |
---|
3888 | camera->GetClippingRange()[0], |
---|
3889 | camera->GetClippingRange()[1]); |
---|
3890 | } |
---|
3891 | #endif |
---|
3892 | |
---|
3893 | /** |
---|
3894 | * \brief Set the RGB background color to render into the image |
---|
3895 | */ |
---|
3896 | void Renderer::setBackgroundColor(float color[3]) |
---|
3897 | { |
---|
3898 | _bgColor[0] = color[0]; |
---|
3899 | _bgColor[1] = color[1]; |
---|
3900 | _bgColor[2] = color[2]; |
---|
3901 | _renderer->SetBackground(_bgColor[0], _bgColor[1], _bgColor[2]); |
---|
3902 | _needsRedraw = true; |
---|
3903 | } |
---|
3904 | |
---|
3905 | /** |
---|
3906 | * \brief Set the opacity of the specified DataSet's associated graphics objects |
---|
3907 | */ |
---|
3908 | void Renderer::setDataSetOpacity(const DataSetId& id, double opacity) |
---|
3909 | { |
---|
3910 | DataSetHashmap::iterator itr; |
---|
3911 | |
---|
3912 | bool doAll = false; |
---|
3913 | |
---|
3914 | if (id.compare("all") == 0) { |
---|
3915 | itr = _dataSets.begin(); |
---|
3916 | if (itr == _dataSets.end()) |
---|
3917 | return; |
---|
3918 | doAll = true; |
---|
3919 | } else { |
---|
3920 | itr = _dataSets.find(id); |
---|
3921 | } |
---|
3922 | if (itr == _dataSets.end()) { |
---|
3923 | ERROR("Unknown dataset %s", id.c_str()); |
---|
3924 | return; |
---|
3925 | } |
---|
3926 | |
---|
3927 | do { |
---|
3928 | itr->second->setOpacity(opacity); |
---|
3929 | } while (doAll && ++itr != _dataSets.end()); |
---|
3930 | |
---|
3931 | if (id.compare("all") == 0 || getGraphicsObject<Contour2D>(id) != NULL) |
---|
3932 | setGraphicsObjectOpacity<Contour2D>(id, opacity); |
---|
3933 | if (id.compare("all") == 0 || getGraphicsObject<Contour3D>(id) != NULL) |
---|
3934 | setGraphicsObjectOpacity<Contour3D>(id, opacity); |
---|
3935 | if (id.compare("all") == 0 || getGraphicsObject<Cutplane>(id) != NULL) |
---|
3936 | setGraphicsObjectOpacity<Cutplane>(id, opacity); |
---|
3937 | if (id.compare("all") == 0 || getGraphicsObject<Glyphs>(id) != NULL) |
---|
3938 | setGraphicsObjectOpacity<Glyphs>(id, opacity); |
---|
3939 | if (id.compare("all") == 0 || getGraphicsObject<HeightMap>(id) != NULL) |
---|
3940 | setGraphicsObjectOpacity<HeightMap>(id, opacity); |
---|
3941 | if (id.compare("all") == 0 || getGraphicsObject<LIC>(id) != NULL) |
---|
3942 | setGraphicsObjectOpacity<LIC>(id, opacity); |
---|
3943 | if (id.compare("all") == 0 || getGraphicsObject<Molecule>(id) != NULL) |
---|
3944 | setGraphicsObjectOpacity<Molecule>(id, opacity); |
---|
3945 | if (id.compare("all") == 0 || getGraphicsObject<Outline>(id) != NULL) |
---|
3946 | setGraphicsObjectOpacity<Outline>(id, opacity); |
---|
3947 | if (id.compare("all") == 0 || getGraphicsObject<PolyData>(id) != NULL) |
---|
3948 | setGraphicsObjectOpacity<PolyData>(id, opacity); |
---|
3949 | if (id.compare("all") == 0 || getGraphicsObject<PseudoColor>(id) != NULL) |
---|
3950 | setGraphicsObjectOpacity<PseudoColor>(id, opacity); |
---|
3951 | if (id.compare("all") == 0 || getGraphicsObject<Streamlines>(id) != NULL) |
---|
3952 | setGraphicsObjectOpacity<Streamlines>(id, opacity); |
---|
3953 | if (id.compare("all") == 0 || getGraphicsObject<Volume>(id) != NULL) |
---|
3954 | setGraphicsObjectOpacity<Volume>(id, opacity); |
---|
3955 | if (id.compare("all") == 0 || getGraphicsObject<Warp>(id) != NULL) |
---|
3956 | setGraphicsObjectOpacity<Warp>(id, opacity); |
---|
3957 | |
---|
3958 | _needsRedraw = true; |
---|
3959 | } |
---|
3960 | |
---|
3961 | /** |
---|
3962 | * \brief Turn on/off rendering of the specified DataSet's associated graphics objects |
---|
3963 | */ |
---|
3964 | void Renderer::setDataSetVisibility(const DataSetId& id, bool state) |
---|
3965 | { |
---|
3966 | DataSetHashmap::iterator itr; |
---|
3967 | |
---|
3968 | bool doAll = false; |
---|
3969 | |
---|
3970 | if (id.compare("all") == 0) { |
---|
3971 | itr = _dataSets.begin(); |
---|
3972 | doAll = true; |
---|
3973 | if (itr == _dataSets.end()) |
---|
3974 | return; |
---|
3975 | } else { |
---|
3976 | itr = _dataSets.find(id); |
---|
3977 | } |
---|
3978 | if (itr == _dataSets.end()) { |
---|
3979 | ERROR("Unknown dataset %s", id.c_str()); |
---|
3980 | return; |
---|
3981 | } |
---|
3982 | |
---|
3983 | do { |
---|
3984 | itr->second->setVisibility(state); |
---|
3985 | } while (doAll && ++itr != _dataSets.end()); |
---|
3986 | |
---|
3987 | if (id.compare("all") == 0 || getGraphicsObject<Contour2D>(id) != NULL) |
---|
3988 | setGraphicsObjectVisibility<Contour2D>(id, state); |
---|
3989 | if (id.compare("all") == 0 || getGraphicsObject<Contour3D>(id) != NULL) |
---|
3990 | setGraphicsObjectVisibility<Contour3D>(id, state); |
---|
3991 | if (id.compare("all") == 0 || getGraphicsObject<Cutplane>(id) != NULL) |
---|
3992 | setGraphicsObjectVisibility<Cutplane>(id, state); |
---|
3993 | if (id.compare("all") == 0 || getGraphicsObject<Glyphs>(id) != NULL) |
---|
3994 | setGraphicsObjectVisibility<Glyphs>(id, state); |
---|
3995 | if (id.compare("all") == 0 || getGraphicsObject<HeightMap>(id) != NULL) |
---|
3996 | setGraphicsObjectVisibility<HeightMap>(id, state); |
---|
3997 | if (id.compare("all") == 0 || getGraphicsObject<LIC>(id) != NULL) |
---|
3998 | setGraphicsObjectVisibility<LIC>(id, state); |
---|
3999 | if (id.compare("all") == 0 || getGraphicsObject<Molecule>(id) != NULL) |
---|
4000 | setGraphicsObjectVisibility<Molecule>(id, state); |
---|
4001 | if (id.compare("all") == 0 || getGraphicsObject<Outline>(id) != NULL) |
---|
4002 | setGraphicsObjectVisibility<Outline>(id, state); |
---|
4003 | if (id.compare("all") == 0 || getGraphicsObject<PolyData>(id) != NULL) |
---|
4004 | setGraphicsObjectVisibility<PolyData>(id, state); |
---|
4005 | if (id.compare("all") == 0 || getGraphicsObject<PseudoColor>(id) != NULL) |
---|
4006 | setGraphicsObjectVisibility<PseudoColor>(id, state); |
---|
4007 | if (id.compare("all") == 0 || getGraphicsObject<Streamlines>(id) != NULL) |
---|
4008 | setGraphicsObjectVisibility<Streamlines>(id, state); |
---|
4009 | if (id.compare("all") == 0 || getGraphicsObject<Volume>(id) != NULL) |
---|
4010 | setGraphicsObjectVisibility<Volume>(id, state); |
---|
4011 | if (id.compare("all") == 0 || getGraphicsObject<Warp>(id) != NULL) |
---|
4012 | setGraphicsObjectVisibility<Warp>(id, state); |
---|
4013 | |
---|
4014 | _needsRedraw = true; |
---|
4015 | } |
---|
4016 | |
---|
4017 | /** |
---|
4018 | * \brief Set a user clipping plane |
---|
4019 | * |
---|
4020 | * TODO: Fix clip plane positions after a change in actor bounds |
---|
4021 | */ |
---|
4022 | void Renderer::setClipPlane(Axis axis, double ratio, int direction) |
---|
4023 | { |
---|
4024 | double bounds[6]; |
---|
4025 | collectBounds(bounds, false); |
---|
4026 | |
---|
4027 | switch (axis) { |
---|
4028 | case X_AXIS: |
---|
4029 | if (direction > 0) { |
---|
4030 | if (ratio > 0.0) { |
---|
4031 | if (_userClipPlanes[0] == NULL) { |
---|
4032 | _userClipPlanes[0] = vtkSmartPointer<vtkPlane>::New(); |
---|
4033 | _userClipPlanes[0]->SetNormal(1, 0, 0); |
---|
4034 | } |
---|
4035 | _userClipPlanes[0]->SetOrigin(bounds[0] + (bounds[1]-bounds[0])*ratio, 0, 0); |
---|
4036 | } else { |
---|
4037 | _userClipPlanes[0] = NULL; |
---|
4038 | } |
---|
4039 | } else { |
---|
4040 | if (ratio < 1.0) { |
---|
4041 | if (_userClipPlanes[1] == NULL) { |
---|
4042 | _userClipPlanes[1] = vtkSmartPointer<vtkPlane>::New(); |
---|
4043 | _userClipPlanes[1]->SetNormal(-1, 0, 0); |
---|
4044 | } |
---|
4045 | _userClipPlanes[1]->SetOrigin(bounds[0] + (bounds[1]-bounds[0])*ratio, 0, 0); |
---|
4046 | } else { |
---|
4047 | _userClipPlanes[1] = NULL; |
---|
4048 | } |
---|
4049 | } |
---|
4050 | break; |
---|
4051 | case Y_AXIS: |
---|
4052 | if (direction > 0) { |
---|
4053 | if (ratio > 0.0) { |
---|
4054 | if (_userClipPlanes[2] == NULL) { |
---|
4055 | _userClipPlanes[2] = vtkSmartPointer<vtkPlane>::New(); |
---|
4056 | _userClipPlanes[2]->SetNormal(0, 1, 0); |
---|
4057 | } |
---|
4058 | _userClipPlanes[2]->SetOrigin(0, bounds[2] + (bounds[3]-bounds[2])*ratio, 0); |
---|
4059 | } else { |
---|
4060 | _userClipPlanes[2] = NULL; |
---|
4061 | } |
---|
4062 | } else { |
---|
4063 | if (ratio < 1.0) { |
---|
4064 | if (_userClipPlanes[3] == NULL) { |
---|
4065 | _userClipPlanes[3] = vtkSmartPointer<vtkPlane>::New(); |
---|
4066 | _userClipPlanes[3]->SetNormal(0, -1, 0); |
---|
4067 | } |
---|
4068 | _userClipPlanes[3]->SetOrigin(0, bounds[2] + (bounds[3]-bounds[2])*ratio, 0); |
---|
4069 | } else { |
---|
4070 | _userClipPlanes[3] = NULL; |
---|
4071 | } |
---|
4072 | } |
---|
4073 | break; |
---|
4074 | case Z_AXIS: |
---|
4075 | if (direction > 0) { |
---|
4076 | if (ratio > 0.0) { |
---|
4077 | if (_userClipPlanes[4] == NULL) { |
---|
4078 | _userClipPlanes[4] = vtkSmartPointer<vtkPlane>::New(); |
---|
4079 | _userClipPlanes[4]->SetNormal(0, 0, 1); |
---|
4080 | } |
---|
4081 | _userClipPlanes[4]->SetOrigin(0, 0, bounds[4] + (bounds[5]-bounds[4])*ratio); |
---|
4082 | } else { |
---|
4083 | _userClipPlanes[4] = NULL; |
---|
4084 | } |
---|
4085 | } else { |
---|
4086 | if (ratio < 1.0) { |
---|
4087 | if (_userClipPlanes[5] == NULL) { |
---|
4088 | _userClipPlanes[5] = vtkSmartPointer<vtkPlane>::New(); |
---|
4089 | _userClipPlanes[5]->SetNormal(0, 0, -1); |
---|
4090 | } |
---|
4091 | _userClipPlanes[5]->SetOrigin(0, 0, bounds[4] + (bounds[5]-bounds[4])*ratio); |
---|
4092 | } else { |
---|
4093 | _userClipPlanes[5] = NULL; |
---|
4094 | } |
---|
4095 | } |
---|
4096 | break; |
---|
4097 | default: |
---|
4098 | ; |
---|
4099 | } |
---|
4100 | |
---|
4101 | _needsRedraw = true; |
---|
4102 | } |
---|
4103 | |
---|
4104 | /** |
---|
4105 | * \brief Set up clipping planes for image camera mode if needed |
---|
4106 | */ |
---|
4107 | void Renderer::setCameraClippingPlanes() |
---|
4108 | { |
---|
4109 | /* XXX: Note that there appears to be a bug with setting the |
---|
4110 | * clipping plane collection to NULL in the VTK Mappers -- |
---|
4111 | * the old clip planes are still applied. The workaround here |
---|
4112 | * is to keep the PlaneCollection and add/remove the planes |
---|
4113 | * to/from the PlaneCollection as needed. |
---|
4114 | */ |
---|
4115 | if (_cameraMode == IMAGE) { |
---|
4116 | if (_activeClipPlanes->GetNumberOfItems() == 0) { |
---|
4117 | for (int i = 0; i < 4; i++) |
---|
4118 | _activeClipPlanes->AddItem(_cameraClipPlanes[i]); |
---|
4119 | } |
---|
4120 | } else { |
---|
4121 | if (_activeClipPlanes->GetNumberOfItems() > 0) |
---|
4122 | _activeClipPlanes->RemoveAllItems(); |
---|
4123 | for (int i = 0; i < 6; i++) { |
---|
4124 | if (_userClipPlanes[i] != NULL) { |
---|
4125 | _activeClipPlanes->AddItem(_userClipPlanes[i]); |
---|
4126 | } |
---|
4127 | } |
---|
4128 | } |
---|
4129 | |
---|
4130 | /* Ensure all Mappers are using the PlaneCollection |
---|
4131 | * This will not change the state or timestamp of |
---|
4132 | * Mappers already using the PlaneCollection |
---|
4133 | */ |
---|
4134 | setGraphicsObjectClippingPlanes<Arc>(_activeClipPlanes); |
---|
4135 | setGraphicsObjectClippingPlanes<Arrow>(_activeClipPlanes); |
---|
4136 | setGraphicsObjectClippingPlanes<Box>(_activeClipPlanes); |
---|
4137 | setGraphicsObjectClippingPlanes<Cone>(_activeClipPlanes); |
---|
4138 | setGraphicsObjectClippingPlanes<Contour2D>(_activeClipPlanes); |
---|
4139 | setGraphicsObjectClippingPlanes<Contour3D>(_activeClipPlanes); |
---|
4140 | setGraphicsObjectClippingPlanes<Cutplane>(_activeClipPlanes); |
---|
4141 | setGraphicsObjectClippingPlanes<Cylinder>(_activeClipPlanes); |
---|
4142 | setGraphicsObjectClippingPlanes<Disk>(_activeClipPlanes); |
---|
4143 | setGraphicsObjectClippingPlanes<Glyphs>(_activeClipPlanes); |
---|
4144 | setGraphicsObjectClippingPlanes<Group>(_activeClipPlanes); |
---|
4145 | setGraphicsObjectClippingPlanes<HeightMap>(_activeClipPlanes); |
---|
4146 | setGraphicsObjectClippingPlanes<LIC>(_activeClipPlanes); |
---|
4147 | setGraphicsObjectClippingPlanes<Line>(_activeClipPlanes); |
---|
4148 | setGraphicsObjectClippingPlanes<Molecule>(_activeClipPlanes); |
---|
4149 | setGraphicsObjectClippingPlanes<Outline>(_activeClipPlanes); |
---|
4150 | setGraphicsObjectClippingPlanes<PolyData>(_activeClipPlanes); |
---|
4151 | setGraphicsObjectClippingPlanes<Polygon>(_activeClipPlanes); |
---|
4152 | setGraphicsObjectClippingPlanes<PseudoColor>(_activeClipPlanes); |
---|
4153 | setGraphicsObjectClippingPlanes<Sphere>(_activeClipPlanes); |
---|
4154 | setGraphicsObjectClippingPlanes<Streamlines>(_activeClipPlanes); |
---|
4155 | setGraphicsObjectClippingPlanes<Volume>(_activeClipPlanes); |
---|
4156 | setGraphicsObjectClippingPlanes<Warp>(_activeClipPlanes); |
---|
4157 | } |
---|
4158 | |
---|
4159 | /** |
---|
4160 | * \brief Control the use of two sided lighting |
---|
4161 | */ |
---|
4162 | void Renderer::setUseTwoSidedLighting(bool state) |
---|
4163 | { |
---|
4164 | _renderer->SetTwoSidedLighting(state ? 1 : 0); |
---|
4165 | _needsRedraw = true; |
---|
4166 | } |
---|
4167 | |
---|
4168 | /** |
---|
4169 | * \brief Control the use of the depth peeling algorithm for transparency |
---|
4170 | */ |
---|
4171 | void Renderer::setUseDepthPeeling(bool state) |
---|
4172 | { |
---|
4173 | _renderer->SetUseDepthPeeling(state ? 1 : 0); |
---|
4174 | _needsRedraw = true; |
---|
4175 | } |
---|
4176 | |
---|
4177 | /** |
---|
4178 | * \brief Sets flag to trigger rendering next time render() is called |
---|
4179 | */ |
---|
4180 | void Renderer::eventuallyRender() |
---|
4181 | { |
---|
4182 | _needsRedraw = true; |
---|
4183 | } |
---|
4184 | |
---|
4185 | /** |
---|
4186 | * \brief Cause the rendering to render a new image if needed |
---|
4187 | * |
---|
4188 | * The _needsRedraw flag indicates if a state change has occured since |
---|
4189 | * the last rendered frame |
---|
4190 | */ |
---|
4191 | bool Renderer::render() |
---|
4192 | { |
---|
4193 | TRACE("Enter: redraw: %d axesReset: %d cameraReset: %d clippingRangeReset: %d", |
---|
4194 | _needsRedraw ? 1 : 0, _needsAxesReset ? 1 : 0, _needsCameraReset ? 1 : 0, _needsCameraClippingRangeReset ? 1 : 0); |
---|
4195 | if (_needsRedraw) { |
---|
4196 | if (_needsAxesReset) { |
---|
4197 | resetAxes(); |
---|
4198 | _needsAxesReset = false; |
---|
4199 | } |
---|
4200 | if (_needsCameraReset) { |
---|
4201 | initCamera(); |
---|
4202 | _needsCameraReset = false; |
---|
4203 | _needsCameraClippingRangeReset = false; |
---|
4204 | } else if (_needsCameraClippingRangeReset && _cameraMode != IMAGE) { |
---|
4205 | resetCameraClippingRange(); |
---|
4206 | _needsCameraClippingRangeReset = false; |
---|
4207 | } |
---|
4208 | setCameraClippingPlanes(); |
---|
4209 | _renderWindow->Render(); |
---|
4210 | _needsRedraw = false; |
---|
4211 | return true; |
---|
4212 | } else |
---|
4213 | return false; |
---|
4214 | } |
---|
4215 | |
---|
4216 | /// Get the pixel width of the render window/image |
---|
4217 | int Renderer::getWindowWidth() const |
---|
4218 | { |
---|
4219 | return _windowWidth; |
---|
4220 | } |
---|
4221 | |
---|
4222 | /// Get the pixel height of the render window/image |
---|
4223 | int Renderer::getWindowHeight() const |
---|
4224 | { |
---|
4225 | return _windowHeight; |
---|
4226 | } |
---|
4227 | |
---|
4228 | /** |
---|
4229 | * \brief Read back the rendered framebuffer image |
---|
4230 | */ |
---|
4231 | void Renderer::getRenderedFrame(vtkUnsignedCharArray *imgData) |
---|
4232 | { |
---|
4233 | #ifdef RENDER_TARGA |
---|
4234 | _renderWindow->MakeCurrent(); |
---|
4235 | // Must clear previous errors first. |
---|
4236 | while (glGetError() != GL_NO_ERROR){ |
---|
4237 | ; |
---|
4238 | } |
---|
4239 | int bytesPerPixel = TARGA_BYTES_PER_PIXEL; |
---|
4240 | int size = bytesPerPixel * _windowWidth * _windowHeight; |
---|
4241 | |
---|
4242 | if (imgData->GetMaxId() + 1 != size) |
---|
4243 | { |
---|
4244 | imgData->SetNumberOfComponents(bytesPerPixel); |
---|
4245 | imgData->SetNumberOfValues(size); |
---|
4246 | } |
---|
4247 | glDisable(GL_TEXTURE_2D); |
---|
4248 | if (_renderWindow->GetDoubleBuffer()) { |
---|
4249 | glReadBuffer(static_cast<GLenum>(vtkOpenGLRenderWindow::SafeDownCast(_renderWindow)->GetBackLeftBuffer())); |
---|
4250 | } else { |
---|
4251 | glReadBuffer(static_cast<GLenum>(vtkOpenGLRenderWindow::SafeDownCast(_renderWindow)->GetFrontLeftBuffer())); |
---|
4252 | } |
---|
4253 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
---|
4254 | #ifdef WANT_TRACE |
---|
4255 | struct timeval t1, t2; |
---|
4256 | glFinish(); |
---|
4257 | gettimeofday(&t1, 0); |
---|
4258 | #endif |
---|
4259 | if (bytesPerPixel == 4) { |
---|
4260 | glReadPixels(0, 0, _windowWidth, _windowHeight, GL_BGRA, |
---|
4261 | GL_UNSIGNED_BYTE, imgData->GetPointer(0)); |
---|
4262 | } else { |
---|
4263 | glReadPixels(0, 0, _windowWidth, _windowHeight, GL_BGR, |
---|
4264 | GL_UNSIGNED_BYTE, imgData->GetPointer(0)); |
---|
4265 | } |
---|
4266 | #ifdef WANT_TRACE |
---|
4267 | gettimeofday(&t2, 0); |
---|
4268 | static unsigned int numFrames = 0; |
---|
4269 | static double accum = 0; |
---|
4270 | numFrames++; |
---|
4271 | accum += MSECS_ELAPSED(t1, t2); |
---|
4272 | #endif |
---|
4273 | TRACE("Readback time: %g ms", MSECS_ELAPSED(t1, t2)); |
---|
4274 | TRACE("Readback avg: %g ms", accum/numFrames); |
---|
4275 | if (glGetError() != GL_NO_ERROR) { |
---|
4276 | ERROR("glReadPixels"); |
---|
4277 | } |
---|
4278 | #else |
---|
4279 | _renderWindow->GetPixelData(0, 0, _windowWidth-1, _windowHeight-1, |
---|
4280 | !_renderWindow->GetDoubleBuffer(), imgData); |
---|
4281 | #endif |
---|
4282 | TRACE("Image data size: %d", imgData->GetSize()); |
---|
4283 | } |
---|
4284 | |
---|
4285 | /** |
---|
4286 | * \brief Get nearest data value given display coordinates x,y |
---|
4287 | * |
---|
4288 | * FIXME: This doesn't work when actors are scaled |
---|
4289 | * |
---|
4290 | * Note: no interpolation is performed on data |
---|
4291 | */ |
---|
4292 | bool Renderer::getScalarValueAtPixel(const DataSetId& id, int x, int y, double *value) |
---|
4293 | { |
---|
4294 | vtkSmartPointer<vtkCoordinate> coord = vtkSmartPointer<vtkCoordinate>::New(); |
---|
4295 | coord->SetCoordinateSystemToDisplay(); |
---|
4296 | coord->SetValue(x, _windowHeight - y, 0); |
---|
4297 | double *worldCoords = coord->GetComputedWorldValue(_renderer); |
---|
4298 | |
---|
4299 | TRACE("Pixel coords: %d, %d\nWorld coords: %g, %g, %g", x, y, |
---|
4300 | worldCoords[0], |
---|
4301 | worldCoords[1], |
---|
4302 | worldCoords[2]); |
---|
4303 | |
---|
4304 | return getScalarValue(id, worldCoords[0], worldCoords[1], worldCoords[2], value); |
---|
4305 | } |
---|
4306 | |
---|
4307 | /** |
---|
4308 | * \brief Get nearest data value given world coordinates x,y,z |
---|
4309 | * |
---|
4310 | * Note: no interpolation is performed on data |
---|
4311 | */ |
---|
4312 | bool Renderer::getScalarValue(const DataSetId& id, double x, double y, double z, double *value) |
---|
4313 | { |
---|
4314 | DataSet *ds = getDataSet(id); |
---|
4315 | if (ds == NULL) |
---|
4316 | return false; |
---|
4317 | |
---|
4318 | return ds->getScalarValue(x, y, z, value); |
---|
4319 | } |
---|
4320 | |
---|
4321 | /** |
---|
4322 | * \brief Get nearest data value given display coordinates x,y |
---|
4323 | * |
---|
4324 | * Note: no interpolation is performed on data |
---|
4325 | */ |
---|
4326 | bool Renderer::getVectorValueAtPixel(const DataSetId& id, int x, int y, double vector[3]) |
---|
4327 | { |
---|
4328 | vtkSmartPointer<vtkCoordinate> coord = vtkSmartPointer<vtkCoordinate>::New(); |
---|
4329 | coord->SetCoordinateSystemToDisplay(); |
---|
4330 | coord->SetValue(x, _windowHeight - y, 0); |
---|
4331 | double *worldCoords = coord->GetComputedWorldValue(_renderer); |
---|
4332 | |
---|
4333 | TRACE("Pixel coords: %d, %d\nWorld coords: %g, %g, %g", x, y, |
---|
4334 | worldCoords[0], |
---|
4335 | worldCoords[1], |
---|
4336 | worldCoords[2]); |
---|
4337 | |
---|
4338 | return getVectorValue(id, worldCoords[0], worldCoords[1], worldCoords[2], vector); |
---|
4339 | } |
---|
4340 | |
---|
4341 | /** |
---|
4342 | * \brief Get nearest data value given world coordinates x,y,z |
---|
4343 | * |
---|
4344 | * Note: no interpolation is performed on data |
---|
4345 | */ |
---|
4346 | bool Renderer::getVectorValue(const DataSetId& id, double x, double y, double z, double vector[3]) |
---|
4347 | { |
---|
4348 | DataSet *ds = getDataSet(id); |
---|
4349 | if (ds == NULL) |
---|
4350 | return false; |
---|
4351 | |
---|
4352 | return ds->getVectorValue(x, y, z, vector); |
---|
4353 | } |
---|