From bb337415522291573c72cf92d5460865e766b4fb Mon Sep 17 00:00:00 2001 From: BobLd Date: Tue, 28 Apr 2020 22:01:50 +0100 Subject: [PATCH] Fix KdTree.FindNearestNeighbours(k) returning the pivot itself --- src/UglyToad.PdfPig.Core/PdfRectangle.cs | 4 ++++ .../Distances.cs | 9 +++++---- .../KdTree.cs | 2 +- src/UglyToad.PdfPig/Content/Word.cs | 14 +++++++------- src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/UglyToad.PdfPig.Core/PdfRectangle.cs b/src/UglyToad.PdfPig.Core/PdfRectangle.cs index 4a57c5b0..808b1d48 100644 --- a/src/UglyToad.PdfPig.Core/PdfRectangle.cs +++ b/src/UglyToad.PdfPig.Core/PdfRectangle.cs @@ -81,6 +81,7 @@ /// /// Rotation angle of the rectangle. Counterclockwise, in degrees. + /// -180 ≤ θ ≤ 180 /// public double Rotation { @@ -173,6 +174,9 @@ BottomLeft.Translate(dx, dy), BottomRight.Translate(dx, dy)); } + /// + /// -π ≤ θ ≤ π + /// private double GetT() { if (!BottomRight.Equals(BottomLeft)) diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Distances.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Distances.cs index bb13f97a..3dc8688e 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Distances.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/Distances.cs @@ -49,12 +49,13 @@ /// /// The angle in degrees between the horizontal axis and the line between two points. + /// -180 ≤ θ ≤ 180 /// - /// The first point. - /// The second point. - public static double Angle(PdfPoint point1, PdfPoint point2) + /// The first point. + /// The second point. + public static double Angle(PdfPoint startPoint, PdfPoint endPoint) { - return Math.Atan2(point2.Y - point1.Y, point2.X - point1.X) * 57.29577951; + return Math.Atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X) * 180 / Math.PI; } /// diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/KdTree.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/KdTree.cs index 7fedbac2..648fed75 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/KdTree.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/KdTree.cs @@ -250,7 +250,7 @@ var point = pivotPointFunc(pivot); var currentNearestNode = node; var currentDistance = distance(node.Value, point); - if (!queue.IsFull || currentDistance <= queue.LastDistance) + if ((!queue.IsFull || currentDistance <= queue.LastDistance) && !node.Element.Equals(pivot)) { queue.Add(currentDistance, currentNearestNode); currentDistance = queue.LastDistance; diff --git a/src/UglyToad.PdfPig/Content/Word.cs b/src/UglyToad.PdfPig/Content/Word.cs index d1b731b4..3c89c758 100644 --- a/src/UglyToad.PdfPig/Content/Word.cs +++ b/src/UglyToad.PdfPig/Content/Word.cs @@ -333,33 +333,33 @@ // Find the orientation of the OBB, using the baseline angle var firstLetter = letters[0]; var lastLetter = letters[letters.Count - 1]; + var baseLineAngle = Math.Atan2( lastLetter.EndBaseLine.Y - firstLetter.StartBaseLine.Y, lastLetter.EndBaseLine.X - firstLetter.StartBaseLine.X) * 180 / Math.PI; - var bbox = obb; - var deltaAngle = Math.Abs(baseLineAngle - angleRad * 180 / Math.PI); - + double deltaAngle = Math.Abs(baseLineAngle - obb.Rotation); double deltaAngle1 = Math.Abs(baseLineAngle - obb1.Rotation); if (deltaAngle1 < deltaAngle) { deltaAngle = deltaAngle1; - bbox = obb1; + obb = obb1; } double deltaAngle2 = Math.Abs(baseLineAngle - obb2.Rotation); if (deltaAngle2 < deltaAngle) { deltaAngle = deltaAngle2; - bbox = obb2; + obb = obb2; } double deltaAngle3 = Math.Abs(baseLineAngle - obb3.Rotation); if (deltaAngle3 < deltaAngle) { - bbox = obb3; + obb = obb3; } - return new Tuple(builder.ToString(), bbox); + + return new Tuple(builder.ToString(), obb); } #endregion diff --git a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs index 6c09b1f4..1538d9f6 100644 --- a/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs +++ b/src/UglyToad.PdfPig/Geometry/GeometryExtensions.cs @@ -230,7 +230,7 @@ var slope = sumProduct / sumDiffSquaredX; // Rotate the points to build the axis-aligned bounding box (AABB) - var angleRad = Math.Atan(slope); + var angleRad = Math.Atan(slope); // -π/2 ≤ θ ≤ π/2 var cos = Math.Cos(angleRad); var sin = Math.Sin(angleRad);