source: nanovis/tags/1.1.3/Volume.h @ 4988

Last change on this file since 4988 was 4818, checked in by ldelgass, 9 years ago

Merge in 'cutplane visible' command

  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
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 *
10 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
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 */
16#ifndef VOLUME_H
17#define VOLUME_H
18
19#include <cstring>
20#include <string>
21#include <vector>
22
23#include <vrmath/Vector3f.h>
24
25#include "Texture3D.h"
26#include "AxisRange.h"
27#include "TransferFunction.h"
28
29struct CutPlane {
30    /// orientation - 1: xy slice, 2: yz slice, 3: xz slice
31    int orient;
32    float offset;       ///< normalized offset [0,1] in the volume
33    bool enabled;
34
35    CutPlane(int _orient, float _offset) :
36        orient(_orient),
37        offset(_offset),
38        enabled(true)
39    {
40    }
41};
42
43class VolumeInterpolator;
44
45class Volume
46{
47public:
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    void location(const vrmath::Vector3f& loc)
107    {
108        _location = loc;
109    }
110
111    vrmath::Vector3f location() const
112    {
113        return _location;
114    }
115
116    int isosurface() const
117    {
118        return _isosurface;
119    }
120
121    void isosurface(int iso)
122    {
123        _isosurface = iso;
124    }
125
126    int numComponents() const
127    {
128        return _numComponents;
129    }
130
131    double nonZeroMin() const
132    {
133        return _nonZeroMin;
134    }
135
136    int volumeType() const
137    {
138        return _volumeType;
139    }
140
141    const float *data() const
142    {
143        return _data;
144    }
145
146    const Texture3D *tex() const
147    {
148        return _tex;
149    }
150
151    int numSlices() const
152    {
153        return _numSlices;
154    }
155
156    void numSlices(int n)
157    {
158        _numSlices = n;
159    }
160
161    // methods related to cutplanes
162    /// add a plane and returns its index
163    int addCutplane(int orientation, float location);
164
165    void enableCutplane(int index);
166
167    void disableCutplane(int index);
168
169    void cutplanesVisible(bool state)
170    {
171        _cutplanesVisible = state;
172    }
173
174    bool cutplanesVisible() const
175    {
176        return _cutplanesVisible;
177    }
178
179    void moveCutplane(int index, float location);
180
181    CutPlane *getCutplane(int index);
182
183    /// returns the number of cutplanes in the volume
184    int getCutplaneCount();
185
186    /// check if a cutplane is enabled
187    bool isCutplaneEnabled(int index) const;
188
189    // methods related to shading. These parameters are per volume
190
191    /// Get ambient coefficient
192    float ambient() const
193    {
194        return _ambient;
195    }
196
197    /// Set ambient coefficient [0,1]
198    void ambient(float value)
199    {
200        if (value < 0.0f) value = 0.0f;
201        if (value > 1.0f) value = 1.0f;
202        _ambient = value;
203    }
204
205    /// Get diffuse coefficient
206    float diffuse() const
207    {
208        return _diffuse;
209    }
210
211    /// Set diffuse coefficient [0,1]
212    void diffuse(float value)
213    {
214        if (value < 0.0f) value = 0.0f;
215        if (value > 1.0f) value = 1.0f;
216        _diffuse = value;
217    }
218
219    /// Get specular coefficient
220    float specularLevel() const
221    {
222        return _specular;
223    }
224
225    /// Set specular coefficient [0,1]
226    void specularLevel(float value)
227    {
228        if (value < 0.0f) value = 0.0f;
229        if (value > 1.0f) value = 1.0f;
230        _specular = value;
231    }
232
233    /// Get specular exponent
234    float specularExponent() const
235    {
236        return _specularExp;
237    }
238
239    /// Set specular exponent [0,128]
240    void specularExponent(float value)
241    {
242        if (value < 0.0f) value = 0.0f;
243        if (value > 128.0f) value = 128.0f;
244        _specularExp = value;
245    }
246
247    bool twoSidedLighting() const
248    {
249        return _lightTwoSide;
250    }
251
252    void twoSidedLighting(bool value)
253    {
254        _lightTwoSide = value;
255    }
256
257    float opacityScale() const
258    {
259        return _opacityScale;
260    }
261
262    void opacityScale(float value)
263    {
264        _opacityScale = value;
265    }
266
267    void dataEnabled(bool value)
268    {
269        _dataEnabled = value;
270    }
271
272    bool dataEnabled() const
273    {
274        return _dataEnabled;
275    }
276
277    void outline(bool value)
278    {
279        _outlineEnabled = value;
280    }
281
282    bool outline()
283    {
284        return _outlineEnabled;
285    }
286
287    TransferFunction *transferFunction()
288    {
289        return _transferFunc;
290    }
291
292    void transferFunction(TransferFunction *transferFunc)
293    {
294        _transferFunc = transferFunc;
295    }
296
297    void setOutlineColor(float *rgb);
298
299    void getOutlineColor(float *rgb);
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 getWorldSpaceBounds(vrmath::Vector3f& bboxMin,
311                             vrmath::Vector3f& bboxMax) const;
312 
313    double sampleDistanceX() const
314    {
315        return (xAxis.length() / ((double)_width-1.0));
316    }
317
318    double sampleDistanceY() const
319    {
320        return (yAxis.length() / ((double)_height-1.0));
321    }
322
323    double sampleDistanceZ() const
324    {
325        if (_depth == 1)
326            return sampleDistanceX();
327        return (zAxis.length() / ((double)_depth-1.0));
328    }
329
330    const char *name() const
331    {
332        return _name.c_str();
333    }
334
335    void name(const char *name)
336    {
337        _name = name;
338    }
339
340    GLuint textureID() const
341    {
342        return _id;
343    }
344
345    AxisRange xAxis, yAxis, zAxis, wAxis;
346
347    static bool updatePending;
348    static double valueMin, valueMax;
349
350    friend class VolumeInterpolator;
351
352protected:
353    float *data()
354    {
355        return _data;
356    }
357
358    Texture3D *tex()
359    {
360        return _tex;
361    }
362
363    GLuint _id;         ///< OpenGL textue identifier (==_tex->id)
364
365    // Width, height and depth are point resolution, NOT physical
366    // units
367    /// The resolution of the data (how many points in X direction)
368    int _width;
369    /// The resolution of the data (how many points in Y direction)
370    int _height;
371    /// The resolution of the data (how many points in Z direction)
372    int _depth;
373
374    /**
375     * This is the designated transfer function to use to
376     * render this volume.
377     */
378    TransferFunction *_transferFunc;
379
380    float _ambient;      ///< Ambient material coefficient
381    float _diffuse;      ///< Diffuse material coefficient
382    float _specular;     ///< Specular level material coefficient
383    float _specularExp;  ///< Specular exponent
384    bool _lightTwoSide;  ///< Two-sided lighting flag
385
386    /**
387     * The scale multiplied to the opacity assigned by the
388     * transfer function. Rule of thumb: higher opacity_scale
389     * the object is to appear like plastic
390     */
391    float _opacityScale;
392
393    std::string _name;
394
395    float *_data;
396
397    int _numComponents;
398
399    double _nonZeroMin;
400
401    bool _cutplanesVisible;
402    std::vector<CutPlane> _plane; ///< cut planes
403
404    Texture3D *_tex;            ///< OpenGL texture storing the volume
405
406    vrmath::Vector3f _location;
407
408    /**
409     * Number of slices when rendered. The greater
410     * the better quality, lower speed.
411     */
412    int _numSlices;
413    bool _enabled;
414    bool _dataEnabled;          ///< show/hide cloud of volume data
415    bool _outlineEnabled;       ///< show/hide outline around volume
416    float _outlineColor[3];     ///< color for outline around volume
417    int _volumeType;            ///< cubic or zincblende
418    int _isosurface;
419};
420
421inline int
422Volume::addCutplane(int orientation, float location)
423{
424    _plane.push_back(CutPlane(orientation, location));
425    return _plane.size() - 1;
426}
427
428inline void
429Volume::enableCutplane(int index)
430{
431    //assert(index < plane.size());
432    _plane[index].enabled = true;
433}
434
435inline void
436Volume::disableCutplane(int index)
437{
438    //assert(index < plane.size());
439    _plane[index].enabled = false;
440}
441
442inline void
443Volume::moveCutplane(int index, float location)
444{
445    //assert(index < plane.size());
446    _plane[index].offset = location;
447}
448
449inline CutPlane *
450Volume::getCutplane(int index)
451{
452    //assert(index < plane.size());
453    return &_plane[index];
454}
455
456inline int
457Volume::getCutplaneCount()
458{
459    return _plane.size();
460}
461
462inline bool
463Volume::isCutplaneEnabled(int index) const
464{
465    //assert(index < plane.size());
466    return _plane[index].enabled;
467}
468
469inline void
470Volume::setOutlineColor(float *rgb)
471{
472    memcpy(_outlineColor, rgb, sizeof(float)*3);
473}
474
475inline void
476Volume::getOutlineColor(float *rgb)
477{
478    memcpy(rgb, _outlineColor, sizeof(float)*3);
479}
480
481#endif
Note: See TracBrowser for help on using the repository browser.