source: trunk/packages/vizservers/nanovis/VolumeInterpolator.cpp @ 1446

Last change on this file since 1446 was 1111, checked in by gah, 16 years ago

nanovis/heightmap update

File size: 4.9 KB
Line 
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
13VolumeInterpolator::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
25void 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 
35    struct timeval clock;
36    gettimeofday(&clock, NULL);
37    _start_time = clock.tv_sec + clock.tv_usec/1000000.0;
38
39    Trace("End Start - VolumeInterpolator\n");
40}
41
42void VolumeInterpolator::stop()
43{
44    _started = false;
45}
46
47Volume* VolumeInterpolator::update(float fraction)
48{
49    int key1, key2;
50    float interp;
51
52    computeKeys(fraction, _volumes.size(), &interp, &key1, &key2);
53
54    if (interp == 0.0f) {
55        memcpy(_volume->_data, _volumes[key1]->_data, _numBytes);
56        _volume->tex->update(_volume->_data);
57    } else {
58        float* data1 = _volumes[key1]->_data;
59        float* data2 = _volumes[key2]->_data;
60        float* result = _volume->_data;
61
62        Vector3 normal1, normal2, normal;
63        for (unsigned int i = 0; i < _dataCount; ++i) {
64            *result = interp * (*data2 - *data1) + *data1;
65            normal1 = (*(Vector3*)(data1 + 1) - 0.5) * 2;
66            normal2 = (*(Vector3*)(data2 + 1) - 0.5) * 2;
67            normal = (normal2 - normal2) * interp + normal1;
68            normal = normal.normalize();
69            normal = normal * 0.5 + 0.5;
70            *((Vector3*)(result + 1)) = normal;
71
72            result += _n_components;
73            data1 += _n_components;
74            data2 += _n_components;
75        }
76
77        _volume->tex->update(_volume->_data);
78    }
79   
80    return _volume;
81}
82
83void
84VolumeInterpolator::computeKeys(float fraction, int count, float* interp,
85                                int* key1, int* key2)
86{
87    int limit = (int) count - 1;
88    if (fraction <= 0) {
89        *key1 = *key2 = 0;
90        *interp = 0.0f;
91    } else if (fraction >= 1.0f) {
92        *key1 = *key2 = limit;
93        *interp = 0.0f;
94    } else {
95        int n;
96        for (n = 0;n < limit; n++){
97            if (fraction >= (n / (count - 1.0f)) &&
98                fraction < ((n+1)/(count-1.0f))) {
99                break;
100            }
101        }
102       
103        Trace("n = %d count = %d\n", n, count);
104        if (n >= limit){
105            *key1 = *key2 = limit;
106            *interp = 0.0f;
107           
108        } else {
109            *key1 = n;
110            *key2 = n+1;
111            *interp = (fraction - (n / (count -1.0f))) / ((n + 1) / (count - 1.0f) - n / (count - 1.0f));
112            //*ret = inter * (keyValue[n + 1] - keyValue[n]) + keyValue[n];
113        }
114    }
115}
116
117void
118VolumeInterpolator::clearAll()
119{
120    _volumes.clear();
121
122    if (_volume) delete _volume;
123}
124
125void
126VolumeInterpolator::addVolume(Volume* volume, unsigned int volumeId)
127{
128    if (_volumes.size() != 0) {
129        if (_volumes[0]->width != volume->width ||
130            _volumes[0]->height != volume->height ||   
131            _volumes[0]->depth != volume->depth ||
132            _volumes[0]->n_components != volume->n_components) {
133            printf("The volume should be the same width, height, number of components\n");
134            return;
135        }
136       
137    } else {
138        _dataCount = volume->width * volume->height * volume->depth;
139        _n_components = volume->n_components;
140        _numBytes = _dataCount * _n_components * sizeof(float);
141        _volume = new Volume(volume->location.x,
142                             volume->location.y,
143                             volume->location.z,
144                             volume->width, volume->height, volume->depth,
145                             volume->size,
146                             volume->n_components,
147                             volume->_data,
148                             volume->wAxis.min(),
149                             volume->wAxis.max(),
150                             volume->nonzero_min);
151        _referenceOfVolume = volumeId;
152        _volume->set_n_slice(256-1);
153        _volume->disable_cutplane(0);
154        _volume->disable_cutplane(1);
155        _volume->disable_cutplane(2);
156        _volume->enable();
157        _volume->enable_data();
158        _volume->set_specular(volume->get_specular());
159        _volume->set_diffuse(volume->get_diffuse());
160        _volume->set_opacity_scale(volume->get_opacity_scale());
161        _volume->set_isosurface(0);
162       
163        Trace("VOL : location %f %f %f\n\tid : %d\n", _volume->location.x,
164                _volume->location.y, _volume->location.z, volumeId);
165    }
166
167    _volumes.push_back(volume);
168    Trace("a Volume[%d] is added to VolumeInterpolator\n", volumeId);
169}
170
171Volume* VolumeInterpolator::getVolume()
172{
173    return _volume;
174    //return _volumes[0];
175}
176
Note: See TracBrowser for help on using the repository browser.