1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <vtkSmartPointer.h> |
---|
9 | #include <vtkImageData.h> |
---|
10 | #include <vtkImageActor.h> |
---|
11 | #include <vtkImageSlice.h> |
---|
12 | #include <vtkImageProperty.h> |
---|
13 | #include <vtkImageMapper3D.h> |
---|
14 | #include <vtkImageResliceMapper.h> |
---|
15 | #include <vtkLookupTable.h> |
---|
16 | |
---|
17 | #include "Image.h" |
---|
18 | #include "Trace.h" |
---|
19 | |
---|
20 | using namespace VtkVis; |
---|
21 | |
---|
22 | #define USE_RESLICE_MAPPER |
---|
23 | |
---|
24 | Image::Image() : |
---|
25 | GraphicsObject(), |
---|
26 | _colorMap(NULL) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | Image::~Image() |
---|
31 | { |
---|
32 | TRACE("Deleting Image"); |
---|
33 | } |
---|
34 | |
---|
35 | void Image::initProp() |
---|
36 | { |
---|
37 | if (_prop == NULL) { |
---|
38 | #ifdef USE_RESLICE_MAPPER |
---|
39 | _prop = vtkSmartPointer<vtkImageSlice>::New(); |
---|
40 | #else |
---|
41 | _prop = vtkSmartPointer<vtkImageActor>::New(); |
---|
42 | #endif |
---|
43 | vtkImageProperty *property = getImageProperty(); |
---|
44 | property->SetInterpolationTypeToLinear(); |
---|
45 | property->SetBackingColor(_color[0], _color[1], _color[2]); |
---|
46 | property->BackingOff(); |
---|
47 | if (_dataSet != NULL) |
---|
48 | _opacity = _dataSet->getOpacity(); |
---|
49 | property->SetOpacity(_opacity); |
---|
50 | |
---|
51 | if (_dataSet != NULL) |
---|
52 | setVisibility(_dataSet->getVisibility()); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | void Image::update() |
---|
57 | { |
---|
58 | if (_dataSet == NULL) |
---|
59 | return; |
---|
60 | |
---|
61 | TRACE("DataSet: %s", _dataSet->getName().c_str()); |
---|
62 | |
---|
63 | vtkDataSet *ds = _dataSet->getVtkDataSet(); |
---|
64 | vtkImageData *imageData = vtkImageData::SafeDownCast(ds); |
---|
65 | |
---|
66 | if (imageData == NULL) { |
---|
67 | ERROR("DataSet is not an image."); |
---|
68 | return; |
---|
69 | } |
---|
70 | |
---|
71 | initProp(); |
---|
72 | |
---|
73 | vtkImageActor *actor = getImageActor(); |
---|
74 | vtkImageMapper3D *mapper = getImageMapper(); |
---|
75 | if (mapper == NULL) { |
---|
76 | TRACE("Creating mapper"); |
---|
77 | vtkSmartPointer<vtkImageResliceMapper> newMapper = vtkSmartPointer<vtkImageResliceMapper>::New(); |
---|
78 | getImageSlice()->SetMapper(newMapper); |
---|
79 | mapper = getImageMapper(); |
---|
80 | assert(mapper != NULL); |
---|
81 | } |
---|
82 | if (actor != NULL) { |
---|
83 | TRACE("Have actor"); |
---|
84 | actor->SetInputData(imageData); |
---|
85 | actor->InterpolateOn(); |
---|
86 | } else { |
---|
87 | TRACE("No actor"); |
---|
88 | mapper->SetInputData(imageData); |
---|
89 | } |
---|
90 | |
---|
91 | mapper->SliceAtFocalPointOn(); |
---|
92 | mapper->SliceFacesCameraOn(); |
---|
93 | |
---|
94 | vtkImageResliceMapper *resliceMapper = getImageResliceMapper(); |
---|
95 | if (resliceMapper) { |
---|
96 | TRACE("Mapper is a vtkImageResliceMapper"); |
---|
97 | resliceMapper->AutoAdjustImageQualityOff(); |
---|
98 | resliceMapper->ResampleToScreenPixelsOff(); |
---|
99 | } else { |
---|
100 | TRACE("Mapper is a %s", mapper->GetClassName()); |
---|
101 | } |
---|
102 | |
---|
103 | mapper->Update(); |
---|
104 | |
---|
105 | TRACE("Window: %g Level: %g", getWindow(), getLevel()); |
---|
106 | } |
---|
107 | |
---|
108 | void Image::setColor(float color[3]) |
---|
109 | { |
---|
110 | GraphicsObject::setColor(color); |
---|
111 | |
---|
112 | if (getImageProperty() != NULL) { |
---|
113 | getImageProperty()->SetBackingColor(color[0], color[1], color[2]); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * \brief Associate a colormap lookup table with the DataSet |
---|
119 | */ |
---|
120 | void Image::setColorMap(ColorMap *cmap) |
---|
121 | { |
---|
122 | if (cmap == NULL) { |
---|
123 | _colorMap = NULL; |
---|
124 | _lut = NULL; |
---|
125 | if (getImageProperty() != NULL) { |
---|
126 | getImageProperty()->SetLookupTable(NULL); |
---|
127 | } |
---|
128 | return; |
---|
129 | } |
---|
130 | |
---|
131 | _colorMap = cmap; |
---|
132 | |
---|
133 | if (_lut == NULL) { |
---|
134 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
135 | if (getImageProperty() != NULL) { |
---|
136 | getImageProperty()->UseLookupTableScalarRangeOn(); |
---|
137 | getImageProperty()->SetLookupTable(_lut); |
---|
138 | } |
---|
139 | _lut->DeepCopy(cmap->getLookupTable()); |
---|
140 | _lut->SetRange(_dataRange); |
---|
141 | } else { |
---|
142 | double range[2]; |
---|
143 | _lut->GetTableRange(range); |
---|
144 | _lut->DeepCopy(cmap->getLookupTable()); |
---|
145 | _lut->SetRange(range); |
---|
146 | _lut->Modified(); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | void Image::setAspect(double aspect) |
---|
151 | { |
---|
152 | double bounds[6]; |
---|
153 | vtkDataSet *ds = _dataSet->getVtkDataSet(); |
---|
154 | ds->GetBounds(bounds); |
---|
155 | double size[3]; |
---|
156 | size[0] = bounds[1] - bounds[0]; |
---|
157 | size[1] = bounds[3] - bounds[2]; |
---|
158 | size[2] = bounds[5] - bounds[4]; |
---|
159 | double scale[3]; |
---|
160 | scale[0] = scale[1] = scale[2] = 1.; |
---|
161 | |
---|
162 | vtkImageMapper3D *mapper = getImageMapper(); |
---|
163 | if (mapper == NULL) |
---|
164 | return; |
---|
165 | |
---|
166 | mapper->Update(); |
---|
167 | |
---|
168 | double normal[3]; |
---|
169 | mapper->GetSlicePlane()->GetNormal(normal); |
---|
170 | |
---|
171 | Axis sliceAxis = Z_AXIS; |
---|
172 | if (fabs(normal[0]) == 1.0 && |
---|
173 | normal[1] == 0.0 && |
---|
174 | normal[2] == 0.0) { |
---|
175 | sliceAxis = X_AXIS; |
---|
176 | } else if (normal[0] == 0.0 && |
---|
177 | fabs(normal[1]) == 1.0 && |
---|
178 | normal[2] == 0.0) { |
---|
179 | sliceAxis = Y_AXIS; |
---|
180 | } else if (normal[0] == 0.0 && |
---|
181 | normal[1] == 0.0 && |
---|
182 | fabs(normal[2]) == 1.0) { |
---|
183 | sliceAxis = Z_AXIS; |
---|
184 | } else { |
---|
185 | TRACE("Non orthogonal slice plane, setting native aspect"); |
---|
186 | aspect = 0.0; |
---|
187 | } |
---|
188 | |
---|
189 | if (aspect == 1.0) { |
---|
190 | // Square |
---|
191 | switch (sliceAxis) { |
---|
192 | case X_AXIS: { |
---|
193 | if (size[1] > size[2] && size[2] > 0.0) { |
---|
194 | scale[2] = size[1] / size[2]; |
---|
195 | } else if (size[2] > size[1] && size[1] > 0.0) { |
---|
196 | scale[1] = size[2] / size[1]; |
---|
197 | } |
---|
198 | } |
---|
199 | break; |
---|
200 | case Y_AXIS: { |
---|
201 | if (size[0] > size[2] && size[2] > 0.0) { |
---|
202 | scale[2] = size[0] / size[2]; |
---|
203 | } else if (size[2] > size[0] && size[0] > 0.0) { |
---|
204 | scale[0] = size[2] / size[0]; |
---|
205 | } |
---|
206 | } |
---|
207 | break; |
---|
208 | case Z_AXIS: { |
---|
209 | if (size[0] > size[1] && size[1] > 0.0) { |
---|
210 | scale[1] = size[0] / size[1]; |
---|
211 | } else if (size[1] > size[0] && size[0] > 0.0) { |
---|
212 | scale[0] = size[1] / size[0]; |
---|
213 | } |
---|
214 | } |
---|
215 | break; |
---|
216 | } |
---|
217 | } else if (aspect != 0.0) { |
---|
218 | switch (sliceAxis) { |
---|
219 | case X_AXIS: { |
---|
220 | if (aspect > 1.0) { |
---|
221 | if (size[2] > size[1] && size[1] > 0.0) { |
---|
222 | scale[1] = (size[2] / aspect) / size[1]; |
---|
223 | } else if (size[2] > 0.0) { |
---|
224 | scale[2] = (size[1] * aspect) / size[2]; |
---|
225 | } |
---|
226 | } else { |
---|
227 | if (size[1] > size[2] && size[2] > 0.0) { |
---|
228 | scale[2] = (size[1] * aspect) / size[2]; |
---|
229 | } else if (size[1] > 0.0) { |
---|
230 | scale[1] = (size[2] / aspect) / size[1]; |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | break; |
---|
235 | case Y_AXIS: { |
---|
236 | if (aspect > 1.0) { |
---|
237 | if (size[0] > size[2] && size[2] > 0.0) { |
---|
238 | scale[2] = (size[0] / aspect) / size[2]; |
---|
239 | } else if (size[0] > 0.0) { |
---|
240 | scale[0] = (size[2] * aspect) / size[0]; |
---|
241 | } |
---|
242 | } else { |
---|
243 | if (size[2] > size[0] && size[0] > 0.0) { |
---|
244 | scale[0] = (size[2] * aspect) / size[0]; |
---|
245 | } else if (size[2] > 0.0) { |
---|
246 | scale[2] = (size[0] / aspect) / size[2]; |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | break; |
---|
251 | case Z_AXIS: { |
---|
252 | if (aspect > 1.0) { |
---|
253 | if (size[0] > size[1] && size[1] > 0.0) { |
---|
254 | scale[1] = (size[0] / aspect) / size[1]; |
---|
255 | } else if (size[0] > 0.0) { |
---|
256 | scale[0] = (size[1] * aspect) / size[0]; |
---|
257 | } |
---|
258 | } else { |
---|
259 | if (size[1] > size[0] && size[0] > 0.0) { |
---|
260 | scale[0] = (size[1] * aspect) / size[0]; |
---|
261 | } else if (size[1] > 0.0) { |
---|
262 | scale[1] = (size[0] / aspect) / size[1]; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | break; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | TRACE("%s dims %g,%g,%g", _dataSet->getName().c_str(), |
---|
271 | size[0], size[1], size[2]); |
---|
272 | TRACE("Setting scale to %g,%g,%g", scale[0], scale[1], scale[2]); |
---|
273 | setScale(scale); |
---|
274 | mapper->Modified(); |
---|
275 | mapper->Update(); |
---|
276 | } |
---|
277 | |
---|
278 | void Image::setClippingPlanes(vtkPlaneCollection *planes) |
---|
279 | { |
---|
280 | vtkImageMapper3D *mapper = getImageMapper(); |
---|
281 | if (mapper != NULL) { |
---|
282 | mapper->SetClippingPlanes(planes); |
---|
283 | } |
---|
284 | } |
---|