Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hugin_math.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
27 #ifndef _HUGIN_MATH_HUGIN_MATH_H
28 #define _HUGIN_MATH_HUGIN_MATH_H
29 
30 #include <hugin_shared.h>
31 #include <cmath>
32 #include <math.h>
33 #include <iostream>
34 #include <vigra/diff2d.hxx>
35 
36 #ifndef M_PI
37  #define M_PI 3.14159265358979323846
38 #endif
39 
40 #ifndef PI
41  #define PI 3.14159265358979323846
42 #endif
43 
44 #define DEG_TO_RAD( x ) ( (x) * 2.0 * PI / 360.0 )
45 #define RAD_TO_DEG( x ) ( (x) * 360.0 / ( 2.0 * PI ) )
46 
48 namespace hugin_utils
49 {
50  inline double round(double x)
51  {
52  return floor(x+0.5);
53  }
54 
55  inline float roundf(float x)
56  {
57  return (float) floor(x+0.5f);
58  }
59 
60  inline int ceili(double x)
61  {
62  return (int) ceil(x);
63  }
64 
65  inline int floori(double x)
66  {
67  return (int) floor(x);
68  }
69 
70 
71 
72  template <class T>
73  inline int roundi(T x)
74  {
75  return ((x < 0.0) ?
76  ((x < (float)INT_MIN) ? INT_MIN : static_cast<int>(x - 0.5)) :
77  ((x > (float)INT_MAX) ? INT_MAX : static_cast<int>(x + 0.5)));
78  }
79 
80  // a simple point class
81  template <class T>
82  struct TDiff2D
83  {
85  : x(0), y(0)
86  { }
87  TDiff2D(T x, T y)
88  : x(x), y(y)
89  { }
90  TDiff2D(const vigra::Diff2D &d)
91  : x(d.x), y(d.y)
92  { }
93 
94  bool operator==(TDiff2D rhs) const
95  {
96  return x == rhs.x && y == rhs.y;
97  }
98 
99  bool operator!=(TDiff2D rhs) const
100  {
101  return x != rhs.x || y != rhs.y;
102  }
103 
105  {
106  return TDiff2D (x+rhs.x, y+rhs.y);
107  }
108 
110  {
111  return TDiff2D (x-rhs.x, y-rhs.y);
112  }
113 
114  TDiff2D & operator*=(double val)
115  {
116  x = x*val;
117  y = y*val;
118  return *this;
119  }
120 
121  TDiff2D operator*(double val) const
122  {
123  TDiff2D<T> result;
124  result.x = x * val;
125  result.y = y * val;
126  return result;
127  }
128 
129  double operator*(TDiff2D val) const
130  {
131  return x * val.x + y * val.y;
132  }
133 
134  vigra::Diff2D toDiff2D() const
135  {
136  return vigra::Diff2D(roundi(x), roundi(y));
137  }
138 
140  T squareDistance(TDiff2D<T> other) const
141  {
142  return (other - *this).squareLength();
143  }
144 
146  T squareLength() const
147  {
148  return x*x + y*y;
149  }
150 
151  double x,y;
152  };
153 
156 
157 
162  template <class T>
163  T simpleClipPoint(const T & point, const T & min, const T & max)
164  {
165  T p(point);
166  if (p.x < min.x) p.x = min.x;
167  if (p.x > max.x) p.x = max.x;
168  if (p.y < min.y) p.y = min.y;
169  if (p.y > max.y) p.y = max.y;
170  return p;
171  }
172 
173  template <class T>
174  T sqr(T t)
175  {
176  return t*t;
177  }
178 
179  template <class T>
180  double norm(T t)
181  {
182  return sqrt(t.x*t.x + t.y*t.y);
183  }
184 
187  template <class InputIterator1, class InputIterator2>
188  double euclid_dist(InputIterator1 first1, InputIterator1 last1,
189  InputIterator2 first2)
190  {
191  typename InputIterator1::value_type res = 0;
192  InputIterator1 i(first1);
193  while (i != last1) {
194  double a = *i;
195  double b = *(first2 + (i - first1));
196  res = res + a*a + b*b;
197  ++i;
198  }
199  return sqrt(res);
200  }
201 
204  template <class InputIterator1, class InputIterator2, class T>
205  T sqr_dist(InputIterator1 first1, InputIterator1 last1,
206  InputIterator2 first2, T res)
207  {
208  InputIterator1 i(first1);
209  while (i != last1) {
210  T a = (T)(*i) - (T) (*(first2 + (i - first1)));
211  res = res + a*a;
212  ++i;
213  }
214  return res;
215  }
216 
221  template <class POINT>
222  vigra::Rect2D calcCircleROIFromPoints(const POINT& p1, const POINT & p2)
223  {
224  double dx = p2.x - p1.x;
225  double dy = p2.y - p1.y;
226  double r = sqrt(dx*dx + dy*dy) / 2.0;
227  double mx = p1.x + dx/2;
228  double my = p1.y + dy/2;
229 
230  vigra::Rect2D rect;
231  rect.setUpperLeft(vigra::Point2D(roundi(mx-r), roundi(my -r)));
232  rect.setLowerRight(vigra::Point2D(roundi(mx+r), roundi(my+r)));
233  return rect;
234  }
235 
238  int IMPEX gcd(int a, int b);
239 
240 } // namespace
241 
242 template <class T>
243 inline std::ostream & operator<<(std::ostream & o, const hugin_utils::TDiff2D<T> & d)
244 {
245  return o << "( " << d.x << " " << d.y << " )";
246 }
247 
248 inline hugin_utils::FDiff2D operator/(const hugin_utils::FDiff2D & lhs, double val)
249 {
250  return hugin_utils::FDiff2D(lhs.x/val, lhs.y/val);
251 }
252 
253 // uses ceil for rounding.
254 inline vigra::Diff2D operator*(const vigra::Diff2D & d, double scale)
255 {
256  return vigra::Diff2D((int)(ceil(d.x * scale)),
257  (int)(ceil(d.y * scale)));
258 }
259 
261 inline vigra::Rect2D operator*(const vigra::Rect2D & r, double scale)
262 {
263  return vigra::Rect2D( (int)floor(r.left()*scale),
264  (int)floor(r.top()*scale),
265  (int)ceil(r.right()*scale),
266  (int)ceil(r.bottom()*scale));
267 }
268 
269 #endif // _H
int floori(double x)
Definition: hugin_math.h:65
int gcd(int a, int b)
function to calculate greated common divisor using Euclidean algorithm both arguments should be &gt;=0 ...
Definition: hugin_math.cpp:35
double norm(T t)
Definition: hugin_math.h:180
int roundi(T x)
Definition: hugin_math.h:73
T sqr_dist(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T res)
calculate squared Euclidean distance between two vectors.
Definition: hugin_math.h:205
double euclid_dist(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
calculate squared Euclidean distance between two vectors.
Definition: hugin_math.h:188
float roundf(float x)
Definition: hugin_math.h:55
T sqr(T t)
Definition: hugin_math.h:174
vigra::Diff2D operator*(const vigra::Diff2D &d, double scale)
Definition: hugin_math.h:254
double operator*(TDiff2D val) const
Definition: hugin_math.h:129
double round(double x)
Definition: hugin_math.h:50
bool operator==(TDiff2D rhs) const
Definition: hugin_math.h:94
int ceili(double x)
Definition: hugin_math.h:60
bool operator!=(TDiff2D rhs) const
Definition: hugin_math.h:99
TDiff2D operator-(TDiff2D rhs) const
Definition: hugin_math.h:109
TDiff2D operator+(TDiff2D rhs) const
Definition: hugin_math.h:104
TDiff2D< double > FDiff2D
Definition: hugin_math.h:155
hugin_utils::FDiff2D operator/(const hugin_utils::FDiff2D &lhs, double val)
Definition: hugin_math.h:248
#define IMPEX
Definition: hugin_shared.h:39
T squareLength() const
Return the square of the length of the vector.
Definition: hugin_math.h:146
TDiff2D & operator*=(double val)
Definition: hugin_math.h:114
static T max(T x, T y)
Definition: svm.cpp:65
T simpleClipPoint(const T &point, const T &min, const T &max)
clip a point to fit int [min, max] does not do a mathematical clipping, just sets p...
Definition: hugin_math.h:163
vigra::Diff2D toDiff2D() const
Definition: hugin_math.h:134
TDiff2D operator*(double val) const
Definition: hugin_math.h:121
static T min(T x, T y)
Definition: svm.cpp:62
TDiff2D(const vigra::Diff2D &d)
Definition: hugin_math.h:90
T squareDistance(TDiff2D< T > other) const
Return square of the distance to another point.
Definition: hugin_math.h:140
vigra::Rect2D calcCircleROIFromPoints(const POINT &p1, const POINT &p2)
calculate the bounding box of a circle that goes through both points.
Definition: hugin_math.h:222