isce3 0.25.0
Loading...
Searching...
No Matches
LUT2d.h
1// Source Author: Bryan Riel
2// Co-Author: Joshua Cohen
3// Copyright 2017
4//
5
6#pragma once
7
8#include "forward.h"
9
10#include <Eigen/Dense>
11#include <valarray>
12#include "Constants.h"
13#include "Linspace.h"
14#include "Matrix.h"
15#include "Utilities.h"
16
19template <typename T>
21
22 public:
23 // Constructors
24 inline LUT2d(const T& ref_value = T{0});
25 inline LUT2d(isce3::core::dataInterpMethod method);
26 LUT2d(double xstart, double ystart, double dx, double dy,
27 const isce3::core::Matrix<T> & data,
28 isce3::core::dataInterpMethod method = isce3::core::BILINEAR_METHOD,
29 bool boundsError = true);
30 LUT2d(const std::valarray<double> & xcoord,
31 const std::valarray<double> & ycoord,
32 const isce3::core::Matrix<T> & data,
33 isce3::core::dataInterpMethod method = isce3::core::BILINEAR_METHOD,
34 bool boundsError = true);
35
36 // Deep copy constructor
37 inline LUT2d(const LUT2d<T> & lut);
38
39 // Deep assignment operator
40 inline LUT2d & operator=(const LUT2d<T> & lut);
41
42 // Set data from external data.
43 // No-op if shape is 0x0, sets refValue if shape is 1x1.
44 void setFromData(const std::valarray<double> & xcoord,
45 const std::valarray<double> & ycoord,
46 const isce3::core::Matrix<T> & data);
47
48 // Get interpolator method
49 isce3::core::dataInterpMethod interpMethod() const;
50
51 // Set interpolator method
52 void interpMethod(isce3::core::dataInterpMethod method);
53
54 // Get starting X-coordinate
55 inline double xStart() const { return _xstart; }
56 // Get starting Y-coordinate
57 inline double yStart() const { return _ystart; }
58 // Get end X-coordinate
59 inline double xEnd() const { return xStart() + (width() - 1) * xSpacing(); }
60 // Get end Y-coordinate
61 inline double yEnd() const { return yStart() + (length() - 1) * ySpacing(); }
62 // Get X-spacing
63 inline double xSpacing() const { return _dx; }
64 // Get Y-spacing
65 inline double ySpacing() const { return _dy; }
66 // Get LUT length (number of lines)
67 inline size_t length() const { return _data.length(); }
68 // Get LUT width (number of samples)
69 inline size_t width() const { return _data.width(); }
70 // Coordinate axes
71 inline Linspace<double> xAxis() const {
72 return Linspace<double>(xStart(), xSpacing(), width()); }
73 inline Linspace<double> yAxis() const {
74 return Linspace<double>(yStart(), ySpacing(), length()); }
75 // Get the reference value
76 inline T refValue() const { return _refValue; }
77 // Set the reference value
78 inline void refValue(const T& val) { _refValue = val; }
79 // Get flag for having data
80 inline bool haveData() const { return _haveData; }
81 // Get bounds error flag
82 inline bool boundsError() const { return _boundsError; }
83 // Get read-only reference to data
84 inline const isce3::core::Matrix<T> & data() const { return _data; }
85
86 // Set bounds error floag
87 inline void boundsError(bool flag) { _boundsError = flag; }
88
89 // Evaluate LUT
90 T eval(const double y, const double x) const;
91
92 Eigen::Matrix<T, Eigen::Dynamic, 1>
93 eval(double y, const Eigen::Ref<const Eigen::VectorXd>& x) const;
94
96 inline bool contains(double y, double x) const
97 {
98 // Treat default-constructed LUT as having infinite extent.
99 if (not _haveData) {
100 return true;
101 }
102
103 return (x >= xStart()) and (x <= xEnd()) and
104 (y >= yStart()) and (y <= yEnd());
105 }
106
107 private:
108 // Flags
109 bool _haveData, _boundsError;
110 T _refValue;
111 // Data
112 double _xstart, _ystart, _dx, _dy;
114 // Interpolation method
116
117 private:
122 void _setInterpolator(dataInterpMethod method);
123
124 // BVR: I'm placing the comparison operator implementations inline here because
125 // it wasn't clear to me how to handle the template arguments out-of-line
126 public:
127
128 // Comparison operator for floating point
129 template <typename U = T, std::enable_if_t<!std::is_compound<U>::value, int> = 0>
130 inline bool operator==(const isce3::core::LUT2d<T> & other) const {
131 // Check coordinates and dimensions first
132 bool equal = _data.width() == other.width();
133 equal *= _data.length() == other.length();
134 if (!equal) {
135 return false;
136 }
137 equal *= isce3::core::compareFloatingPoint(_xstart, other.xStart());
138 equal *= isce3::core::compareFloatingPoint(_ystart, other.yStart());
139 equal *= isce3::core::compareFloatingPoint(_dx, other.xSpacing());
140 equal *= isce3::core::compareFloatingPoint(_dy, other.ySpacing());
141 if (!equal) {
142 return false;
143 }
144 // If we made it this far, check contents
145 const isce3::core::Matrix<T> & otherMat = other.data();
146 for (size_t i = 0; i < _data.length(); ++i) {
147 for (size_t j = 0; j < _data.width(); ++j) {
148 equal *= isce3::core::compareFloatingPoint(_data(i,j), otherMat(i,j));
149 }
150 }
151 return equal;
152 }
153
154 // Comparison operator for complex
155 template <typename U = T, std::enable_if_t<std::is_compound<U>::value, int> = 0>
156 inline bool operator==(const isce3::core::LUT2d<T> & other) const {
157 // Check coordinates and dimensions first
158 bool equal = _data.width() == other.width();
159 equal *= _data.length() == other.length();
160 if (!equal) {
161 return false;
162 }
163 equal *= isce3::core::compareFloatingPoint(_xstart, other.xStart());
164 equal *= isce3::core::compareFloatingPoint(_ystart, other.yStart());
165 equal *= isce3::core::compareFloatingPoint(_dx, other.xSpacing());
166 equal *= isce3::core::compareFloatingPoint(_dy, other.ySpacing());
167 if (!equal) {
168 return false;
169 }
170 // If we made it this far, check contents
171 const isce3::core::Matrix<T> & otherMat = other.data();
172 for (size_t i = 0; i < _data.length(); ++i) {
173 for (size_t j = 0; j < _data.width(); ++j) {
174 equal *= isce3::core::compareComplex(_data(i,j), otherMat(i,j));
175 }
176 }
177 return equal;
178 }
179};
180
181// Default constructor using bilinear interpolator
182template <typename T>
184LUT2d(const T& ref_value) : _haveData(false), _boundsError(true), _refValue(ref_value) {
185 _setInterpolator(isce3::core::BILINEAR_METHOD);
186}
187
188// Constructor with specified interpolator
189template <typename T>
190isce3::core::LUT2d<T>::
191LUT2d(isce3::core::dataInterpMethod method) : _haveData(false), _boundsError(true),
192 _refValue(0.0) {
193 _setInterpolator(method);
194}
195
196// Deep copy constructor
197template <typename T>
198isce3::core::LUT2d<T>::
199LUT2d(const isce3::core::LUT2d<T> & lut) : _haveData(lut.haveData()),
200 _boundsError(lut.boundsError()),
201 _refValue(lut.refValue()),
202 _xstart(lut.xStart()), _ystart(lut.yStart()),
203 _dx(lut.xSpacing()), _dy(lut.ySpacing()),
204 _data(lut.data()) {
205 _setInterpolator(lut.interpMethod());
206}
207
208// Deep assignment operator
209template <typename T>
210isce3::core::LUT2d<T> &
211isce3::core::LUT2d<T>::
212operator=(const LUT2d<T> & lut) {
213 _refValue = lut.refValue();
214 _xstart = lut.xStart();
215 _ystart = lut.yStart();
216 _dx = lut.xSpacing();
217 _dy = lut.ySpacing();
218 _data = lut.data();
219 _haveData = lut.haveData();
220 _boundsError = lut.boundsError();
221 _setInterpolator(lut.interpMethod());
222 return *this;
223}
Definition of parent Interpolator.
Definition Interpolator.h:18
Data structure to store 2D Lookup table.
Definition LUT2d.h:20
void setFromData(const std::valarray< double > &xcoord, const std::valarray< double > &ycoord, const isce3::core::Matrix< T > &data)
Definition LUT2d.cpp:53
bool contains(double y, double x) const
Check if point resides in domain of LUT.
Definition LUT2d.h:96
T eval(const double y, const double x) const
Definition LUT2d.cpp:127
A uniformly-spaced sequence of values over some interval.
Definition Linspace.h:9
Data structure for a 2D row-major matrix.
Definition Matrix.h:23
size_t length() const
Get matrix length.
Definition Matrix.h:77
size_t width() const
Get matrix width.
Definition Matrix.h:74

Generated for ISCE3.0 by doxygen 1.13.2.