OpenANN  1.1.0
An open source library for artificial neural networks.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SigmaPiConstraints.h
Go to the documentation of this file.
1 #ifndef OPENANN_LAYERS_SIGMA_PI_CONSTRAINTS_H_
2 #define OPENANN_LAYERS_SIGMA_PI_CONSTRAINTS_H_
3 
6 #include <iostream>
7 #include <map>
8 
9 namespace OpenANN
10 {
11 
16 {
17  DistanceConstraint(size_t width, size_t height)
18  : width(width), height(height)
19  {}
20 
21  virtual ~DistanceConstraint() {}
22 
23  virtual double operator()(int p1, int p2) const
24  {
25  double x1 = p1 % width;
26  double y1 = p1 / width;
27  double x2 = p2 % width;
28  double y2 = p2 / width;
29 
30  OPENANN_CHECK(y1 < height);
31  OPENANN_CHECK(y2 < height);
32 
33  return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
34  }
35 
36 private:
37  size_t width;
38  size_t height;
39 };
40 
41 
46 {
47  SlopeConstraint(size_t width, size_t height)
48  : width(width), height(height)
49  {
50  }
51 
52  virtual ~SlopeConstraint() {}
53 
54  virtual double operator()(int p1, int p2) const
55  {
56  double x1 = p1 % width;
57  double y1 = p1 / width;
58  double x2 = p2 % width;
59  double y2 = p2 / width;
60 
61  OPENANN_CHECK(y1 < height);
62  OPENANN_CHECK(y2 < height);
63 
64  return (x1 == x2) ? (M_PI / 2) : std::atan((y2 - y1) / (x2 - x1));
65  }
66 
67 private:
68  size_t width;
69  size_t height;
70 };
71 
72 
77 {
78  // Simple AngleTuple for storing in a std::map
79  struct AngleTuple
80  {
81  AngleTuple(double a, double b, double c) : alpha(a), beta(b), gamma(c)
82  {}
83 
84  bool operator< (const AngleTuple& tuple) const
85  {
86  if(fabs(alpha - tuple.alpha) > 0.001)
87  return alpha < tuple.alpha;
88  else if(fabs(beta - tuple.beta) > 0.001)
89  return beta < tuple.beta;
90  else
91  return fabs(gamma - tuple.gamma) > 0.001 && gamma < tuple.gamma;
92  }
93 
94  double alpha;
95  double beta;
96  double gamma;
97  };
98 
105  TriangleConstraint(size_t width, size_t height, double resolution = M_PI / 4)
106  : width(width), height(height), resolution(resolution)
107  {}
108 
109  virtual ~TriangleConstraint() {}
110 
111  virtual double operator()(int p1, int p2, int p3) const
112  {
113  int nr = partition.size() / 3.0;
114 
115  // p1 is always the top left point from the correlation
116  int x1 = p1 % width;
117  int y1 = p1 / width;
118 
119  int x2 = p2 % width;
120  int y2 = p2 / width;
121 
122  int x3 = p3 % width;
123  int y3 = p3 / width;
124 
125  // p2 should be always located at the right side of p1
126  if(x2 < x3)
127  {
128  std::swap(x2, x3);
129  std::swap(y2, y3);
130  }
131 
132  int as = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
133  int bs = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
134  int cs = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
135 
136  // compute the angles
137  double alpha = std::floor(std::acos((as - bs - cs) / (-2 * std::sqrt(bs * cs))) / resolution);
138  double beta = std::floor(std::acos((bs - cs - as) / (-2 * std::sqrt(cs * as))) / resolution);
139  double gamma = std::floor(std::acos((cs - as - bs) / (-2 * std::sqrt(as * bs))) / resolution);
140 
141  AngleTuple t1(alpha, beta, gamma);
142  AngleTuple t2(beta, gamma, alpha);
143  AngleTuple t3(gamma, alpha, beta);
144 
145  std::map<AngleTuple, int>::const_iterator it = partition.find(t1);
146 
147  std::map<AngleTuple, int>& p = const_cast<std::map<AngleTuple, int>&>(partition);
148 
149  if(it == partition.end())
150  {
151  p[t1] = nr;
152  p[t2] = nr;
153  p[t3] = nr;
154  }
155  else
156  {
157  return it->second;
158  }
159 
160  return nr;
161  }
162 
163 private:
164  std::map<AngleTuple, int> partition;
165 
166  size_t width;
167  size_t height;
168  double resolution;
169 };
170 
171 } // namespace OpenANN
172 
173 #endif // OPENANN_LAYERS_SIGMA_PI_CONSTRAINTS_H_