[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[3502] | 2 | /* |
---|
| 3 | * Copyright (C) 2004-2013 HUBzero Foundation, LLC |
---|
| 4 | * |
---|
| 5 | */ |
---|
[884] | 6 | #include <string.h> |
---|
| 7 | #include <memory.h> |
---|
| 8 | #include <time.h> |
---|
| 9 | #include <sys/time.h> |
---|
| 10 | #include <math.h> |
---|
| 11 | #include <stdlib.h> |
---|
[2853] | 12 | |
---|
[3492] | 13 | #include <vrmath/Vector3f.h> |
---|
| 14 | |
---|
[2853] | 15 | #include "VolumeInterpolator.h" |
---|
| 16 | #include "Volume.h" |
---|
[884] | 17 | #include "Trace.h" |
---|
| 18 | |
---|
[3611] | 19 | using namespace nv; |
---|
[3492] | 20 | using namespace vrmath; |
---|
| 21 | |
---|
[927] | 22 | VolumeInterpolator::VolumeInterpolator() : |
---|
| 23 | _volume(0), |
---|
| 24 | _interval(8.0), |
---|
| 25 | _started(false), |
---|
| 26 | _numBytes(0), |
---|
| 27 | _dataCount(0), |
---|
[2953] | 28 | _numComponents(0) |
---|
[884] | 29 | { |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | void VolumeInterpolator::start() |
---|
| 33 | { |
---|
[3630] | 34 | if (!_volumes.empty()) { |
---|
[3452] | 35 | TRACE("Volume Interpolation Started"); |
---|
[927] | 36 | _started = true; |
---|
| 37 | } else { |
---|
[3452] | 38 | TRACE("Volume Interpolation NOT Started"); |
---|
[884] | 39 | _started = false; |
---|
| 40 | } |
---|
| 41 | struct timeval clock; |
---|
| 42 | gettimeofday(&clock, NULL); |
---|
[2877] | 43 | _startTime = clock.tv_sec + clock.tv_usec/1000000.0; |
---|
[3452] | 44 | TRACE("Leave"); |
---|
[884] | 45 | } |
---|
| 46 | |
---|
| 47 | void VolumeInterpolator::stop() |
---|
| 48 | { |
---|
[927] | 49 | _started = false; |
---|
[884] | 50 | } |
---|
| 51 | |
---|
[2853] | 52 | Volume *VolumeInterpolator::update(float fraction) |
---|
[884] | 53 | { |
---|
| 54 | int key1, key2; |
---|
| 55 | float interp; |
---|
| 56 | |
---|
| 57 | computeKeys(fraction, _volumes.size(), &interp, &key1, &key2); |
---|
| 58 | |
---|
[927] | 59 | if (interp == 0.0f) { |
---|
[1478] | 60 | memcpy(_volume->data(), _volumes[key1]->data(), _numBytes); |
---|
| 61 | _volume->tex()->update(_volume->data()); |
---|
[927] | 62 | } else { |
---|
[2853] | 63 | float *data1 = _volumes[key1]->data(); |
---|
| 64 | float *data2 = _volumes[key2]->data(); |
---|
| 65 | float *result = _volume->data(); |
---|
[884] | 66 | |
---|
[3492] | 67 | Vector3f normal1, normal2, normal; |
---|
[927] | 68 | for (unsigned int i = 0; i < _dataCount; ++i) { |
---|
[884] | 69 | *result = interp * (*data2 - *data1) + *data1; |
---|
[3492] | 70 | normal1 = (*(Vector3f*)(data1 + 1) - 0.5) * 2; |
---|
| 71 | normal2 = (*(Vector3f*)(data2 + 1) - 0.5) * 2; |
---|
[900] | 72 | normal = (normal2 - normal2) * interp + normal1; |
---|
| 73 | normal = normal.normalize(); |
---|
| 74 | normal = normal * 0.5 + 0.5; |
---|
[3492] | 75 | *((Vector3f*)(result + 1)) = normal; |
---|
[884] | 76 | |
---|
[2877] | 77 | result += _numComponents; |
---|
| 78 | data1 += _numComponents; |
---|
| 79 | data2 += _numComponents; |
---|
[884] | 80 | } |
---|
| 81 | |
---|
[1478] | 82 | _volume->tex()->update(_volume->data()); |
---|
[884] | 83 | } |
---|
[2853] | 84 | |
---|
[884] | 85 | return _volume; |
---|
| 86 | } |
---|
| 87 | |
---|
[2853] | 88 | void |
---|
| 89 | VolumeInterpolator::computeKeys(float fraction, int count, float *interp, |
---|
| 90 | int *key1, int *key2) |
---|
[884] | 91 | { |
---|
[927] | 92 | int limit = (int) count - 1; |
---|
| 93 | if (fraction <= 0) { |
---|
| 94 | *key1 = *key2 = 0; |
---|
[884] | 95 | *interp = 0.0f; |
---|
[927] | 96 | } else if (fraction >= 1.0f) { |
---|
| 97 | *key1 = *key2 = limit; |
---|
[884] | 98 | *interp = 0.0f; |
---|
[927] | 99 | } else { |
---|
[884] | 100 | int n; |
---|
[2853] | 101 | for (n = 0; n < limit; n++){ |
---|
[927] | 102 | if (fraction >= (n / (count - 1.0f)) && |
---|
| 103 | fraction < ((n+1)/(count-1.0f))) { |
---|
| 104 | break; |
---|
| 105 | } |
---|
[884] | 106 | } |
---|
[2853] | 107 | |
---|
[3452] | 108 | TRACE("n = %d count = %d", n, count); |
---|
[2853] | 109 | if (n >= limit) { |
---|
[927] | 110 | *key1 = *key2 = limit; |
---|
[884] | 111 | *interp = 0.0f; |
---|
[927] | 112 | } else { |
---|
| 113 | *key1 = n; |
---|
[884] | 114 | *key2 = n+1; |
---|
[927] | 115 | *interp = (fraction - (n / (count -1.0f))) / ((n + 1) / (count - 1.0f) - n / (count - 1.0f)); |
---|
[884] | 116 | //*ret = inter * (keyValue[n + 1] - keyValue[n]) + keyValue[n]; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[2853] | 121 | void |
---|
[927] | 122 | VolumeInterpolator::clearAll() |
---|
[884] | 123 | { |
---|
[927] | 124 | _volumes.clear(); |
---|
[884] | 125 | } |
---|
| 126 | |
---|
[2853] | 127 | void |
---|
[3597] | 128 | VolumeInterpolator::addVolume(Volume *volume) |
---|
[884] | 129 | { |
---|
[3630] | 130 | if (!_volumes.empty()) { |
---|
[3597] | 131 | if (_volumes[0]->width() != volume->width() || |
---|
| 132 | _volumes[0]->height() != volume->height() || |
---|
| 133 | _volumes[0]->depth() != volume->depth() || |
---|
| 134 | _volumes[0]->numComponents() != volume->numComponents()) { |
---|
[3452] | 135 | TRACE("The volume should be the same width, height, number of components"); |
---|
[884] | 136 | return; |
---|
| 137 | } |
---|
[927] | 138 | } else { |
---|
[3597] | 139 | _dataCount = volume->width() * volume->height() * volume->depth(); |
---|
| 140 | _numComponents = volume->numComponents(); |
---|
[2877] | 141 | _numBytes = _dataCount * _numComponents * sizeof(float); |
---|
[3630] | 142 | _volume = new Volume(volume->width(), |
---|
[3597] | 143 | volume->height(), |
---|
| 144 | volume->depth(), |
---|
| 145 | volume->numComponents(), |
---|
| 146 | volume->data(), |
---|
| 147 | volume->wAxis.min(), |
---|
| 148 | volume->wAxis.max(), |
---|
| 149 | volume->nonZeroMin()); |
---|
[3362] | 150 | |
---|
[3630] | 151 | _volume->setPosition(volume->getPosition()); |
---|
| 152 | _volume->setScale(volume->getScale()); |
---|
[2877] | 153 | _volume->disableCutplane(0); |
---|
| 154 | _volume->disableCutplane(1); |
---|
| 155 | _volume->disableCutplane(2); |
---|
[1474] | 156 | _volume->visible(true); |
---|
[2877] | 157 | _volume->dataEnabled(true); |
---|
[3597] | 158 | _volume->ambient(volume->ambient()); |
---|
| 159 | _volume->diffuse(volume->diffuse()); |
---|
| 160 | _volume->specularLevel(volume->specularLevel()); |
---|
| 161 | _volume->specularExponent(volume->specularExponent()); |
---|
| 162 | _volume->opacityScale(volume->opacityScale()); |
---|
[1478] | 163 | _volume->isosurface(0); |
---|
[884] | 164 | } |
---|
[3630] | 165 | _volumes.push_back(volume); |
---|
| 166 | TRACE("Volume \"%s\" is added to VolumeInterpolator", volume->name()); |
---|
[884] | 167 | } |
---|
| 168 | |
---|
[2853] | 169 | Volume *VolumeInterpolator::getVolume() |
---|
[900] | 170 | { |
---|
| 171 | return _volume; |
---|
| 172 | } |
---|