|
5 | 5 | // file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt) |
6 | 6 | // |
7 | 7 |
|
8 | | -using Clipper2Lib; |
| 8 | +using Microsoft.Graphics.Canvas.Geometry; |
9 | 9 | using System; |
10 | 10 | using System.Collections.Generic; |
11 | | -using System.Linq; |
12 | 11 | using System.Numerics; |
13 | 12 | using Telegram.Native.AI; |
14 | 13 |
|
15 | 14 | namespace Telegram.AI |
16 | 15 | { |
17 | | - public static class RecognizedTextBoundingBoxSimplifier |
| 16 | + public static partial class RecognizedTextBoundingBoxSimplifier |
18 | 17 | { |
19 | 18 | public static List<List<Vector2>> Union<T>(IEnumerable<T> boxes, float tolerance, float padding) where T : IOcrObject |
20 | 19 | { |
@@ -43,27 +42,98 @@ public static List<List<Vector2>> Union<T>(IEnumerable<T> boxes, float tolerance |
43 | 42 | return vectors; |
44 | 43 | } |
45 | 44 |
|
46 | | - private static IEnumerable<List<Vector2>> GetUnionOfBoundingBoxes<T>(IEnumerable<T> boxes, float padding) where T : IOcrObject |
| 45 | + private static List<List<Vector2>> GetUnionOfBoundingBoxes<T>(IEnumerable<T> boxes, float padding) where T : IOcrObject |
47 | 46 | { |
48 | | - var clipper = new Clipper64(); |
49 | | - var paths = boxes.Select(box => ConvertToClipperPath(box.BoundingBox.Inflate(padding))).ToList(); |
| 47 | + var empty = true; |
50 | 48 |
|
51 | | - clipper.AddSubject(new Paths64(paths)); |
52 | | - var solution = new Paths64(); |
53 | | - clipper.Execute(ClipType.Union, Clipper2Lib.FillRule.NonZero, solution); |
| 49 | + using (var builder = new CanvasPathBuilder(null)) |
| 50 | + { |
| 51 | + // The quads overlap and all wind the same way, so under the winding rule the |
| 52 | + // filled region is their union, and Outline traces exactly that boundary. |
| 53 | + builder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Winding); |
| 54 | + |
| 55 | + foreach (var box in boxes) |
| 56 | + { |
| 57 | + var quad = box.BoundingBox.Inflate(padding); |
| 58 | + |
| 59 | + builder.BeginFigure(quad.TopLeft); |
| 60 | + builder.AddLine(quad.TopRight); |
| 61 | + builder.AddLine(quad.BottomRight); |
| 62 | + builder.AddLine(quad.BottomLeft); |
| 63 | + builder.EndFigure(CanvasFigureLoop.Closed); |
| 64 | + |
| 65 | + empty = false; |
| 66 | + } |
| 67 | + |
| 68 | + if (empty) |
| 69 | + { |
| 70 | + return new List<List<Vector2>>(); |
| 71 | + } |
| 72 | + |
| 73 | + var receiver = new PolygonReceiver(); |
54 | 74 |
|
55 | | - return solution.Select(path => path.Select(p => new Vector2((float)(p.X / 1000), (float)(p.Y / 1000))).Distinct().ToList()); |
| 75 | + using (var quads = CanvasGeometry.CreatePath(builder)) |
| 76 | + using (var outline = quads.Outline()) |
| 77 | + { |
| 78 | + outline.SendPathTo(receiver); |
| 79 | + } |
| 80 | + |
| 81 | + return receiver.Polygons; |
| 82 | + } |
56 | 83 | } |
57 | 84 |
|
58 | | - private static Path64 ConvertToClipperPath(RecognizedTextBoundingBox box) |
| 85 | + private partial class PolygonReceiver : ICanvasPathReceiver |
59 | 86 | { |
60 | | - return new Path64 |
| 87 | + public List<List<Vector2>> Polygons { get; } = new(); |
| 88 | + |
| 89 | + private List<Vector2> _figure; |
| 90 | + |
| 91 | + public void BeginFigure(Vector2 startPoint, CanvasFigureFill figureFill) |
61 | 92 | { |
62 | | - new Point64((long)(box.TopLeft.X * 1000), (long)(box.TopLeft.Y * 1000)), |
63 | | - new Point64((long)(box.TopRight.X * 1000), (long)(box.TopRight.Y * 1000)), |
64 | | - new Point64((long)(box.BottomRight.X * 1000), (long)(box.BottomRight.Y * 1000)), |
65 | | - new Point64((long)(box.BottomLeft.X * 1000), (long)(box.BottomLeft.Y * 1000)) |
66 | | - }; |
| 93 | + _figure = new List<Vector2> { startPoint }; |
| 94 | + } |
| 95 | + |
| 96 | + public void AddLine(Vector2 endPoint) |
| 97 | + { |
| 98 | + Add(endPoint); |
| 99 | + } |
| 100 | + |
| 101 | + // The outline of a polygonal path has no curves, but the interface requires these. |
| 102 | + public void AddArc(Vector2 endPoint, float radiusX, float radiusY, float rotationAngle, CanvasSweepDirection sweepDirection, CanvasArcSize arcSize) => Add(endPoint); |
| 103 | + public void AddCubicBezier(Vector2 controlPoint1, Vector2 controlPoint2, Vector2 endPoint) => Add(endPoint); |
| 104 | + public void AddQuadraticBezier(Vector2 controlPoint, Vector2 endPoint) => Add(endPoint); |
| 105 | + |
| 106 | + public void SetFilledRegionDetermination(CanvasFilledRegionDetermination filledRegionDetermination) { } |
| 107 | + public void SetSegmentOptions(CanvasFigureSegmentOptions figureSegmentOptions) { } |
| 108 | + |
| 109 | + public void EndFigure(CanvasFigureLoop figureLoop) |
| 110 | + { |
| 111 | + if (_figure.Count > 1 && IsCoincident(_figure[0], _figure[^1])) |
| 112 | + { |
| 113 | + _figure.RemoveAt(_figure.Count - 1); |
| 114 | + } |
| 115 | + |
| 116 | + if (_figure.Count > 2) |
| 117 | + { |
| 118 | + Polygons.Add(_figure); |
| 119 | + } |
| 120 | + |
| 121 | + _figure = null; |
| 122 | + } |
| 123 | + |
| 124 | + // The rounding pass normalizes (curr - prev), so a repeated vertex would give it NaN. |
| 125 | + private void Add(Vector2 point) |
| 126 | + { |
| 127 | + if (!IsCoincident(_figure[^1], point)) |
| 128 | + { |
| 129 | + _figure.Add(point); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static bool IsCoincident(Vector2 a, Vector2 b) |
| 134 | + { |
| 135 | + return Vector2.DistanceSquared(a, b) < 0.0001f; |
| 136 | + } |
67 | 137 | } |
68 | 138 |
|
69 | 139 | private static void SimplifySection(List<Vector2> points, int start, int end, float tolerance, bool[] keep) |
|
0 commit comments