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