1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * Volume.h: 3d volume class |
---|
5 | * |
---|
6 | * ====================================================================== |
---|
7 | * AUTHOR: Wei Qiao <qiaow@purdue.edu> |
---|
8 | * Purdue Rendering and Perceptualization Lab (PURPL) |
---|
9 | * |
---|
10 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
11 | * |
---|
12 | * See the file "license.terms" for information on usage and |
---|
13 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | * ====================================================================== |
---|
15 | */ |
---|
16 | #ifndef VOLUME_H |
---|
17 | #define VOLUME_H |
---|
18 | |
---|
19 | #include <string> |
---|
20 | #include <vector> |
---|
21 | |
---|
22 | #include "Color.h" |
---|
23 | #include "Texture3D.h" |
---|
24 | #include "Vector3.h" |
---|
25 | #include "AxisRange.h" |
---|
26 | #include "TransferFunction.h" |
---|
27 | |
---|
28 | struct CutPlane { |
---|
29 | /// orientation - 1: xy slice, 2: yz slice, 3: xz slice |
---|
30 | int orient; |
---|
31 | float offset; ///< normalized offset [0,1] in the volume |
---|
32 | bool enabled; |
---|
33 | |
---|
34 | CutPlane(int _orient, float _offset) : |
---|
35 | orient(_orient), |
---|
36 | offset(_offset), |
---|
37 | enabled(true) |
---|
38 | { |
---|
39 | } |
---|
40 | }; |
---|
41 | |
---|
42 | class VolumeInterpolator; |
---|
43 | |
---|
44 | class Volume |
---|
45 | { |
---|
46 | public: |
---|
47 | enum VolumeType { |
---|
48 | CUBIC, |
---|
49 | VOLQD, |
---|
50 | ZINCBLENDE |
---|
51 | }; |
---|
52 | |
---|
53 | /** |
---|
54 | * \brief Volume data constructor |
---|
55 | * |
---|
56 | * Represents a 3D regular grid with uniform spacing along |
---|
57 | * each axis. Sample spacing may differ between X, Y and Z |
---|
58 | * |
---|
59 | * \param x X location |
---|
60 | * \param y Y location |
---|
61 | * \param z Z location |
---|
62 | * \param width Number of samples in X |
---|
63 | * \param height Number of samples in Y |
---|
64 | * \param depth Number of samples in Z |
---|
65 | * \param numComponents Number of components per sample |
---|
66 | * \param data width * height * depth * numComponent sample array |
---|
67 | * \param vmin Scalar value minimum |
---|
68 | * \param vmax Scalar value maximum |
---|
69 | * \param nonZeroMin Scalar minimum which is greater than zero |
---|
70 | */ |
---|
71 | Volume(float x, float y, float z, |
---|
72 | int width, int height, int depth, |
---|
73 | int numComponents, |
---|
74 | float *data, |
---|
75 | double vmin, double vmax, |
---|
76 | double nonZeroMin); |
---|
77 | |
---|
78 | virtual ~Volume(); |
---|
79 | |
---|
80 | int width() const |
---|
81 | { |
---|
82 | return _width; |
---|
83 | } |
---|
84 | |
---|
85 | int height() const |
---|
86 | { |
---|
87 | return _height; |
---|
88 | } |
---|
89 | |
---|
90 | int depth() const |
---|
91 | { |
---|
92 | return _depth; |
---|
93 | } |
---|
94 | |
---|
95 | void visible(bool value) |
---|
96 | { |
---|
97 | _enabled = value; |
---|
98 | } |
---|
99 | |
---|
100 | bool visible() const |
---|
101 | { |
---|
102 | return _enabled; |
---|
103 | } |
---|
104 | |
---|
105 | void location(const Vector3& loc) |
---|
106 | { |
---|
107 | _location = loc; |
---|
108 | } |
---|
109 | |
---|
110 | Vector3 location() const |
---|
111 | { |
---|
112 | return _location; |
---|
113 | } |
---|
114 | |
---|
115 | int isosurface() const |
---|
116 | { |
---|
117 | return _isosurface; |
---|
118 | } |
---|
119 | |
---|
120 | void isosurface(int iso) |
---|
121 | { |
---|
122 | _isosurface = iso; |
---|
123 | } |
---|
124 | |
---|
125 | int numComponents() const |
---|
126 | { |
---|
127 | return _numComponents; |
---|
128 | } |
---|
129 | |
---|
130 | double nonZeroMin() const |
---|
131 | { |
---|
132 | return _nonZeroMin; |
---|
133 | } |
---|
134 | |
---|
135 | int volumeType() const |
---|
136 | { |
---|
137 | return _volumeType; |
---|
138 | } |
---|
139 | |
---|
140 | const float *data() const |
---|
141 | { |
---|
142 | return _data; |
---|
143 | } |
---|
144 | |
---|
145 | const Texture3D *tex() const |
---|
146 | { |
---|
147 | return _tex; |
---|
148 | } |
---|
149 | |
---|
150 | int numSlices() const |
---|
151 | { |
---|
152 | return _numSlices; |
---|
153 | } |
---|
154 | |
---|
155 | void numSlices(int n) |
---|
156 | { |
---|
157 | _numSlices = n; |
---|
158 | } |
---|
159 | |
---|
160 | // methods related to cutplanes |
---|
161 | /// add a plane and returns its index |
---|
162 | int addCutplane(int orientation, float location); |
---|
163 | |
---|
164 | void enableCutplane(int index); |
---|
165 | |
---|
166 | void disableCutplane(int index); |
---|
167 | |
---|
168 | void moveCutplane(int index, float location); |
---|
169 | |
---|
170 | CutPlane *getCutplane(int index); |
---|
171 | |
---|
172 | /// returns the number of cutplanes in the volume |
---|
173 | int getCutplaneCount(); |
---|
174 | |
---|
175 | /// check if a cutplane is enabled |
---|
176 | bool isCutplaneEnabled(int index) const; |
---|
177 | |
---|
178 | // methods related to shading. These parameters are per volume |
---|
179 | |
---|
180 | /// Get ambient coefficient |
---|
181 | float ambient() const |
---|
182 | { |
---|
183 | return _ambient; |
---|
184 | } |
---|
185 | |
---|
186 | /// Set ambient coefficient [0,1] |
---|
187 | void ambient(float value) |
---|
188 | { |
---|
189 | if (value < 0.0f) value = 0.0f; |
---|
190 | if (value > 1.0f) value = 1.0f; |
---|
191 | _ambient = value; |
---|
192 | } |
---|
193 | |
---|
194 | /// Get diffuse coefficient |
---|
195 | float diffuse() const |
---|
196 | { |
---|
197 | return _diffuse; |
---|
198 | } |
---|
199 | |
---|
200 | /// Set diffuse coefficient [0,1] |
---|
201 | void diffuse(float value) |
---|
202 | { |
---|
203 | if (value < 0.0f) value = 0.0f; |
---|
204 | if (value > 1.0f) value = 1.0f; |
---|
205 | _diffuse = value; |
---|
206 | } |
---|
207 | |
---|
208 | /// Get specular coefficient |
---|
209 | float specularLevel() const |
---|
210 | { |
---|
211 | return _specular; |
---|
212 | } |
---|
213 | |
---|
214 | /// Set specular coefficient [0,1] |
---|
215 | void specularLevel(float value) |
---|
216 | { |
---|
217 | if (value < 0.0f) value = 0.0f; |
---|
218 | if (value > 1.0f) value = 1.0f; |
---|
219 | _specular = value; |
---|
220 | } |
---|
221 | |
---|
222 | /// Get specular exponent |
---|
223 | float specularExponent() const |
---|
224 | { |
---|
225 | return _specularExp; |
---|
226 | } |
---|
227 | |
---|
228 | /// Set specular exponent [0,128] |
---|
229 | void specularExponent(float value) |
---|
230 | { |
---|
231 | if (value < 0.0f) value = 0.0f; |
---|
232 | if (value > 128.0f) value = 128.0f; |
---|
233 | _specularExp = value; |
---|
234 | } |
---|
235 | |
---|
236 | bool twoSidedLighting() const |
---|
237 | { |
---|
238 | return _lightTwoSide; |
---|
239 | } |
---|
240 | |
---|
241 | void twoSidedLighting(bool value) |
---|
242 | { |
---|
243 | _lightTwoSide = value; |
---|
244 | } |
---|
245 | |
---|
246 | float opacityScale() const |
---|
247 | { |
---|
248 | return _opacityScale; |
---|
249 | } |
---|
250 | |
---|
251 | void opacityScale(float value) |
---|
252 | { |
---|
253 | _opacityScale = value; |
---|
254 | } |
---|
255 | |
---|
256 | void dataEnabled(bool value) |
---|
257 | { |
---|
258 | _dataEnabled = value; |
---|
259 | } |
---|
260 | |
---|
261 | bool dataEnabled() const |
---|
262 | { |
---|
263 | return _dataEnabled; |
---|
264 | } |
---|
265 | |
---|
266 | void outline(bool value) |
---|
267 | { |
---|
268 | _outlineEnabled = value; |
---|
269 | } |
---|
270 | |
---|
271 | bool outline() |
---|
272 | { |
---|
273 | return _outlineEnabled; |
---|
274 | } |
---|
275 | |
---|
276 | TransferFunction *transferFunction() |
---|
277 | { |
---|
278 | return _tfPtr; |
---|
279 | } |
---|
280 | |
---|
281 | void transferFunction(TransferFunction *tfPtr) |
---|
282 | { |
---|
283 | _tfPtr = tfPtr; |
---|
284 | } |
---|
285 | |
---|
286 | void setOutlineColor(float *rgb); |
---|
287 | |
---|
288 | void getOutlineColor(float *rgb); |
---|
289 | |
---|
290 | Vector3 getPhysicalScaling() const |
---|
291 | { |
---|
292 | Vector3 scale; |
---|
293 | scale.x = 1; |
---|
294 | scale.y = yAxis.length() / xAxis.length(); |
---|
295 | scale.z = zAxis.length() / xAxis.length(); |
---|
296 | return scale; |
---|
297 | } |
---|
298 | |
---|
299 | double sampleDistanceX() const |
---|
300 | { |
---|
301 | return (xAxis.length() / ((double)_width-1.0)); |
---|
302 | } |
---|
303 | |
---|
304 | double sampleDistanceY() const |
---|
305 | { |
---|
306 | return (yAxis.length() / ((double)_height-1.0)); |
---|
307 | } |
---|
308 | |
---|
309 | double sampleDistanceZ() const |
---|
310 | { |
---|
311 | if (_depth == 1) |
---|
312 | return sampleDistanceX(); |
---|
313 | return (zAxis.length() / ((double)_depth-1.0)); |
---|
314 | } |
---|
315 | |
---|
316 | const char *name() const |
---|
317 | { |
---|
318 | return _name; |
---|
319 | } |
---|
320 | |
---|
321 | void name(const char *name) |
---|
322 | { |
---|
323 | _name = name; |
---|
324 | } |
---|
325 | |
---|
326 | GLuint textureID() const |
---|
327 | { |
---|
328 | return _id; |
---|
329 | } |
---|
330 | |
---|
331 | AxisRange xAxis, yAxis, zAxis, wAxis; |
---|
332 | |
---|
333 | static bool updatePending; |
---|
334 | static double valueMin, valueMax; |
---|
335 | |
---|
336 | friend class VolumeInterpolator; |
---|
337 | |
---|
338 | protected: |
---|
339 | float *data() |
---|
340 | { |
---|
341 | return _data; |
---|
342 | } |
---|
343 | |
---|
344 | Texture3D *tex() |
---|
345 | { |
---|
346 | return _tex; |
---|
347 | } |
---|
348 | |
---|
349 | GLuint _id; ///< OpenGL textue identifier (==_tex->id) |
---|
350 | |
---|
351 | // Width, height and depth are point resolution, NOT physical |
---|
352 | // units |
---|
353 | /// The resolution of the data (how many points in X direction) |
---|
354 | int _width; |
---|
355 | /// The resolution of the data (how many points in Y direction) |
---|
356 | int _height; |
---|
357 | /// The resolution of the data (how many points in Z direction) |
---|
358 | int _depth; |
---|
359 | |
---|
360 | /** |
---|
361 | * This is the designated transfer function to use to |
---|
362 | * render this volume. |
---|
363 | */ |
---|
364 | TransferFunction *_tfPtr; |
---|
365 | |
---|
366 | float _ambient; ///< Ambient material coefficient |
---|
367 | float _diffuse; ///< Diffuse material coefficient |
---|
368 | float _specular; ///< Specular level material coefficient |
---|
369 | float _specularExp; ///< Specular exponent |
---|
370 | bool _lightTwoSide; ///< Two-sided lighting flag |
---|
371 | |
---|
372 | /** |
---|
373 | * The scale multiplied to the opacity assigned by the |
---|
374 | * transfer function. Rule of thumb: higher opacity_scale |
---|
375 | * the object is to appear like plastic |
---|
376 | */ |
---|
377 | float _opacityScale; |
---|
378 | |
---|
379 | const char *_name; |
---|
380 | float *_data; |
---|
381 | |
---|
382 | int _numComponents; |
---|
383 | |
---|
384 | double _nonZeroMin; |
---|
385 | |
---|
386 | std::vector<CutPlane> _plane; ///< cut planes |
---|
387 | |
---|
388 | Texture3D *_tex; ///< OpenGL texture storing the volume |
---|
389 | |
---|
390 | Vector3 _location; |
---|
391 | |
---|
392 | /** |
---|
393 | * Number of slices when rendered. The greater |
---|
394 | * the better quality, lower speed. |
---|
395 | */ |
---|
396 | int _numSlices; |
---|
397 | bool _enabled; |
---|
398 | bool _dataEnabled; ///< show/hide cloud of volume data |
---|
399 | bool _outlineEnabled; ///< show/hide outline around volume |
---|
400 | Color _outlineColor; ///< color for outline around volume |
---|
401 | int _volumeType; ///< cubic or zincblende |
---|
402 | int _isosurface; |
---|
403 | }; |
---|
404 | |
---|
405 | inline int |
---|
406 | Volume::addCutplane(int orientation, float location) |
---|
407 | { |
---|
408 | _plane.push_back(CutPlane(orientation, location)); |
---|
409 | return _plane.size() - 1; |
---|
410 | } |
---|
411 | |
---|
412 | inline void |
---|
413 | Volume::enableCutplane(int index) |
---|
414 | { |
---|
415 | //assert(index < plane.size()); |
---|
416 | _plane[index].enabled = true; |
---|
417 | } |
---|
418 | |
---|
419 | inline void |
---|
420 | Volume::disableCutplane(int index) |
---|
421 | { |
---|
422 | //assert(index < plane.size()); |
---|
423 | _plane[index].enabled = false; |
---|
424 | } |
---|
425 | |
---|
426 | inline void |
---|
427 | Volume::moveCutplane(int index, float location) |
---|
428 | { |
---|
429 | //assert(index < plane.size()); |
---|
430 | _plane[index].offset = location; |
---|
431 | } |
---|
432 | |
---|
433 | inline CutPlane * |
---|
434 | Volume::getCutplane(int index) |
---|
435 | { |
---|
436 | //assert(index < plane.size()); |
---|
437 | return &_plane[index]; |
---|
438 | } |
---|
439 | |
---|
440 | inline int |
---|
441 | Volume::getCutplaneCount() |
---|
442 | { |
---|
443 | return _plane.size(); |
---|
444 | } |
---|
445 | |
---|
446 | inline bool |
---|
447 | Volume::isCutplaneEnabled(int index) const |
---|
448 | { |
---|
449 | //assert(index < plane.size()); |
---|
450 | return _plane[index].enabled; |
---|
451 | } |
---|
452 | |
---|
453 | inline void |
---|
454 | Volume::setOutlineColor(float *rgb) |
---|
455 | { |
---|
456 | _outlineColor = Color(rgb[0], rgb[1], rgb[2]); |
---|
457 | } |
---|
458 | |
---|
459 | inline void |
---|
460 | Volume::getOutlineColor(float *rgb) |
---|
461 | { |
---|
462 | _outlineColor.getRGB(rgb); |
---|
463 | } |
---|
464 | |
---|
465 | #endif |
---|