00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef GIL_UTILITIES_H
00010 #define GIL_UTILITIES_H
00011
00012 #include "gil_config.hpp"
00013 #include <functional>
00014 #include <cmath>
00015 #include <cstddef>
00016
00025
00026 ADOBE_GIL_NAMESPACE_BEGIN
00027
00029
00036
00037 template <typename T>
00038 class point2 {
00039 public:
00040 typedef T value_type;
00041 template <std::size_t D> struct axis { typedef value_type coord_t; };
00042 static const std::size_t num_dimensions=2;
00043
00044 point2() : x(0), y(0) {}
00045 point2(T newX, T newY) : x(newX), y(newY) {}
00046 template <typename T1> explicit point2(const point2<T1>& p) : x((T)p.x), y((T)p.y) {}
00047 ~point2() {}
00048
00049 const point2& operator=(const point2& p) { x=p.x; y=p.y; return *this; }
00050
00051 point2 operator<<(int shift) const { return point2(x<<shift,y<<shift); }
00052 point2 operator>>(int shift) const { return point2(x>>shift,y>>shift); }
00053 const point2& operator+=(const point2& p) { x+=p.x; y+=p.y; return *this; }
00054 const point2& operator-=(const point2& p) { x-=p.x; y-=p.y; return *this; }
00055 const point2& operator/=(double t) { x/=t; y/=t; return *this; }
00056 point2 operator-() const { return point2<T>(-x,-y); }
00057
00058 const T& operator[](std::size_t i) const { return (reinterpret_cast<const T*>(&x))[i]; }
00059 T& operator[](std::size_t i) { return (reinterpret_cast<T*>(&x))[i]; }
00060
00061 T x,y;
00062 };
00063
00065 template <typename T> GIL_FORCEINLINE
00066 bool operator==(const point2<T>& p1, const point2<T>& p2) { return (p1.x==p2.x && p1.y==p2.y); }
00068 template <typename T> GIL_FORCEINLINE
00069 bool operator!=(const point2<T>& p1, const point2<T>& p2) { return p1.x!=p2.x || p1.y!=p2.y; }
00071 template <typename T> GIL_FORCEINLINE
00072 point2<T> operator+(const point2<T>& p1, const point2<T>& p2) { return point2<T>(p1.x+p2.x,p1.y+p2.y); }
00074 template <typename T> GIL_FORCEINLINE
00075 point2<T> operator-(const point2<T>& p1, const point2<T>& p2) { return point2<T>(p1.x-p2.x,p1.y-p2.y); }
00077 template <typename T> GIL_FORCEINLINE
00078 T operator*(const point2<T>& p1, const point2<T>& p2) { return p1.x*p2.x + p1.y*p2.y; }
00080 template <typename T> GIL_FORCEINLINE
00081 point2<double> operator/(const point2<T>& p, double t) { return t==0 ? point2<double>(0,0):point2<double>(p.x/t,p.y/t); }
00083 template <typename T> GIL_FORCEINLINE
00084 point2<double> operator*(const point2<T>& p, double t) { return point2<double>(p.x*t,p.y*t); }
00085
00091
00092 inline int iround(float x ) { return static_cast<int>(x + (x < 0.0f ? -0.5f : 0.5f)); }
00093 inline int iround(double x) { return static_cast<int>(x + (x < 0.0 ? -0.5 : 0.5)); }
00094 inline int ifloor(float x ) { return static_cast<int>(std::floor(x)); }
00095 inline int ifloor(double x) { return static_cast<int>(std::floor(x)); }
00096 inline int iceil(float x ) { return static_cast<int>(std::ceil(x)); }
00097 inline int iceil(double x) { return static_cast<int>(std::ceil(x)); }
00098
00100 inline point2<int> iround(const point2<float >& p) { return point2<int>(iround(p.x),iround(p.y)); }
00102 inline point2<int> iround(const point2<double>& p) { return point2<int>(iround(p.x),iround(p.y)); }
00104 inline point2<int> ifloor(const point2<float >& p) { return point2<int>(ifloor(p.x),ifloor(p.y)); }
00106 inline point2<int> ifloor(const point2<double>& p) { return point2<int>(ifloor(p.x),ifloor(p.y)); }
00108 inline point2<int> iceil (const point2<float >& p) { return point2<int>(iceil(p.x), iceil(p.y)); }
00110 inline point2<int> iceil (const point2<double>& p) { return point2<int>(iceil(p.x), iceil(p.y)); }
00111
00117
00118 template <typename T> inline T align(T val, int alignment) { return val+(alignment - val%alignment)%alignment; }
00119
00120
00121
00122 ADOBE_GIL_NAMESPACE_END
00123
00124 #endif