1 | |
---|
2 | #include "VolumeInterpolator.h" |
---|
3 | #include "Volume.h" |
---|
4 | #include <string.h> |
---|
5 | #include <memory.h> |
---|
6 | #include "Vector3.h" |
---|
7 | #include <time.h> |
---|
8 | #include <sys/time.h> |
---|
9 | #include <math.h> |
---|
10 | #include <stdlib.h> |
---|
11 | #include "Trace.h" |
---|
12 | |
---|
13 | VolumeInterpolator::VolumeInterpolator() : |
---|
14 | _volume(0), |
---|
15 | _interval(8.0), |
---|
16 | _started(false), |
---|
17 | _numBytes(0), |
---|
18 | _dataCount(0), |
---|
19 | _n_components(0), |
---|
20 | _referenceOfVolume(0) |
---|
21 | { |
---|
22 | /*empty*/ |
---|
23 | } |
---|
24 | |
---|
25 | void VolumeInterpolator::start() |
---|
26 | { |
---|
27 | if (_volumes.size() > 0) { |
---|
28 | Trace("\tVolume Interpolation Started\n"); |
---|
29 | _started = true; |
---|
30 | } else { |
---|
31 | Trace("\tVolume Interpolation did not get started\n"); |
---|
32 | _started = false; |
---|
33 | } |
---|
34 | struct timeval clock; |
---|
35 | gettimeofday(&clock, NULL); |
---|
36 | _start_time = clock.tv_sec + clock.tv_usec/1000000.0; |
---|
37 | Trace("End Start - VolumeInterpolator\n"); |
---|
38 | } |
---|
39 | |
---|
40 | void VolumeInterpolator::stop() |
---|
41 | { |
---|
42 | _started = false; |
---|
43 | } |
---|
44 | |
---|
45 | Volume* VolumeInterpolator::update(float fraction) |
---|
46 | { |
---|
47 | int key1, key2; |
---|
48 | float interp; |
---|
49 | |
---|
50 | computeKeys(fraction, _volumes.size(), &interp, &key1, &key2); |
---|
51 | |
---|
52 | if (interp == 0.0f) { |
---|
53 | memcpy(_volume->data(), _volumes[key1]->data(), _numBytes); |
---|
54 | _volume->tex()->update(_volume->data()); |
---|
55 | } else { |
---|
56 | float* data1 = _volumes[key1]->data(); |
---|
57 | float* data2 = _volumes[key2]->data(); |
---|
58 | float* result = _volume->data(); |
---|
59 | |
---|
60 | Vector3 normal1, normal2, normal; |
---|
61 | for (unsigned int i = 0; i < _dataCount; ++i) { |
---|
62 | *result = interp * (*data2 - *data1) + *data1; |
---|
63 | normal1 = (*(Vector3*)(data1 + 1) - 0.5) * 2; |
---|
64 | normal2 = (*(Vector3*)(data2 + 1) - 0.5) * 2; |
---|
65 | normal = (normal2 - normal2) * interp + normal1; |
---|
66 | normal = normal.normalize(); |
---|
67 | normal = normal * 0.5 + 0.5; |
---|
68 | *((Vector3*)(result + 1)) = normal; |
---|
69 | |
---|
70 | result += _n_components; |
---|
71 | data1 += _n_components; |
---|
72 | data2 += _n_components; |
---|
73 | } |
---|
74 | |
---|
75 | _volume->tex()->update(_volume->data()); |
---|
76 | } |
---|
77 | |
---|
78 | return _volume; |
---|
79 | } |
---|
80 | |
---|
81 | void |
---|
82 | VolumeInterpolator::computeKeys(float fraction, int count, float* interp, |
---|
83 | int* key1, int* key2) |
---|
84 | { |
---|
85 | int limit = (int) count - 1; |
---|
86 | if (fraction <= 0) { |
---|
87 | *key1 = *key2 = 0; |
---|
88 | *interp = 0.0f; |
---|
89 | } else if (fraction >= 1.0f) { |
---|
90 | *key1 = *key2 = limit; |
---|
91 | *interp = 0.0f; |
---|
92 | } else { |
---|
93 | int n; |
---|
94 | for (n = 0;n < limit; n++){ |
---|
95 | if (fraction >= (n / (count - 1.0f)) && |
---|
96 | fraction < ((n+1)/(count-1.0f))) { |
---|
97 | break; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | Trace("n = %d count = %d\n", n, count); |
---|
102 | if (n >= limit){ |
---|
103 | *key1 = *key2 = limit; |
---|
104 | *interp = 0.0f; |
---|
105 | |
---|
106 | } else { |
---|
107 | *key1 = n; |
---|
108 | *key2 = n+1; |
---|
109 | *interp = (fraction - (n / (count -1.0f))) / ((n + 1) / (count - 1.0f) - n / (count - 1.0f)); |
---|
110 | //*ret = inter * (keyValue[n + 1] - keyValue[n]) + keyValue[n]; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void |
---|
116 | VolumeInterpolator::clearAll() |
---|
117 | { |
---|
118 | _volumes.clear(); |
---|
119 | } |
---|
120 | |
---|
121 | void |
---|
122 | VolumeInterpolator::addVolume(Volume* refPtr) |
---|
123 | { |
---|
124 | if (_volumes.size() != 0) { |
---|
125 | if (_volumes[0]->width != refPtr->width || |
---|
126 | _volumes[0]->height != refPtr->height || |
---|
127 | _volumes[0]->depth != refPtr->depth || |
---|
128 | _volumes[0]->n_components() != refPtr->n_components()) { |
---|
129 | printf("The volume should be the same width, height, number of components\n"); |
---|
130 | return; |
---|
131 | } |
---|
132 | |
---|
133 | } else { |
---|
134 | _dataCount = refPtr->width * refPtr->height * refPtr->depth; |
---|
135 | _n_components = refPtr->n_components(); |
---|
136 | _numBytes = _dataCount * _n_components * sizeof(float); |
---|
137 | Vector3 loc = refPtr->location(); |
---|
138 | _volume = new Volume(loc.x, loc.y, loc.z, |
---|
139 | refPtr->width, refPtr->height, refPtr->depth, |
---|
140 | refPtr->size, |
---|
141 | refPtr->n_components(), |
---|
142 | refPtr->data(), |
---|
143 | refPtr->wAxis.min(), |
---|
144 | refPtr->wAxis.max(), |
---|
145 | refPtr->nonzero_min()); |
---|
146 | /* |
---|
147 | _referenceOfVolume = refPtr->dataID(); |
---|
148 | */ |
---|
149 | _volume->n_slices(256-1); |
---|
150 | _volume->disable_cutplane(0); |
---|
151 | _volume->disable_cutplane(1); |
---|
152 | _volume->disable_cutplane(2); |
---|
153 | _volume->visible(true); |
---|
154 | _volume->data_enabled(true); |
---|
155 | _volume->specular(refPtr->specular()); |
---|
156 | _volume->diffuse(refPtr->diffuse()); |
---|
157 | _volume->opacity_scale(refPtr->opacity_scale()); |
---|
158 | _volume->isosurface(0); |
---|
159 | Trace("VOL : location %f %f %f\n\tid : %s\n", loc.x, loc.y, loc.z, |
---|
160 | refPtr->name()); |
---|
161 | } |
---|
162 | _volumes.push_back(_volume); |
---|
163 | Trace("a Volume[%s] is added to VolumeInterpolator\n", refPtr->name()); |
---|
164 | } |
---|
165 | |
---|
166 | Volume* VolumeInterpolator::getVolume() |
---|
167 | { |
---|
168 | return _volume; |
---|
169 | //return _volumes[0]; |
---|
170 | } |
---|
171 | |
---|