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 | #include <vtkDataSet.h> |
---|
9 | #include <vtkDataSetMapper.h> |
---|
10 | #include <vtkProperty.h> |
---|
11 | #include <vtkPointData.h> |
---|
12 | #include <vtkLookupTable.h> |
---|
13 | |
---|
14 | #include "RpPseudoColor.h" |
---|
15 | #include "Trace.h" |
---|
16 | |
---|
17 | using namespace Rappture::VtkVis; |
---|
18 | |
---|
19 | PseudoColor::PseudoColor() : |
---|
20 | _dataSet(NULL), |
---|
21 | _opacity(1.0), |
---|
22 | _edgeWidth(1.0) |
---|
23 | { |
---|
24 | _edgeColor[0] = 0.0; |
---|
25 | _edgeColor[1] = 0.0; |
---|
26 | _edgeColor[2] = 0.0; |
---|
27 | } |
---|
28 | |
---|
29 | PseudoColor::~PseudoColor() |
---|
30 | { |
---|
31 | #ifdef WANT_TRACE |
---|
32 | if (_dataSet != NULL) |
---|
33 | TRACE("Deleting PseudoColor for %s", _dataSet->getName().c_str()); |
---|
34 | else |
---|
35 | TRACE("Deleting PseudoColor with NULL DataSet"); |
---|
36 | #endif |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * \brief Specify input DataSet with scalars to colormap |
---|
41 | * |
---|
42 | * Currently the DataSet must be image data (2D uniform grid) |
---|
43 | */ |
---|
44 | void PseudoColor::setDataSet(DataSet *dataSet) |
---|
45 | { |
---|
46 | _dataSet = dataSet; |
---|
47 | update(); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * \brief Returns the DataSet this PseudoColor renders |
---|
52 | */ |
---|
53 | DataSet *PseudoColor::getDataSet() |
---|
54 | { |
---|
55 | return _dataSet; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * \brief Internal method to set up color mapper after a state change |
---|
60 | */ |
---|
61 | void PseudoColor::update() |
---|
62 | { |
---|
63 | if (_dataSet == NULL) |
---|
64 | return; |
---|
65 | |
---|
66 | vtkDataSet *ds = _dataSet->getVtkDataSet(); |
---|
67 | |
---|
68 | double dataRange[2]; |
---|
69 | _dataSet->getDataRange(dataRange); |
---|
70 | |
---|
71 | // Mapper, actor to render color-mapped data set |
---|
72 | if (_dsMapper == NULL) { |
---|
73 | _dsMapper = vtkSmartPointer<vtkDataSetMapper>::New(); |
---|
74 | } |
---|
75 | _dsMapper->SetInput(ds); |
---|
76 | _dsMapper->StaticOff(); |
---|
77 | |
---|
78 | if (ds->GetPointData() == NULL || |
---|
79 | ds->GetPointData()->GetScalars() == NULL) { |
---|
80 | ERROR("No scalar point data in dataset %s", _dataSet->getName().c_str()); |
---|
81 | if (_lut == NULL) { |
---|
82 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
83 | } |
---|
84 | } else { |
---|
85 | vtkLookupTable *lut = ds->GetPointData()->GetScalars()->GetLookupTable(); |
---|
86 | TRACE("Data set scalars lookup table: %p\n", lut); |
---|
87 | if (_lut == NULL) { |
---|
88 | if (lut) |
---|
89 | _lut = lut; |
---|
90 | else |
---|
91 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | _lut->SetRange(dataRange); |
---|
96 | |
---|
97 | _dsMapper->UseLookupTableScalarRangeOn(); |
---|
98 | _dsMapper->SetLookupTable(_lut); |
---|
99 | //_dsMapper->InterpolateScalarsBeforeMappingOn(); |
---|
100 | |
---|
101 | initActor(); |
---|
102 | _dsActor->SetMapper(_dsMapper); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * \brief Get the VTK Actor for the colormapped dataset |
---|
107 | */ |
---|
108 | vtkActor *PseudoColor::getActor() |
---|
109 | { |
---|
110 | return _dsActor; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * \brief Create and initialize a VTK actor to render the colormapped dataset |
---|
115 | */ |
---|
116 | void PseudoColor::initActor() |
---|
117 | { |
---|
118 | if (_dsActor == NULL) { |
---|
119 | _dsActor = vtkSmartPointer<vtkActor>::New(); |
---|
120 | _dsActor->GetProperty()->SetOpacity(_opacity); |
---|
121 | _dsActor->GetProperty()->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
122 | _dsActor->GetProperty()->SetLineWidth(_edgeWidth); |
---|
123 | _dsActor->GetProperty()->EdgeVisibilityOff(); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * \brief Get the VTK colormap lookup table in use |
---|
129 | */ |
---|
130 | vtkLookupTable *PseudoColor::getLookupTable() |
---|
131 | { |
---|
132 | return _lut; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * \brief Associate a colormap lookup table with the DataSet |
---|
137 | */ |
---|
138 | void PseudoColor::setLookupTable(vtkLookupTable *lut) |
---|
139 | { |
---|
140 | if (lut == NULL) { |
---|
141 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
142 | } else { |
---|
143 | _lut = lut; |
---|
144 | } |
---|
145 | |
---|
146 | if (_dsMapper != NULL) { |
---|
147 | _dsMapper->UseLookupTableScalarRangeOn(); |
---|
148 | _dsMapper->SetLookupTable(_lut); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * \brief Turn on/off rendering of this colormapped dataset |
---|
154 | */ |
---|
155 | void PseudoColor::setVisibility(bool state) |
---|
156 | { |
---|
157 | if (_dsActor != NULL) { |
---|
158 | _dsActor->SetVisibility((state ? 1 : 0)); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * \brief Get visibility state of the colormapped dataset |
---|
164 | * |
---|
165 | * \return Is PseudoColor visible? |
---|
166 | */ |
---|
167 | bool PseudoColor::getVisibility() |
---|
168 | { |
---|
169 | if (_dsActor == NULL) { |
---|
170 | return false; |
---|
171 | } else { |
---|
172 | return (_dsActor->GetVisibility() != 0); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * \brief Set opacity used to render the colormapped dataset |
---|
178 | */ |
---|
179 | void PseudoColor::setOpacity(double opacity) |
---|
180 | { |
---|
181 | _opacity = opacity; |
---|
182 | if (_dsActor != NULL) |
---|
183 | _dsActor->GetProperty()->SetOpacity(opacity); |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * \brief Turn on/off rendering of mesh edges |
---|
188 | */ |
---|
189 | void PseudoColor::setEdgeVisibility(bool state) |
---|
190 | { |
---|
191 | if (_dsActor != NULL) { |
---|
192 | _dsActor->GetProperty()->SetEdgeVisibility((state ? 1 : 0)); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * \brief Set RGB color of polygon edges |
---|
198 | */ |
---|
199 | void PseudoColor::setEdgeColor(float color[3]) |
---|
200 | { |
---|
201 | _edgeColor[0] = color[0]; |
---|
202 | _edgeColor[1] = color[1]; |
---|
203 | _edgeColor[2] = color[2]; |
---|
204 | if (_dsActor != NULL) |
---|
205 | _dsActor->GetProperty()->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
206 | } |
---|
207 | |
---|
208 | /** |
---|
209 | * \brief Set pixel width of polygon edges (may be a no-op) |
---|
210 | */ |
---|
211 | void PseudoColor::setEdgeWidth(float edgeWidth) |
---|
212 | { |
---|
213 | _edgeWidth = edgeWidth; |
---|
214 | if (_dsActor != NULL) |
---|
215 | _dsActor->GetProperty()->SetLineWidth(_edgeWidth); |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * \brief Set a group of world coordinate planes to clip rendering |
---|
220 | * |
---|
221 | * Passing NULL for planes will remove all cliping planes |
---|
222 | */ |
---|
223 | void PseudoColor::setClippingPlanes(vtkPlaneCollection *planes) |
---|
224 | { |
---|
225 | if (_dsMapper != NULL) { |
---|
226 | _dsMapper->SetClippingPlanes(planes); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | /** |
---|
231 | * \brief Turn on/off lighting of this object |
---|
232 | */ |
---|
233 | void PseudoColor::setLighting(bool state) |
---|
234 | { |
---|
235 | if (_dsActor != NULL) |
---|
236 | _dsActor->GetProperty()->SetLighting((state ? 1 : 0)); |
---|
237 | } |
---|