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