Skip to content

Commit 2e76417

Browse files
Stefan Nikoleistefannikolei
authored andcommitted
Quickly integrate SixLabors.PolygonClipper
1 parent 5132451 commit 2e76417

File tree

10 files changed

+83
-3547
lines changed

10 files changed

+83
-3547
lines changed

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<ItemGroup>
4848
<PackageReference Include="SixLabors.Fonts" Version="3.0.0-alpha.0.1" />
4949
<PackageReference Include="SixLabors.ImageSharp" Version="4.0.0-alpha.0.44" />
50+
<PackageReference Include="SixLabors.PolygonClipper" Version="1.0.0-alpha.0.48" />
5051
</ItemGroup>
5152
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
5253
</Project>

src/ImageSharp.Drawing/Processing/ShapeOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.PolygonClipper;
5+
46
namespace SixLabors.ImageSharp.Drawing.Processing;
57

68
/// <summary>
@@ -24,9 +26,9 @@ private ShapeOptions(ShapeOptions source)
2426
/// <summary>
2527
/// Gets or sets the clipping operation.
2628
/// <para/>
27-
/// Defaults to <see cref="ClippingOperation.Difference"/>.
29+
/// Defaults to <see cref="BooleanOperation.Difference"/>.
2830
/// </summary>
29-
public ClippingOperation ClippingOperation { get; set; } = ClippingOperation.Difference;
31+
public BooleanOperation ClippingOperation { get; set; } = BooleanOperation.Difference;
3032

3133
/// <summary>
3234
/// Gets or sets the rule for calculating intersection points.

src/ImageSharp.Drawing/Shapes/ClipPathExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static IPath Clip(
6363
clipper.AddPath(subjectPath, ClippingType.Subject);
6464
clipper.AddPaths(clipPaths, ClippingType.Clip);
6565

66-
IPath[] result = clipper.GenerateClippedShapes(options.ClippingOperation, options.IntersectionRule);
66+
IPath[] result = clipper.GenerateClippedShapes(options.ClippingOperation);
6767

6868
return new ComplexPolygon(result);
6969
}

src/ImageSharp.Drawing/Shapes/ClippingOperation.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,46 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.PolygonClipper;
5+
46
namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper;
57

68
/// <summary>
79
/// Library to clip polygons.
810
/// </summary>
911
internal class Clipper
1012
{
11-
private readonly PolygonClipper polygonClipper;
12-
13-
/// <summary>
14-
/// Initializes a new instance of the <see cref="Clipper"/> class.
15-
/// </summary>
16-
public Clipper()
17-
=> this.polygonClipper = new PolygonClipper();
13+
private SixLabors.PolygonClipper.Polygon? subject;
14+
private SixLabors.PolygonClipper.Polygon? clip;
1815

1916
/// <summary>
2017
/// Generates the clipped shapes from the previously provided paths.
2118
/// </summary>
2219
/// <param name="operation">The clipping operation.</param>
2320
/// <param name="rule">The intersection rule.</param>
2421
/// <returns>The <see cref="T:IPath[]"/>.</returns>
25-
public IPath[] GenerateClippedShapes(ClippingOperation operation, IntersectionRule rule)
22+
public IPath[] GenerateClippedShapes(BooleanOperation operation)
2623
{
27-
PathsF closedPaths = [];
28-
PathsF openPaths = [];
24+
ArgumentNullException.ThrowIfNull(this.subject);
25+
ArgumentNullException.ThrowIfNull(this.clip);
2926

30-
FillRule fillRule = rule == IntersectionRule.EvenOdd ? FillRule.EvenOdd : FillRule.NonZero;
31-
this.polygonClipper.Execute(operation, fillRule, closedPaths, openPaths);
27+
SixLabors.PolygonClipper.PolygonClipper polygonClipper = new(this.subject, this.clip, operation);
3228

33-
IPath[] shapes = new IPath[closedPaths.Count + openPaths.Count];
29+
SixLabors.PolygonClipper.Polygon result = polygonClipper.Run();
3430

35-
int index = 0;
36-
for (int i = 0; i < closedPaths.Count; i++)
37-
{
38-
PathF path = closedPaths[i];
39-
PointF[] points = new PointF[path.Count];
4031

41-
for (int j = 0; j < path.Count; j++)
42-
{
43-
points[j] = path[j];
44-
}
32+
IPath[] shapes = new IPath[result.Count];
4533

46-
shapes[index++] = new Polygon(points);
47-
}
48-
49-
for (int i = 0; i < openPaths.Count; i++)
34+
int index = 0;
35+
for (int i = 0; i < result.Count; i++)
5036
{
51-
PathF path = openPaths[i];
52-
PointF[] points = new PointF[path.Count];
37+
Contour contour = result[i];
38+
PointF[] points = new PointF[contour.Count];
5339

54-
for (int j = 0; j < path.Count; j++)
40+
for (int j = 0; j < contour.Count; j++)
5541
{
56-
points[j] = path[j];
42+
Vertex vertex = contour[j];
43+
points[j] = new PointF((float)vertex.X, (float)vertex.Y);
5744
}
5845

5946
shapes[index++] = new Polygon(points);
@@ -100,12 +87,25 @@ public void AddPath(IPath path, ClippingType clippingType)
10087
internal void AddPath(ISimplePath path, ClippingType clippingType)
10188
{
10289
ReadOnlySpan<PointF> vectors = path.Points.Span;
103-
PathF points = new(vectors.Length);
104-
for (int i = 0; i < vectors.Length; i++)
90+
SixLabors.PolygonClipper.Polygon polygon = [];
91+
Contour contour = new();
92+
polygon.Add(contour);
93+
94+
foreach (PointF point in vectors)
10595
{
106-
points.Add(vectors[i]);
96+
contour.AddVertex(new Vertex(point.X, point.Y));
10797
}
10898

109-
this.polygonClipper.AddPath(points, clippingType, !path.IsClosed);
99+
switch (clippingType)
100+
{
101+
case ClippingType.Clip:
102+
this.clip = polygon;
103+
break;
104+
case ClippingType.Subject:
105+
this.subject = polygon;
106+
break;
107+
default:
108+
throw new ArgumentOutOfRangeException(nameof(clippingType), clippingType, null);
109+
}
110110
}
111111
}

0 commit comments

Comments
 (0)