1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2011, Purdue Research Foundation |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef __RAPPTURE_VTKVIS_VTKGRAPHICSOBJECT_H__ |
---|
9 | #define __RAPPTURE_VTKVIS_VTKGRAPHICSOBJECT_H__ |
---|
10 | |
---|
11 | #include <cmath> |
---|
12 | #include <string> |
---|
13 | |
---|
14 | #include <vtkSmartPointer.h> |
---|
15 | #include <vtkProp.h> |
---|
16 | #include <vtkProp3D.h> |
---|
17 | #include <vtkProp3DCollection.h> |
---|
18 | #include <vtkAssembly.h> |
---|
19 | #include <vtkActor.h> |
---|
20 | #include <vtkVolume.h> |
---|
21 | #include <vtkProperty.h> |
---|
22 | #include <vtkVolumeProperty.h> |
---|
23 | #include <vtkMath.h> |
---|
24 | #include <vtkMatrix4x4.h> |
---|
25 | #include <vtkPlaneCollection.h> |
---|
26 | |
---|
27 | #include "RpVtkDataSet.h" |
---|
28 | #include "Trace.h" |
---|
29 | |
---|
30 | namespace Rappture { |
---|
31 | namespace VtkVis { |
---|
32 | |
---|
33 | /** |
---|
34 | * \brief Base class for graphics objects |
---|
35 | */ |
---|
36 | class VtkGraphicsObject { |
---|
37 | public: |
---|
38 | enum CullFace { |
---|
39 | CULL_FRONT, |
---|
40 | CULL_BACK, |
---|
41 | CULL_FRONT_AND_BACK |
---|
42 | }; |
---|
43 | |
---|
44 | VtkGraphicsObject() : |
---|
45 | _dataSet(NULL), |
---|
46 | _opacity(1.0), |
---|
47 | _edgeWidth(1.0f), |
---|
48 | _pointSize(1.0f), |
---|
49 | _lighting(true), |
---|
50 | _cullFace(CULL_BACK), |
---|
51 | _faceCulling(false) |
---|
52 | { |
---|
53 | _dataRange[0] = 0; |
---|
54 | _dataRange[1] = 1; |
---|
55 | _color[0] = 1.0f; |
---|
56 | _color[1] = 1.0f; |
---|
57 | _color[2] = 1.0f; |
---|
58 | _edgeColor[0] = 0.0f; |
---|
59 | _edgeColor[1] = 0.0f; |
---|
60 | _edgeColor[2] = 0.0f; |
---|
61 | } |
---|
62 | |
---|
63 | virtual ~VtkGraphicsObject() |
---|
64 | {} |
---|
65 | |
---|
66 | /** |
---|
67 | * \brief Return the name of the sublass of VtkGraphicsObject |
---|
68 | */ |
---|
69 | virtual const char *getClassName() const = 0; |
---|
70 | |
---|
71 | /** |
---|
72 | * \brief Specify input DataSet |
---|
73 | * |
---|
74 | * Default implementation calls update() |
---|
75 | * |
---|
76 | * \param[in] dataSet DataSet to use in rendering |
---|
77 | */ |
---|
78 | virtual void setDataSet(DataSet *dataSet) |
---|
79 | { |
---|
80 | if (_dataSet != dataSet) { |
---|
81 | _dataSet = dataSet; |
---|
82 | |
---|
83 | update(); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * \brief Specify input DataSet and information on cumulative data ranges |
---|
89 | * |
---|
90 | * Default implementation calls update() and stores scalarRange to |
---|
91 | * _dataRange based on cumulative range settings |
---|
92 | * |
---|
93 | * \param[in] dataSet DataSet to use in rendering |
---|
94 | * \param[in] useCumulative Whether the cumulative data ranges should be |
---|
95 | * used in place of the dataSet's ranges |
---|
96 | * \param[in] scalarRange Current cumulative scalar data range |
---|
97 | * \param[in] vectorMagnitudeRange Current cumulative vector magnitude data range |
---|
98 | * \param[in] vectorComponentRange Current cumulative vector component data ranges |
---|
99 | */ |
---|
100 | virtual void setDataSet(DataSet *dataSet, |
---|
101 | bool useCumulative, |
---|
102 | double scalarRange[2], |
---|
103 | double vectorMagnitudeRange[2], |
---|
104 | double vectorComponentRange[3][2]) |
---|
105 | { |
---|
106 | if (_dataSet != dataSet) { |
---|
107 | _dataSet = dataSet; |
---|
108 | |
---|
109 | if (useCumulative) { |
---|
110 | _dataRange[0] = scalarRange[0]; |
---|
111 | _dataRange[1] = scalarRange[1]; |
---|
112 | } else { |
---|
113 | _dataSet->getScalarRange(_dataRange); |
---|
114 | } |
---|
115 | |
---|
116 | update(); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * \brief Called when scalar or vector field changes (e.g. active field) or |
---|
122 | * cumulative ranges or settings change |
---|
123 | * |
---|
124 | * \param[in] useCumulative Whether the cumulative data ranges should be |
---|
125 | * used in place of the dataSet's ranges |
---|
126 | * \param[in] scalarRange Current cumulative scalar data range |
---|
127 | * \param[in] vectorMagnitudeRange Current cumulative vector magnitude data range |
---|
128 | * \param[in] vectorComponentRange Current cumulative vector component data ranges |
---|
129 | */ |
---|
130 | virtual void updateRanges(bool useCumulative, |
---|
131 | double scalarRange[2], |
---|
132 | double vectorMagnitudeRange[2], |
---|
133 | double vectorComponentRange[3][2]) |
---|
134 | { |
---|
135 | if (useCumulative) { |
---|
136 | _dataRange[0] = scalarRange[0]; |
---|
137 | _dataRange[1] = scalarRange[1]; |
---|
138 | } else { |
---|
139 | _dataSet->getScalarRange(_dataRange); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * \brief Return the DataSet this object renders |
---|
145 | */ |
---|
146 | inline DataSet *getDataSet() |
---|
147 | { |
---|
148 | return _dataSet; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * \brief Return the VTK prop object to render |
---|
153 | */ |
---|
154 | inline vtkProp *getProp() |
---|
155 | { |
---|
156 | return _prop; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * \brief Cast the vktProp to a vtkProp3D |
---|
161 | * |
---|
162 | * \return NULL or a vtkProp3D pointer |
---|
163 | */ |
---|
164 | inline vtkProp3D *getProp3D() |
---|
165 | { |
---|
166 | return vtkProp3D::SafeDownCast(_prop); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * \brief Cast the vktProp to a vtkActor |
---|
171 | * |
---|
172 | * \return NULL or a vtkActor pointer |
---|
173 | */ |
---|
174 | inline vtkActor *getActor() |
---|
175 | { |
---|
176 | return vtkActor::SafeDownCast(_prop); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * \brief Cast the vktProp to a vtkVolume |
---|
181 | * |
---|
182 | * \return NULL or a vtkVolume pointer |
---|
183 | */ |
---|
184 | inline vtkVolume *getVolume() |
---|
185 | { |
---|
186 | return vtkVolume::SafeDownCast(_prop); |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * \brief Cast the vktProp to a vtkAssembly |
---|
191 | * |
---|
192 | * \return NULL or a vtkAssembly pointer |
---|
193 | */ |
---|
194 | inline vtkAssembly *getAssembly() |
---|
195 | { |
---|
196 | return vtkAssembly::SafeDownCast(_prop); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * \brief Set an additional transform on the prop |
---|
201 | * |
---|
202 | * prop must be a vtkProp3D. The transform is |
---|
203 | * concatenated with the internal transform created |
---|
204 | * by setting the prop position, orientation and scale |
---|
205 | */ |
---|
206 | virtual void setTransform(vtkMatrix4x4 *matrix) |
---|
207 | { |
---|
208 | if (getProp3D() != NULL) { |
---|
209 | getProp3D()->SetUserMatrix(matrix); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * \brief Set the prop center of rotation |
---|
215 | * |
---|
216 | * \param[in] origin The point about which the object rotates |
---|
217 | */ |
---|
218 | virtual void setOrigin(double origin[3]) |
---|
219 | { |
---|
220 | if (getProp3D() != NULL) { |
---|
221 | getProp3D()->SetOrigin(origin); |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * \brief Set the prop orientation with a quaternion |
---|
227 | * |
---|
228 | * \param[in] quat Quaternion with scalar part first |
---|
229 | */ |
---|
230 | virtual void setOrientation(double quat[4]) |
---|
231 | { |
---|
232 | if (getProp3D() != NULL) { |
---|
233 | double angle = vtkMath::DegreesFromRadians(2.0 * acos(quat[0])); |
---|
234 | double axis[3]; |
---|
235 | if (angle < 1.0e-6) { |
---|
236 | axis[0] = 1; |
---|
237 | axis[1] = 0; |
---|
238 | axis[2] = 0; |
---|
239 | } else { |
---|
240 | double denom = sqrt(1. - quat[0] * quat[0]); |
---|
241 | axis[0] = quat[1] / denom; |
---|
242 | axis[1] = quat[2] / denom; |
---|
243 | axis[2] = quat[3] / denom; |
---|
244 | } |
---|
245 | setOrientation(angle, axis); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * \brief Set the prop orientation with a rotation about an axis |
---|
251 | * |
---|
252 | * \param[in] angle Angle in degrees |
---|
253 | * \param[in] axis Axis of rotation |
---|
254 | */ |
---|
255 | virtual void setOrientation(double angle, double axis[3]) |
---|
256 | { |
---|
257 | if (getProp3D() != NULL) { |
---|
258 | getProp3D()->SetOrientation(0, 0, 0); |
---|
259 | getProp3D()->RotateWXYZ(angle, axis[0], axis[1], axis[2]); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * \brief Set the prop position |
---|
265 | * |
---|
266 | * \param[in] pos Position in world coordinates |
---|
267 | */ |
---|
268 | virtual void setPosition(double pos[3]) |
---|
269 | { |
---|
270 | if (getProp3D() != NULL) { |
---|
271 | getProp3D()->SetPosition(pos); |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * \brief Set the prop scaling |
---|
277 | * |
---|
278 | * \param[in] scale Scaling in x,y,z |
---|
279 | */ |
---|
280 | virtual void setScale(double scale[3]) |
---|
281 | { |
---|
282 | if (getProp3D() != NULL) { |
---|
283 | getProp3D()->SetScale(scale); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * \brief Toggle visibility of the prop |
---|
289 | */ |
---|
290 | virtual void setVisibility(bool state) |
---|
291 | { |
---|
292 | if (_prop != NULL) |
---|
293 | _prop->SetVisibility((state ? 1 : 0)); |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * \brief Get visibility state of the prop |
---|
298 | */ |
---|
299 | virtual bool getVisibility() |
---|
300 | { |
---|
301 | if (_prop != NULL && |
---|
302 | _prop->GetVisibility() != 0) { |
---|
303 | return true; |
---|
304 | } else { |
---|
305 | return false; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * \brief Set the opacity of the object |
---|
311 | * |
---|
312 | * The prop must be a vtkActor or vtkAssembly |
---|
313 | */ |
---|
314 | virtual void setOpacity(double opacity) |
---|
315 | { |
---|
316 | _opacity = opacity; |
---|
317 | if (getActor() != NULL) { |
---|
318 | getActor()->GetProperty()->SetOpacity(opacity); |
---|
319 | if (_faceCulling && _opacity < 1.0) { |
---|
320 | setCulling(getActor()->GetProperty(), false); |
---|
321 | TRACE("Culling off"); |
---|
322 | } else if (_faceCulling && _opacity == 1.0) { |
---|
323 | setCulling(getActor()->GetProperty(), true); |
---|
324 | TRACE("Culling on"); |
---|
325 | } |
---|
326 | } else if (getAssembly() != NULL) { |
---|
327 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
328 | vtkProp3D *prop; |
---|
329 | props->InitTraversal(); |
---|
330 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
331 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
332 | vtkActor::SafeDownCast(prop)->GetProperty()->SetOpacity(opacity); |
---|
333 | if (_faceCulling && _opacity < 1.0) { |
---|
334 | setCulling(vtkActor::SafeDownCast(prop)->GetProperty(), false); |
---|
335 | TRACE("Culling off"); |
---|
336 | } else if (_faceCulling && _opacity == 1.0) { |
---|
337 | setCulling(vtkActor::SafeDownCast(prop)->GetProperty(), true); |
---|
338 | TRACE("Culling on"); |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * \brief Get the opacity of the object |
---|
347 | */ |
---|
348 | inline double getOpacity() |
---|
349 | { |
---|
350 | return _opacity; |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * \brief Set the ambient material coefficient |
---|
355 | */ |
---|
356 | virtual void setAmbient(double ambient) |
---|
357 | { |
---|
358 | if (getActor() != NULL) { |
---|
359 | getActor()->GetProperty()->SetAmbient(ambient); |
---|
360 | } else if (getVolume() != NULL) { |
---|
361 | getVolume()->GetProperty()->SetAmbient(ambient); |
---|
362 | } else if (getAssembly() != NULL) { |
---|
363 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
364 | vtkProp3D *prop; |
---|
365 | props->InitTraversal(); |
---|
366 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
367 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
368 | vtkActor::SafeDownCast(prop)->GetProperty()->SetAmbient(ambient); |
---|
369 | } else if (vtkVolume::SafeDownCast(prop) != NULL) { |
---|
370 | vtkVolume::SafeDownCast(prop)->GetProperty()->SetAmbient(ambient); |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * \brief Set the diffuse material coefficient |
---|
378 | */ |
---|
379 | virtual void setDiffuse(double diffuse) |
---|
380 | { |
---|
381 | if (getActor() != NULL) { |
---|
382 | getActor()->GetProperty()->SetDiffuse(diffuse); |
---|
383 | } else if (getVolume() != NULL) { |
---|
384 | getVolume()->GetProperty()->SetDiffuse(diffuse); |
---|
385 | } else if (getAssembly() != NULL) { |
---|
386 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
387 | vtkProp3D *prop; |
---|
388 | props->InitTraversal(); |
---|
389 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
390 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
391 | vtkActor::SafeDownCast(prop)->GetProperty()->SetDiffuse(diffuse); |
---|
392 | } else if (vtkVolume::SafeDownCast(prop) != NULL) { |
---|
393 | vtkVolume::SafeDownCast(prop)->GetProperty()->SetDiffuse(diffuse); |
---|
394 | } |
---|
395 | } |
---|
396 | } |
---|
397 | } |
---|
398 | |
---|
399 | /** |
---|
400 | * \brief Set the specular material coefficient and power |
---|
401 | */ |
---|
402 | virtual void setSpecular(double coeff, double power) |
---|
403 | { |
---|
404 | if (getActor() != NULL) { |
---|
405 | getActor()->GetProperty()->SetSpecular(coeff); |
---|
406 | getActor()->GetProperty()->SetSpecularPower(power); |
---|
407 | } else if (getVolume() != NULL) { |
---|
408 | getVolume()->GetProperty()->SetSpecular(coeff); |
---|
409 | getVolume()->GetProperty()->SetSpecularPower(power); |
---|
410 | } else if (getAssembly() != NULL) { |
---|
411 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
412 | vtkProp3D *prop; |
---|
413 | props->InitTraversal(); |
---|
414 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
415 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
416 | vtkActor::SafeDownCast(prop)->GetProperty()->SetSpecular(coeff); |
---|
417 | vtkActor::SafeDownCast(prop)->GetProperty()->SetSpecularPower(power); |
---|
418 | } else if (vtkVolume::SafeDownCast(prop) != NULL) { |
---|
419 | vtkVolume::SafeDownCast(prop)->GetProperty()->SetSpecular(coeff); |
---|
420 | vtkVolume::SafeDownCast(prop)->GetProperty()->SetSpecularPower(power); |
---|
421 | } |
---|
422 | } |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * \brief Set material properties (coefficients and specular power) of object |
---|
428 | */ |
---|
429 | virtual void setMaterial(double ambient, double diffuse, double specular, double specPower) |
---|
430 | { |
---|
431 | if (getActor() != NULL) { |
---|
432 | vtkProperty *property = getActor()->GetProperty(); |
---|
433 | property->SetAmbient(ambient); |
---|
434 | property->SetDiffuse(diffuse); |
---|
435 | property->SetSpecular(specular); |
---|
436 | property->SetSpecularPower(specPower); |
---|
437 | } else if (getVolume() != NULL) { |
---|
438 | vtkVolumeProperty *property = getVolume()->GetProperty(); |
---|
439 | property->SetAmbient(ambient); |
---|
440 | property->SetDiffuse(diffuse); |
---|
441 | property->SetSpecular(specular); |
---|
442 | property->SetSpecularPower(specPower); |
---|
443 | } else if (getAssembly() != NULL) { |
---|
444 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
445 | vtkProp3D *prop; |
---|
446 | props->InitTraversal(); |
---|
447 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
448 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
449 | vtkProperty *property = vtkActor::SafeDownCast(prop)->GetProperty(); |
---|
450 | property->SetAmbient(ambient); |
---|
451 | property->SetDiffuse(diffuse); |
---|
452 | property->SetSpecular(specular); |
---|
453 | property->SetSpecularPower(specPower); |
---|
454 | } else if (vtkVolume::SafeDownCast(prop) != NULL) { |
---|
455 | vtkVolumeProperty *property = vtkVolume::SafeDownCast(prop)->GetProperty(); |
---|
456 | property->SetAmbient(ambient); |
---|
457 | property->SetDiffuse(diffuse); |
---|
458 | property->SetSpecular(specular); |
---|
459 | property->SetSpecularPower(specPower); |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|
463 | } |
---|
464 | |
---|
465 | /** |
---|
466 | * \brief Set the material color (sets ambient, diffuse, and specular) |
---|
467 | */ |
---|
468 | virtual void setColor(float color[3]) |
---|
469 | { |
---|
470 | for (int i = 0; i < 3; i++) |
---|
471 | _color[i] = color[i]; |
---|
472 | if (getActor() != NULL) { |
---|
473 | getActor()->GetProperty()->SetColor(color[0], color[1], color[2]); |
---|
474 | } else if (getAssembly() != NULL) { |
---|
475 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
476 | vtkProp3D *prop; |
---|
477 | props->InitTraversal(); |
---|
478 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
479 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
480 | vtkActor::SafeDownCast(prop)->GetProperty()->SetColor(color[0], color[1], color[2]); |
---|
481 | } |
---|
482 | } |
---|
483 | } |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | * \brief Set the material ambient color |
---|
488 | */ |
---|
489 | virtual void setAmbientColor(float color[3]) |
---|
490 | { |
---|
491 | if (getActor() != NULL) { |
---|
492 | getActor()->GetProperty()->SetAmbientColor(color[0], color[1], color[2]); |
---|
493 | } else if (getAssembly() != NULL) { |
---|
494 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
495 | vtkProp3D *prop; |
---|
496 | props->InitTraversal(); |
---|
497 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
498 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
499 | vtkActor::SafeDownCast(prop)->GetProperty()->SetAmbientColor(color[0], color[1], color[2]); |
---|
500 | } |
---|
501 | } |
---|
502 | } |
---|
503 | } |
---|
504 | |
---|
505 | /** |
---|
506 | * \brief Set the material diffuse color |
---|
507 | */ |
---|
508 | virtual void setDiffuseColor(float color[3]) |
---|
509 | { |
---|
510 | if (getActor() != NULL) { |
---|
511 | getActor()->GetProperty()->SetDiffuseColor(color[0], color[1], color[2]); |
---|
512 | } else if (getAssembly() != NULL) { |
---|
513 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
514 | vtkProp3D *prop; |
---|
515 | props->InitTraversal(); |
---|
516 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
517 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
518 | vtkActor::SafeDownCast(prop)->GetProperty()->SetDiffuseColor(color[0], color[1], color[2]); |
---|
519 | } |
---|
520 | } |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | /** |
---|
525 | * \brief Set the material specular color |
---|
526 | */ |
---|
527 | virtual void setSpecularColor(float color[3]) |
---|
528 | { |
---|
529 | if (getActor() != NULL) { |
---|
530 | getActor()->GetProperty()->SetSpecularColor(color[0], color[1], color[2]); |
---|
531 | } else if (getAssembly() != NULL) { |
---|
532 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
533 | vtkProp3D *prop; |
---|
534 | props->InitTraversal(); |
---|
535 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
536 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
537 | vtkActor::SafeDownCast(prop)->GetProperty()->SetSpecularColor(color[0], color[1], color[2]); |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | } |
---|
542 | |
---|
543 | /** |
---|
544 | * \brief Toggle lighting of the prop |
---|
545 | */ |
---|
546 | virtual void setLighting(bool state) |
---|
547 | { |
---|
548 | _lighting = state; |
---|
549 | if (getActor() != NULL) { |
---|
550 | getActor()->GetProperty()->SetLighting((state ? 1 : 0)); |
---|
551 | } else if (getVolume() != NULL) { |
---|
552 | getVolume()->GetProperty()->SetShade((state ? 1 : 0)); |
---|
553 | } else if (getAssembly() != NULL) { |
---|
554 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
555 | vtkProp3D *prop; |
---|
556 | props->InitTraversal(); |
---|
557 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
558 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
559 | vtkActor::SafeDownCast(prop)->GetProperty()->SetLighting((state ? 1 : 0)); |
---|
560 | } else if (vtkVolume::SafeDownCast(prop) != NULL) { |
---|
561 | vtkVolume::SafeDownCast(prop)->GetProperty()->SetShade((state ? 1 : 0)); |
---|
562 | } |
---|
563 | } |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | /** |
---|
568 | * \brief Toggle drawing of edges |
---|
569 | */ |
---|
570 | virtual void setEdgeVisibility(bool state) |
---|
571 | { |
---|
572 | if (getActor() != NULL) { |
---|
573 | getActor()->GetProperty()->SetEdgeVisibility((state ? 1 : 0)); |
---|
574 | } else if (getAssembly() != NULL) { |
---|
575 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
576 | vtkProp3D *prop; |
---|
577 | props->InitTraversal(); |
---|
578 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
579 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
580 | vtkActor::SafeDownCast(prop)->GetProperty()->SetEdgeVisibility((state ? 1 : 0)); |
---|
581 | } |
---|
582 | } |
---|
583 | } |
---|
584 | } |
---|
585 | |
---|
586 | /** |
---|
587 | * \brief Set color of edges |
---|
588 | */ |
---|
589 | virtual void setEdgeColor(float color[3]) |
---|
590 | { |
---|
591 | for (int i = 0; i < 3; i++) |
---|
592 | _edgeColor[i] = color[i]; |
---|
593 | if (getActor() != NULL) { |
---|
594 | getActor()->GetProperty()->SetEdgeColor(color[0], color[1], color[2]); |
---|
595 | } else if (getAssembly() != NULL) { |
---|
596 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
597 | vtkProp3D *prop; |
---|
598 | props->InitTraversal(); |
---|
599 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
600 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
601 | vtkActor::SafeDownCast(prop)->GetProperty()->SetEdgeColor(color[0], color[1], color[2]); |
---|
602 | } |
---|
603 | } |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | /** |
---|
608 | * \brief Set pixel width of edges |
---|
609 | * |
---|
610 | * NOTE: May be a no-op if OpenGL implementation doesn't support wide lines |
---|
611 | */ |
---|
612 | virtual void setEdgeWidth(float width) |
---|
613 | { |
---|
614 | _edgeWidth = width; |
---|
615 | if (getActor() != NULL) { |
---|
616 | getActor()->GetProperty()->SetLineWidth(width); |
---|
617 | } else if (getAssembly() != NULL) { |
---|
618 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
619 | vtkProp3D *prop; |
---|
620 | props->InitTraversal(); |
---|
621 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
622 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
623 | vtkActor::SafeDownCast(prop)->GetProperty()->SetLineWidth(width); |
---|
624 | } |
---|
625 | } |
---|
626 | } |
---|
627 | } |
---|
628 | |
---|
629 | /** |
---|
630 | * \brief Set point size |
---|
631 | * |
---|
632 | * NOTE: May be a no-op if OpenGL implementation doesn't support wide points |
---|
633 | */ |
---|
634 | virtual void setPointSize(float size) |
---|
635 | { |
---|
636 | _pointSize = size; |
---|
637 | if (getActor() != NULL) { |
---|
638 | getActor()->GetProperty()->SetPointSize(size); |
---|
639 | } else if (getAssembly() != NULL) { |
---|
640 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
641 | vtkProp3D *prop; |
---|
642 | props->InitTraversal(); |
---|
643 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
644 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
645 | vtkActor::SafeDownCast(prop)->GetProperty()->SetPointSize(size); |
---|
646 | } |
---|
647 | } |
---|
648 | } |
---|
649 | } |
---|
650 | |
---|
651 | /** |
---|
652 | * \brief Toggle wireframe rendering of prop |
---|
653 | */ |
---|
654 | virtual void setWireframe(bool state) |
---|
655 | { |
---|
656 | if (getActor() != NULL) { |
---|
657 | if (state) { |
---|
658 | getActor()->GetProperty()->SetRepresentationToWireframe(); |
---|
659 | getActor()->GetProperty()->LightingOff(); |
---|
660 | } else { |
---|
661 | getActor()->GetProperty()->SetRepresentationToSurface(); |
---|
662 | setLighting(_lighting); |
---|
663 | } |
---|
664 | } else if (getAssembly() != NULL) { |
---|
665 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
666 | vtkProp3D *prop; |
---|
667 | props->InitTraversal(); |
---|
668 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
669 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
670 | if (state) { |
---|
671 | vtkActor::SafeDownCast(prop)->GetProperty()->SetRepresentationToWireframe(); |
---|
672 | vtkActor::SafeDownCast(prop)->GetProperty()->LightingOff(); |
---|
673 | } else { |
---|
674 | vtkActor::SafeDownCast(prop)->GetProperty()->SetRepresentationToSurface(); |
---|
675 | vtkActor::SafeDownCast(prop)->GetProperty()->SetLighting((_lighting ? 1 : 0)); |
---|
676 | } |
---|
677 | } |
---|
678 | } |
---|
679 | } |
---|
680 | } |
---|
681 | |
---|
682 | /** |
---|
683 | * \brief Toggle culling of selected CullFace |
---|
684 | */ |
---|
685 | virtual void setCulling(bool state) |
---|
686 | { |
---|
687 | _faceCulling = state; |
---|
688 | if (state && _opacity < 1.0) |
---|
689 | return; |
---|
690 | if (getActor() != NULL) { |
---|
691 | setCulling(getActor()->GetProperty(), state); |
---|
692 | } else if (getAssembly() != NULL) { |
---|
693 | vtkProp3DCollection *props = getAssembly()->GetParts(); |
---|
694 | vtkProp3D *prop; |
---|
695 | props->InitTraversal(); |
---|
696 | while ((prop = props->GetNextProp3D()) != NULL) { |
---|
697 | if (vtkActor::SafeDownCast(prop) != NULL) { |
---|
698 | setCulling(vtkActor::SafeDownCast(prop)->GetProperty(), state); |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | } |
---|
703 | |
---|
704 | /** |
---|
705 | * \brief Specify which face(s) to cull when culling is enabled |
---|
706 | */ |
---|
707 | virtual void setCullFace(CullFace cull) |
---|
708 | { |
---|
709 | _cullFace = cull; |
---|
710 | setCulling(_faceCulling); |
---|
711 | } |
---|
712 | |
---|
713 | /** |
---|
714 | * \brief Subclasses need to implement setting clipping planes in their mappers |
---|
715 | */ |
---|
716 | virtual void setClippingPlanes(vtkPlaneCollection *planes) = 0; |
---|
717 | |
---|
718 | protected: |
---|
719 | /** |
---|
720 | * \brief Create and initialize a VTK Prop to render the object |
---|
721 | */ |
---|
722 | virtual void initProp() |
---|
723 | { |
---|
724 | if (_prop == NULL) { |
---|
725 | _prop = vtkSmartPointer<vtkActor>::New(); |
---|
726 | vtkProperty *property = getActor()->GetProperty(); |
---|
727 | property->SetColor(_color[0], _color[1], _color[2]); |
---|
728 | property->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
729 | property->SetLineWidth(_edgeWidth); |
---|
730 | property->SetPointSize(_pointSize); |
---|
731 | property->EdgeVisibilityOff(); |
---|
732 | property->SetOpacity(_opacity); |
---|
733 | property->SetAmbient(.2); |
---|
734 | if (!_lighting) |
---|
735 | property->LightingOff(); |
---|
736 | if (_faceCulling && _opacity == 1.0) { |
---|
737 | setCulling(property, true); |
---|
738 | } |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * \brief Subclasses implement this to create the VTK pipeline |
---|
744 | * on a state change (e.g. new DataSet) |
---|
745 | */ |
---|
746 | virtual void update() = 0; |
---|
747 | |
---|
748 | /** |
---|
749 | * \brief Convenience method to set culling state on a vtkProperty |
---|
750 | * |
---|
751 | * Note: Does not change the culling state flag of this VtkGraphicsObject |
---|
752 | */ |
---|
753 | virtual void setCulling(vtkProperty *property, bool state) |
---|
754 | { |
---|
755 | switch (_cullFace) { |
---|
756 | case CULL_FRONT: |
---|
757 | property->SetFrontfaceCulling((state ? 1 : 0)); |
---|
758 | property->BackfaceCullingOff(); |
---|
759 | break; |
---|
760 | case CULL_BACK: |
---|
761 | property->SetBackfaceCulling((state ? 1 : 0)); |
---|
762 | property->FrontfaceCullingOff(); |
---|
763 | break; |
---|
764 | case CULL_FRONT_AND_BACK: |
---|
765 | property->SetBackfaceCulling((state ? 1 : 0)); |
---|
766 | property->SetFrontfaceCulling((state ? 1 : 0)); |
---|
767 | break; |
---|
768 | } |
---|
769 | } |
---|
770 | |
---|
771 | DataSet *_dataSet; |
---|
772 | double _dataRange[2]; |
---|
773 | double _opacity; |
---|
774 | float _color[3]; |
---|
775 | float _edgeColor[3]; |
---|
776 | float _edgeWidth; |
---|
777 | float _pointSize; |
---|
778 | bool _lighting; |
---|
779 | CullFace _cullFace; |
---|
780 | bool _faceCulling; |
---|
781 | |
---|
782 | vtkSmartPointer<vtkProp> _prop; |
---|
783 | }; |
---|
784 | |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | #endif |
---|