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