You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
3.7 KiB
C
150 lines
3.7 KiB
C
|
1 month ago
|
#ifndef samplingdata_h
|
||
|
|
#define samplingdata_h
|
||
|
|
|
||
|
|
#include "TInterval.h"
|
||
|
|
|
||
|
|
namespace NSet
|
||
|
|
{
|
||
|
|
|
||
|
|
/*!\brief holds the fundamental sampling info: start and interval. */
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
class TSamplingData
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
|
||
|
|
inline TSamplingData(T sa=0,T se=1);
|
||
|
|
inline TSamplingData(T x0,T y0,T x1,T y1);
|
||
|
|
template <class FT> inline TSamplingData(const TSamplingData<FT>&);
|
||
|
|
template <class FT> inline TSamplingData(const StepInterval<FT>&);
|
||
|
|
|
||
|
|
inline bool operator==(const TSamplingData&)const;
|
||
|
|
inline bool operator!=(const TSamplingData&)const;
|
||
|
|
|
||
|
|
template <class IT> inline TIntervalStep<T> interval(IT nrsamples) const;
|
||
|
|
template <class FT> inline float getIndex(FT) const;
|
||
|
|
template <class FT> inline int nearestIndex(FT) const;
|
||
|
|
template <class IT> inline T atIndex(IT) const;
|
||
|
|
template <class FT> inline T snap(FT) const;
|
||
|
|
|
||
|
|
template <class FT> inline void set(FT,FT);
|
||
|
|
template <class FT> inline void set(const TSamplingData<FT>&);
|
||
|
|
template <class FT> inline void set(const StepInterval<FT>&);
|
||
|
|
inline void scale(T);
|
||
|
|
|
||
|
|
T start;
|
||
|
|
T step;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
template <class T> inline
|
||
|
|
TSamplingData<T>::TSamplingData( T sa, T se )
|
||
|
|
: start(sa), step(se)
|
||
|
|
{}
|
||
|
|
|
||
|
|
|
||
|
|
template <class T> inline
|
||
|
|
TSamplingData<T>::TSamplingData( T x0, T y0, T x1, T y1 )
|
||
|
|
{
|
||
|
|
step = (y1-y0) / (x1-x0);
|
||
|
|
start = y0 - step*x0;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
TSamplingData<T>::TSamplingData( const TSamplingData<FT>& sd )
|
||
|
|
: start( sd.start ), step( sd.step )
|
||
|
|
{}
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
TSamplingData<T>::TSamplingData( const StepInterval<FT>& intv )
|
||
|
|
: start(intv.start), step(intv.step)
|
||
|
|
{}
|
||
|
|
|
||
|
|
|
||
|
|
template <class T> inline
|
||
|
|
bool TSamplingData<T>::operator==( const TSamplingData& sd ) const
|
||
|
|
{ return start == sd.start && step == sd.step; }
|
||
|
|
|
||
|
|
template <> inline
|
||
|
|
bool TSamplingData<float>::operator==( const TSamplingData<float>& 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<double>::operator==( const TSamplingData<double>& 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 <class T> inline
|
||
|
|
bool TSamplingData<T>::operator!=( const TSamplingData& sd ) const
|
||
|
|
{ return ! (sd == *this); }
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class IT> inline
|
||
|
|
TIntervalStep<T> TSamplingData<T>::interval( IT nrsamp ) const
|
||
|
|
{
|
||
|
|
return nrsamp ? TIntervalStep<T>( start, start+(nrsamp-1)*step, step )
|
||
|
|
: TIntervalStep<T>( start, start, step ); }
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
float TSamplingData<T>::getIndex( FT val ) const
|
||
|
|
{ return (val-start) / ((float)step); }
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
int TSamplingData<T>::nearestIndex( FT x ) const
|
||
|
|
{ const float fidx = getIndex(x); return mNINT(fidx); }
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
T TSamplingData<T>::snap( FT val ) const
|
||
|
|
{ return start + step * nearestIndex(val); }
|
||
|
|
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class IT> inline
|
||
|
|
T TSamplingData<T>::atIndex( IT idx ) const
|
||
|
|
{ return start + step * (T)idx; }
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
void TSamplingData<T>::set( FT sa, FT se )
|
||
|
|
{ start = sa; step = se; }
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
void TSamplingData<T>::set( const StepInterval<FT>& intv )
|
||
|
|
{ start = intv.start; step = intv.step; }
|
||
|
|
|
||
|
|
template <class T>
|
||
|
|
template <class FT> inline
|
||
|
|
void TSamplingData<T>::set( const TSamplingData<FT>& sd )
|
||
|
|
{ start = sd.start; step = sd.step; }
|
||
|
|
|
||
|
|
template <class T> inline
|
||
|
|
void TSamplingData<T>::scale( T scl )
|
||
|
|
{ start *= scl; step *= scl; }
|
||
|
|
|
||
|
|
|
||
|
|
}//namespace
|
||
|
|
|
||
|
|
#endif
|