GeopsyCore/TimeRange.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GeopsyCore.
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 : 2003-09-17
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (ULg, Liège, Belgium)
00025 **    Marc Wathelet (LGIT, Grenoble, France)
00026 **
00027 ***************************************************************************/
00028 
00029 #ifndef TIMERANGE_H
00030 #define TIMERANGE_H
00031 
00032 #include <QtCore>
00033 #include "GeopsyCoreDLLExport.h"
00034 
00035 namespace GeopsyCore {
00036 
00037 class GEOPSYCORE_EXPORT TimeRange
00038 {
00039 public:
00040   inline TimeRange();
00041   inline TimeRange(const TimeRange& o) {operator=(o);}
00042   inline TimeRange(double start, double end);
00043   ~TimeRange() {}
00044 
00045   inline void operator=(const TimeRange& o);
00046 
00047   double start() const {return _start;}
00048   double end() const {return _end;}
00049 
00050   inline void setStart(double m);
00051   inline void setEnd(double m);
00052   inline void sort();
00053 
00054   inline void shift(double s);
00055   inline TimeRange shifted(double s) const;
00056   inline void scale(double center, double factor);
00057 
00058   int lengthSamples(double deltaT) const {return (int)round(lengthSeconds()/deltaT);}
00059   double lengthSeconds() const {return _end-_start;}
00060 
00061   inline TimeRange intersection(const TimeRange& tw) const;
00062   inline bool intersects(const TimeRange& tw) const;
00063 
00064   void printDebug() const;
00065 private:
00066   double _start;
00067   double _end;
00068 };
00069 
00070 inline TimeRange::TimeRange()
00071 {
00072   _start=0.0;
00073   _end=0.0;
00074 }
00075 
00076 inline TimeRange::TimeRange(double start, double end)
00077 {
00078   if(start>end) {
00079     _start=end;
00080     _end=start;
00081   } else {
00082     _start=start;
00083     _end=end;
00084   }
00085 }
00086 
00087 inline void TimeRange::operator=(const TimeRange& o)
00088 {
00089   _start=o._start;
00090   _end=o._end;
00091 }
00092 
00093 inline void TimeRange::setStart(double m)
00094 {
00095   _start=m;
00096 }
00097 
00098 inline void TimeRange::setEnd(double m)
00099 {
00100   _end=m;
00101 }
00102 
00103 inline void TimeRange::sort()
00104 {
00105   if(_start>_end) {
00106     qSwap(_start, _end);
00107   }
00108 }
00109 
00110 inline void TimeRange::shift(double s)
00111 {
00112   _start+=s;
00113   _end+=s;
00114 }
00115 
00116 inline TimeRange TimeRange::shifted(double s) const
00117 {
00118   return TimeRange(_start+s, _end+s);
00119 }
00120 
00121 inline void TimeRange::scale(double center, double factor)
00122 {
00123   _start=center+(_start-center)*factor;
00124   _end=center+(_end-center)*factor;
00125 }
00126 
00127 inline TimeRange TimeRange::intersection(const TimeRange& tw) const
00128 {
00129   TimeRange res;
00130   if(tw._end < _start || tw._start > _end)
00131     return res;
00132   if(tw._start < _start && _start < tw._end)
00133     res._start=_start;
00134   else
00135     res._start=tw._start;
00136   if(tw._start < _end && _end < tw._end)
00137     res._end=_end;
00138   else
00139     res._end=tw._end;
00140   return res;
00141 }
00142 
00143 inline bool TimeRange::intersects(const TimeRange& tw) const
00144 {
00145   if(tw._end < _start || tw._start > _end)
00146     return false;
00147   return true;
00148 }
00149 
00150 } // namespace GeopsyCore
00151 
00152 // Allow passing TimeRange as a QVariant or through SIGNAL/SLOT
00153 Q_DECLARE_METATYPE(GeopsyCore::TimeRange);
00154 
00155 #endif // TIMERANGE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines