nuclear@12: /* nuclear@12: glviewvol is an OpenGL 3D volume data viewer nuclear@12: Copyright (C) 2014 John Tsiombikas nuclear@12: nuclear@12: This program is free software: you can redistribute it and/or modify nuclear@12: it under the terms of the GNU General Public License as published by nuclear@12: the Free Software Foundation, either version 3 of the License, or nuclear@12: (at your option) any later version. nuclear@12: nuclear@12: This program is distributed in the hope that it will be useful, nuclear@12: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@12: GNU General Public License for more details. nuclear@12: nuclear@12: You should have received a copy of the GNU General Public License nuclear@12: along with this program. If not, see . nuclear@12: */ nuclear@4: #include nuclear@4: #include "curve.h" nuclear@4: nuclear@4: #define CLAMP(x, low, high) std::min(std::max(x, low), high) nuclear@4: #define INDEX(x) ((x) * 65535.0) nuclear@4: nuclear@4: void Curve::set_point(float t, float val) nuclear@4: { nuclear@4: uint16_t x = INDEX(CLAMP(t, 0.0, 1.0)); nuclear@4: set_point_int(x, val); nuclear@4: } nuclear@4: nuclear@4: static bool cpcmp(const CurvePoint &a, const CurvePoint &b) nuclear@4: { nuclear@4: return a.t_int < b.t_int; nuclear@4: } nuclear@4: nuclear@4: void Curve::set_point_int(uint16_t ti, float val) nuclear@4: { nuclear@4: CurvePoint *p = get_point_at(ti); nuclear@4: if(p) { nuclear@4: p->value = val; nuclear@4: } else { nuclear@4: CurvePoint p; nuclear@4: p.t_int = ti; nuclear@4: p.value = val; nuclear@4: cp.push_back(p); nuclear@4: std::sort(cp.begin(), cp.end(), cpcmp); nuclear@4: } nuclear@4: } nuclear@4: nuclear@4: bool Curve::delete_point(uint16_t ti) nuclear@4: { nuclear@4: int sz = (int)cp.size(); nuclear@4: for(int i=0; i= (int)cp.size()) { nuclear@4: return 0; nuclear@4: } nuclear@4: return &cp[idx]; nuclear@4: } nuclear@4: nuclear@4: const CurvePoint *Curve::get_point(int idx) const nuclear@4: { nuclear@7: if(idx < 0 || idx >= (int)cp.size()) { nuclear@4: return 0; nuclear@4: } nuclear@4: return &cp[idx]; nuclear@4: } nuclear@4: nuclear@4: int Curve::get_num_points() const nuclear@4: { nuclear@4: return (int)cp.size(); nuclear@4: } nuclear@4: nuclear@4: CurvePoint *Curve::get_point_at(uint16_t ti) nuclear@4: { nuclear@4: int sz = (int)cp.size(); nuclear@4: for(int i=0; i::const_iterator it; nuclear@4: it = std::lower_bound(cp.begin(), cp.end(), p, cpcmp); nuclear@4: if(ti >= it->t_int || it == cp.begin()) { nuclear@4: return it->value; nuclear@4: } nuclear@4: nuclear@4: std::vector::const_iterator prev = it - 1; nuclear@4: nuclear@4: float t = (float)(ti - prev->t_int) / (float)(it->t_int - prev->t_int); nuclear@4: return prev->value + (it->value - prev->value) * t; nuclear@4: }