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