source: trunk/packages/vizservers/nanovis/Volume.h @ 2921

Last change on this file since 2921 was 2877, checked in by ldelgass, 12 years ago

Some minor refactoring, also add some more fine grained config.h defines
(e.g. replace NV40 define with feature defines). Add tests for some required
OpenGL extensions (should always check for extensions or base version before
calling entry points from the extension). Also, clamp diffuse and specular
values on input and warn when they are out of range.

  • Property svn:eol-style set to native
File size: 8.5 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-2006  Purdue Research Foundation
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 <string>
20#include <vector>
21
22#include <R2/R2Object.h>
23
24#include "Color.h"
25#include "Texture3D.h"
26#include "Vector3.h"
27#include "AxisRange.h"
28#include "TransferFunction.h"
29
30struct CutPlane {
31    /// orientation - 1: xy slice, 2: yz slice, 3: xz slice
32    int orient;
33    float offset;       ///< normalized offset [0,1] in the volume
34    bool enabled;
35
36    CutPlane(int _orient, float _offset) :
37        orient(_orient),
38        offset(_offset),
39        enabled(true)
40    {
41    }
42};
43
44class VolumeInterpolator;
45
46class Volume
47{
48public:
49    enum VolumeType {
50        CUBIC,
51        VOLQD,
52        ZINCBLENDE
53    };
54
55    Volume(float x, float y, float z,
56           int width, int height, int depth,
57           float size, int numComponents,
58           float *data,
59           double vmin, double vmax,
60           double nonZeroMin);
61
62    virtual ~Volume();
63
64    void visible(bool value)
65    {
66        _enabled = value;
67    }
68
69    bool visible() const
70    {
71        return _enabled;
72    }
73
74    void location(const Vector3& loc)
75    {
76        _location = loc;
77    }
78
79    Vector3 location() const
80    {
81        return _location;
82    }
83
84    int isosurface() const
85    {
86        return _isosurface;
87    }
88
89    void isosurface(int iso)
90    {
91        _isosurface = iso;
92    }
93
94    int numComponents() const
95    {
96        return _numComponents;
97    }
98
99    double nonZeroMin() const
100    {
101        return _nonZeroMin;
102    }
103
104    int volumeType() const
105    {
106        return _volumeType;
107    }
108
109    float *data()
110    {
111        return _data;
112    }
113
114    Texture3D *tex()
115    {
116        return _tex;
117    }
118   
119    int numSlices() const
120    {
121        return _numSlices;
122    }
123
124    void numSlices(int n)
125    {
126        _numSlices = n;
127    }
128
129    /// set the drawing size of volume
130    void setSize(float s);
131
132    // methods related to cutplanes
133    /// add a plane and returns its index
134    int addCutplane(int orientation, float location);
135
136    void enableCutplane(int index);
137
138    void disableCutplane(int index);
139
140    void moveCutplane(int index, float location);
141
142    CutPlane *getCutplane(int index);
143
144    /// returns the number of cutplanes in the volume
145    int getCutplaneCount();
146
147    /// check if a cutplane is enabled
148    bool isCutplaneEnabled(int index) const;
149
150    // methods related to shading. These parameters are per volume
151
152    /// Get specular exponent
153    float specular() const
154    {
155        return _specular;
156    }
157
158    /// Set specular exponent [0,128]
159    void specular(float value)
160    {
161        if (value < 0.0f) value = 0.0f;
162        if (value > 128.0f) value = 128.0f;
163        _specular = value;
164    }
165
166    /// Get diffuse coefficient
167    float diffuse() const
168    {
169        return _diffuse;
170    }
171
172    /// Set diffuse coefficient [0,1]
173    void diffuse(float value)
174    {
175        if (value < 0.0f) value = 0.0f;
176        if (value > 1.0f) value = 1.0f;
177        _diffuse = value;
178    }
179
180    float opacityScale() const
181    {
182        return _opacityScale;
183    }
184
185    void opacityScale(float value)
186    {
187        _opacityScale = value;
188    }
189
190    void dataEnabled(bool value)
191    {
192        _dataEnabled = value;
193    }
194
195    bool dataEnabled() const
196    {
197        return _dataEnabled;
198    }
199
200    void outline(bool value)
201    {
202        _outlineEnabled = value;
203    }
204
205    bool outline()
206    {
207        return _outlineEnabled;
208    }
209
210    TransferFunction *transferFunction()
211    {
212        return _tfPtr;
213    }
214
215    void transferFunction(TransferFunction *tfPtr)
216    {
217        _tfPtr = tfPtr;
218    }
219
220    void setOutlineColor(float *rgb);
221
222    void getOutlineColor(float *rgb);
223
224    /// change the label displayed on an axis
225    void setLabel(int axis, const char *txt);
226
227    void setPhysicalBBox(const Vector3& min, const Vector3& max);
228
229    const Vector3& getPhysicalBBoxMin() const;
230
231    const Vector3& getPhysicalBBoxMax() const;
232
233    const char *name() const
234    {
235        return _name;
236    }
237
238    void name(const char *name)
239    {
240        _name = name;
241    }
242
243    float aspectRatioWidth;
244    float aspectRatioHeight;
245    float aspectRatioDepth;
246
247    GLuint id;          ///< OpenGL textue identifier (==_tex->id)
248
249    // Width, height and depth are point resolution, NOT physical
250    // units
251    /// The resolution of the data (how many points in X direction)
252    int width;
253    /// The resolution of the data (how many points in Y direction)
254    int height;
255    /// The resolution of the data (how many points in Z direction)
256    int depth;
257    /**
258     * This is the scaling factor that will size the volume on screen.
259     * A render program drawing different objects, always knows how
260     * large an object is in relation to other objects. This size is
261     * provided by the render engine.
262     */
263    float size;
264
265    int pointsetIndex;
266
267    AxisRange xAxis, yAxis, zAxis, wAxis;
268    std::string label[3]; ///< the labels along each axis 0:x, 1:y, 2:z
269
270    static bool updatePending;
271    static double valueMin, valueMax;
272
273protected:
274    /**
275     * This is the designated transfer function to use to
276     * render this volume.
277     */
278    TransferFunction *_tfPtr;
279
280    float _specular;            ///< Specular lighting parameter
281    float _diffuse;             ///< Diffuse lighting parameter
282    /**
283     * The scale multiplied to the opacity assigned by the
284     * transfer function. Rule of thumb: higher opacity_scale
285     * the object is to appear like plastic
286     */
287    float _opacityScale;
288
289    const char *_name;
290    Vector3 _physicalMin;
291    Vector3 _physicalMax;
292    float *_data;
293
294    int _numComponents;
295
296    double _nonZeroMin;
297
298    std::vector<CutPlane> _plane; ///< cut planes
299
300    Texture3D *_tex;            ///< OpenGL texture storing the volume
301
302    Vector3 _location;
303
304    /**
305     * Number of slices when rendered. The greater
306     * the better quality, lower speed.
307     */
308    int _numSlices;
309    bool _enabled;
310    bool _dataEnabled;          ///< show/hide cloud of volume data
311    bool _outlineEnabled;       ///< show/hide outline around volume
312    Color _outlineColor;        ///< color for outline around volume
313    int _volumeType;            ///< cubic or zincblende
314    int _isosurface;
315};
316
317inline int
318Volume::addCutplane(int orientation, float location)
319{
320    _plane.push_back(CutPlane(orientation, location));
321    return _plane.size() - 1;
322}
323
324inline void
325Volume::enableCutplane(int index)
326{
327    //assert(index < plane.size());
328    _plane[index].enabled = true;
329}
330inline void
331Volume::disableCutplane(int index)
332{
333    //assert(index < plane.size());
334    _plane[index].enabled = false;
335}
336
337inline void
338Volume::moveCutplane(int index, float location)
339{
340    //assert(index < plane.size());
341    _plane[index].offset = location;
342}
343
344inline CutPlane *
345Volume::getCutplane(int index)
346{
347    //assert(index < plane.size());
348    return &_plane[index];
349}
350
351inline int
352Volume::getCutplaneCount()
353{
354    return _plane.size();
355}
356
357inline bool
358Volume::isCutplaneEnabled(int index) const
359{
360    //assert(index < plane.size());
361    return _plane[index].enabled;
362}
363
364inline void
365Volume::setSize(float s)
366{
367    size = s;
368    aspectRatioWidth  = s * _tex->aspectRatioWidth();
369    aspectRatioHeight = s * _tex->aspectRatioHeight();
370    aspectRatioDepth  = s * _tex->aspectRatioDepth();
371}
372
373inline void
374Volume::setOutlineColor(float *rgb)
375{
376    _outlineColor = Color(rgb[0], rgb[1], rgb[2]);
377}
378
379inline void
380Volume::getOutlineColor(float *rgb)
381{
382    _outlineColor.getRGB(rgb);
383}
384
385inline void
386Volume::setLabel(int axis, const char* txt)
387{
388    label[axis] = txt;
389}
390
391inline void
392Volume::setPhysicalBBox(const Vector3& min, const Vector3& max)
393{
394    _physicalMin = min;
395    _physicalMax = max;
396
397    /*
398    aspectRatioWidth = size * 1;
399    aspectRatioHeight = size * (max.y - min.y) / (max.x - min.x);
400    aspectRatioDepth = size* (max.z - min.z) / (max.x - min.x);
401
402    location.x = -0.5;
403    location.y = -0.5* aspectRatioHeight;
404    location.z = -0.5* aspectRatioDepth;
405    */
406}
407
408inline const Vector3&
409Volume::getPhysicalBBoxMin() const
410{
411    return _physicalMin;
412}
413
414inline const Vector3&
415Volume::getPhysicalBBoxMax() const
416{
417    return _physicalMax;
418}
419
420#endif
Note: See TracBrowser for help on using the repository browser.