qwt_scale_map.cpp 5.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_scale_map.h"

QT_STATIC_CONST_IMPL double QwtScaleMap::LogMin = 1.0e-150;
QT_STATIC_CONST_IMPL double QwtScaleMap::LogMax = 1.0e150;

//!  Constructor for a linear transformation
QwtScaleTransformation::QwtScaleTransformation(Type type):
    d_type(type)
{
}

QwtScaleTransformation::~QwtScaleTransformation()
{
}

QwtScaleTransformation *QwtScaleTransformation::copy() const
{
    return new QwtScaleTransformation(d_type);
}

/*!
  \brief Transform a value between 2 linear intervals

  \param x value related to the interval [x1, x2]
  \param x1 first border of source interval
  \param x2 first border of source interval
  \param y1 first border of target interval
  \param y2 first border of target interval
38
  \return
pixhawk's avatar
pixhawk committed
39 40 41 42 43 44 45 46 47 48 49
  <dl>
  <dt>linear mapping:<dd>y1 + (y2 - y1) / (x2 - x1) * (x - x1)</dd>
  </dl>
  <dl>
  <dt>log10 mapping: <dd>p1 + (p2 - p1) / log(s2 / s1) * log(x / s1)</dd>
  </dl>
*/

double QwtScaleTransformation::xForm(
    double s, double s1, double s2, double p1, double p2) const
{
50
    if ( d_type == Log10 )
pixhawk's avatar
pixhawk committed
51
        return p1 + (p2 - p1) / log(s2 / s1) * log(s / s1);
52
    else
pixhawk's avatar
pixhawk committed
53 54 55 56 57 58 59 60 61 62 63
        return p1 + (p2 - p1) / (s2 - s1) * (s - s1);
}

/*!
  \brief Transform a value from a linear to a logarithmic interval

  \param x value related to the linear interval [p1, p2]
  \param p1 first border of linear interval
  \param p2 first border of linear interval
  \param s1 first border of logarithmic interval
  \param s2 first border of logarithmic interval
64
  \return
pixhawk's avatar
pixhawk committed
65 66 67 68 69
  <dl>
  <dt>exp((x - p1) / (p2 - p1) * log(s2 / s1)) * s1;
  </dl>
*/

70 71
double QwtScaleTransformation::invXForm(double p, double p1, double p2,
                                        double s1, double s2) const
pixhawk's avatar
pixhawk committed
72
{
73
    if ( d_type == Log10 )
pixhawk's avatar
pixhawk committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        return exp((p - p1) / (p2 - p1) * log(s2 / s1)) * s1;
    else
        return s1 + (s2 - s1) / (p2 - p1) * (p - p1);
}

/*!
  \brief Constructor

  The scale and paint device intervals are both set to [0,1].
*/
QwtScaleMap::QwtScaleMap():
    d_s1(0.0),
    d_s2(1.0),
    d_p1(0.0),
    d_p2(1.0),
    d_cnv(1.0)
{
    d_transformation = new QwtScaleTransformation(
        QwtScaleTransformation::Linear);
}

QwtScaleMap::QwtScaleMap(const QwtScaleMap& other):
    d_s1(other.d_s1),
    d_s2(other.d_s2),
    d_p1(other.d_p1),
    d_p2(other.d_p2),
    d_cnv(other.d_cnv)
{
    d_transformation = other.d_transformation->copy();
}

/*!
  Destructor
*/
QwtScaleMap::~QwtScaleMap()
{
    delete d_transformation;
}

QwtScaleMap &QwtScaleMap::operator=(const QwtScaleMap &other)
{
    d_s1 = other.d_s1;
    d_s2 = other.d_s2;
    d_p1 = other.d_p1;
    d_p2 = other.d_p2;
    d_cnv = other.d_cnv;

    delete d_transformation;
    d_transformation = other.d_transformation->copy();

    return *this;
}

/*!
   Initialize the map with a transformation
*/
void QwtScaleMap::setTransformation(
    QwtScaleTransformation *transformation)
{
    if ( transformation == NULL )
        return;

    delete d_transformation;
    d_transformation = transformation;
    setScaleInterval(d_s1, d_s2);
}

//! Get the transformation
const QwtScaleTransformation *QwtScaleMap::transformation() const
{
    return d_transformation;
}

/*!
  \brief Specify the borders of the scale interval
  \param s1 first border
150
  \param s2 second border
pixhawk's avatar
pixhawk committed
151 152 153 154
  \warning logarithmic scales might be aligned to [LogMin, LogMax]
*/
void QwtScaleMap::setScaleInterval(double s1, double s2)
{
155 156 157 158 159 160 161 162 163 164
    if (d_transformation->type() == QwtScaleTransformation::Log10 ) {
        if (s1 < LogMin)
            s1 = LogMin;
        else if (s1 > LogMax)
            s1 = LogMax;

        if (s2 < LogMin)
            s2 = LogMin;
        else if (s2 > LogMax)
            s2 = LogMax;
pixhawk's avatar
pixhawk committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    }

    d_s1 = s1;
    d_s2 = s2;

    if ( d_transformation->type() != QwtScaleTransformation::Other )
        newFactor();
}

/*!
  \brief Specify the borders of the paint device interval
  \param p1 first border
  \param p2 second border
*/
void QwtScaleMap::setPaintInterval(int p1, int p2)
{
    d_p1 = p1;
    d_p2 = p2;

    if ( d_transformation->type() != QwtScaleTransformation::Other )
        newFactor();
}

/*!
  \brief Specify the borders of the paint device interval
  \param p1 first border
  \param p2 second border
*/
void QwtScaleMap::setPaintXInterval(double p1, double p2)
{
    d_p1 = p1;
    d_p2 = p2;

    if ( d_transformation->type() != QwtScaleTransformation::Other )
        newFactor();
}

/*!
  \brief Re-calculate the conversion factor.
*/
void QwtScaleMap::newFactor()
{
    d_cnv = 0.0;
#if 1
    if (d_s2 == d_s1)
        return;
#endif

213 214 215 216 217 218 219 220 221
    switch( d_transformation->type() ) {
    case QwtScaleTransformation::Linear:
        d_cnv = (d_p2 - d_p1) / (d_s2 - d_s1);
        break;
    case QwtScaleTransformation::Log10:
        d_cnv = (d_p2 - d_p1) / log(d_s2 / d_s1);
        break;
    default:
        ;
pixhawk's avatar
pixhawk committed
222 223
    }
}