Debian Codegraph @

cextern/healpix/src/cxx/cxxsupport/linear_map.h

raw
1 /*
2 * This file is part of libcxxsupport.
3 *
4 * libcxxsupport is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * libcxxsupport is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with libcxxsupport; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21 * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22 * (DLR).
23 */
24
25 /*! \file linear_map.h
26 * Simple class for 1D mapping with linear interpolation.
27 * Adapted from RAY++ source code.
28 *
29 * Copyright (C) 2015 Max-Planck-Society
30 * \author Martin Reinecke
31 */
32
33 #ifndef PLANCK_LINEAR_MAP_H
34 #define PLANCK_LINEAR_MAP_H
35
36 #include <vector>
37 #include "colour.h"
38 #include "sort_utils.h"
39 #include "math_utils.h"
40
41 template<typename T> class linearMap
42 {
43 private:
44 bool sorted;
45 std::vector<double> x;
46 std::vector<T> y;
47
48 public:
49 void addVal (double x_, const T &val)
50 {
51 sorted=x.empty()||(x_>x.back());
52 x.push_back(x_);
53 y.push_back(val);
54 }
55
56 void sortMap()
57 {
58 if (sorted) return;
59 std::vector<size_t> idx;
60 buildIndex(x.begin(),x.end(),idx);
61 sortByIndex(x.begin(),x.end(),idx);
62 sortByIndex(y.begin(),y.end(),idx);
63 sorted = true;
64 }
65
66 void clear()
67 {
68 x.clear(); y.clear(); sorted=true;
69 }
70
71 T getVal_const (double x_) const
72 {
73 planck_assert(x.size()>0,"trying to access an empty map");
74 planck_assert(sorted,"map must be sorted");
75 if (x.size()==1) return y[0];
76 if (x_>=x.back())
77 return y.back();
78 if (x_<=x[0])
79 return y[0];
80 tsize index;
81 double frac;
82 interpol_helper (x.begin(), x.end(), x_, index, frac);
83 return (1.-frac)*y[index]+frac*y[index+1];
84 }
85 T getVal (double x_)
86 {
87 if (!sorted) sortMap();
88 return getVal_const(x_);
89 }
90
91 size_t size() const { return x.size(); }
92 double getX (size_t idx) { if (!sorted) sortMap(); return x[idx]; }
93 T getY (size_t idx) { if (!sorted) sortMap(); return y[idx]; }
94 };
95
96 #endif