source: nanovis/tags/1.2.4/Volume.h @ 5881

Last change on this file since 5881 was 5701, checked in by ldelgass, 9 years ago

Merge some refactoring changes from trunk

  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
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
21namespace nv {
22
23class CutPlane {
24public:
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
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 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    vrmath::Vector3f getPhysicalScaling() const
300    {
301        vrmath::Vector3f scale;
302        scale.x = 1;
303        scale.y = yAxis.length() / xAxis.length();
304        scale.z = zAxis.length() / xAxis.length();
305        return scale;
306    }
307
308    void setScale(const vrmath::Vector3f& scale)
309    {
310        _scale = scale;
311    }
312
313    const vrmath::Vector3f& getScale() const
314    {
315        return _scale;
316    }
317
318    void getBounds(vrmath::Vector3f& bboxMin,
319                   vrmath::Vector3f& bboxMax) const;
320 
321    double sampleDistanceX() const
322    {
323        return (xAxis.length() / ((double)_width-1.0));
324    }
325
326    double sampleDistanceY() const
327    {
328        return (yAxis.length() / ((double)_height-1.0));
329    }
330
331    double sampleDistanceZ() const
332    {
333        if (_depth == 1)
334            return sampleDistanceX();
335        return (zAxis.length() / ((double)_depth-1.0));
336    }
337
338    const char *name() const
339    {
340        return _name.c_str();
341    }
342
343    void name(const char *name)
344    {
345        _name = name;
346    }
347
348    GLuint textureID() const
349    {
350        return _id;
351    }
352
353    AxisRange xAxis, yAxis, zAxis, wAxis;
354
355    static bool updatePending;
356    static double valueMin, valueMax;
357
358    friend class VolumeInterpolator;
359
360protected:
361    float *data()
362    {
363        return _data;
364    }
365
366    Texture3D *tex()
367    {
368        return _tex;
369    }
370
371    GLuint _id;         ///< OpenGL textue identifier (==_tex->id)
372
373    // Width, height and depth are point resolution, NOT physical
374    // units
375    /// The resolution of the data (how many points in X direction)
376    int _width;
377    /// The resolution of the data (how many points in Y direction)
378    int _height;
379    /// The resolution of the data (how many points in Z direction)
380    int _depth;
381
382    /**
383     * This is the designated transfer function to use to
384     * render this volume.
385     */
386    TransferFunction *_transferFunc;
387
388    float _ambient;      ///< Ambient material coefficient
389    float _diffuse;      ///< Diffuse material coefficient
390    float _specular;     ///< Specular level material coefficient
391    float _specularExp;  ///< Specular exponent
392    bool _lightTwoSide;  ///< Two-sided lighting flag
393
394    /**
395     * The scale multiplied to the opacity assigned by the
396     * transfer function.
397     */
398    float _opacityScale;
399
400    std::string _name;
401
402    float *_data;
403
404    int _numComponents;
405
406    double _nonZeroMin;
407
408    bool _cutplanesVisible;
409    std::vector<CutPlane> _plane; ///< cut planes
410
411    Texture3D *_tex;            ///< OpenGL texture storing the volume
412
413    vrmath::Vector3f _position;
414    vrmath::Vector3f _scale;
415
416    /**
417     * Number of slices when rendered. The greater
418     * the better quality, lower speed.
419     */
420    int _numSlices;
421    bool _enabled;
422    bool _dataEnabled;          ///< show/hide cloud of volume data
423    bool _outlineEnabled;       ///< show/hide outline around volume
424    float _outlineColor[3];     ///< color for outline around volume
425    int _volumeType;            ///< cubic or zincblende
426    int _isosurface;
427};
428
429inline int
430Volume::addCutplane(CutPlane::Axis orientation, float location)
431{
432    _plane.push_back(CutPlane(orientation, location));
433    return _plane.size() - 1;
434}
435
436inline void
437Volume::enableCutplane(int index)
438{
439    //assert(index < plane.size());
440    _plane[index].enabled = true;
441}
442
443inline void
444Volume::disableCutplane(int index)
445{
446    //assert(index < plane.size());
447    _plane[index].enabled = false;
448}
449
450inline void
451Volume::setCutplanePosition(int index, float position)
452{
453    //assert(index < plane.size());
454    _plane[index].offset = position;
455}
456
457inline CutPlane *
458Volume::getCutplane(int index)
459{
460    //assert(index < plane.size());
461    return &_plane[index];
462}
463
464inline int
465Volume::getCutplaneCount()
466{
467    return _plane.size();
468}
469
470inline bool
471Volume::isCutplaneEnabled(int index) const
472{
473    //assert(index < plane.size());
474    return _plane[index].enabled;
475}
476
477inline void
478Volume::setOutlineColor(float *rgb)
479{
480    memcpy(_outlineColor, rgb, sizeof(float)*3);
481}
482
483inline void
484Volume::getOutlineColor(float *rgb)
485{
486    memcpy(rgb, _outlineColor, sizeof(float)*3);
487}
488
489}
490
491#endif
Note: See TracBrowser for help on using the repository browser.