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  x += rhs.x;
112  y += rhs.y;
113  return *this;
114  }
115 
117  {
118  return TDiff2D (x-rhs.x, y-rhs.y);
119  }
120 
121  TDiff2D & operator*=(double val)
122  {
123  x = x*val;
124  y = y*val;
125  return *this;
126  }
127 
128  TDiff2D operator*(double val) const
129  {
130  TDiff2D<T> result;
131  result.x = x * val;
132  result.y = y * val;
133  return result;
134  }
135 
136  double operator*(TDiff2D val) const
137  {
138  return x * val.x + y * val.y;
139  }
140 
141  vigra::Diff2D toDiff2D() const
142  {
143  return vigra::Diff2D(roundi(x), roundi(y));
144  }
145 
147  T squareDistance(TDiff2D<T> other) const
148  {
149  return (other - *this).squareLength();
150  }
151 
153  T squareLength() const
154  {
155  return x*x + y*y;
156  }
157 
158  double x,y;
159  };
160 
163 
164 
169  template <class T>
170  T simpleClipPoint(const T & point, const T & min, const T & max)
171  {
172  T p(point);
173  if (p.x < min.x) p.x = min.x;
174  if (p.x > max.x) p.x = max.x;
175  if (p.y < min.y) p.y = min.y;
176  if (p.y > max.y) p.y = max.y;
177  return p;
178  }
179 
180  template <class T>
181  T sqr(T t)
182  {
183  return t*t;
184  }
185 
186  template <class T>
187  double norm(T t)
188  {
189  return sqrt(t.x*t.x + t.y*t.y);
190  }
191 
194  template <class InputIterator1, class InputIterator2>
195  double euclid_dist(InputIterator1 first1, InputIterator1 last1,
196  InputIterator2 first2)
197  {
198  typename InputIterator1::value_type res = 0;
199  InputIterator1 i(first1);
200  while (i != last1) {
201  double a = *i;
202  double b = *(first2 + (i - first1));
203  res = res + a*a + b*b;
204  ++i;
205  }
206  return sqrt(res);
207  }
208 
211  template <class InputIterator1, class InputIterator2, class T>
212  T sqr_dist(InputIterator1 first1, InputIterator1 last1,
213  InputIterator2 first2, T res)
214  {
215  InputIterator1 i(first1);
216  while (i != last1) {
217  T a = (T)(*i) - (T) (*(first2 + (i - first1)));
218  res = res + a*a;
219  ++i;
220  }
221  return res;
222  }
223 
228  template <class POINT>
229  vigra::Rect2D calcCircleROIFromPoints(const POINT& p1, const POINT & p2)
230  {
231  double dx = p2.x - p1.x;
232  double dy = p2.y - p1.y;
233  double r = sqrt(dx*dx + dy*dy) / 2.0;
234  double mx = p1.x + dx/2;
235  double my = p1.y + dy/2;
236 
237  vigra::Rect2D rect;
238  rect.setUpperLeft(vigra::Point2D(roundi(mx-r), roundi(my -r)));
239  rect.setLowerRight(vigra::Point2D(roundi(mx+r), roundi(my+r)));
240  return rect;
241  }
242 
245  int IMPEX gcd(int a, int b);
246 
247 } // namespace
248 
249 template <class T>
250 inline std::ostream & operator<<(std::ostream & o, const hugin_utils::TDiff2D<T> & d)
251 {
252  return o << "( " << d.x << " " << d.y << " )";
253 }
254 
255 inline hugin_utils::FDiff2D operator/(const hugin_utils::FDiff2D & lhs, double val)
256 {
257  return hugin_utils::FDiff2D(lhs.x/val, lhs.y/val);
258 }
259 
260 // uses ceil for rounding.
261 inline vigra::Diff2D operator*(const vigra::Diff2D & d, double scale)
262 {
263  return vigra::Diff2D((int)(ceil(d.x * scale)),
264  (int)(ceil(d.y * scale)));
265 }
266 
268 inline vigra::Rect2D operator*(const vigra::Rect2D & r, double scale)
269 {
270  return vigra::Rect2D( (int)floor(r.left()*scale),
271  (int)floor(r.top()*scale),
272  (int)ceil(r.right()*scale),
273  (int)ceil(r.bottom()*scale));
274 }
275 
276 #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:187
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:212
double euclid_dist(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
calculate squared Euclidean distance between two vectors.
Definition: hugin_math.h:195
float roundf(float x)
Definition: hugin_math.h:55
T sqr(T t)
Definition: hugin_math.h:181
vigra::Diff2D operator*(const vigra::Diff2D &d, double scale)
Definition: hugin_math.h:261
double operator*(TDiff2D val) const
Definition: hugin_math.h:136
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:116
TDiff2D operator+(TDiff2D rhs) const
Definition: hugin_math.h:104
TDiff2D< double > FDiff2D
Definition: hugin_math.h:162
hugin_utils::FDiff2D operator/(const hugin_utils::FDiff2D &lhs, double val)
Definition: hugin_math.h:255
#define IMPEX
Definition: hugin_shared.h:39
T squareLength() const
Return the square of the length of the vector.
Definition: hugin_math.h:153
TDiff2D & operator*=(double val)
Definition: hugin_math.h:121
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:170
TDiff2D operator+=(TDiff2D rhs)
Definition: hugin_math.h:109
vigra::Diff2D toDiff2D() const
Definition: hugin_math.h:141
TDiff2D operator*(double val) const
Definition: hugin_math.h:128
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:147
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:229