licornea_tools
common.h
Go to the documentation of this file.
1 #ifndef LICORNEA_COMMON_H_
2 #define LICORNEA_COMMON_H_
3 
4 #include <cstddef>
5 #include <cstdint>
6 #include <string>
7 #include <stdexcept>
8 #include <utility>
9 #include <iostream>
10 #include <initializer_list>
11 
12 #include <opencv2/core/core.hpp>
13 
14 namespace tlz {
15 
16 using real = double;
17 using byte = std::uint8_t;
18 //using uchar = std::uint8_t;
19 //using ushort = std::uint16_t;
20 
21 
22 using vec2 = cv::Vec<real, 2>;
23 using vec3 = cv::Vec<real, 3>;
24 using vec4 = cv::Vec<real, 4>;
25 
26 using mat33 = cv::Matx<real, 3, 3>;
27 using mat44 = cv::Matx<real, 4, 4>;
28 
29 inline vec3 mul_h(const mat44& mat, const vec3& vec) {
30  vec4 in(vec[0], vec[1], vec[2], 1.0);
31  vec4 out = mat * in;
32  out /= out[3];
33  return vec3(out[0], out[1], out[2]);
34 }
35 inline vec2 mul_h(const mat33& mat, const vec2& vec) {
36  vec3 in(vec[0], vec[1], 1.0);
37  vec3 out = mat * in;
38  out /= out[2];
39  return vec2(out[0], out[1]);
40 }
41 
42 
43 
44 template<typename In, typename Out, typename Func>
45 std::vector<Out> point_vec_conv_tpl(const std::vector<In>& in, Func func) {
46  std::vector<Out> out(in.size());
47  auto out_it = out.begin();
48  auto in_it = in.cbegin();
49  for(; out_it != out.end(); ++out_it, ++in_it) *out_it = func(*in_it);
50  return out;
51 }
52 
53 
54 #define POINT_CONVERT(__name__, __in__, __out__, __expr__) \
55  inline __out__ __name__(const __in__& pt) { \
56  return __expr__; \
57  } \
58  inline std::vector<__out__> __name__(const std::vector<__in__>& pts) { \
59  return point_vec_conv_tpl<__in__, __out__>(pts, static_cast<__out__(*)(const __in__&)>(&__name__)); \
60  }
61 
62 POINT_CONVERT(vec2_to_point2f, vec2, cv::Point2f, cv::Point2f(pt[0], pt[1]) )
63 POINT_CONVERT(point2f_to_vec2, cv::Point2f, vec2, vec2(pt.x, pt.y) )
64 POINT_CONVERT(vec2_to_point, vec2, cv::Point, cv::Point(pt[0], pt[1]) )
65 POINT_CONVERT(point_to_vec2, cv::Point, vec2, vec2(pt.x, pt.y) )
66 POINT_CONVERT(point_to_point2f, cv::Point, cv::Point2f, cv::Point2f(pt.x, pt.y) )
67 POINT_CONVERT(point2f_to_point, cv::Point2f, cv::Point, cv::Point(pt.x, pt.y) )
68 
69 
70 #undef POINT_CONVERT
71 
72 
73 constexpr int enter_keycode = 10;
74 constexpr int escape_keycode = 27;
75 
76 
77 constexpr real pi = 3.14159265358979323846;
78 constexpr real deg_per_rad = 180.0 / pi;
79 constexpr real rad_per_deg = pi / 180.0;
80 
81 constexpr real golden_ratio = 1.61803398874989484820;
82 
83 constexpr inline long double operator"" _deg (long double deg) {
84  return deg * rad_per_deg;
85 }
86 
87 
88 struct index_2d {
89  int x;
90  int y;
91 
92  explicit index_2d(int x_= -1, int y_ = -1) : x(x_), y(y_) { }
93  index_2d(std::initializer_list<int> lst) : x(*lst.begin()), y(*(lst.begin() + 1)) { }
94 };
95 inline bool operator==(const index_2d& a, const index_2d& b) {
96  return (a.y == b.y) && (a.x == b.x);
97 }
98 inline bool operator!=(const index_2d& a, const index_2d& b) {
99  return (a.y != b.y) || (a.x != b.x);
100 }
101 inline bool operator<(const index_2d& a, const index_2d& b) {
102  if(a.y == b.y) return (a.x < b.x);
103  else return (a.y < b.y);
104 }
105 inline bool operator<=(const index_2d& a, const index_2d& b) {
106  return (a == b) || (a < b);
107 }
108 inline bool operator>(const index_2d& a, const index_2d& b) {
109  return !(a <= b);
110 }
111 inline bool operator>=(const index_2d& a, const index_2d& b) {
112  return (a == b) || (a > b);
113 }
114 
115 
117  bool is_valid() const { return (x != -1); }
118  explicit operator bool () const { return is_valid(); }
119 
120  bool is_1d() const { return (y == -1); }
121  bool is_2d() const { return (y != -1); }
122 
123  explicit view_index(int x_ = -1, int y_ = -1) : index_2d(x_, y_) { }
124 };
125 
126 std::string encode_view_index(view_index idx);
127 view_index decode_view_index(const std::string& key);
128 
129 std::ostream& operator<<(std::ostream&, const view_index&);
130 
131 
132 
133 
134 
135 }
136 
137 #endif
index_2d(int x_=-1, int y_=-1)
Definition: common.h:92
bool operator!=(const rgb_color &a, const rgb_color &b)
Definition: color.cc:71
view_index(int x_=-1, int y_=-1)
Definition: common.h:123
constexpr real deg_per_rad
Definition: common.h:78
index_2d(std::initializer_list< int > lst)
Definition: common.h:93
cv::Vec< real, 4 > vec4
Definition: common.h:24
constexpr real golden_ratio
Definition: common.h:81
std::string encode_view_index(view_index idx)
Definition: dataset.cc:275
constexpr real pi
Definition: common.h:77
Definition: color.h:65
bool is_valid() const
Definition: common.h:117
cv::Vec< real, 2 > vec2
Definition: common.h:22
std::vector< Out > point_vec_conv_tpl(const std::vector< In > &in, Func func)
Definition: common.h:45
std::ostream & operator<<(std::ostream &, const view_index &)
Definition: dataset.cc:290
vec3 mul_h(const mat44 &mat, const vec3 &vec)
Definition: common.h:29
#define POINT_CONVERT(__name__, __in__, __out__, __expr__)
Definition: common.h:54
bool operator<(const index_2d &a, const index_2d &b)
Definition: common.h:101
cv::Matx< real, 3, 3 > mat33
Definition: common.h:26
constexpr int escape_keycode
Definition: common.h:74
double real
Definition: common.h:16
bool operator<=(const index_2d &a, const index_2d &b)
Definition: common.h:105
constexpr int enter_keycode
Definition: common.h:73
cv::Vec< real, 3 > vec3
Definition: common.h:23
bool operator>=(const index_2d &a, const index_2d &b)
Definition: common.h:111
bool operator==(const rgb_color &a, const rgb_color &b)
Definition: color.cc:67
constexpr real rad_per_deg
Definition: common.h:79
view_index decode_view_index(const std::string &key)
Definition: dataset.cc:281
bool operator>(const index_2d &a, const index_2d &b)
Definition: common.h:108
std::uint8_t byte
Definition: common.h:17
bool is_1d() const
Definition: common.h:120
bool is_2d() const
Definition: common.h:121
cv::Matx< real, 4, 4 > mat44
Definition: common.h:27