SOPT
Sparse OPTimisation
utilities.h
Go to the documentation of this file.
1 #ifndef SOPT_UTILITIES_H
2 #define SOPT_UTILITIES_H
3 
4 #include <Eigen/Core>
5 #include "sopt/config.h"
6 #include "sopt/types.h"
7 
8 #include <string>
9 
10 namespace sopt::utilities {
11 
13 sopt::Image<> read_tiff(std::string const &name);
15 void write_tiff(Image<> const &image, std::string const &filename);
16 
18 struct bad_lexical_cast : public std::runtime_error {
19  bad_lexical_cast(const std::string& what) : std::runtime_error(what) {}
20 };
21 
23 template<typename T, typename U>
24 T lexical_cast(const U& in) {
25  try {
26  std::stringstream ss;
27  ss << in;
28  T out;
29  ss >> out;
30  return out;
31  } catch(const std::exception& e) {
32  throw bad_lexical_cast(e.what());
33  }
34 }
35 
37 template <typename T = std::string>
38 std::vector<T> split(std::string s, const std::string& sep) {
39  std::vector<T> rtn;
40  while (true) {
41  const size_t delim_pos = s.find(sep);
42  if (delim_pos == std::string::npos) break;
43  const std::string sub = s.substr(0, delim_pos);
44  if (sub.length()) {
45  if constexpr(std::is_same<T, std::string>::value) {
46  rtn.push_back(sub);
47  }
48  else {
49  rtn.push_back(lexical_cast<T>(sub));
50  }
51  }
52  s.replace(0, delim_pos+1, "");
53  }
54  // check for trailing component
55  if (s.length()) {
56  if constexpr(std::is_same<T, std::string>::value) {
57  rtn.push_back(s);
58  }
59  else {
60  rtn.push_back(lexical_cast<T>(s));
61  }
62  }
63  return rtn;
64 }
65 
66 template <typename T>
67 std::vector<float> imageToFloat(sopt::Vector<T> const &image) {
68  std::vector<float> float_image(image.size());
69  for (int i = 0; i < image.size(); i++) {
70  if constexpr(std::is_same<T, t_complex>::value)
71  {
72  float_image[i] = image[i].real();
73  }
74  else
75  {
76  float_image[i] = static_cast<float>(image[i]);
77  }
78  }
79  return float_image;
80 }
81 
82 template <typename T>
83 sopt::Vector<T> floatToImage(std::vector<float> const &float_image) {
84  sopt::Vector<T> image(float_image.size());
85  for (int i = 0; i < float_image.size(); i++) {
86  if constexpr(std::is_same<T, t_complex>::value)
87  {
88  image[i] = t_complex(float_image[i], 0);
89  }
90  else
91  {
92  image[i] = static_cast<T>(float_image[i]);
93  }
94  }
95  return image;
96 }
97 
98 } // namespace sopt::utilities
99 #endif
void write_tiff(Image<> const &image, std::string const &filename)
Writes a tiff greyscale file.
Definition: utilities.cc:68
std::vector< T > split(std::string s, const std::string &sep)
Split a string on a specified delimiter with optional cast to another type.
Definition: utilities.h:38
Image read_tiff(std::string const &filename)
Reads tiff image.
Definition: utilities.cc:38
T lexical_cast(const U &in)
Convert between any types via stringstream.
Definition: utilities.h:24
sopt::Vector< T > floatToImage(std::vector< float > const &float_image)
Definition: utilities.h:83
std::vector< float > imageToFloat(sopt::Vector< T > const &image)
Definition: utilities.h:67
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic > Image
A 2-dimensional list of elements of given type.
Definition: types.h:39
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
std::complex< t_real > t_complex
Root of the type hierarchy for (real) complex numbers.
Definition: types.h:19
Exception to be thrown by lexical_cast (below)
Definition: utilities.h:18
bad_lexical_cast(const std::string &what)
Definition: utilities.h:19
sopt::Image< Scalar > Image
Definition: inpainting.cc:30