Engauge Digitizer  2
ExportOrdinalsSmooth.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "ExportOrdinalsSmooth.h"
8 #include "LinearToLog.h"
9 #include "Logger.h"
10 #include <qdebug.h>
11 #include <qmath.h>
12 #include <QPointF>
13 #include "Spline.h"
14 #include "Transformation.h"
15 
16 using namespace std;
17 
19 {
20 }
21 
23  vector<double> &t,
24  vector<SplinePair> &xy) const
25 {
26  LOG4CPP_INFO_S ((*mainCat)) << "ExportOrdinalsSmooth::loadSplinePairsWithoutTransformation";
27 
28  Points::const_iterator itrP;
29  for (itrP = points.begin(); itrP != points.end(); itrP++) {
30  const Point &point = *itrP;
31  QPointF posScreen = point.posScreen();
32 
33  t.push_back (point.ordinal ());
34  xy.push_back (SplinePair (posScreen.x(),
35  posScreen.y()));
36  }
37 }
38 
40  const Transformation &transformation,
41  bool isLogXTheta,
42  bool isLogYRadius,
43  vector<double> &t,
44  vector<SplinePair> &xy) const
45 {
46  LOG4CPP_INFO_S ((*mainCat)) << "ExportOrdinalsSmooth::loadSplinePairsWithTransformation";
47 
48  LinearToLog linearToLog;
49 
50  Points::const_iterator itrP;
51  for (itrP = points.begin(); itrP != points.end(); itrP++) {
52  const Point &point = *itrP;
53  QPointF posScreen = point.posScreen();
54  QPointF posGraph;
55  transformation.transformScreenToRawGraph (posScreen,
56  posGraph);
57 
58  t.push_back (point.ordinal ());
59  xy.push_back (SplinePair (linearToLog.linearize (posGraph.x(), isLogXTheta),
60  linearToLog.linearize (posGraph.y(), isLogYRadius)));
61  }
62 }
63 
64 ExportValuesOrdinal ExportOrdinalsSmooth::ordinalsAtIntervalsGraph (const vector<double> &t,
65  const vector<SplinePair> &xy,
66  double pointsInterval) const
67 {
68  LOG4CPP_INFO_S ((*mainCat)) << "ExportOrdinalsSmooth::ordinalsAtIntervalsGraph";
69 
70  const double NUM_SMALLER_INTERVALS = 1000;
71 
72  // Results. Initially empty, but at the end it will have tMin, ..., tMax
73  ExportValuesOrdinal ordinals;
74 
75  // Spline class requires at least one point
76  if (xy.size() > 0) {
77 
78  // Fit a spline
79  Spline spline (t,
80  xy);
81 
82  // Integrate the distances for the subintervals
83  double integratedSeparation = 0;
84  QPointF posLast (xy [0].x(),
85  xy [0].y());
86 
87  // Simplest method to find the intervals is to break up the curve into many smaller intervals, and then aggregate them
88  // into intervals that, as much as possible, have the desired length. Simplicity wins out over accuracy in this
89  // approach - accuracy is sacrificed to achieve simplicity
90  double tMin = t.front();
91  double tMax = t.back();
92 
93  double tLast = 0.0;
94  int iTLastInterval = 0;
95  for (int iT = 0; iT < NUM_SMALLER_INTERVALS; iT++) {
96 
97  double t = tMin + ((tMax - tMin) * iT) / (NUM_SMALLER_INTERVALS - 1.0);
98 
99  SplinePair pairNew = spline.interpolateCoeff(t);
100 
101  QPointF posNew = QPointF (pairNew.x(),
102  pairNew.y());
103 
104  QPointF posDelta = posNew - posLast;
105  double integratedSeparationDelta = qSqrt (posDelta.x() * posDelta.x() + posDelta.y() * posDelta.y());
106  integratedSeparation += integratedSeparationDelta;
107 
108  while (integratedSeparation >= pointsInterval) {
109 
110  // End of current interval, and start of next interval. For better accuracy without having to crank up
111  // the number of points by orders of magnitude, we use linear interpolation
112  double sInterp;
113  if (iT == 0) {
114  sInterp = 0.0;
115  } else {
116  sInterp = (double) pointsInterval / (double) integratedSeparation;
117  }
118  double tInterp = (1.0 - sInterp) * tLast + sInterp * t;
119 
120  integratedSeparation -= pointsInterval; // Part of delta that was not used gets applied to next interval
121 
122  tLast = tInterp;
123  ordinals.push_back (tInterp);
124  iTLastInterval = iT;
125  }
126 
127  tLast = t;
128  posLast = posNew;
129  }
130 
131  if (iTLastInterval < NUM_SMALLER_INTERVALS - 1) {
132 
133  // Add last point so we end up at tMax
134  ordinals.push_back (tMax);
135 
136  }
137  }
138 
139  return ordinals;
140 }
Cubic interpolation given independent and dependent value vectors.
Definition: Spline.h:21
SplinePair interpolateCoeff(double t) const
Return interpolated y for specified x.
Definition: Spline.cpp:166
double y() const
Get method for y.
Definition: SplinePair.cpp:71
double linearize(double value, bool isLog) const
Convert log coordinates to linear. This is a noop if the input is already linear. ...
Definition: LinearToLog.cpp:19
Class that represents one digitized point. The screen-to-graph coordinate transformation is always ex...
Definition: Point.h:23
QPointF posScreen() const
Accessor for screen position.
Definition: Point.cpp:392
ExportValuesOrdinal ordinalsAtIntervalsGraph(const std::vector< double > &t, const std::vector< SplinePair > &xy, double pointsInterval) const
Perform the interpolation on the arrays loaded by the other methods.
double ordinal(ApplyHasCheck applyHasCheck=KEEP_HAS_CHECK) const
Get method for ordinal. Skip check if copying one instance to another.
Definition: Point.cpp:374
Affine transformation between screen and graph coordinates, based on digitized axis points...
void loadSplinePairsWithTransformation(const Points &points, const Transformation &transformation, bool isLogXTheta, bool isLogYRadius, std::vector< double > &t, std::vector< SplinePair > &xy) const
Load t (=ordinal) and xy (=screen position) spline pairs, converting screen coordinates to graph coor...
void transformScreenToRawGraph(const QPointF &coordScreen, QPointF &coordGraph) const
Transform from cartesian pixel screen coordinates to cartesian/polar graph coordinates.
double x() const
Get method for x.
Definition: SplinePair.cpp:66
Warps log coordinates to make them linear before passing them to code that accepts only linear coordi...
Definition: LinearToLog.h:7
ExportOrdinalsSmooth()
Single constructor.
Single X/Y pair for cubic spline interpolation initialization and calculations.
Definition: SplinePair.h:11
void loadSplinePairsWithoutTransformation(const Points &points, std::vector< double > &t, std::vector< SplinePair > &xy) const
Load t (=ordinal) and xy (=screen position) spline pairs, without any conversion to graph coordinates...