27 #ifndef _HUGIN_MATH_HUGIN_MATH_H
28 #define _HUGIN_MATH_HUGIN_MATH_H
34 #include <vigra/diff2d.hxx>
37 #define M_PI 3.14159265358979323846
41 #define PI 3.14159265358979323846
44 #define DEG_TO_RAD( x ) ( (x) * 2.0 * PI / 360.0 )
45 #define RAD_TO_DEG( x ) ( (x) * 360.0 / ( 2.0 * PI ) )
57 return (
float) floor(x+0.5f);
67 return (
int) floor(x);
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)));
96 return x == rhs.
x &&
y == rhs.
y;
101 return x != rhs.
x ||
y != rhs.
y;
137 return (other - *
this).squareLength();
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;
177 return sqrt(t.x*t.x + t.y*t.y);
182 template <
class InputIterator1,
class InputIterator2>
184 InputIterator2 first2)
186 typename InputIterator1::value_type res = 0;
187 InputIterator1 i(first1);
190 double b = *(first2 + (i - first1));
191 res = res + a*a + b*b;
199 template <
class InputIterator1,
class InputIterator2,
class T>
200 T
sqr_dist(InputIterator1 first1, InputIterator1 last1,
201 InputIterator2 first2, T res)
203 InputIterator1 i(first1);
205 T a = (T)(*i) - (T) (*(first2 + (i - first1)));
216 template <
class POINT>
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;
226 rect.setUpperLeft(vigra::Point2D(
roundi(mx-r),
roundi(my -r)));
227 rect.setLowerRight(vigra::Point2D(
roundi(mx+r),
roundi(my+r)));
238 inline std::ostream & operator<<(std::ostream & o, const hugin_utils::TDiff2D<T> & d)
240 return o <<
"( " << d.x <<
" " << d.y <<
" )";
249 inline vigra::Diff2D
operator*(
const vigra::Diff2D & d,
double scale)
251 return vigra::Diff2D((
int)(ceil(d.x * scale)),
252 (
int)(ceil(d.y * scale)));
256 inline vigra::Rect2D
operator*(
const vigra::Rect2D & r,
double scale)
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));
int gcd(int a, int b)
function to calculate greated common divisor using Euclidean algorithm both arguments should be >=0 ...
T sqr_dist(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T res)
calculate squared Euclidean distance between two vectors.
double euclid_dist(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
calculate squared Euclidean distance between two vectors.
vigra::Diff2D operator*(const vigra::Diff2D &d, double scale)
bool operator==(TDiff2D rhs) const
bool operator!=(TDiff2D rhs) const
TDiff2D operator-(TDiff2D rhs) const
TDiff2D operator+(TDiff2D rhs) const
TDiff2D< double > FDiff2D
hugin_utils::FDiff2D operator/(const hugin_utils::FDiff2D &lhs, double val)
T squareLength() const
Return the square of the length of the vector.
TDiff2D & operator*=(double val)
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...
vigra::Diff2D toDiff2D() const
TDiff2D operator*(double val)
TDiff2D(const vigra::Diff2D &d)
T squareDistance(TDiff2D< T > other) const
Return square of the distance to another point.
vigra::Rect2D calcCircleROIFromPoints(const POINT &p1, const POINT &p2)
calculate the bounding box of a circle that goes through both points.