| 1 | /* -*- coding: utf-8;mode:c++;c-file-style:"stroustrup" -*- */ |
| 2 | /* |
| 3 | Licensed under the terms of the BSD 3-Clause |
| 4 | (see plotpy/__init__.py for details) |
| 5 | */ |
| 6 | |
| 7 | #ifndef _SCALER_HPP |
| 8 | #define _SCALER_HPP |
| 9 | |
| 10 | #include "points.hpp" |
| 11 | #include "arrays.hpp" |
| 12 | |
| 13 | /* Scaler evaluates int(a*x+b) */ |
| 14 | template <class T, bool is_int = num_trait<T>::is_integer> |
| 15 | struct Scaler |
| 16 | { |
| 17 | typedef num_trait<T> trait; |
| 18 | |
| 19 | Scaler(double _a, double _b) : a(trait::fromdouble(_a)), b(trait::fromdouble(_b)) {} |
| 20 | int scale(T x) const { return trait::toint(a * x + b); } |
| 21 | typename trait::value_type a, b; |
| 22 | }; |
| 23 | template <class T> |
| 24 | struct Scaler<T, true> |
| 25 | { |
| 26 | typedef num_trait<fixed> trait; |
| 27 | |
| 28 | Scaler(double _a, double _b) : a(trait::fromdouble(_a)), b(trait::fromdouble(_b)) {} |
| 29 | int scale(T x) const { return trait::toint(a * x + b); } |
| 30 | typename trait::value_type a, b; |
| 31 | }; |
| 32 | |
| 33 | template <class T, class D> |
| 34 | class LinearScale |
| 35 | { |
| 36 | public: |
| 37 | typedef T source_type; |
| 38 | typedef D dest_type; |
| 39 | LinearScale(double _a, double _b, D _bg, bool apply_bg) : a(_a), b(_b), bg(_bg), has_bg(apply_bg) {} |
| 40 | |
| 41 | D eval(T x) const |
| 42 | { |
| 43 | return a * x + b; |
| 44 | } |
| 45 | void set_bg(D &dest) const |
| 46 | { |
| 47 | if (has_bg) |
| 48 | dest = bg; |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | D a, b; |
| 53 | D bg; |
| 54 | bool has_bg; |
| 55 | }; |
| 56 | |
| 57 | template <class T, class D> |
| 58 | class LutScale |
| 59 | { |
| 60 | public: |
| 61 | typedef T source_type; |
| 62 | typedef D dest_type; |
| 63 | LutScale(double _a, double _b, Array1D<D> &_lut, |
| 64 | D _bg, bool apply_bg) : s(_a, _b), lut(_lut), bg(_bg), has_bg(apply_bg) {} |
| 65 | |
| 66 | D eval(T x) const |
| 67 | { |
| 68 | int val = s.scale(x); |
| 69 | if (val < 0) |
| 70 | { |
| 71 | return lut.value(0); |
| 72 | } |
| 73 | else if (val >= lut.ni) |
| 74 | { |
| 75 | return lut.value(lut.ni - 1); |
| 76 | } |
| 77 | return lut.value(val); |
| 78 | } |
| 79 | void set_bg(D &dest) const |
| 80 | { |
| 81 | if (has_bg) |
| 82 | dest = bg; |
| 83 | } |
| 84 | |
| 85 | protected: |
| 86 | Scaler<T> s; |
| 87 | Array1D<D> &lut; |
| 88 | D bg; |
| 89 | bool has_bg; |
| 90 | }; |
| 91 | |
| 92 | template <class T, class D> |
| 93 | class NoScale |
| 94 | { |
| 95 | public: |
| 96 | typedef T source_type; |
| 97 | typedef D dest_type; |
| 98 | NoScale(dest_type _bg, bool apply_bg) : bg(_bg), has_bg(apply_bg) {} |
| 99 | |
| 100 | dest_type eval(source_type x) const { return x; } |
| 101 | void set_bg(dest_type &dest) const |
| 102 | { |
| 103 | if (has_bg) |
| 104 | dest = bg; |
| 105 | } |
| 106 | |
| 107 | protected: |
| 108 | dest_type bg; |
| 109 | bool has_bg; |
| 110 | }; |
| 111 | |
| 112 | bool check_arrays(PyArrayObject *p_src, PyArrayObject *p_dest); |
| 113 | bool check_lut(PyArrayObject *p_lut); |
| 114 | |
| 115 | #endif // _SCALER_HPP |