#ifndef samplingdata_h #define samplingdata_h #include "TInterval.h" namespace NSet { /*!\brief holds the fundamental sampling info: start and interval. */ template class TSamplingData { public: inline TSamplingData(T sa=0,T se=1); inline TSamplingData(T x0,T y0,T x1,T y1); template inline TSamplingData(const TSamplingData&); template inline TSamplingData(const StepInterval&); inline bool operator==(const TSamplingData&)const; inline bool operator!=(const TSamplingData&)const; template inline TIntervalStep interval(IT nrsamples) const; template inline float getIndex(FT) const; template inline int nearestIndex(FT) const; template inline T atIndex(IT) const; template inline T snap(FT) const; template inline void set(FT,FT); template inline void set(const TSamplingData&); template inline void set(const StepInterval&); inline void scale(T); T start; T step; }; template inline TSamplingData::TSamplingData( T sa, T se ) : start(sa), step(se) {} template inline TSamplingData::TSamplingData( T x0, T y0, T x1, T y1 ) { step = (y1-y0) / (x1-x0); start = y0 - step*x0; } template template inline TSamplingData::TSamplingData( const TSamplingData& sd ) : start( sd.start ), step( sd.step ) {} template template inline TSamplingData::TSamplingData( const StepInterval& intv ) : start(intv.start), step(intv.step) {} template inline bool TSamplingData::operator==( const TSamplingData& sd ) const { return start == sd.start && step == sd.step; } template <> inline bool TSamplingData::operator==( const TSamplingData& sd ) const { float val = start - sd.start; if ( !mIsZero(val,1e-6) ) return false; val = 1 - (step / sd.step); return val < 1e-6 && val > -1e-6; } template <> inline bool TSamplingData::operator==( const TSamplingData& sd ) const { double val = start - sd.start; if ( !mIsZero(val,1e-10) ) return false; val = 1 - (step / sd.step); return val < 1e-10 && val > -1e-10; } template inline bool TSamplingData::operator!=( const TSamplingData& sd ) const { return ! (sd == *this); } template template inline TIntervalStep TSamplingData::interval( IT nrsamp ) const { return nrsamp ? TIntervalStep( start, start+(nrsamp-1)*step, step ) : TIntervalStep( start, start, step ); } template template inline float TSamplingData::getIndex( FT val ) const { return (val-start) / ((float)step); } template template inline int TSamplingData::nearestIndex( FT x ) const { const float fidx = getIndex(x); return mNINT(fidx); } template template inline T TSamplingData::snap( FT val ) const { return start + step * nearestIndex(val); } template template inline T TSamplingData::atIndex( IT idx ) const { return start + step * (T)idx; } template template inline void TSamplingData::set( FT sa, FT se ) { start = sa; step = se; } template template inline void TSamplingData::set( const StepInterval& intv ) { start = intv.start; step = intv.step; } template template inline void TSamplingData::set( const TSamplingData& sd ) { start = sd.start; step = sd.step; } template inline void TSamplingData::scale( T scl ) { start *= scl; step *= scl; } }//namespace #endif