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 <vtkContourFilter.h> |
---|
9 | #include <vtkPolyDataMapper.h> |
---|
10 | #include <vtkProperty.h> |
---|
11 | |
---|
12 | #include "RpContour2D.h" |
---|
13 | #include "Trace.h" |
---|
14 | |
---|
15 | using namespace Rappture::VtkVis; |
---|
16 | |
---|
17 | Contour2D::Contour2D() : |
---|
18 | _dataSet(NULL), |
---|
19 | _numContours(0), |
---|
20 | _edgeWidth(1.0f), |
---|
21 | _opacity(1.0) |
---|
22 | { |
---|
23 | _edgeColor[0] = 0; |
---|
24 | _edgeColor[1] = 0; |
---|
25 | _edgeColor[2] = 0; |
---|
26 | } |
---|
27 | |
---|
28 | Contour2D::~Contour2D() |
---|
29 | { |
---|
30 | #ifdef WANT_TRACE |
---|
31 | if (_dataSet != NULL) |
---|
32 | TRACE("Deleting Contour2D for %s", _dataSet->getName().c_str()); |
---|
33 | else |
---|
34 | TRACE("Deleting Contour2D with NULL DataSet"); |
---|
35 | #endif |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * \brief Specify input DataSet used to extract contours |
---|
40 | */ |
---|
41 | void Contour2D::setDataSet(DataSet *dataSet) |
---|
42 | { |
---|
43 | if (_dataSet != dataSet) { |
---|
44 | _dataSet = dataSet; |
---|
45 | update(); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * \brief Returns the DataSet this Contour2D renders |
---|
51 | */ |
---|
52 | DataSet *Contour2D::getDataSet() |
---|
53 | { |
---|
54 | return _dataSet; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * \brief Get the VTK Actor for the contour lines |
---|
59 | */ |
---|
60 | vtkActor *Contour2D::getActor() |
---|
61 | { |
---|
62 | return _contourActor; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * \brief Create and initialize a VTK actor to render isolines |
---|
67 | */ |
---|
68 | void Contour2D::initActor() |
---|
69 | { |
---|
70 | if (_contourActor == NULL) { |
---|
71 | _contourActor = vtkSmartPointer<vtkActor>::New(); |
---|
72 | _contourActor->GetProperty()->EdgeVisibilityOn(); |
---|
73 | _contourActor->GetProperty()->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
74 | _contourActor->GetProperty()->SetLineWidth(_edgeWidth); |
---|
75 | _contourActor->GetProperty()->SetOpacity(_opacity); |
---|
76 | _contourActor->GetProperty()->LightingOff(); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * \brief Internal method to re-compute contours after a state change |
---|
82 | */ |
---|
83 | void Contour2D::update() |
---|
84 | { |
---|
85 | if (_dataSet == NULL) { |
---|
86 | return; |
---|
87 | } |
---|
88 | |
---|
89 | initActor(); |
---|
90 | |
---|
91 | // Contour filter to generate isolines |
---|
92 | vtkSmartPointer<vtkContourFilter> contourFilter = vtkSmartPointer<vtkContourFilter>::New(); |
---|
93 | contourFilter->SetInput(_dataSet->getVtkDataSet()); |
---|
94 | |
---|
95 | contourFilter->ComputeNormalsOff(); |
---|
96 | contourFilter->ComputeGradientsOff(); |
---|
97 | |
---|
98 | // Speed up multiple contour computation at cost of extra memory use |
---|
99 | if (_numContours > 1) { |
---|
100 | contourFilter->UseScalarTreeOn(); |
---|
101 | } else { |
---|
102 | contourFilter->UseScalarTreeOff(); |
---|
103 | } |
---|
104 | if (_contours.empty()) { |
---|
105 | // Evenly spaced isovalues |
---|
106 | double dataRange[2]; |
---|
107 | _dataSet->getDataRange(dataRange); |
---|
108 | contourFilter->GenerateValues(_numContours, dataRange[0], dataRange[1]); |
---|
109 | } else { |
---|
110 | // User-supplied isovalues |
---|
111 | for (int i = 0; i < (int)_contours.size(); i++) { |
---|
112 | contourFilter->SetValue(i, _contours[i]); |
---|
113 | } |
---|
114 | } |
---|
115 | if (_contourMapper == NULL) { |
---|
116 | _contourMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); |
---|
117 | _contourMapper->SetResolveCoincidentTopologyToPolygonOffset(); |
---|
118 | _contourActor->SetMapper(_contourMapper); |
---|
119 | } |
---|
120 | |
---|
121 | _contourMapper->SetInput(contourFilter->GetOutput()); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * \brief Specify number of evenly spaced contour lines to render |
---|
126 | * |
---|
127 | * Will override any existing contours |
---|
128 | */ |
---|
129 | void Contour2D::setContours(int numContours) |
---|
130 | { |
---|
131 | _contours.clear(); |
---|
132 | _numContours = numContours; |
---|
133 | |
---|
134 | update(); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * \brief Specify an explicit list of contour isovalues |
---|
139 | * |
---|
140 | * Will override any existing contours |
---|
141 | */ |
---|
142 | void Contour2D::setContourList(const std::vector<double>& contours) |
---|
143 | { |
---|
144 | _contours = contours; |
---|
145 | _numContours = _contours.size(); |
---|
146 | |
---|
147 | update(); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * \brief Turn on/off rendering of this contour set |
---|
152 | */ |
---|
153 | void Contour2D::setVisibility(bool state) |
---|
154 | { |
---|
155 | if (_contourActor != NULL) { |
---|
156 | _contourActor->SetVisibility((state ? 1 : 0)); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * \brief Get visibility state of the contour set |
---|
162 | * |
---|
163 | * \return Is contour set visible? |
---|
164 | */ |
---|
165 | bool Contour2D::getVisibility() |
---|
166 | { |
---|
167 | if (_contourActor == NULL) { |
---|
168 | return false; |
---|
169 | } else { |
---|
170 | return (_contourActor->GetVisibility() != 0); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * \brief Set opacity used to render contour lines |
---|
176 | */ |
---|
177 | void Contour2D::setOpacity(double opacity) |
---|
178 | { |
---|
179 | _opacity = opacity; |
---|
180 | if (_contourActor != NULL) |
---|
181 | _contourActor->GetProperty()->SetOpacity(opacity); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * \brief Set RGB color of contour lines |
---|
186 | */ |
---|
187 | void Contour2D::setEdgeColor(float color[3]) |
---|
188 | { |
---|
189 | _edgeColor[0] = color[0]; |
---|
190 | _edgeColor[1] = color[1]; |
---|
191 | _edgeColor[2] = color[2]; |
---|
192 | if (_contourActor != NULL) |
---|
193 | _contourActor->GetProperty()->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * \brief Set pixel width of contour lines (may be a no-op) |
---|
198 | */ |
---|
199 | void Contour2D::setEdgeWidth(float edgeWidth) |
---|
200 | { |
---|
201 | _edgeWidth = edgeWidth; |
---|
202 | if (_contourActor != NULL) |
---|
203 | _contourActor->GetProperty()->SetLineWidth(_edgeWidth); |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * \brief Set a group of world coordinate planes to clip rendering |
---|
208 | * |
---|
209 | * Passing NULL for planes will remove all cliping planes |
---|
210 | */ |
---|
211 | void Contour2D::setClippingPlanes(vtkPlaneCollection *planes) |
---|
212 | { |
---|
213 | if (_contourMapper != NULL) { |
---|
214 | _contourMapper->SetClippingPlanes(planes); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * \brief Turn on/off lighting of this object |
---|
220 | */ |
---|
221 | void Contour2D::setLighting(bool state) |
---|
222 | { |
---|
223 | if (_contourActor != NULL) |
---|
224 | _contourActor->GetProperty()->SetLighting((state ? 1 : 0)); |
---|
225 | } |
---|