QGpCoreTools/Point2D.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2002-04-15
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (ULg, Liège, Belgium)
00025 **    Marc Wathelet (LGIT, Grenoble, France)
00026 **
00027 ***************************************************************************/
00028 
00029 #ifndef POINT2D_H
00030 #define POINT2D_H
00031 
00032 #include <QtCore>
00033 
00034 #include "Angle.h"
00035 #include "QGpCoreToolsDLLExport.h"
00036 
00037 namespace QGpCoreTools {
00038 
00039 class CurvePointOptions;
00040 class StringSection;
00041 class Matrix2x2;
00042 
00043 class QGPCORETOOLS_EXPORT Point2D
00044 {
00045 public:
00046   inline Point2D();
00047   inline Point2D(double x, double y);
00048   inline Point2D(const QPoint& p);
00049   inline Point2D(const QPointF& p);
00050   inline Point2D(const Point2D& p);
00051   Point2D(const QVector<double>& p);
00052 
00053   double x() const {return _x;}
00054   double y() const {return _y;}
00055   double y(const CurvePointOptions *) const {return _y;}
00056   void setX(double v) {_x=v;}
00057   void setY(double v) {_y=v;}
00058   void setY(double v, const CurvePointOptions *) {_y=v;}
00059   void setValid(bool) {}
00060   bool isValid() const {return true;}
00061   void average(const Point2D& p) {_y= 0.5*(_y+p._y);}
00062 
00063   void degreesToDMS();
00064   void DMSToDegrees();
00065   void degreesToDM();
00066   void DMToDegrees();
00067   void geographicalToRectangular(const Point2D& ref, double rotation=0.0);
00068   void rectangularToGeographical(const Point2D& ref, double rotation=0.0);
00069   void geographicalToUtm(const QPoint * zone=0);
00070   void utmToGeographical(const QPoint& zone);
00071   QPoint utmZoneIndex() const;
00072   static QString utmZone(QPoint z, bool * ok=0);
00073   static QPoint utmZone(const QString& z, bool * ok=0);
00074   static double utmLongitudeReference(QPoint z);
00075   static double utmLatitudeReference(QPoint z);
00076 
00077   // Copy operators
00078   inline Point2D& operator=(const Point2D& p);
00079   inline void set(double xi, double yi);
00080 
00081   // Comparison operators
00082   inline bool operator< (const Point2D& p) const;
00083   inline bool operator== (const Point2D& p) const;
00084   bool operator<= (const Point2D& obj) const
00085     {return (*this==obj || *this < obj);}
00086   bool operator>  (const Point2D& obj) const
00087     {return (! (*this <= obj));}
00088   bool operator>= (const Point2D& obj) const
00089     {return (! (*this < obj));}
00090   bool operator!= (const Point2D& obj) const
00091     {return (! (*this==obj));}
00092   inline short compare (const Point2D& p) const;
00093   inline bool isSimilar(const Point2D& p, double tolerance) const;
00094 
00095   // I/O functions
00096   bool fromString(const StringSection& str);
00097   QString toString(int precision=6, char format='g') const;
00098 
00099   // Arithmetic operation
00100   inline void operator+=(const Point2D& p);
00101   inline void operator-=(const Point2D& p);
00102   inline void operator*=(const Point2D& p);
00103   inline void operator*=(double mul);
00104   inline void operator/=(const Point2D& p);
00105   inline void operator/=(double div);
00106   void operator*=(const Matrix2x2& transformation);
00107   inline double scalarProduct(const Point2D& p) const;
00108   inline void interpole(double valX, const Point2D& p1, const Point2D& p2);
00109   void translate(const Point2D& p) {operator+=(p);}
00110   void rotate(const Angle& a);
00111 
00112   inline Point2D operator+(const Point2D& p) const;
00113   inline Point2D operator-(const Point2D& p) const;
00114   inline Point2D operator*(double mul) const;
00115   inline Point2D operator*(const Point2D& p) const;
00116   inline Point2D operator/(double div) const;
00117   inline Point2D operator/(const Point2D& p) const;
00118 
00119   // Miscellaneous
00120   bool isHit(const Point2D &p,double tolX, double tolY) const;
00121   double distanceTo(const Point2D &p) const;
00122   double distanceToSegment(const Point2D &p1, const Point2D &p2) const;
00123   double azimuthTo(const Point2D &p) const;
00124   double geographicalDistanceTo(const Point2D &p) const;
00125   double geographicalAzimuthTo(const Point2D &p) const;
00126   void move(double distance, const Angle& azimuth);
00127 private:
00128   static double utmM(double phi);
00129   // Coordinates of the point
00130   double _x, _y;
00131 };
00132 
00133 QGPCORETOOLS_EXPORT uint qHash(Point2D key);
00134 
00135 } // namespace QGpCoreTools
00136 
00137 namespace QGpCoreTools {
00138 
00139 inline Point2D::Point2D()
00140 {
00141   _x=0.;
00142   _y=0.;
00143 }
00144 
00145 inline Point2D::Point2D(double xi, double yi)
00146 {
00147   _x=xi;
00148   _y=yi;
00149 }
00150 
00151 inline Point2D::Point2D(const QPoint& p)
00152 {
00153   _x=p.x();
00154   _y=p.y();
00155 }
00156 
00157 inline Point2D::Point2D(const QPointF& p)
00158 {
00159   _x=p.x();
00160   _y=p.y();
00161 }
00162 
00163 inline Point2D::Point2D(const Point2D& p)
00164 {
00165   _x=p.x();
00166   _y=p.y();
00167 }
00168 
00169 inline Point2D& Point2D::operator= (const Point2D& p)
00170 {
00171   _x=p._x;
00172   _y=p._y;
00173   return *this;
00174 }
00175 
00176 inline void Point2D::set(double xi, double yi)
00177 {
00178   _x=xi;
00179   _y=yi;
00180 }
00181 
00182 inline bool Point2D::operator<  (const Point2D& p) const
00183 {
00184   if(_x < p._x) return true;
00185   else if(_x==p._x && _y<p._y) return true;
00186   return false;
00187 }
00188 
00189 inline bool Point2D::operator== (const Point2D& p) const
00190 {
00191   if((_x==p._x) &&
00192       (_y==p._y)) return true;
00193   return false;
00194 }
00195 
00196 inline short Point2D::compare (const Point2D& p) const
00197 {
00198   if(_x<p._x) return(-1);
00199   else if(_x>p._x) return(1);
00200   else if(_y<p._y) return(-1);
00201   else if(_y>p._y) return(1);
00202   else return(0);
00203 }
00204 
00205 inline Point2D Point2D::operator+(const Point2D& p) const
00206 {
00207   return Point2D(_x+p._x, _y+p._y);
00208 }
00209 
00210 inline Point2D Point2D::operator-(const Point2D& p) const
00211 {
00212   return Point2D (_x-p._x, _y-p._y);
00213 }
00214 
00215 inline Point2D Point2D::operator*(double mul) const
00216 {
00217   return Point2D (_x*mul, _y*mul);
00218 }
00219 
00220 inline Point2D Point2D::operator*(const Point2D& p) const
00221 {
00222   return Point2D (_x*p._x, _y*p._y);
00223 }
00224 
00225 inline Point2D Point2D::operator/(double div) const
00226 {
00227   double f=1.0/div;
00228   return Point2D (_x*f, _y*f);
00229 }
00230 
00231 inline Point2D Point2D::operator/(const Point2D& p) const
00232 {
00233   return Point2D (_x/p._x, _y/p._y);
00234 }
00235 
00236 inline void Point2D::operator+=(const Point2D& p)
00237 {
00238   _x+=p._x;
00239   _y+=p._y;
00240 }
00241 
00242 inline void Point2D::operator-=(const Point2D& p)
00243 {
00244   _x-=p._x;
00245   _y-=p._y;
00246 }
00247 
00248 inline void Point2D::operator*=(const Point2D& p)
00249 {
00250   _x*=p._x;
00251   _y*=p._y;
00252 }
00253 
00254 inline void Point2D::operator*=(double mul)
00255 {
00256   _x*=mul;
00257   _y*=mul;
00258 }
00259 
00260 inline void Point2D::operator/=(const Point2D& p)
00261 {
00262   _x/=p._x;
00263   _y/=p._y;
00264 }
00265 
00266 inline void Point2D::operator/=(double div)
00267 {
00268   double f=1.0/div;
00269   _x*=f;
00270   _y*=f;
00271 }
00272 
00273 inline double Point2D::scalarProduct(const Point2D& p) const
00274 {
00275   return _x*p._x+_y*p._y;
00276 }
00277 
00278 inline void Point2D::interpole(double valX, const Point2D& p1, const Point2D& p2)
00279 {
00280   double factor=(valX - p1.x())/(p2.x()-p1.x());
00281   setX(valX);
00282   setY(p1.y()+ (p2.y()-p1.y()) * factor);
00283 }
00284 
00285 inline bool Point2D::isSimilar(const Point2D& p, double tolerance) const
00286 {
00287   return fabs(p.x()-x())<tolerance &&
00288          fabs(p.y()-y())<tolerance;
00289 }
00290 
00291 QGPCORETOOLS_EXPORT QTextStream& operator<< (QTextStream& s, const QList<Point2D>& plist);
00292 QGPCORETOOLS_EXPORT QTextStream& operator>> (QTextStream& s, QList<Point2D>& plist);
00293 QGPCORETOOLS_EXPORT QTextStream& operator<< (QTextStream& s, const Point2D& p);
00294 QGPCORETOOLS_EXPORT QTextStream& operator>> (QTextStream& s, Point2D& p);
00295 QGPCORETOOLS_EXPORT QDataStream& operator<< (QDataStream& s, const Point2D& p);
00296 QGPCORETOOLS_EXPORT QDataStream& operator>> (QDataStream& s, Point2D& p);
00297 
00298 } // namespace QGpCoreTools
00299 
00300 #endif // POINT2D.H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines