Qwt源码解读之变换类——QwtTransform、QwtNullTransform、QwtLogTransform、QwtPowerTransform

来源:互联网 发布:委内瑞拉 知乎 编辑:程序博客网 时间:2024/06/01 10:13

先看一看Qwt的文档说明:

A transformation between coordinate systems.

QwtTransform manipulates values, when being mapped between the scale and the paint device coordinate system.

A transformation consists of 2 methods:
transform          正变换
invTransform    逆变换

where one is the inverse function of the other.
When p1, p2 are the boundaries of the paint device coordinates and s1, s2 the boundaries of the scale, QwtScaleMap uses the following calculations:
p1、p2——绘制设备的坐标的边界值

s1、s2——刻度尺的坐标范围的边界值
p = p1 + ( p2 - p1 ) * ( T( s ) - T( s1 ) / ( T( s2 ) - T( s1 ) );
s = invT ( T( s1 ) + ( T( s2 ) - T( s1 ) ) * ( p - p1 ) / ( p2 - p1 ) );

类继承关系

如下图所示:


Qwt提供了一些变换/逆变换类,如下所示:

1、QwtTransform——其他类的基类

class QWT_EXPORT QwtTransform{public:    QwtTransform();    virtual ~QwtTransform();    /*!       Modify value to be a valid value for the transformation.       The default implementation does nothing.     */    virtual double bounded( double value ) const;    /*!        Transformation function        \param value Value        \return Modified value        \sa invTransform()     */    virtual double transform( double value ) const = 0;//正变换,由子类实现    /*!        Inverse transformation function        \param value Value        \return Modified value        \sa transform()     */    virtual double invTransform( double value ) const = 0;//逆变换,由子类实现    //! Virtualized copy operation    virtual QwtTransform *copy() const = 0;//拷贝函数,由子类实现};
成员函数

/*!   \param value Value to be bounded  \return value unmodified */double QwtTransform::bounded( double value ) const{    return value;}
没啥用,一般由子类派生。
2、QwtNullTransform类——这个类基本啥也不做。
/*!   \brief Null transformation   QwtNullTransform returns the values unmodified.    */class QWT_EXPORT QwtNullTransform: public QwtTransform{public:    QwtNullTransform();    virtual ~QwtNullTransform();    virtual double transform( double value ) const;    virtual double invTransform( double value ) const;    virtual QwtTransform *copy() const;};
成员函数
/*!   \param value Value to be transformed  \return value unmodified */double QwtNullTransform::transform( double value ) const{    return value;}/*!   \param value Value to be transformed  \return value unmodified */double QwtNullTransform::invTransform( double value ) const{    return value;}
其实就是返回原始值。
3、QwtLogTransform类——对数变换类

/*!   \brief Logarithmic transformation   QwtLogTransform modifies the values using log() and exp().   \note In the calculations of QwtScaleMap the base of the log function         has no effect on the mapping. So QwtLogTransform can be used          for log2(), log10() or any other logarithmic scale. */class QWT_EXPORT QwtLogTransform: public QwtTransform{   public:    QwtLogTransform();    virtual ~QwtLogTransform();        virtual double transform( double value ) const;    virtual double invTransform( double value ) const;    virtual double bounded( double value ) const;    virtual QwtTransform *copy() const;    QT_STATIC_CONST double LogMin;    QT_STATIC_CONST double LogMax;};
其中

double QwtLogTransform::LogMin = 1.0e-150;
double QwtLogTransform::LogMax = 1.0e150;
成员函数

/*!   \param value Value to be transformed  \return log( value ) */double QwtLogTransform::transform( double value ) const{    return ::log( value );}/*!   \param value Value to be transformed  \return exp( value ) */double QwtLogTransform::invTransform( double value ) const{    return qExp( value );}
正变换为对数变换,逆变换为指数变换。且底数均为自然数e。
/*!   \param value Value to be bounded  \return qBound( LogMin, value, LogMax ) */double QwtLogTransform::bounded( double value ) const{    return qBound( LogMin, value, LogMax );}
限制要变换的值在[LogMin, LogMax]之间。

4.QwtPowerTransform类——幂变换类

/*!   \brief A transformation using pow()   QwtPowerTransform preserves the sign of a value.    F.e. a transformation with a factor of 2   transforms a value of -3 to -9 and v.v. Thus QwtPowerTransform   can be used for scales including negative values. */class QWT_EXPORT QwtPowerTransform: public QwtTransform{public:    QwtPowerTransform( double exponent );    virtual ~QwtPowerTransform();    virtual double transform( double value ) const;    virtual double invTransform( double value ) const;    virtual QwtTransform *copy() const;private:    const double d_exponent;// 指数};
注意:在计算过程中,会保留value的符号,即,如果value为负,不论d_exponent为奇数还是偶数,变换结果均为负数。

成员函数

/*!   \param value Value to be transformed  \return Exponentiation preserving the sign */double QwtPowerTransform::transform( double value ) const{    if ( value < 0.0 )        return -qPow( -value, 1.0 / d_exponent );    else        return qPow( value, 1.0 / d_exponent );    }/*!   \param value Value to be transformed  \return Inverse exponentiation preserving the sign */double QwtPowerTransform::invTransform( double value ) const{    if ( value < 0.0 )        return -qPow( -value, d_exponent );    else        return qPow( value, d_exponent );}
正变换为开方运算,逆变换为幂运算。

1 0
原创粉丝点击