glviewvol

view src/xfermap.cc @ 15:2a67ea257ac0

makefile fixes for macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 06 Feb 2015 23:47:28 +0200
parents 71b479ffb9f7
children
line source
1 /*
2 glviewvol is an OpenGL 3D volume data viewer
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <math.h>
19 #include <algorithm>
20 #include "xfermap.h"
22 TransferFunc::~TransferFunc()
23 {
24 }
27 // ---- TransferWindow ----
29 TransferWindow::TransferWindow()
30 {
31 soft_rad = 0.5;
32 for(int i=0; i<3; i++) {
33 low[i] = 0.5;
34 high[i] = 1.5;
35 }
36 }
38 void TransferWindow::set_handle(int channel, int handle, float val)
39 {
40 float *dest = handle == HANDLE_LOW ? low : high;
42 if(channel == -1) {
43 dest[0] = dest[1] = dest[2] = val;
44 } else {
45 dest[channel] = val;
46 }
47 }
49 float TransferWindow::get_handle(int channel, int handle) const
50 {
51 const float *src = handle == HANDLE_LOW ? low : high;
53 if(channel == -1) {
54 return src[0]; // XXX this doens't make much sense
55 }
56 return src[channel];
57 }
59 int TransferWindow::nearest_handle(int channel, float pos) const
60 {
61 float ldist = 0, hdist = 0;
63 if(channel == -1) {
64 for(int i=0; i<3; i++) {
65 ldist += fabs(low[i] - pos);
66 hdist += fabs(high[i] - pos);
67 }
68 } else {
69 ldist = fabs(low[channel] - pos);
70 hdist = fabs(high[channel] - pos);
71 }
72 return ldist <= hdist ? HANDLE_LOW : HANDLE_HIGH;
73 }
75 void TransferWindow::set_interval(float a, float b)
76 {
77 float v0 = std::min(a, b);
78 float v1 = std::max(a, b);
80 for(int i=0; i<3; i++) {
81 low[i] = v0;
82 high[i] = v1;
83 }
84 }
86 void TransferWindow::set_interval(float *rgba_low, float *rgba_high)
87 {
88 for(int i=0; i<3; i++) {
89 low[i] = std::min(rgba_low[i], rgba_high[i]);
90 high[i] = std::max(rgba_low[i], rgba_high[i]);
91 }
92 }
94 void TransferWindow::set_interval_rgba(int channel, float a, float b)
95 {
96 low[channel] = std::min(a, b);
97 high[channel] = std::max(a, b);
98 }
100 void TransferWindow::get_interval(float *aptr, float *bptr) const
101 {
102 *aptr = low[0];
103 *bptr = high[0];
104 }
106 void TransferWindow::get_interval_rgba(float *rgba_low, float *rgba_high) const
107 {
108 for(int i=0; i<3; i++) {
109 rgba_low[i] = low[i];
110 rgba_high[i] = high[i];
111 }
112 }
114 void TransferWindow::get_interval_rgba(int channel, float *aptr, float *bptr) const
115 {
116 *aptr = low[channel];
117 *bptr = high[channel];
118 }
120 void TransferWindow::set_soft_radius(float s)
121 {
122 soft_rad = s;
123 }
125 float TransferWindow::get_soft_radius() const
126 {
127 return soft_rad;
128 }
130 static inline float smoothstep(float a, float b, float x)
131 {
132 if(x < a) return 0.0f;
133 if(x >= b) return 1.0f;
135 float t = (x - a) / (b - a);
136 return t * t * (3.0f - 2.0f * t);
137 }
139 float TransferWindow::map(float x) const
140 {
141 float rgb[3];
142 map(x, rgb);
144 return std::max(rgb[0], std::max(rgb[1], rgb[2]));
145 }
147 void TransferWindow::map(float x, float *rgb_value) const
148 {
149 for(int i=0; i<3; i++) {
150 float val = smoothstep(low[i] - soft_rad, low[i] + soft_rad, x);
151 val *= 1.0 - smoothstep(high[i] - soft_rad, high[i] + soft_rad, x);
152 rgb_value[i] = val;
153 }
154 }