Engauge Digitizer 2
Loading...
Searching...
No Matches
CallbackPointOrdinal.cpp
Go to the documentation of this file.
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
8#include "EngaugeAssert.h"
9#include "Logger.h"
10#include "mmsubs.h"
11#include "Point.h"
12#include <qmath.h>
13#include "Transformation.h"
14
16 const Transformation &transformation,
17 const QPointF &posScreen) :
18 m_lineStyle (lineStyle),
19 m_transformation (transformation),
20 m_posScreen (posScreen),
21 m_haveMinimumDistanceToLine (false),
22 m_minimumDistanceToLine (0.0),
23 m_minimumProjectedDistanceOutsideLine (0.0),
24 m_ordinal (0)
25{
26}
27
29 const Point &pointStop)
30{
31 double xProjection, yProjection, projectedDistanceOutsideLine, distanceToLine;
32
33 projectPointOntoLine(m_posScreen.x(),
34 m_posScreen.y(),
35 pointStart.posScreen().x(),
36 pointStart.posScreen().y(),
37 pointStop.posScreen().x(),
38 pointStop.posScreen().y(),
39 &xProjection,
40 &yProjection,
41 &projectedDistanceOutsideLine,
42 &distanceToLine);
43
44 // Compare the best so far
45 bool distancesAreEqual = (qAbs (distanceToLine - m_minimumDistanceToLine) <= 0); // Epsilon test prevents compiler warning
46 if (!m_haveMinimumDistanceToLine ||
47 (distanceToLine < m_minimumDistanceToLine) ||
48 (distancesAreEqual && projectedDistanceOutsideLine < m_minimumProjectedDistanceOutsideLine)) {
49
50 // Compute ordinal using epsilon test to prevent compiler warning
51 if (qAbs (projectedDistanceOutsideLine) <= 0) {
52
53 // Put new point inside the line segment
54 m_ordinal = (pointStart.ordinal() + pointStop.ordinal()) / 2.0;
55
56 } else {
57
58 // Put new point just outside the line segment
59 double distanceProjectionToStart = qSqrt ((xProjection - pointStart.posScreen().x()) * (xProjection - pointStart.posScreen().x()) +
60 (yProjection - pointStart.posScreen().y()) * (yProjection - pointStart.posScreen().y()));
61 double distanceProjectionToStop = qSqrt ((xProjection - pointStop.posScreen().x()) * (xProjection - pointStop.posScreen().x()) +
62 (yProjection - pointStop.posScreen().y()) * (yProjection - pointStop.posScreen().y()));
63 if (distanceProjectionToStart < distanceProjectionToStop) {
64
65 // Before start point
66 m_ordinal = pointStart.ordinal() - 0.5;
67
68 } else {
69
70 // After stop point
71 m_ordinal = pointStop.ordinal() + 0.5;
72 }
73 }
74
75 // Save as new 'best'
76 m_haveMinimumDistanceToLine = true;
77 m_minimumDistanceToLine = distanceToLine;
78 m_minimumProjectedDistanceOutsideLine = projectedDistanceOutsideLine;
79 }
80
82}
83
85{
86 return m_ordinal;
87}
CallbackSearchReturn
Return values for search callback methods.
@ CALLBACK_SEARCH_RETURN_CONTINUE
Continue normal execution of the search.
double ordinal() const
Computed ordinal.
CallbackSearchReturn callback(const Point &pointStart, const Point &pointStop)
Callback method.
CallbackPointOrdinal(const LineStyle &lineStyle, const Transformation &transformation, const QPointF &posScreen)
Single constructor.
Details for a specific Line.
Definition LineStyle.h:20
Class that represents one digitized point. The screen-to-graph coordinate transformation is always ex...
Definition Point.h:26
QPointF posScreen() const
Accessor for screen position.
Definition Point.cpp:404
double ordinal(ApplyHasCheck applyHasCheck=KEEP_HAS_CHECK) const
Get method for ordinal. Skip check if copying one instance to another.
Definition Point.cpp:386
Affine transformation between screen and graph coordinates, based on digitized axis points.
void projectPointOntoLine(double xToProject, double yToProject, double xStart, double yStart, double xStop, double yStop, double *xProjection, double *yProjection, double *projectedDistanceOutsideLine, double *distanceToLine)
Find the projection of a point onto a line segment such that the line through the point and its proje...
Definition mmsubs.cpp:251