~showard314/ubuntu/natty/qtiplot/Python2.7_fix

« back to all changes in this revision

Viewing changes to 3rdparty/qwt/src/qwt_scale_map.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-04-04 15:11:55 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080404151155-rjp12ziov4tryj0o
Tags: 0.9.4-1
* New upstream release.
* Refresh patches.
* Remove 04_homepage_url patch. Merged upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 * 
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
#include "qwt_scale_map.h"
 
11
 
 
12
QT_STATIC_CONST_IMPL double QwtScaleMap::LogMin = 1.0e-150;
 
13
QT_STATIC_CONST_IMPL double QwtScaleMap::LogMax = 1.0e150;
 
14
 
 
15
//!  Constructor for a linear transformation
 
16
QwtScaleTransformation::QwtScaleTransformation(Type type):
 
17
    d_type(type)
 
18
{
 
19
}
 
20
 
 
21
QwtScaleTransformation::~QwtScaleTransformation()
 
22
{
 
23
}
 
24
 
 
25
QwtScaleTransformation *QwtScaleTransformation::copy() const
 
26
{
 
27
    return new QwtScaleTransformation(d_type);
 
28
}
 
29
 
 
30
/*!
 
31
  \brief Transform a value between 2 linear intervals
 
32
 
 
33
  \param x value related to the interval [x1, x2]
 
34
  \param x1 first border of source interval
 
35
  \param x2 first border of source interval
 
36
  \param y1 first border of target interval
 
37
  \param y2 first border of target interval
 
38
  \return 
 
39
  <dl>
 
40
  <dt>linear mapping:<dd>y1 + (y2 - y1) / (x2 - x1) * (x - x1)</dd>
 
41
  </dl>
 
42
  <dl>
 
43
  <dt>log10 mapping: <dd>p1 + (p2 - p1) / log(s2 / s1) * log(x / s1)</dd>
 
44
  </dl>
 
45
*/
 
46
 
 
47
double QwtScaleTransformation::xForm(
 
48
    double s, double s1, double s2, double p1, double p2) const
 
49
{
 
50
    if ( d_type == Log10 )  
 
51
        return p1 + (p2 - p1) / log(s2 / s1) * log(s / s1);
 
52
    else 
 
53
        return p1 + (p2 - p1) / (s2 - s1) * (s - s1);
 
54
}
 
55
 
 
56
/*!
 
57
  \brief Transform a value from a linear to a logarithmic interval
 
58
 
 
59
  \param x value related to the linear interval [p1, p2]
 
60
  \param p1 first border of linear interval
 
61
  \param p2 first border of linear interval
 
62
  \param s1 first border of logarithmic interval
 
63
  \param s2 first border of logarithmic interval
 
64
  \return 
 
65
  <dl>
 
66
  <dt>exp((x - p1) / (p2 - p1) * log(s2 / s1)) * s1;
 
67
  </dl>
 
68
*/
 
69
 
 
70
double QwtScaleTransformation::invXForm(double p, double p1, double p2, 
 
71
    double s1, double s2) const
 
72
{
 
73
    if ( d_type == Log10 )  
 
74
        return exp((p - p1) / (p2 - p1) * log(s2 / s1)) * s1;
 
75
    else
 
76
        return s1 + (s2 - s1) / (p2 - p1) * (p - p1);
 
77
}
 
78
 
 
79
/*!
 
80
  \brief Constructor
 
81
 
 
82
  The scale and paint device intervals are both set to [0,1].
 
83
*/
 
84
QwtScaleMap::QwtScaleMap():
 
85
    d_s1(0.0),
 
86
    d_s2(1.0),
 
87
    d_p1(0.0),
 
88
    d_p2(1.0),
 
89
    d_cnv(1.0)
 
90
{
 
91
    d_transformation = new QwtScaleTransformation(
 
92
        QwtScaleTransformation::Linear);
 
93
}
 
94
 
 
95
QwtScaleMap::QwtScaleMap(const QwtScaleMap& other):
 
96
    d_s1(other.d_s1),
 
97
    d_s2(other.d_s2),
 
98
    d_p1(other.d_p1),
 
99
    d_p2(other.d_p2),
 
100
    d_cnv(other.d_cnv)
 
