QGpCoreTools/Point.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 POINT_H
00030 #define POINT_H
00031 
00032 #include "StringSection.h"
00033 #include "Point2D.h"
00034 #include "QGpCoreToolsDLLExport.h"
00035 
00036 namespace QGpCoreTools {
00037 
00038 class CurvePointOptions;
00039 class Matrix3x3;
00040 class Matrix4x4;
00041 
00042 class QGPCORETOOLS_EXPORT Point: public Point2D
00043 {
00044 public:
00045   inline Point();
00046   inline Point(double x, double y, double z=0.0);
00047   inline Point(const QPoint& p);
00048   inline Point(const QPointF& p);
00049   inline Point(const Point2D& p);
00050   inline Point(const Point& p);
00051   inline Point(const QVector<double>& p);
00052 
00053   double z() const {return _z;}
00054   void setZ(double v) {_z=v;}
00055   void setValid(bool) {}
00056   bool isValid() const {return true;}
00057   inline void average(const Point& p);
00058 
00059   // Copy operators
00060   inline Point& operator=(const Point& p);
00061   inline Point& operator=(const Point2D& p);
00062   inline void set(double xi, double yi, double zi=0.0);
00063 
00064   // Comparison operators
00065   inline bool operator< (const Point& p) const;
00066   inline bool operator==(const Point& p) const;
00067   bool operator<=(const Point& obj) const
00068     {return *this==obj || *this<obj;}
00069   bool operator>(const Point& obj) const
00070     {return ! (*this<=obj);}
00071   bool operator>=(const Point& obj) const
00072     {return ! (*this<obj);}
00073   bool operator!=(const Point& obj) const
00074     {return ! (*this==obj);}
00075   inline short compare(const Point& p) const;
00076   inline bool isSimilar(const Point& p, double tolerance) const;
00077 
00078   // Arithmetic operation
00079   inline void operator+=(const Point& p);
00080   inline void operator-=(const Point& p);
00081   inline void operator*=(const Point& p);
00082   inline void operator*=(double mul);
00083   inline void operator/=(const Point& p);
00084   inline void operator/=(double div);
00085   void operator*=(const Matrix3x3& transformation);
00086   void operator*=(const Matrix4x4& transformation);
00087   inline double scalarProduct(const Point& p) const;
00088   inline void vectorialProduct(const Point& p1,const Point& p2);
00089   inline void interpole(double valX, const Point& p1, const Point& p2);
00090   void translate(const Point& p) {operator+=(p);}
00091 
00092   inline Point operator+(const Point& p) const;
00093   inline Point operator-(const Point& p) const;
00094   inline Point operator*(double mul) const;
00095   inline Point operator*(const Point& p) const;
00096   inline Point operator/(double div) const;
00097   inline Point operator/(const Point& p) const;
00098 
00099   // I/O functions
00100   bool fromString(const StringSection& str);
00101   QString toString(int precision=6, char format='g') const;
00102 
00103   double distanceTo(const Point &p) const;
00104   double distanceToSegment(const Point &p1, const Point &p2) const;
00105   double elevationTo(const Point &p) const;
00106   void move(double distance, const Angle& azimuth, const Angle& elevation);
00107 private:
00108   double _z;
00109 };
00110 
00111 } // namespace QGpCoreTools
00112 
00113 namespace QGpCoreTools {
00114 
00115 inline Point::Point()
00116   : Point2D()
00117 {
00118   _z=0.0;
00119 }
00120 
00121 inline Point::Point(double xi, double yi, double zi)
00122   : Point2D(xi, yi)
00123 {
00124   _z=zi;
00125 }
00126 
00127 inline Point::Point(const QPoint& p)
00128   : Point2D(p)
00129 {
00130   _z=0.0;
00131 }
00132 
00133 inline Point::Point(const QPointF& p)
00134   : Point2D(p)
00135 {
00136   _z=0.0;
00137 }
00138 
00139 inline Point::Point(const Point2D& p)
00140   : Point2D(p)
00141 {
00142   _z=0.0;
00143 }
00144 
00145 inline Point::Point(const Point& p)
00146   : Point2D(p)
00147 {
00148   _z=p._z;
00149 }
00150 
00151 inline Point& Point::operator=(const Point& p)
00152 {
00153   Point2D::operator=(p);
00154   _z=p._z;
00155   return *this;
00156 }
00157 
00158 inline Point& Point::operator=(const Point2D& p)
00159 {
00160   Point2D::operator=(p);
00161   _z=0.0;
00162   return *this;
00163 }
00164 
00165 inline void Point::set(double xi, double yi, double zi)
00166 {
00167   Point2D::set(xi, yi);
00168   _z=zi;
00169 }
00170 
00171 inline bool Point::operator<(const Point& p) const
00172 {
00173   if(x()<p.x()) return true;
00174   else if(x()==p.x()) {
00175     if(y()< p.y()) return true;
00176     else if(y()==p.y() && _z < p._z) return true;
00177   }
00178   return false;
00179 }
00180 
00181 inline bool Point::operator==(const Point& p) const
00182 {
00183   return Point2D::operator==(p) && _z==p._z;
00184 }
00185 
00186 inline short Point::compare (const Point& p) const
00187 {
00188   int c=Point2D::compare(p);
00189   if(c==0) {
00190     if(_z < p._z) return -1;
00191     else if(_z > p._z) return 1;
00192     else return 0;
00193   } else {
00194     return c;
00195   }
00196 }
00197 
00198 inline void Point::average(const Point& p)
00199 {
00200   Point2D::average(p);
00201   _z=0.5*(_z+p._z);
00202 }
00203 
00204 inline Point Point::operator+(const Point& p) const
00205 {
00206   return Point(x()+p.x(), y()+p.y(), _z+p._z);
00207 }
00208 
00209 inline Point Point::operator-(const Point& p) const
00210 {
00211   return Point(x()-p.x(), y()-p.y(), _z-p._z);
00212 }
00213 
00214 inline Point Point::operator*(double mul) const
00215 {
00216   return Point(x()*mul, y()*mul, _z*mul);
00217 }
00218 
00219 inline Point Point::operator*(const Point& p) const
00220 {
00221   return Point (x()*p.x(), y()*p.y(), _z*p._z);
00222 }
00223 
00224 inline Point Point::operator/(double div) const
00225 {
00226   double f=1.0/div;
00227   return Point (x()*f, y()*f, _z*f);
00228 }
00229 
00230 inline Point Point::operator/(const Point& p) const
00231 {
00232   return Point (x()/p.x(), y()/p.y(), _z/p._z);
00233 }
00234 
00235 inline void Point::operator+=(const Point& p)
00236 {
00237   Point2D::operator+=(p);
00238   _z+=p._z;
00239 }
00240 
00241 inline void Point::operator-=(const Point& p)
00242 {
00243   Point2D::operator-=(p);
00244   _z-=p._z;
00245 }
00246 
00247 inline void Point::operator*=(const Point& p)
00248 {
00249   Point2D::operator*=(p);
00250   _z*=p._z;
00251 }
00252 
00253 inline void Point::operator*=(double mul)
00254 {
00255   Point2D::operator*=(mul);
00256   _z*=mul;
00257 }
00258 
00259 inline void Point::operator/=(const Point& p)
00260 {
00261   Point2D::operator/=(p);
00262   _z/=p._z;
00263 }
00264 
00265 inline void Point::operator/=(double div)
00266 {
00267   double f=1.0/div;
00268   Point2D::operator*=(f);
00269   _z*=f;
00270 }
00271 
00272 inline double Point::scalarProduct(const Point& p) const
00273 {
00274   return x()*p.x()+y()*p.y()+_z*p._z;
00275 }
00276 
00277 inline void Point::vectorialProduct(const Point& p1,const Point& p2)
00278 {
00279   setX(p1.y()*p2.z()-p2.y()*p1.z());
00280   setY(-p1.x()*p2.z()+p2.x()*p1.z());
00281   setZ(p1.x()*p2.y()-p2.x()*p1.y());
00282 }
00283 
00284 inline void Point::interpole(double valX, const Point& p1, const Point& p2)
00285 {
00286   double factor=(valX-p1.x())/(p2.x()-p1.x());
00287   setX(valX);
00288   setY(p1.y()+(p2.y()-p1.y())*factor);
00289   setZ(p1.z()+(p2.z()-p1.z())*factor);
00290 }
00291 
00292 
00293 inline bool Point::isSimilar(const Point& p, double tolerance) const
00294 {
00295   return Point2D::isSimilar(p, tolerance) &&
00296          fabs(p.z()-z())<tolerance;
00297 }
00298 
00299 QGPCORETOOLS_EXPORT QTextStream& operator<< (QTextStream& s, const QList<Point>& plist);
00300 QGPCORETOOLS_EXPORT QTextStream& operator>> (QTextStream& s, QList<Point>& plist);
00301 QGPCORETOOLS_EXPORT QTextStream& operator<< (QTextStream& s, const Point& p);
00302 QGPCORETOOLS_EXPORT QTextStream& operator>> (QTextStream& s, Point& p);
00303 QGPCORETOOLS_EXPORT QDataStream& operator<< (QDataStream& s, const Point& p);
00304 QGPCORETOOLS_EXPORT QDataStream& operator>> (QDataStream& s, Point& p);
00305 
00306 } // namespace QGpCoreTools
00307 
00308 #endif // POINT_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines