00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef POINT2D_H_DEFINED
00039 #define POINT2D_H_DEFINED
00040
00041 #include "Image/Dims.H"
00042 #include "Util/Promotions.H"
00043 #include "Util/log.H"
00044 #include <cmath>
00045 #include <string>
00046 #include <vector>
00047
00048
00049
00050
00051
00052 template <class T>
00053 class Point2D
00054 {
00055 public:
00056
00057 inline Point2D();
00058
00059
00060 inline Point2D(const T ii, const T jj);
00061
00062
00063 explicit inline Point2D(const Dims& d);
00064
00065
00066
00067
00068 template <class U>
00069 explicit inline Point2D(const Point2D<U>& a);
00070
00071
00072 inline Point2D<T>& operator+=(const Point2D<T> &p);
00073
00074 inline Point2D<T>& operator-=(const Point2D<T> &p);
00075
00076 inline Point2D<T>& operator*=(const Point2D<T> &p);
00077
00078 inline Point2D<T>& operator/=(const Point2D<T> &p);
00079
00080
00081 template <class U>
00082 inline Point2D<typename promote_trait<T,U>::TP>
00083 operator+(const Point2D<U> &p) const;
00084
00085 template <class U>
00086 inline Point2D<typename promote_trait<T,U>::TP>
00087 operator-(const Point2D<U> &p) const;
00088
00089 template <class U>
00090 inline Point2D<typename promote_trait<T,U>::TP>
00091 operator*(const Point2D<U> &p) const;
00092
00093 template <class U>
00094 inline Point2D<typename promote_trait<T,U>::TP>
00095 operator/(const Point2D<U> &p) const;
00096
00097
00098 inline Point2D<T>& operator+=(const T val);
00099
00100 inline Point2D<T>& operator-=(const T val);
00101
00102 inline Point2D<T>& operator*=(const T val);
00103
00104 inline Point2D<T>& operator/=(const T val);
00105
00106
00107 template <class U>
00108 inline Point2D<typename promote_trait<T,U>::TP> operator+(const U val) const;
00109
00110
00111 template <class U>
00112 inline Point2D<typename promote_trait<T,U>::TP> operator-(const U val) const;
00113
00114
00115 template <class U>
00116 inline Point2D<typename promote_trait<T,U>::TP> operator*(const U val) const;
00117
00118
00119 template <class U>
00120 inline Point2D<typename promote_trait<T,U>::TP> operator/(const U val) const;
00121
00122
00123 inline bool isValid() const;
00124
00125
00126 inline typename promote_trait<T,float>::TP
00127 squdist(const Point2D<T>& p) const;
00128
00129
00130 inline typename promote_trait<T,float>::TP
00131 distance(const Point2D<T>& p) const;
00132
00133
00134 inline void clampToDims(const Dims& dims);
00135
00136
00137 T i, j;
00138 };
00139
00140
00141
00142 template <class T, class U>
00143 inline bool operator==(const Point2D<T>& p1, const Point2D<U>& p2);
00144
00145
00146 template <class T, class U>
00147 inline bool operator!=(const Point2D<T>& p1, const Point2D<U>& p2);
00148
00149
00150 template <class T, class U>
00151 inline bool operator>(const Point2D<T>& p1, const Point2D<U>& p2);
00152
00153
00154 template <class T, class U>
00155 inline bool operator<(const Point2D<T>& p1, const Point2D<U>& p2);
00156
00157
00158
00159 template <class T>
00160 std::string convertToString(const Point2D<T>& val);
00161
00162
00163 template <class T>
00164 void convertFromString(const std::string& str, Point2D<T>& val);
00165
00166
00167 inline bool pnpoly(const std::vector<Point2D<int> > &polygon,
00168 const Point2D<int>& p);
00169
00170
00171
00172 template <class T>
00173 inline Point2D<T>::Point2D()
00174 { i = 0; j = 0; }
00175
00176
00177 template <class T>
00178 inline Point2D<T>::Point2D(const T ii, const T jj)
00179 { i = ii; j = jj; }
00180
00181
00182 template <class T>
00183 inline Point2D<T>::Point2D(const Dims& d)
00184 { i = d.w(); j = d.h(); }
00185
00186
00187 template <class T>
00188 template <class U>
00189 inline Point2D<T>::Point2D(const Point2D<U>& a)
00190 : i(clamped_convert<T>(a.i)), j(clamped_convert<T>(a.j))
00191 { }
00192
00193
00194 template <class T>
00195 inline Point2D<T>& Point2D<T>::operator+=(const Point2D<T> &p)
00196 { i += p.i; j += p.j; return *this; }
00197
00198
00199 template <class T>
00200 inline Point2D<T>& Point2D<T>::operator-=(const Point2D<T> &p)
00201 { i -= p.i; j -= p.j; return *this; }
00202
00203
00204 template <class T>
00205 inline Point2D<T>& Point2D<T>::operator*=(const Point2D<T> &p)
00206 { i *= p.i; j *= p.j; return *this; }
00207
00208
00209 template <class T>
00210 inline Point2D<T>& Point2D<T>::operator/=(const Point2D<T> &p)
00211 { i /= p.i; j /= p.j; return *this; }
00212
00213
00214 template <class T>
00215 template <class U>
00216 inline Point2D<typename promote_trait<T,U>::TP>
00217 Point2D<T>::operator+(const Point2D<U> &p) const
00218 { return Point2D<typename promote_trait<T,U>::TP>(i + p.i, j + p.j); }
00219
00220
00221 template <class T>
00222 template <class U>
00223 inline Point2D<typename promote_trait<T,U>::TP>
00224 Point2D<T>::operator-(const Point2D<U> &p) const
00225 { return Point2D<typename promote_trait<T,U>::TP>(i - p.i, j - p.j); }
00226
00227
00228 template <class T>
00229 template <class U>
00230 inline Point2D<typename promote_trait<T,U>::TP>
00231 Point2D<T>::operator*(const Point2D<U> &p) const
00232 { return Point2D<typename promote_trait<T,U>::TP>(i * p.i, j * p.j); }
00233
00234
00235 template <class T>
00236 template <class U>
00237 inline Point2D<typename promote_trait<T,U>::TP>
00238 Point2D<T>::operator/(const Point2D<U> &p) const
00239 { return Point2D<typename promote_trait<T,U>::TP>(i / p.i, j / p.j); }
00240
00241
00242 template <class T>
00243 inline Point2D<T>& Point2D<T>::operator+=(const T val)
00244 { i += val; j += val; return *this; }
00245
00246
00247 template <class T>
00248 inline Point2D<T>& Point2D<T>::operator-=(const T val)
00249 { i -= val; j -= val; return *this; }
00250
00251
00252 template <class T>
00253 inline Point2D<T>& Point2D<T>::operator*=(const T val)
00254 { i *= val; j *= val; return *this; }
00255
00256
00257 template <class T>
00258 inline Point2D<T>& Point2D<T>::operator/=(const T val)
00259 { i /= val; j /= val; return *this; }
00260
00261
00262 template <class T>
00263 template <class U>
00264 inline Point2D<typename promote_trait<T,U>::TP>
00265 Point2D<T>::operator+(const U val) const
00266 { return Point2D<typename promote_trait<T,U>::TP>(this->i+val, this->j+val); }
00267
00268
00269 template <class T>
00270 template <class U>
00271 inline Point2D<typename promote_trait<T,U>::TP>
00272 Point2D<T>::operator-(const U val) const
00273 { return Point2D<typename promote_trait<T,U>::TP>(this->i-val, this->j-val); }
00274
00275
00276 template <class T>
00277 template <class U>
00278 inline Point2D<typename promote_trait<T,U>::TP>
00279 Point2D<T>::operator*(const U val) const
00280 { return Point2D<typename promote_trait<T,U>::TP>(this->i*val, this->j*val); }
00281
00282
00283 template <class T>
00284 template <class U>
00285 inline Point2D<typename promote_trait<T,U>::TP>
00286 Point2D<T>::operator/(const U val) const
00287 { return Point2D<typename promote_trait<T,U>::TP>(this->i/val, this->j/val); }
00288
00289
00290 template <class T, class U>
00291 inline bool operator==(const Point2D<T>& p1, const Point2D<U>& p2)
00292 { return p1.i == p2.i && p1.j == p2.j; }
00293
00294
00295 template <class T, class U>
00296 inline bool operator!=(const Point2D<T>& p1, const Point2D<U>& p2)
00297 { return p1.i != p2.i || p1.j != p2.j; }
00298
00299
00300 template <class T, class U>
00301 inline bool operator>(const Point2D<T>& p1, const Point2D<U>& p2)
00302 { return p1.i > p2.i && p1.j > p2.j; }
00303
00304
00305 template <class T, class U>
00306 inline bool operator<(const Point2D<T>& p1, const Point2D<U>& p2)
00307 { return p1.i < p2.i && p1.j < p2.j; }
00308
00309
00310 template <class T>
00311 inline bool Point2D<T>::isValid() const
00312 { return ((i >= 0) && (j >= 0)); }
00313
00314
00315 template <class T>
00316 inline typename promote_trait<T,float>::TP
00317 Point2D<T>::squdist(const Point2D<T>& p) const
00318 {
00319 typedef typename promote_trait<T,float>::TP TF;
00320 TF d1 = p.i - i, d2 = p.j - j;
00321 return (d1 * d1 + d2 * d2);
00322 }
00323
00324
00325 template <class T>
00326 inline typename promote_trait<T,float>::TP
00327 Point2D<T>::distance(const Point2D<T>& p) const
00328 { return sqrt(squdist(p)); }
00329
00330
00331 template <class T>
00332 inline void Point2D<T>::clampToDims(const Dims& dims)
00333 {
00334 if (dims.isEmpty())
00335 LFATAL("clampToDims() requires non-empty Dims");
00336
00337 if (i < 0) i = 0; else if (i >= dims.w()) i = dims.w()-1;
00338 if (j < 0) j = 0; else if (j >= dims.h()) j = dims.h()-1;
00339 }
00340
00341
00342
00343
00344 inline bool pnpoly(const std::vector<Point2D<int> > &polygon,
00345 const Point2D<int>& p)
00346 {
00347 bool c = false; const uint ps = polygon.size();
00348 for (uint i = 0, j = ps-1; i < ps; j = i++)
00349 if ((((polygon[i].j <= p.j) && (p.j < polygon[j].j)) ||
00350 ((polygon[j].j <= p.j) && (p.j < polygon[i].j))) &&
00351 (p.i < (polygon[j].i - polygon[i].i) *
00352 (p.j - polygon[i].j) / (polygon[j].j - polygon[i].j)
00353 + polygon[i].i))
00354 c = !c;
00355
00356 return c;
00357 }
00358
00359
00360
00361
00362
00363
00364
00365 #endif