101
{
 
102
    d_transformation = other.d_transformation->copy();
 
103
}
 
104
 
 
105
/*!
 
106
  Destructor
 
107
*/
 
108
QwtScaleMap::~QwtScaleMap()
 
109
{
 
110
    delete d_transformation;
 
111
}
 
112
 
 
113
QwtScaleMap &QwtScaleMap::operator=(const QwtScaleMap &other)
 
114
{
 
115
    d_s1 = other.d_s1;
 
116
    d_s2 = other.d_s2;
 
117
    d_p1 = other.d_p1;
 
118
    d_p2 = other.d_p2;
 
119
    d_cnv = other.d_cnv;
 
120
 
 
121
    delete d_transformation;
 
122
    d_transformation = other.d_transformation->copy();
 
123
 
 
124
    return *this;
 
125
}
 
126
 
 
127
/*!
 
128
   Initialize the map with a transformation
 
129
*/
 
130
void QwtScaleMap::setTransformation(
 
131
    QwtScaleTransformation *transformation)
 
132
{
 
133
    if ( transformation == NULL )
 
134
        return;
 
135
 
 
136
    delete d_transformation;
 
137
    d_transformation = transformation;
 
138
    setScaleInterval(d_s1, d_s2);
 
139
}
 
140
 
 
141
//! Get the transformation
 
142
const QwtScaleTransformation *QwtScaleMap::transformation() const
 
143
{
 
144
    return d_transformation;
 
145
}
 
146
 
 
147
/*!
 
148
  \brief Specify the borders of the scale interval
 
149
  \param s1 first border
 
150
  \param s2 second border 
 
151
  \warning logarithmic scales might be aligned to [LogMin, LogMax]
 
152
*/
 
153
void QwtScaleMap::setScaleInterval(double s1, double s2)
 
154
{
 
155
    if (d_transformation->type() == QwtScaleTransformation::Log10 )
 
156
    {
 
157
        if (s1 < LogMin) 
 
158
           s1 = LogMin;
 
159
        else if (s1 > LogMax) 
 
160
           s1 = LogMax;
 
161
        
 
162
        if (s2 < LogMin) 
 
163
           s2 = LogMin;
 
164
        else if (s2 > LogMax) 
 
165
           s2 = LogMax;
 
166
    }
 
167
 
 
168
    d_s1 = s1;
 
169
    d_s2 = s2;
 
170
 
 
171
    if ( d_transformation->type() != QwtScaleTransformation::Other )
 
172
        newFactor();
 
173
}
 
174
 
 
175
/*!
 
176
  \brief Specify the borders of the paint device interval
 
177
  \param p1 first border
 
178
  \param p2 second border
 
179
*/
 
180
void QwtScaleMap::setPaintInterval(int p1, int p2)
 
181
{
 
182
    d_p1 = p1;
 
183
    d_p2 = p2;
 
184
 
 
185
    if ( d_transformation->type() != QwtScaleTransformation::Other )
 
186
        newFactor();
 
187
}
 
188
 
 
189
/*!
 
190
  \brief Specify the borders of the paint device interval
 
191
  \param p1 first border
 
192
  \param p2 second border
 
193
*/
 
194
void QwtScaleMap::setPaintXInterval(double p1, double p2)
 
195
{
 
196
    d_p1 = p1;
 
197
    d_p2 = p2;
 
198
 
 
199
    if ( d_transformation->type() != QwtScaleTransformation::Other )
 
200
        newFactor();
 
201
}
 
202
 
 
203
/*!
 
204
  \brief Re-calculate the conversion factor.
 
205
*/
 
206
void QwtScaleMap::newFactor()
 
207
{
 
208
    d_cnv = 0.0;
 
209
#if 1
 
210
    if (d_s2 == d_s1)
 
211
        return;
 
212
#endif
 
213
 
 
214
    switch( d_transformation->type() )
 
215
    {
 
216
        case QwtScaleTransformation::Linear:
 
217
            d_cnv = (d_p2 - d_p1) / (d_s2 - d_s1); 
 
218
            break;
 
219
        case QwtScaleTransformation::Log10:
 
220
            d_cnv = (d_p2 - d_p1) / log(d_s2 / d_s1);
 
221
            break;
 
222
        default:;
 
223
    }
 
224
}