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