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