QGpCoreTools/PointND.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 : 2008-11-09
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef POINTND_H
00029 #define POINTND_H
00030 
00031 #include "Trace.h"
00032 #include "StringSection.h"
00033 #include "QGpCoreToolsDLLExport.h"
00034 
00035 namespace QGpCoreTools {
00036 
00037 class CurvePointOptions;
00038 
00039 class QGPCORETOOLS_EXPORT PointND
00040 {
00041 public:
00042   PointND() {_x=0.0;}
00043   PointND(double x, const QVector<double>& y) {_x=x; _y=y;}
00044   PointND(const PointND& o) {_x=o._x; _y=o._y;}
00045   PointND(const QVector<double>& p);
00046 
00047   int count() const {return _y.count();}
00048 
00049   bool operator<(const PointND& p) const {return _x<p._x;}
00050   inline bool operator==(const PointND& p) const;
00051   inline void operator=(const PointND& p);
00052 
00053   double x() const {return _x;}
00054   void setX(double x) {_x=x;}
00055   double y(const CurvePointOptions * options) const;
00056   void setY(double y, const CurvePointOptions * options);
00057   inline double y(int index) const;
00058   inline void setY(int index, double y);
00059 
00060   void setValid(bool) {}
00061   bool isValid() const {return true;}
00062   inline void interpole(double valX, const PointND& p1, const PointND& p2);
00063   inline void average(const PointND&);
00064 
00065   // I/O functions
00066   bool fromString(const StringSection& str);
00067   QString toString(int precision=6, char format='g') const;
00068 private:
00069   double _x;
00070   QVector<double> _y;
00071 };
00072 
00073 inline bool PointND::operator==(const PointND& p) const
00074 {
00075   if(_x!=p._x) return false;
00076   int n=_y.count();
00077   if(n!=p._y.count()) return false;
00078   for(int i=0; i< n; i++) {
00079     if(_y.at(i)!=p._y.at(i)) return false;
00080   }
00081   return true;
00082 }
00083 
00084 inline void PointND::operator=(const PointND& p)
00085 {
00086   _x=p._x;
00087   _y=p._y;
00088 }
00089 
00090 inline double PointND::y(int index) const
00091 {
00092   TRACE;
00093   if(index >= _y.count()) {
00094     if(_y.isEmpty()) return 0.0; else return _y.last();
00095   } else {
00096     return _y[index];
00097   }
00098 }
00099 
00100 inline void PointND::setY(int index, double y)
00101 {
00102   TRACE;
00103   if(index >= _y.count()) {
00104     _y.resize(index+1);
00105   }
00106   _y[index]=y;
00107 }
00108 
00109 inline void PointND::interpole(double valX, const PointND& p1, const PointND& p2)
00110 {
00111   TRACE;
00112   double factor=(valX - p1.x())/(p2.x()-p1.x());
00113   setX(valX);
00114   int n=p1.count();
00115   if(n<p2.count()) n=p2.count();
00116   for(int i=0; i< n; i++) {
00117     setY(i, p1.y(i)+ (p2.y(i)-p1.y(i)) * factor);
00118   }
00119 }
00120 
00121 inline void PointND::average(const PointND& p)
00122 {
00123   TRACE;
00124   int n=p.count();
00125   if(n<count()) n=count();
00126   for(int i=0; i< n; i++) {
00127     setY(i, 0.5*(y(i) + p.y(i)));
00128   }
00129 }
00130 
00131 } // namespace QGpCoreTools
00132 
00133 #endif // POINTND_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines