21 #define checkVecLen(v,l) isce3::core::checkVecLenDebug(v,l,#v,__PRETTY_FUNCTION__)
22 #define check2dVecLen(v,l,w) isce3::core::check2dVecLenDebug(v,l,w,#v,__PRETTY_FUNCTION__)
24 namespace isce3 {
namespace core {
31 inline void checkVecLenDebug(
const std::vector<T> &vec,
size_t len,
const char *vec_name,
32 const char *parent_func) {
33 if (vec.size() != len) {
34 std::string errstr =
"In '" + std::string(parent_func) +
"': Vector '" +
35 std::string(vec_name) +
"' has size " +
36 std::to_string(vec.size()) +
", expected size " +
37 std::to_string(len) +
".";
38 throw std::invalid_argument(errstr);
44 inline void check2dVecLenDebug(
const std::vector<std::vector<T>> &vec,
45 size_t len,
size_t width,
const char *vec_name,
46 const char *parent_func) {
47 if ((vec.size() != len) && (vec[0].size() != width)) {
48 std::string errstr =
"In '" + std::string(parent_func) +
"': Vector '" +
49 std::string(vec_name) +
"' has size (" +
50 std::to_string(vec.size()) +
" x " +
51 std::to_string(vec[0].size()) +
"), expected size (" +
52 std::to_string(len) +
" x " + std::to_string(width) +
").";
53 throw std::invalid_argument(errstr);
62 inline std::vector<T>
arange(T low, T high, T increment) {
68 while (current < high) {
69 data.push_back(current);
81 inline std::vector<T>
linspace(T low, T high, std::size_t number) {
85 T increment = (high - low) / (number - 1);
95 for (std::size_t i = 0; i < number - 1; ++i) {
96 data.push_back(low + i * increment);
108 template <
typename T>
109 inline void linspace(T low, T high, std::vector<T> & data) {
111 int number = data.size();
112 T increment = (high - low) / (number - 1);
122 for (std::size_t i = 0; i < number; ++i) {
123 data[i] = low + i * increment;
132 template <
typename T>
133 inline void linspace(T low, T high, std::valarray<T> & data) {
135 int number = data.size();
136 T increment = (high - low) / (number - 1);
146 for (std::size_t i = 0; i < number; ++i) {
147 data[i] = low + i * increment;
157 template <
typename T>
158 inline std::vector<T>
logspace(T first, T last, std::size_t number, T base=10) {
162 T increment = (last - first) / (number - 1);
168 data.push_back(pow(base, first));
172 for (std::size_t i = 0; i < number - 1; ++i) {
173 T value = pow(base, first + i * increment);
174 data.push_back(value);
177 data.push_back(pow(base, last));
185 template <
typename T>
188 std::stringstream stream(str);
192 while (stream >> number) {
193 vec.push_back(number);
202 template <
typename T>
205 std::stringstream stream;
207 std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(stream,
" "));
216 std::string
stringFromVRT(
const char * filename,
int bandNum=1);
224 template <
typename T>
227 const T scale = std::max({
static_cast<T
>(1.0), std::abs(first), std::abs(second)});
228 return std::abs(first - second) <= std::numeric_limits<T>::epsilon() * scale;
235 template <
typename T>
246 template <
typename T>
247 inline std::valarray<bool>
248 makeMask(
const std::valarray<std::complex<T>> & v, std::complex<T> noDataValue) {
249 std::valarray<bool> mask(v.size());
250 for (
size_t i = 0; i < v.size(); ++i) {
264 template <
typename T>
265 inline std::valarray<bool>
266 makeMask(
const std::valarray<T> & v, T noDataValue) {
267 std::valarray<bool> mask(v.size());
268 for (
size_t i = 0; i < v.size(); ++i) {
280 std::valarray<double> & b,
281 std::valarray<double> & c) {
282 for (
int i = 1; i < a.size(); ++i) {
284 const double tempa = a[i];
285 const double tempb = b[i];
286 const double tempc = c[i];
287 while (j >= 0 && a[j] > tempa) {
301 std::valarray<double> & b) {
302 for (
int i = 1; i < a.size(); ++i) {
304 const double tempa = a[i];
305 const double tempb = b[i];
306 while (j >= 0 && a[j] > tempa) {
317 inline int binarySearch(
const std::valarray<double> & array,
double value) {
321 int right = array.size() - 1;
323 while (left <= right) {
324 const int middle =
static_cast<int>(std::round(0.5 * (left + right)));
325 if (left == (right - 1)) {
329 if (array[middle] <= value) {
331 }
else if (array[middle] > value) {
341 inline const T &
clamp(
const T & x,
const T & lower,
const T & upper) {
342 return std::min(upper, std::max(x, lower));
bool compareComplex(std::complex< T > first, std::complex< T > second)
Tolerance test for real and imaginary components for scalar value.
Definition: Utilities.h:237
void checkVecLenDebug(const std::vector< T > &vec, size_t len, const char *vec_name, const char *parent_func)
Inline function for input checking on vector lengths (primarily to check to see if 3D vector has the ...
Definition: Utilities.h:31
std::vector< T > linspace(T low, T high, std::size_t number)
Function to return a Matlab/Python style linspace vector.
Definition: Utilities.h:81
std::vector< T > stringToVector(const std::string &str)
Parse a space-separated string into a vector.
Definition: Utilities.h:186
const T & clamp(const T &x, const T &lower, const T &upper)
Clip a number between an upper and lower range (implements std::clamp for older GCC) ...
Definition: Utilities.h:341
std::string vectorToString(const std::vector< T > &vec)
Generate a string of space-separated values from a vector.
Definition: Utilities.h:203
bool compareFloatingPoint(T first, T second)
Combined absolute and relative tolerance test.
Definition: Utilities.h:226
std::valarray< bool > makeMask(const std::valarray< std::complex< T >> &v, std::complex< T > noDataValue)
making a boolean mask for an array based on noDataValue
Definition: Utilities.h:248
std::vector< T > arange(T low, T high, T increment)
Function to return a Python style arange vector.
Definition: Utilities.h:62
void insertionSort(std::valarray< double > &a, std::valarray< double > &b, std::valarray< double > &c)
Sort arrays a, b, c by the values in array a.
Definition: Utilities.h:279
std::vector< T > logspace(T first, T last, std::size_t number, T base=10)
Function to return a Matlab/Python style logspace vector.
Definition: Utilities.h:158
std::string stringFromVRT(const char *filename, int bandNum=1)
Read metadata from a VRT file and return an std::string.
Definition: Utilities.cpp:5
int binarySearch(const std::valarray< double > &array, double value)
Searches array for index closest to provided value.
Definition: Utilities.h:317