[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[835] | 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 | * |
---|
[3177] | 10 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[835] | 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 | */ |
---|
[2844] | 16 | #ifndef VOLUME_H |
---|
| 17 | #define VOLUME_H |
---|
[835] | 18 | |
---|
[3492] | 19 | #include <cstring> |
---|
[835] | 20 | #include <string> |
---|
| 21 | #include <vector> |
---|
| 22 | |
---|
[3492] | 23 | #include <vrmath/Vector3f.h> |
---|
| 24 | |
---|
[835] | 25 | #include "Texture3D.h" |
---|
[929] | 26 | #include "AxisRange.h" |
---|
[1478] | 27 | #include "TransferFunction.h" |
---|
[835] | 28 | |
---|
[2816] | 29 | struct CutPlane { |
---|
[2844] | 30 | /// orientation - 1: xy slice, 2: yz slice, 3: xz slice |
---|
| 31 | int orient; |
---|
| 32 | float offset; ///< normalized offset [0,1] in the volume |
---|
[835] | 33 | bool enabled; |
---|
| 34 | |
---|
[2816] | 35 | CutPlane(int _orient, float _offset) : |
---|
| 36 | orient(_orient), |
---|
| 37 | offset(_offset), |
---|
[2844] | 38 | enabled(true) |
---|
[2816] | 39 | { |
---|
[835] | 40 | } |
---|
| 41 | }; |
---|
| 42 | |
---|
[1478] | 43 | class VolumeInterpolator; |
---|
[835] | 44 | |
---|
[2816] | 45 | class Volume |
---|
| 46 | { |
---|
[1478] | 47 | public: |
---|
[2844] | 48 | enum VolumeType { |
---|
| 49 | CUBIC, |
---|
| 50 | VOLQD, |
---|
| 51 | ZINCBLENDE |
---|
| 52 | }; |
---|
[835] | 53 | |
---|
[2932] | 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 | */ |
---|
[2844] | 72 | Volume(float x, float y, float z, |
---|
| 73 | int width, int height, int depth, |
---|
[3362] | 74 | int numComponents, |
---|
[2844] | 75 | float *data, |
---|
| 76 | double vmin, double vmax, |
---|
[2877] | 77 | double nonZeroMin); |
---|
[835] | 78 | |
---|
[2288] | 79 | virtual ~Volume(); |
---|
[2816] | 80 | |
---|
[3362] | 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 | |
---|
[2844] | 96 | void visible(bool value) |
---|
| 97 | { |
---|
| 98 | _enabled = value; |
---|
[1474] | 99 | } |
---|
[2844] | 100 | |
---|
| 101 | bool visible() const |
---|
| 102 | { |
---|
| 103 | return _enabled; |
---|
[1474] | 104 | } |
---|
[2844] | 105 | |
---|
[3492] | 106 | void location(const vrmath::Vector3f& loc) |
---|
[2844] | 107 | { |
---|
| 108 | _location = loc; |
---|
[1474] | 109 | } |
---|
[2844] | 110 | |
---|
[3492] | 111 | vrmath::Vector3f location() const |
---|
[2844] | 112 | { |
---|
| 113 | return _location; |
---|
[1478] | 114 | } |
---|
[2844] | 115 | |
---|
| 116 | int isosurface() const |
---|
| 117 | { |
---|
[2877] | 118 | return _isosurface; |
---|
[1478] | 119 | } |
---|
[2844] | 120 | |
---|
| 121 | void isosurface(int iso) |
---|
| 122 | { |
---|
[2877] | 123 | _isosurface = iso; |
---|
[1478] | 124 | } |
---|
[2844] | 125 | |
---|
[2877] | 126 | int numComponents() const |
---|
[2844] | 127 | { |
---|
[2877] | 128 | return _numComponents; |
---|
[1478] | 129 | } |
---|
[2844] | 130 | |
---|
[2877] | 131 | double nonZeroMin() const |
---|
[2844] | 132 | { |
---|
[2877] | 133 | return _nonZeroMin; |
---|
[1478] | 134 | } |
---|
[2844] | 135 | |
---|
[2877] | 136 | int volumeType() const |
---|
[2844] | 137 | { |
---|
[2877] | 138 | return _volumeType; |
---|
[1478] | 139 | } |
---|
[2844] | 140 | |
---|
[3362] | 141 | const float *data() const |
---|
[2844] | 142 | { |
---|
| 143 | return _data; |
---|
[1478] | 144 | } |
---|
[1474] | 145 | |
---|
[3362] | 146 | const Texture3D *tex() const |
---|
[2844] | 147 | { |
---|
| 148 | return _tex; |
---|
| 149 | } |
---|
[3362] | 150 | |
---|
[2877] | 151 | int numSlices() const |
---|
[2844] | 152 | { |
---|
[2877] | 153 | return _numSlices; |
---|
[1478] | 154 | } |
---|
[2844] | 155 | |
---|
[2877] | 156 | void numSlices(int n) |
---|
[2844] | 157 | { |
---|
[2877] | 158 | _numSlices = n; |
---|
[1478] | 159 | } |
---|
[870] | 160 | |
---|
[2844] | 161 | // methods related to cutplanes |
---|
| 162 | /// add a plane and returns its index |
---|
[2877] | 163 | int addCutplane(int orientation, float location); |
---|
[2844] | 164 | |
---|
[2877] | 165 | void enableCutplane(int index); |
---|
[2844] | 166 | |
---|
[2877] | 167 | void disableCutplane(int index); |
---|
[835] | 168 | |
---|
[2877] | 169 | void moveCutplane(int index, float location); |
---|
[2844] | 170 | |
---|
[2877] | 171 | CutPlane *getCutplane(int index); |
---|
[2844] | 172 | |
---|
| 173 | /// returns the number of cutplanes in the volume |
---|
[2877] | 174 | int getCutplaneCount(); |
---|
[2844] | 175 | |
---|
| 176 | /// check if a cutplane is enabled |
---|
[2877] | 177 | bool isCutplaneEnabled(int index) const; |
---|
[2844] | 178 | |
---|
| 179 | // methods related to shading. These parameters are per volume |
---|
[2877] | 180 | |
---|
[3362] | 181 | /// Get ambient coefficient |
---|
| 182 | float ambient() const |
---|
[2844] | 183 | { |
---|
[3362] | 184 | return _ambient; |
---|
[1474] | 185 | } |
---|
[2844] | 186 | |
---|
[3362] | 187 | /// Set ambient coefficient [0,1] |
---|
| 188 | void ambient(float value) |
---|
[2844] | 189 | { |
---|
[2877] | 190 | if (value < 0.0f) value = 0.0f; |
---|
[3362] | 191 | if (value > 1.0f) value = 1.0f; |
---|
| 192 | _ambient = value; |
---|
[1474] | 193 | } |
---|
[2844] | 194 | |
---|
[2877] | 195 | /// Get diffuse coefficient |
---|
[2844] | 196 | float diffuse() const |
---|
| 197 | { |
---|
| 198 | return _diffuse; |
---|
[1474] | 199 | } |
---|
[2844] | 200 | |
---|
[2877] | 201 | /// Set diffuse coefficient [0,1] |
---|
[2844] | 202 | void diffuse(float value) |
---|
| 203 | { |
---|
[2877] | 204 | if (value < 0.0f) value = 0.0f; |
---|
| 205 | if (value > 1.0f) value = 1.0f; |
---|
[2844] | 206 | _diffuse = value; |
---|
[1474] | 207 | } |
---|
[2844] | 208 | |
---|
[3362] | 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 | |
---|
[2877] | 247 | float opacityScale() const |
---|
[2844] | 248 | { |
---|
[2877] | 249 | return _opacityScale; |
---|
[1474] | 250 | } |
---|
[2844] | 251 | |
---|
[2877] | 252 | void opacityScale(float value) |
---|
[2844] | 253 | { |
---|
[2877] | 254 | _opacityScale = value; |
---|
[1474] | 255 | } |
---|
[2844] | 256 | |
---|
[2877] | 257 | void dataEnabled(bool value) |
---|
[2844] | 258 | { |
---|
[2877] | 259 | _dataEnabled = value; |
---|
[1474] | 260 | } |
---|
[2844] | 261 | |
---|
[2877] | 262 | bool dataEnabled() const |
---|
[2844] | 263 | { |
---|
[2877] | 264 | return _dataEnabled; |
---|
[1474] | 265 | } |
---|
[2844] | 266 | |
---|
| 267 | void outline(bool value) |
---|
| 268 | { |
---|
[2877] | 269 | _outlineEnabled = value; |
---|
[1474] | 270 | } |
---|
[2844] | 271 | |
---|
| 272 | bool outline() |
---|
| 273 | { |
---|
[2877] | 274 | return _outlineEnabled; |
---|
[1474] | 275 | } |
---|
[2844] | 276 | |
---|
| 277 | TransferFunction *transferFunction() |
---|
| 278 | { |
---|
| 279 | return _tfPtr; |
---|
[1478] | 280 | } |
---|
[2844] | 281 | |
---|
| 282 | void transferFunction(TransferFunction *tfPtr) |
---|
| 283 | { |
---|
| 284 | _tfPtr = tfPtr; |
---|
[1478] | 285 | } |
---|
[2844] | 286 | |
---|
[2877] | 287 | void setOutlineColor(float *rgb); |
---|
[2844] | 288 | |
---|
[2877] | 289 | void getOutlineColor(float *rgb); |
---|
| 290 | |
---|
[3492] | 291 | vrmath::Vector3f getPhysicalScaling() const |
---|
[3362] | 292 | { |
---|
[3492] | 293 | vrmath::Vector3f scale; |
---|
[3362] | 294 | scale.x = 1; |
---|
| 295 | scale.y = yAxis.length() / xAxis.length(); |
---|
| 296 | scale.z = zAxis.length() / xAxis.length(); |
---|
| 297 | return scale; |
---|
| 298 | } |
---|
[1475] | 299 | |
---|
[3492] | 300 | void getWorldSpaceBounds(vrmath::Vector3f& bboxMin, |
---|
| 301 | vrmath::Vector3f& bboxMax) const; |
---|
| 302 | |
---|
[3362] | 303 | double sampleDistanceX() const |
---|
| 304 | { |
---|
| 305 | return (xAxis.length() / ((double)_width-1.0)); |
---|
| 306 | } |
---|
[2844] | 307 | |
---|
[3362] | 308 | double sampleDistanceY() const |
---|
| 309 | { |
---|
| 310 | return (yAxis.length() / ((double)_height-1.0)); |
---|
| 311 | } |
---|
[2844] | 312 | |
---|
[3362] | 313 | double sampleDistanceZ() const |
---|
| 314 | { |
---|
| 315 | if (_depth == 1) |
---|
| 316 | return sampleDistanceX(); |
---|
| 317 | return (zAxis.length() / ((double)_depth-1.0)); |
---|
| 318 | } |
---|
| 319 | |
---|
[2844] | 320 | const char *name() const |
---|
| 321 | { |
---|
| 322 | return _name; |
---|
[1478] | 323 | } |
---|
[2844] | 324 | |
---|
| 325 | void name(const char *name) |
---|
| 326 | { |
---|
| 327 | _name = name; |
---|
[1478] | 328 | } |
---|
[2844] | 329 | |
---|
[3362] | 330 | GLuint textureID() const |
---|
| 331 | { |
---|
| 332 | return _id; |
---|
| 333 | } |
---|
[2844] | 334 | |
---|
[3362] | 335 | AxisRange xAxis, yAxis, zAxis, wAxis; |
---|
[2844] | 336 | |
---|
[3362] | 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 | |
---|
[2844] | 355 | // Width, height and depth are point resolution, NOT physical |
---|
| 356 | // units |
---|
| 357 | /// The resolution of the data (how many points in X direction) |
---|
[3362] | 358 | int _width; |
---|
[2844] | 359 | /// The resolution of the data (how many points in Y direction) |
---|
[3362] | 360 | int _height; |
---|
[2844] | 361 | /// The resolution of the data (how many points in Z direction) |
---|
[3362] | 362 | int _depth; |
---|
[2844] | 363 | |
---|
| 364 | /** |
---|
| 365 | * This is the designated transfer function to use to |
---|
| 366 | * render this volume. |
---|
| 367 | */ |
---|
| 368 | TransferFunction *_tfPtr; |
---|
| 369 | |
---|
[3362] | 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 | |
---|
[2844] | 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 | */ |
---|
[2877] | 381 | float _opacityScale; |
---|
[2844] | 382 | |
---|
| 383 | const char *_name; |
---|
| 384 | float *_data; |
---|
| 385 | |
---|
[2877] | 386 | int _numComponents; |
---|
[2844] | 387 | |
---|
[2877] | 388 | double _nonZeroMin; |
---|
[2844] | 389 | |
---|
| 390 | std::vector<CutPlane> _plane; ///< cut planes |
---|
| 391 | |
---|
| 392 | Texture3D *_tex; ///< OpenGL texture storing the volume |
---|
| 393 | |
---|
[3492] | 394 | vrmath::Vector3f _location; |
---|
[2844] | 395 | |
---|
| 396 | /** |
---|
| 397 | * Number of slices when rendered. The greater |
---|
| 398 | * the better quality, lower speed. |
---|
| 399 | */ |
---|
[2877] | 400 | int _numSlices; |
---|
[2844] | 401 | bool _enabled; |
---|
[2877] | 402 | bool _dataEnabled; ///< show/hide cloud of volume data |
---|
| 403 | bool _outlineEnabled; ///< show/hide outline around volume |
---|
[3492] | 404 | float _outlineColor[3]; ///< color for outline around volume |
---|
[2877] | 405 | int _volumeType; ///< cubic or zincblende |
---|
| 406 | int _isosurface; |
---|
[835] | 407 | }; |
---|
| 408 | |
---|
[2816] | 409 | inline int |
---|
[2877] | 410 | Volume::addCutplane(int orientation, float location) |
---|
[2816] | 411 | { |
---|
| 412 | _plane.push_back(CutPlane(orientation, location)); |
---|
[1478] | 413 | return _plane.size() - 1; |
---|
[849] | 414 | } |
---|
| 415 | |
---|
[927] | 416 | inline void |
---|
[2877] | 417 | Volume::enableCutplane(int index) |
---|
[927] | 418 | { |
---|
[849] | 419 | //assert(index < plane.size()); |
---|
[1478] | 420 | _plane[index].enabled = true; |
---|
[849] | 421 | } |
---|
[3362] | 422 | |
---|
[927] | 423 | inline void |
---|
[2877] | 424 | Volume::disableCutplane(int index) |
---|
[927] | 425 | { |
---|
[849] | 426 | //assert(index < plane.size()); |
---|
[1478] | 427 | _plane[index].enabled = false; |
---|
[849] | 428 | } |
---|
| 429 | |
---|
[927] | 430 | inline void |
---|
[2877] | 431 | Volume::moveCutplane(int index, float location) |
---|
[927] | 432 | { |
---|
[849] | 433 | //assert(index < plane.size()); |
---|
[1478] | 434 | _plane[index].offset = location; |
---|
[849] | 435 | } |
---|
| 436 | |
---|
[2844] | 437 | inline CutPlane * |
---|
[2877] | 438 | Volume::getCutplane(int index) |
---|
[927] | 439 | { |
---|
[849] | 440 | //assert(index < plane.size()); |
---|
[1478] | 441 | return &_plane[index]; |
---|
[849] | 442 | } |
---|
| 443 | |
---|
[927] | 444 | inline int |
---|
[2877] | 445 | Volume::getCutplaneCount() |
---|
[927] | 446 | { |
---|
[1478] | 447 | return _plane.size(); |
---|
[849] | 448 | } |
---|
| 449 | |
---|
[927] | 450 | inline bool |
---|
[2877] | 451 | Volume::isCutplaneEnabled(int index) const |
---|
[927] | 452 | { |
---|
[849] | 453 | //assert(index < plane.size()); |
---|
[1478] | 454 | return _plane[index].enabled; |
---|
[849] | 455 | } |
---|
| 456 | |
---|
[927] | 457 | inline void |
---|
[2877] | 458 | Volume::setOutlineColor(float *rgb) |
---|
[927] | 459 | { |
---|
[3492] | 460 | memcpy(_outlineColor, rgb, sizeof(float)*3); |
---|
[849] | 461 | } |
---|
| 462 | |
---|
[927] | 463 | inline void |
---|
[2877] | 464 | Volume::getOutlineColor(float *rgb) |
---|
[927] | 465 | { |
---|
[3492] | 466 | memcpy(rgb, _outlineColor, sizeof(float)*3); |
---|
[849] | 467 | } |
---|
| 468 | |
---|
[835] | 469 | #endif |
---|