Direct Graphical Models  v.1.7.0
macroses.h
1 #pragma once
2 
3 namespace DirectGraphicalModels {
4 #ifndef _CRT_STRINGIZE
5  #define __CRT_STRINGIZE(_Value) #_Value
6  #define _CRT_STRINGIZE(_Value) __CRT_STRINGIZE(_Value)
7 #endif
8 #define __ATTRIBUTES__ " in \"" __FILE__ "\", line " _CRT_STRINGIZE(__LINE__) ""
9 #define DGM_ASSERT(_condition_) \
10  do { \
11  if (!(_condition_)) { \
12  printf("Assertion failed: %s", #_condition_ __ATTRIBUTES__); \
13  abort (); \
14  } \
15  } while (0)
16 
17 #define DGM_ASSERT_MSG(_condition_, _format_, ...) \
18  do { \
19  if (!(_condition_)) { \
20  printf("Assertion failed: %s\n", #_condition_ __ATTRIBUTES__); \
21  printf(_format_"\n", ##__VA_ARGS__); \
22  abort (); \
23  } \
24  } while (0)
25 
26 #define DGM_IF_WARNING(_condition_, _format_, ...) \
27  do { \
28  if (_condition_) { \
29  printf("WARNING: %s:\n", #_condition_ __ATTRIBUTES__); \
30  printf(_format_"\n", ##__VA_ARGS__); \
31  } \
32  } while (0)
33 
34 #define DGM_WARNING(_format_, ...) \
35  do { \
36  printf("WARNING:%s:\n", __ATTRIBUTES__); \
37  printf(_format_"\n", ##__VA_ARGS__); \
38  } while (0)
39 
40 #define SIGN(a) (((a) >= 0) ? 1 : -1)
41 
42 
43  // Approximative pow() function
44  // Taken from: http://martin.ankerl.com/2012/01/25/optimized-approximative-pow-in-c-and-cpp/
45  inline double fastPow(double a, double b)
46  {
47  union {
48  double d;
49  int x[2];
50  } u = { a };
51  u.x[1] = (int)(b * (u.x[1] - 1072632447) + 1072632447);
52  u.x[0] = 0;
53  return u.d;
54  }
55 
56  //template<typename T>
57  //inline T SIGN(T a) {return (a >= 0) ? 1 : -1;}
58 
59  // For: CPriorNode::addNodeGroundtruth
60  template<typename T, void (T::*SomeMethod)(byte b)>
61  inline void DGM_ELEMENTWISE1(T &self, const Mat &m)
62  {
63  // Assertions
64  DGM_ASSERT(m.type() == CV_8UC1);
65 
66  for (int y = 0; y < m.rows; y++) {
67  const byte *pM = m.ptr<byte>(y);
68  for (int x = 0; x < m.cols; x++)
69  (self.*SomeMethod)(pM[x]);
70  }
71  }
72 
73  // For: CCmat::estimate()
74  template<typename T, void (T::*SomeMethod)(byte b1, byte b2)>
75  inline void DGM_ELEMENTWISE2(T &self, const Mat &m1, const Mat &m2)
76  {
77  // Assertions
78  DGM_ASSERT(m1.size() == m2.size());
79  DGM_ASSERT(m1.type() == m2.type());
80  DGM_ASSERT(m1.type() == CV_8UC1);
81 
82  for (int y = 0; y < m1.rows; y++) {
83  const byte *pM1 = m1.ptr<byte>(y);
84  const byte *pM2 = m2.ptr<byte>(y);
85  for (int x = 0; x < m1.cols; x++)
86  (self.*SomeMethod)(pM1[x], pM2[x]);
87  }
88  }
89 
90  template<typename T, void (T::*SomeMethod)(byte b1, byte b2)>
91  inline void DGM_ELEMENTWISE2(T &self, const Mat &m1, const Mat &m2, const Mat &mask)
92  {
93  // Assertions
94  DGM_ASSERT((m1.size() == m2.size()) && (m2.size() == mask.size()));
95  DGM_ASSERT((m1.type() == m2.type()) && mask.type() == CV_8UC1);
96  DGM_ASSERT(m1.type() == CV_8UC1);
97 
98  for (int y = 0; y < m1.rows; y++) {
99  const byte *pM1 = m1.ptr<byte>(y);
100  const byte *pM2 = m2.ptr<byte>(y);
101  const byte *pMask = mask.ptr<byte>(y);
102  for (int x = 0; x < m1.cols; x++)
103  if (pMask[x]) (self.*SomeMethod)(pM1[x], pM2[x]);
104  }
105  }
106 
107  // For: CTrainNode::addFeatureVec()
108  template<typename T, void (T::*SomeMethod)(const Mat &vec, byte b)>
109  inline void DGM_VECTORWISE1(T &self, const Mat &m1, const Mat &m2)
110  {
111  // Assertions
112  DGM_ASSERT(m1.size() == m2.size());
113  DGM_ASSERT(m1.depth() == CV_8U);
114  DGM_ASSERT(m2.type() == CV_8UC1);
115 
116  Mat vec(m1.channels(), 1, CV_8UC1);
117  for (int y = 0; y < m2.rows; y++) {
118  const byte *pM1 = m1.ptr<byte>(y);
119  const byte *pM2 = m2.ptr<byte>(y);
120  for (int x = 0; x < m2.cols; x++) {
121  for (int f = 0; f < vec.rows; f++) vec.at<byte>(f, 0) = pM1[vec.rows * x + f];
122  (self.*SomeMethod)(vec, pM2[x]);
123  } // x
124  } // y
125  }
126 
127  template<typename T, void (T::*SomeMethod)(const Mat &vec, byte b)>
128  inline void DGM_VECTORWISE1(T &self, const vec_mat_t &m1, const Mat &m2)
129  {
130  // Assertions
131  DGM_ASSERT(m1[0].size() == m2.size());
132  DGM_ASSERT(m1[0].type() == CV_8UC1);
133  DGM_ASSERT(m2.type() == CV_8UC1);
134 
135  const word nFeatures = static_cast<word>(m1.size());
136  Mat vec(nFeatures, 1, CV_8UC1);
137  std::vector<const byte *> vM1(nFeatures);
138  for (int y = 0; y < m2.rows; y++) {
139  for (int f = 0; f < vec.rows; f++) vM1[f] = m1[f].ptr<byte>(y);
140  const byte *pM2 = m2.ptr<byte>(y);
141  for (int x = 0; x < m2.cols; x++) {
142  for (int f = 0; f < vec.rows; f++) vec.at<byte>(f, 0) = vM1[f][x];
143  (self.*SomeMethod)(vec, pM2[x]);
144  } // x
145  } // y
146  }
147 
148  // For: CTrinLink::addFeatureVec()
149  template<typename T, void (T::*SomeMethod)(const Mat &vec, byte b1, byte b2)>
150  inline void DGM_VECTORWISE2(T &self, const Mat &m1, const Mat &m2, const Mat &m3)
151  {
152  // Assertions
153  DGM_ASSERT(m1.size() == m2.size());
154  DGM_ASSERT(m1.size() == m3.size());
155  DGM_ASSERT(m1.depth() == CV_8U);
156  DGM_ASSERT(m2.type() == CV_8UC1);
157  DGM_ASSERT(m3.type() == CV_8UC1);
158 
159  Mat vec(m1.channels(), 1, CV_8UC1);
160  for (int y = 0; y < m2.rows; y++) {
161  const byte *pM1 = m1.ptr<byte>(y);
162  const byte *pM2 = m2.ptr<byte>(y);
163  const byte *pM3 = m3.ptr<byte>(y);
164  for (int x = 0; x < m2.cols; x++) {
165  for (int f = 0; f < vec.rows; f++) vec.at<byte>(f, 0) = pM1[vec.rows * x + f];
166  (self.*SomeMethod)(vec, pM2[x], pM3[x]);
167  } // x
168  } // y
169  }
170 
171  template<typename T, void (T::*SomeMethod)(const Mat &vec, byte b1, byte b2)>
172  inline void DGM_VECTORWISE2(T &self, const vec_mat_t &m1, const Mat &m2, const Mat &m3)
173  {
174  // Assertions
175  DGM_ASSERT(m1[0].size() == m2.size());
176  DGM_ASSERT(m1[0].size() == m3.size());
177  DGM_ASSERT(m1[0].type() == CV_8UC1);
178  DGM_ASSERT(m2.type() == CV_8UC1);
179  DGM_ASSERT(m3.type() == CV_8UC1);
180 
181  const word nFeatures = static_cast<word>(m1.size());
182  Mat vec(nFeatures, 1, CV_8UC1);
183  std::vector<const byte *> vM1(nFeatures);
184  for (int y = 0; y < m2.rows; y++) {
185  for (int f = 0; f < vec.rows; f++) vM1[f] = m1[f].ptr<byte>(y);
186  const byte *pM2 = m2.ptr<byte>(y);
187  const byte *pM3 = m3.ptr<byte>(y);
188  for (int x = 0; x < m2.cols; x++) {
189  for (int f = 0; f < vec.rows; f++) vec.at<byte>(f, 0) = vM1[f][x];
190  (self.*SomeMethod)(vec, pM2[x], pM3[x]);
191  } // x
192  } // y
193  }
194 }
void DGM_VECTORWISE1(T &self, const Mat &m1, const Mat &m2)
Definition: macroses.h:109
double fastPow(double a, double b)
Definition: macroses.h:45
void DGM_ELEMENTWISE2(T &self, const Mat &m1, const Mat &m2)
Definition: macroses.h:75
void DGM_ELEMENTWISE1(T &self, const Mat &m)
Definition: macroses.h:61
void DGM_VECTORWISE2(T &self, const Mat &m1, const Mat &m2, const Mat &m3)
Definition: macroses.h:150