KChart

CartesianCoordinateTransformation.h
1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef CARTESIANCOORDINATETRANSFORMATION_H
21 #define CARTESIANCOORDINATETRANSFORMATION_H
22 
23 #include <QList>
24 #include <QRectF>
25 #include <QPointF>
26 
27 #include "KChartZoomParameters.h"
28 
29 #include <cmath>
30 #include <limits>
31 
32 namespace KChart {
33 
34  // FIXME: if this struct is used more often, we need to make it a class
35  // with proper accessor methods:
36 
41 
43  : axesCalcModeY( CartesianCoordinatePlane::Linear ),
44  axesCalcModeX( CartesianCoordinatePlane::Linear ),
45  isPositiveX( true ),
46  isPositiveY( true )
47  {}
48 
49  CartesianCoordinatePlane::AxesCalcMode axesCalcModeY;
50  CartesianCoordinatePlane::AxesCalcMode axesCalcModeX;
51 
52  ZoomParameters zoom;
53 
54  QTransform transform;
55  QTransform backTransform;
56  // a logarithmic scale cannot cross zero, so we have to know which side we are on.
57  bool isPositiveX;
58  bool isPositiveY;
59 
60  qreal logTransform( qreal value, bool isPositiveRange ) const
61  {
62  if ( isPositiveRange ) {
63  return log10( value );
64  } else {
65  return -log10( -value );
66  }
67  }
68 
69  qreal logTransformBack( qreal value, bool wasPositive ) const
70  {
71  if ( wasPositive ) {
72  return pow( 10.0, value );
73  } else {
74  return -pow( 10.0, -value );
75  }
76  }
77 
78  void updateTransform( const QRectF& constDataRect, const QRectF& screenRect )
79  {
80  QRectF dataRect = constDataRect;
81  if ( axesCalcModeX == CartesianCoordinatePlane::Logarithmic ) {
82  // the data will be scaled by logTransform() later, so scale its bounds as well
83  isPositiveX = dataRect.left() >= 0.0;
84  dataRect.setLeft( logTransform( dataRect.left(), isPositiveX ) );
85  dataRect.setRight( logTransform( dataRect.right(), isPositiveX ) );
86  }
87  if ( axesCalcModeY == CartesianCoordinatePlane::Logarithmic ) {
88  isPositiveY = dataRect.top() >= 0.0;
89  dataRect.setTop( logTransform( dataRect.top(), isPositiveY ) );
90  dataRect.setBottom( logTransform( dataRect.bottom(), isPositiveY ) );
91  }
92 
93  transform.reset();
94  // read the following transformation sequence from bottom to top(!)
95  transform.translate( screenRect.left(), screenRect.bottom() );
96  transform.scale( screenRect.width(), screenRect.height() );
97 
98  // TODO: mirror in case of "reverse" axes?
99 
100  // transform into screen space
101  transform.translate( 0.5, -0.5 );
102  transform.scale( zoom.xFactor, zoom.yFactor );
103  transform.translate( -zoom.xCenter, 1.0 - zoom.yCenter );
104  // zoom
105  transform.scale( 1.0 / dataRect.width(), 1.0 / dataRect.height() );
106  transform.translate( -dataRect.left(), -dataRect.bottom() );
107  // transform into the unit square
108 
109  backTransform = transform.inverted();
110  }
111 
112  // convert data space point to screen point
113  inline QPointF translate( const QPointF& dataPoint ) const
114  {
115  QPointF data = dataPoint;
116  if ( axesCalcModeX == CartesianCoordinatePlane::Logarithmic ) {
117  data.setX( logTransform( data.x(), isPositiveX ) );
118  }
119  if ( axesCalcModeY == CartesianCoordinatePlane::Logarithmic ) {
120  data.setY( logTransform( data.y(), isPositiveY ) );
121  }
122 
123  return transform.map( data );
124  }
125 
126  // convert screen point to data space point
127  inline const QPointF translateBack( const QPointF& screenPoint ) const
128  {
129  QPointF ret = backTransform.map( screenPoint );
130  if ( axesCalcModeX == CartesianCoordinatePlane::Logarithmic ) {
131  ret.setX( logTransformBack( ret.x(), isPositiveX ) );
132  }
133  if ( axesCalcModeY == CartesianCoordinatePlane::Logarithmic ) {
134  ret.setY( logTransformBack( ret.y(), isPositiveY ) );
135  }
136  return ret;
137  }
138  };
139 
141 
142 }
143 
144 #endif
void reset()
void setRight(qreal x)
QPoint map(const QPoint &point) const const
qreal top() const const
qreal left() const const
QTransform inverted(bool *invertible) const const
void setLeft(qreal x)
qreal bottom() const const
QTransform & translate(qreal dx, qreal dy)
qreal x() const const
qreal y() const const
QTransform & scale(qreal sx, qreal sy)
ZoomParameters stores the center and the factor of zooming internally.
qreal right() const const
qreal width() const const
void setBottom(qreal y)
void setTop(qreal y)
void setX(qreal x)
void setY(qreal y)
qreal height() const const
Global namespace.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Sep 24 2020 22:36:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.