glviewvol
view src/xfermap.cc @ 6:f22be47a3572
moved to TransferFuncs completely
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 30 Dec 2014 06:22:54 +0200 |
parents | |
children | 71b479ffb9f7 |
line source
1 #include <algorithm>
2 #include "xfermap.h"
4 TransferFunc::~TransferFunc()
5 {
6 }
9 // ---- TransferWindow ----
11 TransferWindow::TransferWindow()
12 {
13 soft_rad = 0.5;
14 for(int i=0; i<4; i++) {
15 low[i] = 0.5;
16 high[i] = 2.0;
17 }
18 }
20 void TransferWindow::set_interval(float a, float b)
21 {
22 float v0 = std::min(a, b);
23 float v1 = std::max(a, b);
25 for(int i=0; i<4; i++) {
26 low[i] = v0;
27 high[i] = v1;
28 }
29 }
31 void TransferWindow::set_interval(float *rgba_low, float *rgba_high)
32 {
33 for(int i=0; i<4; i++) {
34 low[i] = std::min(rgba_low[i], rgba_high[i]);
35 high[i] = std::max(rgba_low[i], rgba_high[i]);
36 }
37 }
39 void TransferWindow::set_interval_rgba(int channel, float a, float b)
40 {
41 low[channel] = std::min(a, b);
42 high[channel] = std::max(a, b);
43 }
45 void TransferWindow::set_soft_radius(float s)
46 {
47 soft_rad = s;
48 }
50 float TransferWindow::get_soft_radius() const
51 {
52 return soft_rad;
53 }
55 static inline float smoothstep(float a, float b, float x)
56 {
57 if(x < a) return 0.0f;
58 if(x >= b) return 1.0f;
60 float t = (x - a) / (b - a);
61 return t * t * (3.0f - 2.0f * t);
62 }
64 float TransferWindow::map(float x) const
65 {
66 return smoothstep(low[3] - soft_rad, high[3] - soft_rad, x) *
67 (1.0 - smoothstep(low[3] + soft_rad, high[3] + soft_rad, x));
68 }
70 void TransferWindow::map(float x, float *rgba_value) const
71 {
72 for(int i=0; i<4; i++) {
73 float val = smoothstep(low[i] - soft_rad, high[i] - soft_rad, x);
74 val *= 1.0 - smoothstep(low[i] + soft_rad, high[i] + soft_rad, x);
75 rgba_value[i] = val;
76 }
77 }