source: nanovis/branches/1.1/Volume.cpp @ 4906

Last change on this file since 4906 was 4904, checked in by ldelgass, 9 years ago

Merge serveral changes from trunk. Does not include threading, world space
changes, etc.

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 * Volume.cpp: 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#include <memory.h>
17#include <assert.h>
18#include <float.h>
19
20#include <vrmath/Vector4f.h>
21#include <vrmath/Matrix4x4d.h>
22
23#include "Volume.h"
24#include "Trace.h"
25
26using namespace nv;
27using namespace vrmath;
28
29bool Volume::updatePending = false;
30double Volume::valueMin = 0.0;
31double Volume::valueMax = 1.0;
32
33Volume::Volume(float x, float y, float z,
34               int width, int height, int depth,
35               int numComponents, float *data,
36               double vmin, double vmax, double nonZeroMin) :
37    _id(0),
38    _width(width),
39    _height(height),
40    _depth(depth),
41    _transferFunc(NULL),
42    _ambient(0.6f),
43    _diffuse(0.4f),
44    _specular(0.3f),
45    _specularExp(90.0f),
46    _lightTwoSide(false),
47    _opacityScale(0.5f),
48    _data(NULL),
49    _numComponents(numComponents),
50    _nonZeroMin(nonZeroMin),
51    _cutplanesVisible(true),
52    _tex(NULL),
53    _position(x, y, z),
54    _scale(1, 1, 1),
55    _numSlices(512),
56    _enabled(true),
57    _dataEnabled(true),
58    _outlineEnabled(true),
59    _volumeType(CUBIC),
60    _isosurface(0)
61{
62    TRACE("Enter: %dx%dx%d", _width, _height, _depth);
63
64    _outlineColor[0] = _outlineColor[1] = _outlineColor[2] = 1.0f;
65
66    _tex = new Texture3D(_width, _height, _depth, GL_FLOAT, GL_LINEAR, _numComponents);
67    int fcount = _width * _height * _depth * _numComponents;
68    _data = new float[fcount];
69    if (data != NULL) {
70        memcpy(_data, data, fcount * sizeof(float));
71    } else {
72        TRACE("data is NULL");
73        memset(_data, 0, fcount * sizeof(float));
74    }
75    _tex->initialize(_data);
76    _id = _tex->id();
77
78    wAxis.setRange(vmin, vmax);
79
80    //Add cut planes. We create 3 default cut planes facing x, y, z directions.
81    //The default location of cut plane is in the middle of the data.
82    _plane.clear();
83    addCutplane(CutPlane::X_AXIS, 0.5f);
84    addCutplane(CutPlane::Y_AXIS, 0.5f);
85    addCutplane(CutPlane::Z_AXIS, 0.5f);
86
87    TRACE("Leave");
88}
89
90Volume::~Volume()
91{
92    TRACE("Enter");
93
94    delete [] _data;
95    delete _tex;
96}
97
98void Volume::getWorldSpaceBounds(Vector3f& bboxMin, Vector3f& bboxMax) const
99{
100    Vector3f scale = getPhysicalScaling();
101
102    Matrix4x4d mat;
103    mat.makeTranslation(_position);
104    Matrix4x4d mat2;
105    mat2.makeScale(scale);
106
107    mat.multiply(mat2);
108
109    bboxMin.set(FLT_MAX, FLT_MAX, FLT_MAX);
110    bboxMax.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
111
112    Vector3f modelMin(0, 0, 0);
113    Vector3f modelMax(1, 1, 1);
114
115    Vector4f bvert[8];
116    bvert[0] = Vector4f(modelMin.x, modelMin.y, modelMin.z, 1);
117    bvert[1] = Vector4f(modelMax.x, modelMin.y, modelMin.z, 1);
118    bvert[2] = Vector4f(modelMin.x, modelMax.y, modelMin.z, 1);
119    bvert[3] = Vector4f(modelMin.x, modelMin.y, modelMax.z, 1);
120    bvert[4] = Vector4f(modelMax.x, modelMax.y, modelMin.z, 1);
121    bvert[5] = Vector4f(modelMax.x, modelMin.y, modelMax.z, 1);
122    bvert[6] = Vector4f(modelMin.x, modelMax.y, modelMax.z, 1);
123    bvert[7] = Vector4f(modelMax.x, modelMax.y, modelMax.z, 1);
124
125    for (int i = 0; i < 8; i++) {
126        Vector4f worldVert = mat.transform(bvert[i]);
127        if (worldVert.x < bboxMin.x) bboxMin.x = worldVert.x;
128        if (worldVert.x > bboxMax.x) bboxMax.x = worldVert.x;
129        if (worldVert.y < bboxMin.y) bboxMin.y = worldVert.y;
130        if (worldVert.y > bboxMax.y) bboxMax.y = worldVert.y;
131        if (worldVert.z < bboxMin.z) bboxMin.z = worldVert.z;
132        if (worldVert.z > bboxMax.z) bboxMax.z = worldVert.z;
133    }
134}
Note: See TracBrowser for help on using the repository browser.