1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: |
---|
6 | * Wei Qiao <qiaow@purdue.edu> |
---|
7 | */ |
---|
8 | #ifndef NV_VOLUME_H |
---|
9 | #define NV_VOLUME_H |
---|
10 | |
---|
11 | #include <cstring> |
---|
12 | #include <string> |
---|
13 | #include <vector> |
---|
14 | |
---|
15 | #include <vrmath/Vector3f.h> |
---|
16 | |
---|
17 | #include "Texture3D.h" |
---|
18 | #include "AxisRange.h" |
---|
19 | #include "TransferFunction.h" |
---|
20 | |
---|
21 | namespace nv { |
---|
22 | |
---|
23 | class CutPlane { |
---|
24 | public: |
---|
25 | enum Axis { |
---|
26 | X_AXIS = 1, |
---|
27 | Y_AXIS = 2, |
---|
28 | Z_AXIS = 3 |
---|
29 | }; |
---|
30 | |
---|
31 | CutPlane(Axis _orient, float _offset) : |
---|
32 | orient(_orient), |
---|
33 | offset(_offset), |
---|
34 | enabled(true) |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | Axis orient; |
---|
39 | float offset; ///< normalized offset [0,1] in the volume |
---|
40 | bool enabled; |
---|
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 | int isosurface() const |
---|
107 | { |
---|
108 | return _isosurface; |
---|
109 | } |
---|
110 | |
---|
111 | void isosurface(int iso) |
---|
112 | { |
---|
113 | _isosurface = iso; |
---|
114 | } |
---|
115 | |
---|
116 | int numComponents() const |
---|
117 | { |
---|
118 | return _numComponents; |
---|
119 | } |
---|
120 | |
---|
121 | double nonZeroMin() const |
---|
122 | { |
---|
123 | return _nonZeroMin; |
---|
124 | } |
---|
125 | |
---|
126 | int volumeType() const |
---|
127 | { |
---|
128 | return _volumeType; |
---|
129 | } |
---|
130 | |
---|
131 | const float *data() const |
---|
132 | { |
---|
133 | return _data; |
---|
134 | } |
---|
135 | |
---|
136 | const Texture3D *tex() const |
---|
137 | { |
---|
138 | return _tex; |
---|
139 | } |
---|
140 | |
---|
141 | int numSlices() const |
---|
142 | { |
---|
143 | return _numSlices; |
---|
144 | } |
---|
145 | |
---|
146 | void numSlices(int n) |
---|
147 | { |
---|
148 | _numSlices = n; |
---|
149 | } |
---|
150 | |
---|
151 | // methods related to cutplanes |
---|
152 | /// add a plane and returns its index |
---|
153 | int addCutplane(CutPlane::Axis orientation, float location); |
---|
154 | |
---|
155 | void enableCutplane(int index); |
---|
156 | |
---|
157 | void disableCutplane(int index); |
---|
158 | |
---|
159 | void cutplanesVisible(bool state) |
---|
160 | { |
---|
161 | _cutplanesVisible = state; |
---|
162 | } |
---|
163 | |
---|
164 | bool cutplanesVisible() const |
---|
165 | { |
---|
166 | return _cutplanesVisible; |
---|
167 | } |
---|
168 | |
---|
169 | void setCutplanePosition(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 | void setPosition(const vrmath::Vector3f& pos) |
---|
292 | { |
---|
293 | _position = pos; |
---|
294 | } |
---|
295 | |
---|
296 | const vrmath::Vector3f& getPosition() const |
---|
297 | { |
---|
298 | return _position; |
---|
299 | } |
---|
300 | |
---|
301 | vrmath::Vector3f getPhysicalScaling() const |
---|
302 | { |
---|
303 | vrmath::Vector3f scale; |
---|
304 | scale.x = 1; |
---|
305 | scale.y = yAxis.length() / xAxis.length(); |
---|
306 | scale.z = zAxis.length() / xAxis.length(); |
---|
307 | return scale; |
---|
308 | } |
---|
309 | |
---|
310 | void setScale(const vrmath::Vector3f& scale) |
---|
311 | { |
---|
312 | _scale = scale; |
---|
313 | } |
---|
314 | |
---|
315 | const vrmath::Vector3f& getScale() const |
---|
316 | { |
---|
317 | return _scale; |
---|
318 | } |
---|
319 | |
---|
320 | void getWorldSpaceBounds(vrmath::Vector3f& bboxMin, |
---|
321 | vrmath::Vector3f& bboxMax) const; |
---|
322 | |
---|
323 | double sampleDistanceX() const |
---|
324 | { |
---|
325 | return (xAxis.length() / ((double)_width-1.0)); |
---|
326 | } |
---|
327 | |
---|
328 | double sampleDistanceY() const |
---|
329 | { |
---|
330 | return (yAxis.length() / ((double)_height-1.0)); |
---|
331 | } |
---|
332 | |
---|
333 | double sampleDistanceZ() const |
---|
334 | { |
---|
335 | if (_depth == 1) |
---|
336 | return sampleDistanceX(); |
---|
337 | return (zAxis.length() / ((double)_depth-1.0)); |
---|
338 | } |
---|
339 | |
---|
340 | const char *name() const |
---|
341 | { |
---|
342 | return _name.c_str(); |
---|
343 | } |
---|
344 | |
---|
345 | void name(const char *name) |
---|
346 | { |
---|
347 | _name = name; |
---|
348 | } |
---|
349 | |
---|
350 | GLuint textureID() const |
---|
351 | { |
---|
352 | return _id; |
---|
353 | } |
---|
354 | |
---|
355 | AxisRange xAxis, yAxis, zAxis, wAxis; |
---|
356 | |
---|
357 | static bool updatePending; |
---|
358 | static double valueMin, valueMax; |
---|
359 | |
---|
360 | friend class VolumeInterpolator; |
---|
361 | |
---|
362 | protected: |
---|
363 | float *data() |
---|
364 | { |
---|
365 | return _data; |
---|
366 | } |
---|
367 | |
---|
368 | Texture3D *tex() |
---|
369 | { |
---|
370 | return _tex; |
---|
371 | } |
---|
372 | |
---|
373 | GLuint _id; ///< OpenGL textue identifier (==_tex->id) |
---|
374 | |
---|
375 | // Width, height and depth are point resolution, NOT physical |
---|
376 | // units |
---|
377 | /// The resolution of the data (how many points in X direction) |
---|
378 | int _width; |
---|
379 | /// The resolution of the data (how many points in Y direction) |
---|
380 | int _height; |
---|
381 | /// The resolution of the data (how many points in Z direction) |
---|
382 | int _depth; |
---|
383 | |
---|
384 | /** |
---|
385 | * This is the designated transfer function to use to |
---|
386 | * render this volume. |
---|
387 | */ |
---|
388 | TransferFunction *_transferFunc; |
---|
389 | |
---|
390 | float _ambient; ///< Ambient material coefficient |
---|
391 | float _diffuse; ///< Diffuse material coefficient |
---|
392 | float _specular; ///< Specular level material coefficient |
---|
393 | float _specularExp; ///< Specular exponent |
---|
394 | bool _lightTwoSide; ///< Two-sided lighting flag |
---|
395 | |
---|
396 | /** |
---|
397 | * The scale multiplied to the opacity assigned by the |
---|
398 | * transfer function. |
---|
399 | */ |
---|
400 | float _opacityScale; |
---|
401 | |
---|
402 | std::string _name; |
---|
403 | |
---|
404 | float *_data; |
---|
405 | |
---|
406 | int _numComponents; |
---|
407 | |
---|
408 | double _nonZeroMin; |
---|
409 | |
---|
410 | bool _cutplanesVisible; |
---|
411 | std::vector<CutPlane> _plane; ///< cut planes |
---|
412 | |
---|
413 | Texture3D *_tex; ///< OpenGL texture storing the volume |
---|
414 | |
---|
415 | vrmath::Vector3f _position; |
---|
416 | vrmath::Vector3f _scale; |
---|
417 | |
---|
418 | /** |
---|
419 | * Number of slices when rendered. The greater |
---|
420 | * the better quality, lower speed. |
---|
421 | */ |
---|
422 | int _numSlices; |
---|
423 | bool _enabled; |
---|
424 | bool _dataEnabled; ///< show/hide cloud of volume data |
---|
425 | bool _outlineEnabled; ///< show/hide outline around volume |
---|
426 | float _outlineColor[3]; ///< color for outline around volume |
---|
427 | int _volumeType; ///< cubic or zincblende |
---|
428 | int _isosurface; |
---|
429 | }; |
---|
430 | |
---|
431 | inline int |
---|
432 | Volume::addCutplane(CutPlane::Axis orientation, float location) |
---|
433 | { |
---|
434 | _plane.push_back(CutPlane(orientation, location)); |
---|
435 | return _plane.size() - 1; |
---|
436 | } |
---|
437 | |
---|
438 | inline void |
---|
439 | Volume::enableCutplane(int index) |
---|
440 | { |
---|
441 | //assert(index < plane.size()); |
---|
442 | _plane[index].enabled = true; |
---|
443 | } |
---|
444 | |
---|
445 | inline void |
---|
446 | Volume::disableCutplane(int index) |
---|
447 | { |
---|
448 | //assert(index < plane.size()); |
---|
449 | _plane[index].enabled = false; |
---|
450 | } |
---|
451 | |
---|
452 | inline void |
---|
453 | Volume::setCutplanePosition(int index, float position) |
---|
454 | { |
---|
455 | //assert(index < plane.size()); |
---|
456 | _plane[index].offset = position; |
---|
457 | } |
---|
458 | |
---|
459 | inline CutPlane * |
---|
460 | Volume::getCutplane(int index) |
---|
461 | { |
---|
462 | //assert(index < plane.size()); |
---|
463 | return &_plane[index]; |
---|
464 | } |
---|
465 | |
---|
466 | inline int |
---|
467 | Volume::getCutplaneCount() |
---|
468 | { |
---|
469 | return _plane.size(); |
---|
470 | } |
---|
471 | |
---|
472 | inline bool |
---|
473 | Volume::isCutplaneEnabled(int index) const |
---|
474 | { |
---|
475 | //assert(index < plane.size()); |
---|
476 | return _plane[index].enabled; |
---|
477 | } |
---|
478 | |
---|
479 | inline void |
---|
480 | Volume::setOutlineColor(float *rgb) |
---|
481 | { |
---|
482 | memcpy(_outlineColor, rgb, sizeof(float)*3); |
---|
483 | } |
---|
484 | |
---|
485 | inline void |
---|
486 | Volume::getOutlineColor(float *rgb) |
---|
487 | { |
---|
488 | memcpy(rgb, _outlineColor, sizeof(float)*3); |
---|
489 | } |
---|
490 | |
---|
491 | } |
---|
492 | |
---|
493 | #endif |
---|