linear_map.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef PLANCK_LINEAR_MAP_H
00034 #define PLANCK_LINEAR_MAP_H
00035
00036 #include <vector>
00037 #include "colour.h"
00038 #include "sort_utils.h"
00039 #include "math_utils.h"
00040
00041 template<typename T> class linearMap
00042 {
00043 private:
00044 bool sorted;
00045 std::vector<double> x;
00046 std::vector<T> y;
00047
00048 public:
00049 void addVal (double x_, const T &val)
00050 {
00051 sorted=x.empty()||(x_>x.back());
00052 x.push_back(x_);
00053 y.push_back(val);
00054 }
00055
00056 void sortMap()
00057 {
00058 if (sorted) return;
00059 std::vector<size_t> idx;
00060 buildIndex(x.begin(),x.end(),idx);
00061 sortByIndex(x.begin(),x.end(),idx);
00062 sortByIndex(y.begin(),y.end(),idx);
00063 sorted = true;
00064 }
00065
00066 void clear()
00067 {
00068 x.clear(); y.clear(); sorted=true;
00069 }
00070
00071 T getVal_const (double x_) const
00072 {
00073 planck_assert(x.size()>0,"trying to access an empty map");
00074 planck_assert(sorted,"map must be sorted");
00075 if (x.size()==1) return y[0];
00076 if (x_>=x.back())
00077 return y.back();
00078 if (x_<=x[0])
00079 return y[0];
00080 tsize index;
00081 double frac;
00082 interpol_helper (x.begin(), x.end(), x_, index, frac);
00083 return (1.-frac)*y[index]+frac*y[index+1];
00084 }
00085 T getVal (double x_)
00086 {
00087 if (!sorted) sortMap();
00088 return getVal_const(x_);
00089 }
00090
00091 size_t size() const { return x.size(); }
00092 double getX (size_t idx) { if (!sorted) sortMap(); return x[idx]; }
00093 T getY (size_t idx) { if (!sorted) sortMap(); return y[idx]; }
00094 };
00095
00096 #endif