glviewvol

view src/curve.cc @ 12:773f89037a35

added copyright headers
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 31 Dec 2014 07:57:04 +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 <algorithm>
19 #include "curve.h"
21 #define CLAMP(x, low, high) std::min<float>(std::max<float>(x, low), high)
22 #define INDEX(x) ((x) * 65535.0)
24 void Curve::set_point(float t, float val)
25 {
26 uint16_t x = INDEX(CLAMP(t, 0.0, 1.0));
27 set_point_int(x, val);
28 }
30 static bool cpcmp(const CurvePoint &a, const CurvePoint &b)
31 {
32 return a.t_int < b.t_int;
33 }
35 void Curve::set_point_int(uint16_t ti, float val)
36 {
37 CurvePoint *p = get_point_at(ti);
38 if(p) {
39 p->value = val;
40 } else {
41 CurvePoint p;
42 p.t_int = ti;
43 p.value = val;
44 cp.push_back(p);
45 std::sort(cp.begin(), cp.end(), cpcmp);
46 }
47 }
49 bool Curve::delete_point(uint16_t ti)
50 {
51 int sz = (int)cp.size();
52 for(int i=0; i<sz; i++) {
53 if(cp[i].t_int == ti) {
54 cp.erase(cp.begin() + i);
55 return true;
56 }
57 }
58 return false;
59 }
61 CurvePoint *Curve::get_point(int idx)
62 {
63 if(idx < 0 || idx >= (int)cp.size()) {
64 return 0;
65 }
66 return &cp[idx];
67 }
69 const CurvePoint *Curve::get_point(int idx) const
70 {
71 if(idx < 0 || idx >= (int)cp.size()) {
72 return 0;
73 }
74 return &cp[idx];
75 }
77 int Curve::get_num_points() const
78 {
79 return (int)cp.size();
80 }
82 CurvePoint *Curve::get_point_at(uint16_t ti)
83 {
84 int sz = (int)cp.size();
85 for(int i=0; i<sz; i++) {
86 if(cp[i].t_int == ti) {
87 return &cp[i];
88 }
89 }
90 return 0;
91 }
93 const CurvePoint *Curve::get_point_at(uint16_t ti) const
94 {
95 int sz = (int)cp.size();
96 for(int i=0; i<sz; i++) {
97 if(cp[i].t_int == ti) {
98 return &cp[i];
99 }
100 }
101 return 0;
102 }
104 float Curve::value(float t) const
105 {
106 uint16_t x = INDEX(CLAMP(t, 0.0, 1.0));
107 return value_int(x);
108 }
110 float Curve::value_int(uint16_t ti) const
111 {
112 CurvePoint p = { ti, 0 };
113 std::vector<CurvePoint>::const_iterator it;
114 it = std::lower_bound(cp.begin(), cp.end(), p, cpcmp);
115 if(ti >= it->t_int || it == cp.begin()) {
116 return it->value;
117 }
119 std::vector<CurvePoint>::const_iterator prev = it - 1;
121 float t = (float)(ti - prev->t_int) / (float)(it->t_int - prev->t_int);
122 return prev->value + (it->value - prev->value) * t;
123 }