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