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