1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * ---------------------------------------------------------------------- |
---|
4 | * ColorMap.h: color map class contains an array of (RGBA) values |
---|
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 | |
---|
17 | #include <memory.h> |
---|
18 | #include <assert.h> |
---|
19 | |
---|
20 | #include <vrmath/Vector3f.h> |
---|
21 | |
---|
22 | #include "TransferFunction.h" |
---|
23 | |
---|
24 | using namespace nv; |
---|
25 | using namespace vrmath; |
---|
26 | |
---|
27 | TransferFunction::TransferFunction(const char *name, int size, float *data) : |
---|
28 | _name(name) |
---|
29 | { |
---|
30 | // _size : # of slot, 4 : rgba |
---|
31 | _size = size * 4; |
---|
32 | _data = new float[_size]; |
---|
33 | memcpy(_data, data, sizeof(float) * _size); |
---|
34 | |
---|
35 | _tex = new Texture1D(size, GL_FLOAT, GL_LINEAR, 4, data); |
---|
36 | _id = _tex->id(); |
---|
37 | } |
---|
38 | |
---|
39 | TransferFunction::~TransferFunction() |
---|
40 | { |
---|
41 | delete [] _data; |
---|
42 | delete _tex; |
---|
43 | } |
---|
44 | |
---|
45 | void |
---|
46 | TransferFunction::update(float *data) |
---|
47 | { |
---|
48 | memcpy(_data, data, sizeof(float) * _size); |
---|
49 | _tex->update(_data); |
---|
50 | } |
---|
51 | |
---|
52 | void |
---|
53 | TransferFunction::update(int size, float *data) |
---|
54 | { |
---|
55 | // TBD.. |
---|
56 | //assert((size*4) == _size); |
---|
57 | update(data); |
---|
58 | } |
---|
59 | |
---|
60 | void |
---|
61 | TransferFunction::sample(float fraction, float *keys, Vector3f *keyValues, int count, Vector3f *ret) |
---|
62 | { |
---|
63 | int limit = count - 1; |
---|
64 | if (fraction <= keys[0]) { |
---|
65 | *ret = keyValues[0]; |
---|
66 | } else if (fraction >= keys[limit]) { |
---|
67 | *ret = keyValues[limit]; |
---|
68 | } else { |
---|
69 | int n; |
---|
70 | for (n = 0; n < limit; n++) { |
---|
71 | if (fraction >= keys[n] && fraction < keys[n+1]) break; |
---|
72 | } |
---|
73 | if (n >= limit) { |
---|
74 | *ret = keyValues[limit]; |
---|
75 | } else { |
---|
76 | float inter = (fraction - keys[n]) / (keys[n + 1] - keys[n]); |
---|
77 | ret->set(inter * (keyValues[n + 1].x - keyValues[n].x) + keyValues[n].x, |
---|
78 | inter * (keyValues[n + 1].y - keyValues[n].y) + keyValues[n].y, |
---|
79 | inter * (keyValues[n + 1].z - keyValues[n].z) + keyValues[n].z); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | void |
---|
85 | TransferFunction::sample(float fraction, float *keys, float *keyValues, int count, float *ret) |
---|
86 | { |
---|
87 | int limit = count - 1; |
---|
88 | if (fraction <= keys[0]) { |
---|
89 | *ret = keyValues[0]; |
---|
90 | } else if (fraction >= keys[limit]) { |
---|
91 | *ret = keyValues[limit]; |
---|
92 | } else { |
---|
93 | int n; |
---|
94 | for (n = 0; n < limit; n++) { |
---|
95 | if (fraction >= keys[n] && fraction < keys[n+1]) break; |
---|
96 | } |
---|
97 | if (n >= limit) { |
---|
98 | *ret = keyValues[limit]; |
---|
99 | } else { |
---|
100 | float inter = (fraction - keys[n]) / (keys[n + 1] - keys[n]); |
---|
101 | *ret = inter * (keyValues[n + 1] - keyValues[n]) + keyValues[n]; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|