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 <cassert> |
---|
9 | #include <cfloat> |
---|
10 | #include <cstring> |
---|
11 | |
---|
12 | #include <vtkDataSet.h> |
---|
13 | #include <vtkPointData.h> |
---|
14 | #include <vtkCellData.h> |
---|
15 | #include <vtkPolyDataMapper.h> |
---|
16 | #include <vtkUnstructuredGrid.h> |
---|
17 | #include <vtkProperty.h> |
---|
18 | #include <vtkImageData.h> |
---|
19 | #include <vtkLookupTable.h> |
---|
20 | #include <vtkTransform.h> |
---|
21 | #include <vtkDelaunay2D.h> |
---|
22 | #include <vtkDelaunay3D.h> |
---|
23 | #include <vtkGaussianSplatter.h> |
---|
24 | #include <vtkExtractVOI.h> |
---|
25 | #include <vtkCutter.h> |
---|
26 | #include <vtkDataSetSurfaceFilter.h> |
---|
27 | |
---|
28 | #include "Cutplane.h" |
---|
29 | #include "Renderer.h" |
---|
30 | #include "Trace.h" |
---|
31 | |
---|
32 | using namespace VtkVis; |
---|
33 | |
---|
34 | Cutplane::Cutplane() : |
---|
35 | GraphicsObject(), |
---|
36 | _pipelineInitialized(false), |
---|
37 | _cloudStyle(CLOUD_MESH), |
---|
38 | _colorMap(NULL), |
---|
39 | _colorMode(COLOR_BY_SCALAR), |
---|
40 | _colorFieldType(DataSet::POINT_DATA), |
---|
41 | _renderer(NULL) |
---|
42 | { |
---|
43 | _colorFieldRange[0] = DBL_MAX; |
---|
44 | _colorFieldRange[1] = -DBL_MAX; |
---|
45 | } |
---|
46 | |
---|
47 | Cutplane::~Cutplane() |
---|
48 | { |
---|
49 | #ifdef WANT_TRACE |
---|
50 | if (_dataSet != NULL) |
---|
51 | TRACE("Deleting Cutplane for %s", _dataSet->getName().c_str()); |
---|
52 | else |
---|
53 | TRACE("Deleting Cutplane with NULL DataSet"); |
---|
54 | #endif |
---|
55 | } |
---|
56 | |
---|
57 | void Cutplane::setDataSet(DataSet *dataSet, |
---|
58 | Renderer *renderer) |
---|
59 | { |
---|
60 | if (_dataSet != dataSet) { |
---|
61 | _dataSet = dataSet; |
---|
62 | |
---|
63 | _renderer = renderer; |
---|
64 | |
---|
65 | if (renderer->getUseCumulativeRange()) { |
---|
66 | renderer->getCumulativeDataRange(_dataRange, |
---|
67 | _dataSet->getActiveScalarsName(), |
---|
68 | 1); |
---|
69 | renderer->getCumulativeDataRange(_vectorMagnitudeRange, |
---|
70 | _dataSet->getActiveVectorsName(), |
---|
71 | 3); |
---|
72 | for (int i = 0; i < 3; i++) { |
---|
73 | renderer->getCumulativeDataRange(_vectorComponentRange[i], |
---|
74 | _dataSet->getActiveVectorsName(), |
---|
75 | 3, i); |
---|
76 | } |
---|
77 | } else { |
---|
78 | _dataSet->getScalarRange(_dataRange); |
---|
79 | _dataSet->getVectorRange(_vectorMagnitudeRange); |
---|
80 | for (int i = 0; i < 3; i++) { |
---|
81 | _dataSet->getVectorRange(_vectorComponentRange[i], i); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | update(); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * \brief Create and initialize a VTK Prop to render the object |
---|
91 | */ |
---|
92 | void Cutplane::initProp() |
---|
93 | { |
---|
94 | if (_prop == NULL) { |
---|
95 | _prop = vtkSmartPointer<vtkAssembly>::New(); |
---|
96 | |
---|
97 | for (int i = 0; i < 3; i++) { |
---|
98 | _actor[i] = vtkSmartPointer<vtkActor>::New(); |
---|
99 | _borderActor[i] = vtkSmartPointer<vtkActor>::New(); |
---|
100 | //_actor[i]->VisibilityOff(); |
---|
101 | vtkProperty *property = _actor[i]->GetProperty(); |
---|
102 | property->SetColor(_color[0], _color[1], _color[2]); |
---|
103 | property->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
104 | property->SetLineWidth(_edgeWidth); |
---|
105 | property->SetPointSize(_pointSize); |
---|
106 | property->EdgeVisibilityOff(); |
---|
107 | property->SetOpacity(_opacity); |
---|
108 | property->SetAmbient(.2); |
---|
109 | if (!_lighting) |
---|
110 | property->LightingOff(); |
---|
111 | if (_faceCulling && _opacity == 1.0) { |
---|
112 | setCulling(property, true); |
---|
113 | } |
---|
114 | property = _borderActor[i]->GetProperty(); |
---|
115 | property->SetColor(_color[0], _color[1], _color[2]); |
---|
116 | property->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
117 | property->SetLineWidth(_edgeWidth); |
---|
118 | property->SetPointSize(_pointSize); |
---|
119 | property->EdgeVisibilityOff(); |
---|
120 | property->SetOpacity(_opacity); |
---|
121 | property->SetAmbient(.2); |
---|
122 | property->LightingOff(); |
---|
123 | |
---|
124 | getAssembly()->AddPart(_borderActor[i]); |
---|
125 | } |
---|
126 | } else { |
---|
127 | for (int i = 0; i < 3; i++) { |
---|
128 | getAssembly()->RemovePart(_actor[i]); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * \brief Set the material color (sets ambient, diffuse, and specular) |
---|
135 | */ |
---|
136 | void Cutplane::setColor(float color[3]) |
---|
137 | { |
---|
138 | for (int i = 0; i < 3; i++) |
---|
139 | _color[i] = color[i]; |
---|
140 | |
---|
141 | for (int i = 0; i < 3; i++) { |
---|
142 | if (_borderActor[i] != NULL) { |
---|
143 | _borderActor[i]->GetProperty()->SetColor(color[0], color[1], color[2]); |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | void Cutplane::update() |
---|
149 | { |
---|
150 | if (_dataSet == NULL) |
---|
151 | return; |
---|
152 | |
---|
153 | vtkDataSet *ds = _dataSet->getVtkDataSet(); |
---|
154 | |
---|
155 | double bounds[6]; |
---|
156 | _dataSet->getBounds(bounds); |
---|
157 | // Mapper, actor to render color-mapped data set |
---|
158 | for (int i = 0; i < 3; i++) { |
---|
159 | if (_mapper[i] == NULL) { |
---|
160 | _mapper[i] = vtkSmartPointer<vtkPolyDataMapper>::New(); |
---|
161 | // Map scalars through lookup table regardless of type |
---|
162 | _mapper[i]->SetColorModeToMapScalars(); |
---|
163 | } |
---|
164 | if (_cutPlane[i] == NULL) { |
---|
165 | _cutPlane[i] = vtkSmartPointer<vtkPlane>::New(); |
---|
166 | switch (i) { |
---|
167 | case 0: |
---|
168 | _cutPlane[i]->SetNormal(1, 0, 0); |
---|
169 | _cutPlane[i]->SetOrigin(bounds[0] + (bounds[1]-bounds[0])/2., |
---|
170 | 0, |
---|
171 | 0); |
---|
172 | break; |
---|
173 | case 1: |
---|
174 | _cutPlane[i]->SetNormal(0, 1, 0); |
---|
175 | _cutPlane[i]->SetOrigin(0, |
---|
176 | bounds[2] + (bounds[3]-bounds[2])/2., |
---|
177 | 0); |
---|
178 | break; |
---|
179 | case 2: |
---|
180 | default: |
---|
181 | _cutPlane[i]->SetNormal(0, 0, 1); |
---|
182 | _cutPlane[i]->SetOrigin(0, |
---|
183 | 0, |
---|
184 | bounds[4] + (bounds[5]-bounds[4])/2.); |
---|
185 | break; |
---|
186 | } |
---|
187 | } |
---|
188 | if (_cutter[i] == NULL) { |
---|
189 | _cutter[i] = vtkSmartPointer<vtkCutter>::New(); |
---|
190 | _cutter[i]->SetCutFunction(_cutPlane[i]); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | initProp(); |
---|
195 | |
---|
196 | if (!_pipelineInitialized) { |
---|
197 | _splatter = NULL; |
---|
198 | if (_dataSet->isCloud()) { |
---|
199 | // DataSet is a point cloud |
---|
200 | PrincipalPlane plane; |
---|
201 | double offset; |
---|
202 | if (_dataSet->is2D(&plane, &offset)) { |
---|
203 | // DataSet is a 2D point cloud |
---|
204 | if (_cloudStyle == CLOUD_MESH) { |
---|
205 | vtkSmartPointer<vtkDelaunay2D> mesher = vtkSmartPointer<vtkDelaunay2D>::New(); |
---|
206 | if (plane == PLANE_ZY) { |
---|
207 | vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); |
---|
208 | trans->RotateWXYZ(90, 0, 1, 0); |
---|
209 | if (offset != 0.0) { |
---|
210 | trans->Translate(-offset, 0, 0); |
---|
211 | } |
---|
212 | mesher->SetTransform(trans); |
---|
213 | _actor[1]->VisibilityOff(); |
---|
214 | _actor[2]->VisibilityOff(); |
---|
215 | } else if (plane == PLANE_XZ) { |
---|
216 | vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); |
---|
217 | trans->RotateWXYZ(-90, 1, 0, 0); |
---|
218 | if (offset != 0.0) { |
---|
219 | trans->Translate(0, -offset, 0); |
---|
220 | } |
---|
221 | mesher->SetTransform(trans); |
---|
222 | _actor[0]->VisibilityOff(); |
---|
223 | _actor[2]->VisibilityOff(); |
---|
224 | } else if (offset != 0.0) { |
---|
225 | // XY with Z offset |
---|
226 | vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New(); |
---|
227 | trans->Translate(0, 0, -offset); |
---|
228 | mesher->SetTransform(trans); |
---|
229 | _actor[0]->VisibilityOff(); |
---|
230 | _actor[1]->VisibilityOff(); |
---|
231 | } |
---|
232 | mesher->SetInputData(ds); |
---|
233 | for (int i = 0; i < 3; i++) { |
---|
234 | _mapper[i]->SetInputConnection(mesher->GetOutputPort()); |
---|
235 | } |
---|
236 | } else { |
---|
237 | // _cloudStyle == CLOUD_SPLAT |
---|
238 | if (_splatter == NULL) { |
---|
239 | _splatter = vtkSmartPointer<vtkGaussianSplatter>::New(); |
---|
240 | } |
---|
241 | _splatter->SetInputData(ds); |
---|
242 | int dims[3]; |
---|
243 | _splatter->GetSampleDimensions(dims); |
---|
244 | TRACE("Sample dims: %d %d %d", dims[0], dims[1], dims[2]); |
---|
245 | if (plane == PLANE_ZY) { |
---|
246 | dims[0] = 3; |
---|
247 | } else if (plane == PLANE_XZ) { |
---|
248 | dims[1] = 3; |
---|
249 | } else { |
---|
250 | dims[2] = 3; |
---|
251 | } |
---|
252 | _splatter->SetSampleDimensions(dims); |
---|
253 | for (int i = 0; i < 3; i++) { |
---|
254 | _cutter[i]->SetInputConnection(_splatter->GetOutputPort()); |
---|
255 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
256 | gf->UseStripsOn(); |
---|
257 | gf->SetInputConnection(_cutter[i]->GetOutputPort()); |
---|
258 | _mapper[i]->SetInputConnection(gf->GetOutputPort()); |
---|
259 | } |
---|
260 | } |
---|
261 | } else { |
---|
262 | if (_cloudStyle == CLOUD_MESH) { |
---|
263 | // Data Set is a 3D point cloud |
---|
264 | // Result of Delaunay3D mesher is unstructured grid |
---|
265 | vtkSmartPointer<vtkDelaunay3D> mesher = vtkSmartPointer<vtkDelaunay3D>::New(); |
---|
266 | mesher->SetInputData(ds); |
---|
267 | // Sample a plane within the grid bounding box |
---|
268 | for (int i = 0; i < 3; i++) { |
---|
269 | _cutter[i]->SetInputConnection(mesher->GetOutputPort()); |
---|
270 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
271 | gf->UseStripsOn(); |
---|
272 | gf->SetInputConnection(_cutter[i]->GetOutputPort()); |
---|
273 | _mapper[i]->SetInputConnection(gf->GetOutputPort()); |
---|
274 | } |
---|
275 | } else { |
---|
276 | // _cloudStyle == CLOUD_SPLAT |
---|
277 | if (_splatter == NULL) { |
---|
278 | _splatter = vtkSmartPointer<vtkGaussianSplatter>::New(); |
---|
279 | } |
---|
280 | _splatter->SetInputData(ds); |
---|
281 | int dims[3]; |
---|
282 | dims[0] = dims[1] = dims[2] = 64; |
---|
283 | TRACE("Generating volume with dims (%d,%d,%d) from point cloud", |
---|
284 | dims[0], dims[1], dims[2]); |
---|
285 | _splatter->SetSampleDimensions(dims); |
---|
286 | for (int i = 0; i < 3; i++) { |
---|
287 | _cutter[i]->SetInputConnection(_splatter->GetOutputPort()); |
---|
288 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
289 | gf->UseStripsOn(); |
---|
290 | gf->SetInputConnection(_cutter[i]->GetOutputPort()); |
---|
291 | _mapper[i]->SetInputConnection(gf->GetOutputPort()); |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | } else { |
---|
296 | // DataSet can be: image/volume/uniform grid, structured grid, unstructured grid, rectilinear grid, or |
---|
297 | // PolyData with cells other than points |
---|
298 | PrincipalPlane plane; |
---|
299 | double offset; |
---|
300 | if (!_dataSet->is2D(&plane, &offset)) { |
---|
301 | // Sample a plane within the grid bounding box |
---|
302 | for (int i = 0; i < 3; i++) { |
---|
303 | _cutter[i]->SetInputData(ds); |
---|
304 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
305 | gf->UseStripsOn(); |
---|
306 | gf->SetInputConnection(_cutter[i]->GetOutputPort()); |
---|
307 | _mapper[i]->SetInputConnection(gf->GetOutputPort()); |
---|
308 | } |
---|
309 | } else { |
---|
310 | // 2D data |
---|
311 | if (plane == PLANE_ZY) { |
---|
312 | _actor[1]->VisibilityOff(); |
---|
313 | _actor[2]->VisibilityOff(); |
---|
314 | } else if (plane == PLANE_XZ) { |
---|
315 | _actor[0]->VisibilityOff(); |
---|
316 | _actor[2]->VisibilityOff(); |
---|
317 | } else if (offset != 0.0) { |
---|
318 | // XY with Z offset |
---|
319 | _actor[0]->VisibilityOff(); |
---|
320 | _actor[1]->VisibilityOff(); |
---|
321 | } |
---|
322 | for (int i = 0; i < 3; i++) { |
---|
323 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
324 | gf->UseStripsOn(); |
---|
325 | gf->SetInputData(ds); |
---|
326 | _mapper[i]->SetInputConnection(gf->GetOutputPort()); |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | for (int i = 0; i < 3; i++) { |
---|
333 | if (_mapper[i] != NULL && _borderMapper[i] == NULL) { |
---|
334 | _borderMapper[i] = vtkSmartPointer<vtkPolyDataMapper>::New(); |
---|
335 | #ifdef CUTPLANE_TIGHT_OUTLINE |
---|
336 | _outlineFilter[i] = vtkSmartPointer<vtkOutlineFilter>::New(); |
---|
337 | _outlineFilter[i]->SetInputConnection(_mapper[i]->GetInputConnection(0, 0)); |
---|
338 | _borderMapper[i]->SetInputConnection(_outlineFilter[i]->GetOutputPort()); |
---|
339 | #else |
---|
340 | _outlineSource[i] = vtkSmartPointer<vtkOutlineSource>::New(); |
---|
341 | switch (i) { |
---|
342 | case 0: |
---|
343 | _outlineSource[i]->SetBounds(bounds[0] + (bounds[1]-bounds[0])/2., |
---|
344 | bounds[0] + (bounds[1]-bounds[0])/2., |
---|
345 | bounds[2], bounds[3], |
---|
346 | bounds[4], bounds[5]); |
---|
347 | break; |
---|
348 | case 1: |
---|
349 | _outlineSource[i]->SetBounds(bounds[0], bounds[1], |
---|
350 | bounds[2] + (bounds[3]-bounds[2])/2., |
---|
351 | bounds[2] + (bounds[3]-bounds[2])/2., |
---|
352 | bounds[4], bounds[5]); |
---|
353 | break; |
---|
354 | case 2: |
---|
355 | _outlineSource[i]->SetBounds(bounds[0], bounds[1], |
---|
356 | bounds[2], bounds[3], |
---|
357 | bounds[4] + (bounds[5]-bounds[4])/2., |
---|
358 | bounds[4] + (bounds[5]-bounds[4])/2.); |
---|
359 | break; |
---|
360 | default: |
---|
361 | ; |
---|
362 | } |
---|
363 | _borderMapper[i]->SetInputConnection(_outlineSource[i]->GetOutputPort()); |
---|
364 | #endif |
---|
365 | _borderMapper[i]->SetResolveCoincidentTopologyToPolygonOffset(); |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | setInterpolateBeforeMapping(true); |
---|
370 | |
---|
371 | if (_lut == NULL) { |
---|
372 | setColorMap(ColorMap::getDefault()); |
---|
373 | if (ds->GetPointData()->GetScalars() == NULL && |
---|
374 | ds->GetPointData()->GetVectors() != NULL) { |
---|
375 | TRACE("Setting color mode to vector magnitude"); |
---|
376 | setColorMode(COLOR_BY_VECTOR_MAGNITUDE); |
---|
377 | } else { |
---|
378 | TRACE("Setting color mode to scalar"); |
---|
379 | setColorMode(COLOR_BY_SCALAR); |
---|
380 | } |
---|
381 | } else if (!_pipelineInitialized) { |
---|
382 | double *rangePtr = _colorFieldRange; |
---|
383 | if (_colorFieldRange[0] > _colorFieldRange[1]) { |
---|
384 | rangePtr = NULL; |
---|
385 | } |
---|
386 | setColorMode(_colorMode, _colorFieldType, _colorFieldName.c_str(), rangePtr); |
---|
387 | } |
---|
388 | |
---|
389 | _pipelineInitialized = true; |
---|
390 | |
---|
391 | for (int i = 0; i < 3; i++) { |
---|
392 | if (_mapper[i] != NULL) { |
---|
393 | _actor[i]->SetMapper(_mapper[i]); |
---|
394 | _mapper[i]->Update(); |
---|
395 | } |
---|
396 | if (_borderMapper[i] != NULL) { |
---|
397 | _borderActor[i]->SetMapper(_borderMapper[i]); |
---|
398 | _borderMapper[i]->Update(); |
---|
399 | } |
---|
400 | // Only add cutter actor to assembly if geometry was |
---|
401 | // produced, in order to prevent messing up assembly bounds |
---|
402 | double bounds[6]; |
---|
403 | _actor[i]->GetBounds(bounds); |
---|
404 | if (bounds[0] <= bounds[1]) { |
---|
405 | getAssembly()->AddPart(_actor[i]); |
---|
406 | } |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | void Cutplane::setCloudStyle(CloudStyle style) |
---|
411 | { |
---|
412 | if (style != _cloudStyle) { |
---|
413 | _cloudStyle = style; |
---|
414 | if (_dataSet != NULL) { |
---|
415 | _pipelineInitialized = false; |
---|
416 | update(); |
---|
417 | } |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | void Cutplane::setInterpolateBeforeMapping(bool state) |
---|
422 | { |
---|
423 | for (int i = 0; i < 3; i++) { |
---|
424 | if (_mapper[i] != NULL) { |
---|
425 | _mapper[i]->SetInterpolateScalarsBeforeMapping((state ? 1 : 0)); |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | /** |
---|
431 | * \brief Select a 2D slice plane from a 3D DataSet |
---|
432 | * |
---|
433 | * \param[in] axis Axis of slice plane |
---|
434 | * \param[in] ratio Position [0,1] of slice plane along axis |
---|
435 | */ |
---|
436 | void Cutplane::selectVolumeSlice(Axis axis, double ratio) |
---|
437 | { |
---|
438 | if (_dataSet->is2D()) { |
---|
439 | WARN("DataSet not 3D, returning"); |
---|
440 | return; |
---|
441 | } |
---|
442 | |
---|
443 | if ((axis == X_AXIS &&_cutPlane[0] == NULL) || |
---|
444 | (axis == Y_AXIS &&_cutPlane[1] == NULL) || |
---|
445 | (axis == Z_AXIS &&_cutPlane[2] == NULL)) { |
---|
446 | WARN("Called before update() or DataSet is not a volume"); |
---|
447 | return; |
---|
448 | } |
---|
449 | |
---|
450 | double bounds[6]; |
---|
451 | _dataSet->getBounds(bounds); |
---|
452 | #if 0 |
---|
453 | if (ratio == 0.0) |
---|
454 | ratio = 0.001; |
---|
455 | if (ratio == 1.0) |
---|
456 | ratio = 0.999; |
---|
457 | #endif |
---|
458 | switch (axis) { |
---|
459 | case X_AXIS: |
---|
460 | _cutPlane[0]->SetOrigin(bounds[0] + (bounds[1]-bounds[0]) * ratio, |
---|
461 | 0, |
---|
462 | 0); |
---|
463 | #ifndef CUTPLANE_TIGHT_OUTLINE |
---|
464 | _outlineSource[0]->SetBounds(bounds[0] + (bounds[1]-bounds[0]) * ratio, |
---|
465 | bounds[0] + (bounds[1]-bounds[0]) * ratio, |
---|
466 | bounds[2], bounds[3], |
---|
467 | bounds[4], bounds[5]); |
---|
468 | #endif |
---|
469 | break; |
---|
470 | case Y_AXIS: |
---|
471 | _cutPlane[1]->SetOrigin(0, |
---|
472 | bounds[2] + (bounds[3]-bounds[2]) * ratio, |
---|
473 | 0); |
---|
474 | #ifndef CUTPLANE_TIGHT_OUTLINE |
---|
475 | _outlineSource[1]->SetBounds(bounds[0], bounds[1], |
---|
476 | bounds[2] + (bounds[3]-bounds[2]) * ratio, |
---|
477 | bounds[2] + (bounds[3]-bounds[2]) * ratio, |
---|
478 | bounds[4], bounds[5]); |
---|
479 | #endif |
---|
480 | break; |
---|
481 | case Z_AXIS: |
---|
482 | _cutPlane[2]->SetOrigin(0, |
---|
483 | 0, |
---|
484 | bounds[4] + (bounds[5]-bounds[4]) * ratio); |
---|
485 | #ifndef CUTPLANE_TIGHT_OUTLINE |
---|
486 | _outlineSource[2]->SetBounds(bounds[0], bounds[1], |
---|
487 | bounds[2], bounds[3], |
---|
488 | bounds[4] + (bounds[5]-bounds[4]) * ratio, |
---|
489 | bounds[4] + (bounds[5]-bounds[4]) * ratio); |
---|
490 | #endif |
---|
491 | break; |
---|
492 | default: |
---|
493 | ERROR("Invalid Axis"); |
---|
494 | return; |
---|
495 | } |
---|
496 | update(); |
---|
497 | } |
---|
498 | |
---|
499 | void Cutplane::updateRanges(Renderer *renderer) |
---|
500 | { |
---|
501 | if (_dataSet == NULL) { |
---|
502 | ERROR("called before setDataSet"); |
---|
503 | return; |
---|
504 | } |
---|
505 | |
---|
506 | if (renderer->getUseCumulativeRange()) { |
---|
507 | renderer->getCumulativeDataRange(_dataRange, |
---|
508 | _dataSet->getActiveScalarsName(), |
---|
509 | 1); |
---|
510 | renderer->getCumulativeDataRange(_vectorMagnitudeRange, |
---|
511 | _dataSet->getActiveVectorsName(), |
---|
512 | 3); |
---|
513 | for (int i = 0; i < 3; i++) { |
---|
514 | renderer->getCumulativeDataRange(_vectorComponentRange[i], |
---|
515 | _dataSet->getActiveVectorsName(), |
---|
516 | 3, i); |
---|
517 | } |
---|
518 | } else { |
---|
519 | _dataSet->getScalarRange(_dataRange); |
---|
520 | _dataSet->getVectorRange(_vectorMagnitudeRange); |
---|
521 | for (int i = 0; i < 3; i++) { |
---|
522 | _dataSet->getVectorRange(_vectorComponentRange[i], i); |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | // Need to update color map ranges |
---|
527 | double *rangePtr = _colorFieldRange; |
---|
528 | if (_colorFieldRange[0] > _colorFieldRange[1]) { |
---|
529 | rangePtr = NULL; |
---|
530 | } |
---|
531 | setColorMode(_colorMode, _colorFieldType, _colorFieldName.c_str(), rangePtr); |
---|
532 | } |
---|
533 | |
---|
534 | void Cutplane::setColorMode(ColorMode mode) |
---|
535 | { |
---|
536 | _colorMode = mode; |
---|
537 | if (_dataSet == NULL) |
---|
538 | return; |
---|
539 | |
---|
540 | switch (mode) { |
---|
541 | case COLOR_BY_SCALAR: |
---|
542 | setColorMode(mode, |
---|
543 | _dataSet->getActiveScalarsType(), |
---|
544 | _dataSet->getActiveScalarsName()); |
---|
545 | break; |
---|
546 | case COLOR_BY_VECTOR_MAGNITUDE: |
---|
547 | setColorMode(mode, |
---|
548 | _dataSet->getActiveVectorsType(), |
---|
549 | _dataSet->getActiveVectorsName()); |
---|
550 | break; |
---|
551 | case COLOR_BY_VECTOR_X: |
---|
552 | setColorMode(mode, |
---|
553 | _dataSet->getActiveVectorsType(), |
---|
554 | _dataSet->getActiveVectorsName()); |
---|
555 | break; |
---|
556 | case COLOR_BY_VECTOR_Y: |
---|
557 | setColorMode(mode, |
---|
558 | _dataSet->getActiveVectorsType(), |
---|
559 | _dataSet->getActiveVectorsName()); |
---|
560 | break; |
---|
561 | case COLOR_BY_VECTOR_Z: |
---|
562 | setColorMode(mode, |
---|
563 | _dataSet->getActiveVectorsType(), |
---|
564 | _dataSet->getActiveVectorsName()); |
---|
565 | break; |
---|
566 | default: |
---|
567 | ; |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | void Cutplane::setColorMode(ColorMode mode, |
---|
572 | const char *name, double range[2]) |
---|
573 | { |
---|
574 | if (_dataSet == NULL) |
---|
575 | return; |
---|
576 | DataSet::DataAttributeType type; |
---|
577 | int numComponents; |
---|
578 | if (!_dataSet->getFieldInfo(name, &type, &numComponents)) { |
---|
579 | ERROR("Field not found: %s", name); |
---|
580 | return; |
---|
581 | } |
---|
582 | setColorMode(mode, type, name, range); |
---|
583 | } |
---|
584 | |
---|
585 | void Cutplane::setColorMode(ColorMode mode, |
---|
586 | DataSet::DataAttributeType type, |
---|
587 | const char *name, double range[2]) |
---|
588 | { |
---|
589 | _colorMode = mode; |
---|
590 | _colorFieldType = type; |
---|
591 | if (name == NULL) |
---|
592 | _colorFieldName.clear(); |
---|
593 | else |
---|
594 | _colorFieldName = name; |
---|
595 | if (range == NULL) { |
---|
596 | _colorFieldRange[0] = DBL_MAX; |
---|
597 | _colorFieldRange[1] = -DBL_MAX; |
---|
598 | } else { |
---|
599 | memcpy(_colorFieldRange, range, sizeof(double)*2); |
---|
600 | } |
---|
601 | |
---|
602 | if (_dataSet == NULL || |
---|
603 | _mapper[0] == NULL || |
---|
604 | _mapper[1] == NULL || |
---|
605 | _mapper[2] == NULL) |
---|
606 | return; |
---|
607 | |
---|
608 | switch (type) { |
---|
609 | case DataSet::POINT_DATA: |
---|
610 | for (int i = 0; i < 3; i++) { |
---|
611 | _mapper[i]->SetScalarModeToUsePointFieldData(); |
---|
612 | } |
---|
613 | break; |
---|
614 | case DataSet::CELL_DATA: |
---|
615 | for (int i = 0; i < 3; i++) { |
---|
616 | _mapper[i]->SetScalarModeToUseCellFieldData(); |
---|
617 | } |
---|
618 | break; |
---|
619 | default: |
---|
620 | ERROR("Unsupported DataAttributeType: %d", type); |
---|
621 | return; |
---|
622 | } |
---|
623 | |
---|
624 | if (_splatter != NULL) { |
---|
625 | if (name != NULL && strlen(name) > 0) { |
---|
626 | _splatter->SetInputArrayToProcess(0, 0, 0, vtkDataObject::FIELD_ASSOCIATION_POINTS, name); |
---|
627 | } |
---|
628 | for (int i = 0; i < 3; i++) { |
---|
629 | _mapper[i]->SelectColorArray("SplatterValues"); |
---|
630 | } |
---|
631 | } else if (name != NULL && strlen(name) > 0) { |
---|
632 | for (int i = 0; i < 3; i++) { |
---|
633 | _mapper[i]->SelectColorArray(name); |
---|
634 | } |
---|
635 | } else { |
---|
636 | for (int i = 0; i < 3; i++) { |
---|
637 | _mapper[i]->SetScalarModeToDefault(); |
---|
638 | } |
---|
639 | } |
---|
640 | |
---|
641 | if (_lut != NULL) { |
---|
642 | if (range != NULL) { |
---|
643 | _lut->SetRange(range); |
---|
644 | } else if (name != NULL && strlen(name) > 0) { |
---|
645 | double r[2]; |
---|
646 | int comp = -1; |
---|
647 | if (mode == COLOR_BY_VECTOR_X) |
---|
648 | comp = 0; |
---|
649 | else if (mode == COLOR_BY_VECTOR_Y) |
---|
650 | comp = 1; |
---|
651 | else if (mode == COLOR_BY_VECTOR_Z) |
---|
652 | comp = 2; |
---|
653 | |
---|
654 | if (_renderer->getUseCumulativeRange()) { |
---|
655 | int numComponents; |
---|
656 | if (!_dataSet->getFieldInfo(name, type, &numComponents)) { |
---|
657 | ERROR("Field not found: %s, type: %d", name, type); |
---|
658 | return; |
---|
659 | } else if (numComponents < comp+1) { |
---|
660 | ERROR("Request for component %d in field with %d components", |
---|
661 | comp, numComponents); |
---|
662 | return; |
---|
663 | } |
---|
664 | _renderer->getCumulativeDataRange(r, name, type, numComponents, comp); |
---|
665 | } else { |
---|
666 | _dataSet->getDataRange(r, name, type, comp); |
---|
667 | } |
---|
668 | _lut->SetRange(r); |
---|
669 | } else { |
---|
670 | switch (mode) { |
---|
671 | case COLOR_BY_SCALAR: |
---|
672 | _lut->SetRange(_dataRange); |
---|
673 | break; |
---|
674 | case COLOR_BY_VECTOR_MAGNITUDE: |
---|
675 | _lut->SetRange(_vectorMagnitudeRange); |
---|
676 | break; |
---|
677 | case COLOR_BY_VECTOR_X: |
---|
678 | _lut->SetRange(_vectorComponentRange[0]); |
---|
679 | break; |
---|
680 | case COLOR_BY_VECTOR_Y: |
---|
681 | _lut->SetRange(_vectorComponentRange[1]); |
---|
682 | break; |
---|
683 | case COLOR_BY_VECTOR_Z: |
---|
684 | _lut->SetRange(_vectorComponentRange[2]); |
---|
685 | break; |
---|
686 | default: |
---|
687 | break; |
---|
688 | } |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | |
---|
693 | switch (mode) { |
---|
694 | case COLOR_BY_SCALAR: |
---|
695 | for (int i = 0; i < 3; i++) { |
---|
696 | _mapper[i]->ScalarVisibilityOn(); |
---|
697 | } |
---|
698 | break; |
---|
699 | case COLOR_BY_VECTOR_MAGNITUDE: |
---|
700 | for (int i = 0; i < 3; i++) { |
---|
701 | _mapper[i]->ScalarVisibilityOn(); |
---|
702 | } |
---|
703 | if (_lut != NULL) { |
---|
704 | _lut->SetVectorModeToMagnitude(); |
---|
705 | } |
---|
706 | break; |
---|
707 | case COLOR_BY_VECTOR_X: |
---|
708 | for (int i = 0; i < 3; i++) { |
---|
709 | _mapper[i]->ScalarVisibilityOn(); |
---|
710 | } |
---|
711 | if (_lut != NULL) { |
---|
712 | _lut->SetVectorModeToComponent(); |
---|
713 | _lut->SetVectorComponent(0); |
---|
714 | } |
---|
715 | break; |
---|
716 | case COLOR_BY_VECTOR_Y: |
---|
717 | for (int i = 0; i < 3; i++) { |
---|
718 | _mapper[i]->ScalarVisibilityOn(); |
---|
719 | } |
---|
720 | if (_lut != NULL) { |
---|
721 | _lut->SetVectorModeToComponent(); |
---|
722 | _lut->SetVectorComponent(1); |
---|
723 | } |
---|
724 | break; |
---|
725 | case COLOR_BY_VECTOR_Z: |
---|
726 | for (int i = 0; i < 3; i++) { |
---|
727 | _mapper[i]->ScalarVisibilityOn(); |
---|
728 | } |
---|
729 | if (_lut != NULL) { |
---|
730 | _lut->SetVectorModeToComponent(); |
---|
731 | _lut->SetVectorComponent(2); |
---|
732 | } |
---|
733 | break; |
---|
734 | default: |
---|
735 | for (int i = 0; i < 3; i++) { |
---|
736 | _mapper[i]->ScalarVisibilityOff(); |
---|
737 | } |
---|
738 | break; |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * \brief Called when the color map has been edited |
---|
744 | */ |
---|
745 | void Cutplane::updateColorMap() |
---|
746 | { |
---|
747 | setColorMap(_colorMap); |
---|
748 | } |
---|
749 | |
---|
750 | /** |
---|
751 | * \brief Associate a colormap lookup table with the DataSet |
---|
752 | */ |
---|
753 | void Cutplane::setColorMap(ColorMap *cmap) |
---|
754 | { |
---|
755 | if (cmap == NULL) |
---|
756 | return; |
---|
757 | |
---|
758 | _colorMap = cmap; |
---|
759 | |
---|
760 | if (_lut == NULL) { |
---|
761 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
762 | for (int i = 0; i < 3; i++) { |
---|
763 | if (_mapper[i] != NULL) { |
---|
764 | _mapper[i]->UseLookupTableScalarRangeOn(); |
---|
765 | _mapper[i]->SetLookupTable(_lut); |
---|
766 | } |
---|
767 | } |
---|
768 | _lut->DeepCopy(cmap->getLookupTable()); |
---|
769 | switch (_colorMode) { |
---|
770 | case COLOR_BY_SCALAR: |
---|
771 | _lut->SetRange(_dataRange); |
---|
772 | break; |
---|
773 | case COLOR_BY_VECTOR_MAGNITUDE: |
---|
774 | _lut->SetRange(_vectorMagnitudeRange); |
---|
775 | break; |
---|
776 | case COLOR_BY_VECTOR_X: |
---|
777 | _lut->SetRange(_vectorComponentRange[0]); |
---|
778 | break; |
---|
779 | case COLOR_BY_VECTOR_Y: |
---|
780 | _lut->SetRange(_vectorComponentRange[1]); |
---|
781 | break; |
---|
782 | case COLOR_BY_VECTOR_Z: |
---|
783 | _lut->SetRange(_vectorComponentRange[2]); |
---|
784 | break; |
---|
785 | default: |
---|
786 | break; |
---|
787 | } |
---|
788 | } else { |
---|
789 | double range[2]; |
---|
790 | _lut->GetTableRange(range); |
---|
791 | _lut->DeepCopy(cmap->getLookupTable()); |
---|
792 | _lut->SetRange(range); |
---|
793 | _lut->Modified(); |
---|
794 | } |
---|
795 | |
---|
796 | switch (_colorMode) { |
---|
797 | case COLOR_BY_VECTOR_MAGNITUDE: |
---|
798 | _lut->SetVectorModeToMagnitude(); |
---|
799 | break; |
---|
800 | case COLOR_BY_VECTOR_X: |
---|
801 | _lut->SetVectorModeToComponent(); |
---|
802 | _lut->SetVectorComponent(0); |
---|
803 | break; |
---|
804 | case COLOR_BY_VECTOR_Y: |
---|
805 | _lut->SetVectorModeToComponent(); |
---|
806 | _lut->SetVectorComponent(1); |
---|
807 | break; |
---|
808 | case COLOR_BY_VECTOR_Z: |
---|
809 | _lut->SetVectorModeToComponent(); |
---|
810 | _lut->SetVectorComponent(2); |
---|
811 | break; |
---|
812 | default: |
---|
813 | break; |
---|
814 | } |
---|
815 | } |
---|
816 | |
---|
817 | /** |
---|
818 | * \brief Turn on/off lighting of this object |
---|
819 | */ |
---|
820 | void Cutplane::setLighting(bool state) |
---|
821 | { |
---|
822 | _lighting = state; |
---|
823 | for (int i = 0; i < 3; i++) { |
---|
824 | if (_actor[i] != NULL) |
---|
825 | _actor[i]->GetProperty()->SetLighting((state ? 1 : 0)); |
---|
826 | } |
---|
827 | } |
---|
828 | |
---|
829 | /** |
---|
830 | * \brief Turn on/off rendering of mesh edges |
---|
831 | */ |
---|
832 | void Cutplane::setEdgeVisibility(bool state) |
---|
833 | { |
---|
834 | for (int i = 0; i < 3; i++) { |
---|
835 | if (_actor[i] != NULL) { |
---|
836 | _actor[i]->GetProperty()->SetEdgeVisibility((state ? 1 : 0)); |
---|
837 | } |
---|
838 | } |
---|
839 | } |
---|
840 | |
---|
841 | /** |
---|
842 | * \brief Turn on/off rendering of outlines |
---|
843 | */ |
---|
844 | void Cutplane::setOutlineVisibility(bool state) |
---|
845 | { |
---|
846 | for (int i = 0; i < 3; i++) { |
---|
847 | if (_borderActor[i] != NULL) { |
---|
848 | _borderActor[i]->SetVisibility((state ? 1 : 0)); |
---|
849 | } |
---|
850 | } |
---|
851 | } |
---|
852 | |
---|
853 | /** |
---|
854 | * \brief Set visibility of cutplane on specified axis |
---|
855 | */ |
---|
856 | void Cutplane::setSliceVisibility(Axis axis, bool state) |
---|
857 | { |
---|
858 | switch (axis) { |
---|
859 | case X_AXIS: |
---|
860 | if (_actor[0] != NULL) |
---|
861 | _actor[0]->SetVisibility((state ? 1 : 0)); |
---|
862 | if (_borderActor[0] != NULL) |
---|
863 | _borderActor[0]->SetVisibility((state ? 1 : 0)); |
---|
864 | break; |
---|
865 | case Y_AXIS: |
---|
866 | if (_actor[1] != NULL) |
---|
867 | _actor[1]->SetVisibility((state ? 1 : 0)); |
---|
868 | if (_borderActor[1] != NULL) |
---|
869 | _borderActor[1]->SetVisibility((state ? 1 : 0)); |
---|
870 | break; |
---|
871 | case Z_AXIS: |
---|
872 | default: |
---|
873 | if (_actor[2] != NULL) |
---|
874 | _actor[2]->SetVisibility((state ? 1 : 0)); |
---|
875 | if (_borderActor[2] != NULL) |
---|
876 | _borderActor[2]->SetVisibility((state ? 1 : 0)); |
---|
877 | break; |
---|
878 | } |
---|
879 | } |
---|
880 | |
---|
881 | /** |
---|
882 | * \brief Set a group of world coordinate planes to clip rendering |
---|
883 | * |
---|
884 | * Passing NULL for planes will remove all cliping planes |
---|
885 | */ |
---|
886 | void Cutplane::setClippingPlanes(vtkPlaneCollection *planes) |
---|
887 | { |
---|
888 | for (int i = 0; i < 3; i++) { |
---|
889 | if (_mapper[i] != NULL) { |
---|
890 | _mapper[i]->SetClippingPlanes(planes); |
---|
891 | } |
---|
892 | if (_borderMapper[i] != NULL) { |
---|
893 | _borderMapper[i]->SetClippingPlanes(planes); |
---|
894 | } |
---|
895 | } |
---|
896 | } |
---|