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