hugin_math.h

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00027 #ifndef _HUGIN_MATH_HUGIN_MATH_H
00028 #define _HUGIN_MATH_HUGIN_MATH_H
00029 
00030 #include <hugin_shared.h>
00031 #include <cmath>
00032 #include <math.h>
00033 #include <iostream>
00034 #include <vigra/diff2d.hxx>
00035 
00036 #ifndef M_PI
00037         #define M_PI 3.14159265358979323846
00038 #endif
00039 
00040 #ifndef PI
00041         #define PI 3.14159265358979323846
00042 #endif
00043 
00044 #define DEG_TO_RAD( x )         ( (x) * 2.0 * PI / 360.0 )
00045 #define RAD_TO_DEG( x )         ( (x) * 360.0 / ( 2.0 * PI ) )
00046 
00048 namespace hugin_utils
00049 {
00050     inline double round(double x)
00051     {
00052         return floor(x+0.5);
00053     }
00054 
00055     inline float roundf(float x)
00056     {
00057         return (float) floor(x+0.5f);
00058     }
00059 
00060     inline int ceili(double x)
00061     {
00062         return (int) ceil(x);
00063     }
00064 
00065     inline int floori(double x)
00066     {
00067         return (int) floor(x);
00068     }
00069 
00070     
00071     
00072     template <class T>
00073     inline int roundi(T x)
00074     {
00075         return ((x < 0.0) ?
00076                     ((x < (float)INT_MIN) ? INT_MIN : static_cast<int>(x - 0.5)) :
00077                     ((x > (float)INT_MAX) ? INT_MAX : static_cast<int>(x + 0.5)));
00078     }
00079 
00080     // a simple point class
00081     template <class T>
00082     struct TDiff2D
00083     {
00084         TDiff2D()
00085             : x(0), y(0)
00086             { }
00087         TDiff2D(T x, T y)
00088             : x(x), y(y)
00089             { }
00090         TDiff2D(const vigra::Diff2D &d)
00091             : x(d.x), y(d.y)
00092             { }
00093 
00094         bool operator==(TDiff2D rhs) const
00095             {
00096                 return x == rhs.x &&  y == rhs.y;
00097             }
00098         
00099         bool operator!=(TDiff2D rhs) const
00100             {
00101                 return x != rhs.x || y != rhs.y;
00102             }
00103 
00104         TDiff2D operator+(TDiff2D rhs) const
00105             {
00106                 return TDiff2D (x+rhs.x, y+rhs.y);
00107             }
00108 
00109         TDiff2D operator-(TDiff2D rhs) const
00110             {
00111                 return TDiff2D (x-rhs.x, y-rhs.y);
00112             }
00113 
00114         TDiff2D & operator*=(double val)
00115             {
00116                 x = x*val;
00117                 y = y*val;
00118                 return *this;
00119             }
00120         
00121         TDiff2D operator*(double val)
00122             {
00123                 TDiff2D<T> result;
00124                 result.x = x * val;
00125                 result.y = y * val;
00126                 return result;
00127             }
00128 
00129         vigra::Diff2D toDiff2D() const
00130             {
00131                 return vigra::Diff2D(roundi(x), roundi(y));
00132             }
00133         
00135         T squareDistance(TDiff2D<T> other) const
00136             {
00137                 return (other - *this).squareLength();
00138             }
00139         
00141         T squareLength() const
00142             {
00143                 return x*x + y*y;
00144             }
00145 
00146         double x,y;
00147     };
00148     
00150     typedef TDiff2D<double> FDiff2D;
00151     
00152 
00157     template <class T>
00158     T simpleClipPoint(const T & point, const T & min, const T & max)
00159     {
00160         T p(point);
00161         if (p.x < min.x) p.x = min.x;
00162         if (p.x > max.x) p.x = max.x;
00163         if (p.y < min.y) p.y = min.y;
00164         if (p.y > max.y) p.y = max.y;
00165         return p;
00166     }
00167 
00168     template <class T>
00169     T sqr(T t)
00170     {
00171         return t*t;
00172     }
00173 
00174     template <class T>
00175     double norm(T t)
00176     {
00177         return sqrt(t.x*t.x + t.y*t.y);
00178     }
00179 
00182     template <class InputIterator1, class InputIterator2>
00183     double euclid_dist(InputIterator1 first1, InputIterator1 last1,
00184                          InputIterator2 first2)
00185     {
00186         typename InputIterator1::value_type res = 0;
00187         InputIterator1 i(first1);
00188         while (i != last1) {
00189             double a = *i;
00190             double b = *(first2 + (i - first1));
00191             res = res + a*a + b*b;
00192             ++i;
00193         }
00194         return sqrt(res);
00195     }
00196 
00199     template <class InputIterator1, class InputIterator2, class T>
00200     T sqr_dist(InputIterator1 first1, InputIterator1 last1,
00201                          InputIterator2 first2, T res)
00202     {
00203         InputIterator1 i(first1);
00204         while (i != last1) {
00205             T a = (T)(*i) - (T) (*(first2 + (i - first1)));
00206             res = res + a*a;
00207             ++i;
00208         }
00209         return res;
00210     }
00211 
00216     template <class POINT>
00217     vigra::Rect2D calcCircleROIFromPoints(const POINT& p1, const POINT & p2)
00218     {
00219         double dx = p2.x - p1.x;
00220         double dy = p2.y - p1.y;
00221         double r = sqrt(dx*dx + dy*dy) / 2.0;
00222         double mx = p1.x + dx/2;
00223         double my = p1.y + dy/2;
00224 
00225         vigra::Rect2D rect;
00226         rect.setUpperLeft(vigra::Point2D(roundi(mx-r), roundi(my -r)));
00227         rect.setLowerRight(vigra::Point2D(roundi(mx+r), roundi(my+r)));
00228         return rect;
00229     }
00230     
00231     
00232 
00233 } // namespace
00234 
00235 template <class T>
00236 inline std::ostream & operator<<(std::ostream & o, const hugin_utils::TDiff2D<T> & d)
00237 {
00238     return o << "( " << d.x << " " << d.y << " )";
00239 }
00240 
00241 inline hugin_utils::FDiff2D operator/(const hugin_utils::FDiff2D & lhs, double val)
00242 {
00243     return hugin_utils::FDiff2D(lhs.x/val, lhs.y/val);
00244 }
00245 
00246 // uses ceil for rounding.
00247 inline vigra::Diff2D operator*(const vigra::Diff2D & d, double scale)
00248 {
00249     return vigra::Diff2D((int)(ceil(d.x * scale)), 
00250                          (int)(ceil(d.y * scale)));
00251 }
00252 
00254 inline vigra::Rect2D operator*(const vigra::Rect2D & r, double scale)
00255 {
00256     return vigra::Rect2D( (int)floor(r.left()*scale),
00257                           (int)floor(r.top()*scale),
00258                           (int)ceil(r.right()*scale),
00259                           (int)ceil(r.bottom()*scale));
00260 }
00261 
00262 #endif // _H

Generated on 27 Sep 2016 for Hugintrunk by  doxygen 1.4.7