linear_map.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file linear_map.h
00026  *  Simple class for 1D mapping with linear interpolation.
00027  *  Adapted from RAY++ source code.
00028  *
00029  *  Copyright (C) 2015 Max-Planck-Society
00030  *  \author Martin Reinecke
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

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library