Skip to content

Commit 1d0a834

Browse files
Remove intersections methods
1 parent a83e103 commit 1d0a834

18 files changed

Lines changed: 20 additions & 1033 deletions

src/ImageSharp.Drawing/Processing/PathGradientBrush.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public Edge(Path path, Color startColor, Color endColor)
167167
Span<PointF> intersections,
168168
Span<PointOrientation> orientations)
169169
{
170-
int pathIntersections = ((IPathInternals)this.Path).FindIntersections(
170+
int pathIntersections = this.Path.FindIntersections(
171171
start,
172172
end,
173173
intersections.Slice(0, this.Path.MaxIntersections),

src/ImageSharp.Drawing/Shapes/ComplexPolygon.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Numerics;
9-
using SixLabors.ImageSharp.Drawing.Utilities;
109

1110
namespace SixLabors.ImageSharp.Drawing
1211
{
@@ -173,61 +172,6 @@ public IPath AsClosedPath()
173172
return new ComplexPolygon(paths);
174173
}
175174

176-
/// <inheritdoc />
177-
int IPathInternals.FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
178-
=> ((IPathInternals)this).FindIntersections(start, end, intersections, orientations, IntersectionRule.OddEven);
179-
180-
/// <inheritdoc />
181-
int IPathInternals.FindIntersections(
182-
PointF start,
183-
PointF end,
184-
Span<PointF> intersections,
185-
Span<PointOrientation> orientations,
186-
IntersectionRule intersectionRule)
187-
{
188-
int totalAdded = 0;
189-
foreach (InternalPath ip in this.internalPaths)
190-
{
191-
Span<PointF> subBuffer = intersections.Slice(totalAdded);
192-
Span<PointOrientation> subOrientationsSpan = orientations.Slice(totalAdded);
193-
194-
int position = ip.FindIntersectionsWithOrientation(start, end, subBuffer, subOrientationsSpan);
195-
totalAdded += position;
196-
}
197-
198-
// Avoid pool overhead for short runs.
199-
// This method can be called in high volume.
200-
const int maxStackSize = 1024 / sizeof(float);
201-
float[] rentedFromPool = null;
202-
Span<float> buffer =
203-
totalAdded > maxStackSize
204-
? (rentedFromPool = ArrayPool<float>.Shared.Rent(totalAdded))
205-
: stackalloc float[maxStackSize];
206-
207-
Span<float> distances = buffer.Slice(0, totalAdded);
208-
209-
for (int i = 0; i < totalAdded; i++)
210-
{
211-
distances[i] = Vector2.DistanceSquared(start, intersections[i]);
212-
}
213-
214-
Span<PointF> activeIntersections = intersections.Slice(0, totalAdded);
215-
Span<PointOrientation> activeOrientations = orientations.Slice(0, totalAdded);
216-
SortUtility.Sort(distances, activeIntersections, activeOrientations);
217-
218-
if (intersectionRule == IntersectionRule.Nonzero)
219-
{
220-
totalAdded = InternalPath.ApplyNonZeroIntersectionRules(activeIntersections, activeOrientations);
221-
}
222-
223-
if (rentedFromPool != null)
224-
{
225-
ArrayPool<float>.Shared.Return(rentedFromPool);
226-
}
227-
228-
return totalAdded;
229-
}
230-
231175
/// <inheritdoc/>
232176
SegmentInfo IPathInternals.PointAlongPath(float distance)
233177
{

src/ImageSharp.Drawing/Shapes/EllipsePolygon.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,6 @@ public IEnumerable<ISimplePath> Flatten()
9393
yield return this;
9494
}
9595

96-
/// <inheritdoc/>
97-
int IPathInternals.FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
98-
=> this.innerPath.FindIntersections(start, end, intersections, orientations, IntersectionRule.OddEven);
99-
100-
/// <inheritdoc/>
101-
int IPathInternals.FindIntersections(
102-
PointF start,
103-
PointF end,
104-
Span<PointF> intersections,
105-
Span<PointOrientation> orientations,
106-
IntersectionRule intersectionRule)
107-
=> this.innerPath.FindIntersections(start, end, intersections, orientations, intersectionRule);
108-
10996
/// <inheritdoc/>
11097
public bool Contains(PointF point) => this.innerPath.PointInPolygon(point);
11198

src/ImageSharp.Drawing/Shapes/IPathInternals.cs

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

4-
using System;
5-
64
namespace SixLabors.ImageSharp.Drawing
75
{
86
/// <summary>
@@ -15,44 +13,6 @@ internal interface IPathInternals : IPath
1513
/// </summary>
1614
int MaxIntersections { get; }
1715

18-
/// <summary>
19-
/// Based on a line described by <paramref name="start"/> and <paramref name="end"/>
20-
/// populate a buffer for all points on the polygon that the line intersects.
21-
/// </summary>
22-
/// <param name="start">The start position.</param>
23-
/// <param name="end">The end position.</param>
24-
/// <param name="intersections">The buffer for storing each intersection.</param>
25-
/// <param name="orientations">
26-
/// The buffer for storing the orientation of each intersection.
27-
/// Must be the same length as <paramref name="intersections"/>.
28-
/// </param>
29-
/// <returns>
30-
/// The number of intersections found.
31-
/// </returns>
32-
int FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations);
33-
34-
/// <summary>
35-
/// Based on a line described by <paramref name="start"/> and <paramref name="end"/>
36-
/// populate a buffer for all points on the polygon that the line intersects.
37-
/// </summary>
38-
/// <param name="start">The start position.</param>
39-
/// <param name="end">The end position.</param>
40-
/// <param name="intersections">The buffer for storing each intersection.</param>
41-
/// <param name="orientations">
42-
/// The buffer for storing the orientation of each intersection.
43-
/// Must be the same length as <paramref name="intersections"/>.
44-
/// </param>
45-
/// <param name="intersectionRule">How intersections should be handled.</param>
46-
/// <returns>
47-
/// The number of intersections found.
48-
/// </returns>
49-
int FindIntersections(
50-
PointF start,
51-
PointF end,
52-
Span<PointF> intersections,
53-
Span<PointOrientation> orientations,
54-
IntersectionRule intersectionRule);
55-
5616
/// <summary>
5717
/// Returns information about a point at a given distance along a path.
5818
/// </summary>

src/ImageSharp.Drawing/Shapes/InternalPath.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ public bool PointInPolygon(PointF point)
469469
/// <returns>
470470
/// Returns details about a point along a path.
471471
/// </returns>
472+
/// <exception cref="InvalidOperationException">Thrown if no points found.</exception>
472473
internal SegmentInfo PointAlongPath(float distanceAlongPath)
473474
{
474475
distanceAlongPath %= this.Length;
@@ -494,10 +495,8 @@ internal SegmentInfo PointAlongPath(float distanceAlongPath)
494495
Angle = (float)(Math.Atan2(diff.Y, diff.X) % (Math.PI * 2))
495496
};
496497
}
497-
else
498-
{
499-
distanceAlongPath -= this.points[next].Length;
500-
}
498+
499+
distanceAlongPath -= this.points[next].Length;
501500
}
502501

503502
// TODO: Perf - Throwhelper.

src/ImageSharp.Drawing/Shapes/Path.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,23 @@ public IEnumerable<ISimplePath> Flatten()
111111
/// <inheritdoc />
112112
public bool Contains(PointF point) => this.InnerPath.PointInPolygon(point);
113113

114-
/// <inheritdoc />
115-
int IPathInternals.FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
114+
/// <summary>
115+
/// Based on a line described by <paramref name="start"/> and <paramref name="end"/>
116+
/// populate a buffer for all points on the polygon that the line intersects.
117+
/// </summary>
118+
/// <param name="start">The start position.</param>
119+
/// <param name="end">The end position.</param>
120+
/// <param name="intersections">The buffer for storing each intersection.</param>
121+
/// <param name="orientations">
122+
/// The buffer for storing the orientation of each intersection.
123+
/// Must be the same length as <paramref name="intersections"/>.
124+
/// </param>
125+
/// <returns>
126+
/// The number of intersections found.
127+
/// </returns>
128+
internal int FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
116129
=> this.InnerPath.FindIntersections(start, end, intersections, orientations);
117130

118-
/// <inheritdoc />
119-
int IPathInternals.FindIntersections(
120-
PointF start,
121-
PointF end,
122-
Span<PointF> intersections,
123-
Span<PointOrientation> orientations,
124-
IntersectionRule intersectionRule)
125-
=> this.InnerPath.FindIntersections(start, end, intersections, orientations, intersectionRule);
126-
127131
/// <inheritdoc/>
128132
SegmentInfo IPathInternals.PointAlongPath(float distance)
129133
=> this.InnerPath.PointAlongPath(distance);

src/ImageSharp.Drawing/Shapes/RectangularPolygon.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -164,42 +164,6 @@ public static explicit operator RectangularPolygon(Polygon polygon)
164164
public bool Contains(PointF point)
165165
=> Vector2.Clamp(point, this.topLeft, this.bottomRight) == (Vector2)point;
166166

167-
/// <inheritdoc />
168-
int IPathInternals.FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
169-
=> ((IPathInternals)this).FindIntersections(start, end, intersections, orientations, IntersectionRule.OddEven);
170-
171-
/// <inheritdoc />
172-
int IPathInternals.FindIntersections(
173-
PointF start,
174-
PointF end,
175-
Span<PointF> intersections,
176-
Span<PointOrientation> orientations,
177-
IntersectionRule intersectionRule)
178-
{
179-
int offset = 0;
180-
int discovered = 0;
181-
var startPoint = Vector2.Clamp(start, this.topLeft, this.bottomRight);
182-
var endPoint = Vector2.Clamp(end, this.topLeft, this.bottomRight);
183-
184-
// Start doesn't change when its inside the shape thus not crossing
185-
if (startPoint != (Vector2)start && startPoint == Vector2.Clamp(startPoint, start, end))
186-
{
187-
// If start closest is within line then its a valid point
188-
discovered++;
189-
intersections[offset++] = startPoint;
190-
}
191-
192-
// End didn't change it must not intercept with an edge
193-
if (endPoint != (Vector2)end && endPoint == Vector2.Clamp(endPoint, start, end))
194-
{
195-
// If start closest is within line then its a valid point
196-
discovered++;
197-
intersections[offset] = endPoint;
198-
}
199-
200-
return discovered;
201-
}
202-
203167
/// <inheritdoc/>
204168
public IPath Transform(Matrix3x2 matrix)
205169
{

tests/ImageSharp.Drawing.Tests/Drawing/Paths/ShapeRegionTests.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public abstract class MockPath : IPath, IPathInternals
1717

1818
public IPath AsClosedPath() => this;
1919

20-
public abstract SegmentInfo PointAlongPath(float distanceAlongPath);
20+
public abstract SegmentInfo PointAlongPath(float distance);
2121

2222
public abstract IEnumerable<ISimplePath> Flatten();
2323

@@ -29,27 +29,6 @@ public abstract class MockPath : IPath, IPathInternals
2929

3030
public abstract int MaxIntersections { get; }
3131

32-
int IPathInternals.FindIntersections(PointF start, PointF end, Span<PointF> intersections, Span<PointOrientation> orientations)
33-
=> ((IPathInternals)this).FindIntersections(start, end, intersections, orientations, IntersectionRule.OddEven);
34-
35-
int IPathInternals.FindIntersections(
36-
PointF start,
37-
PointF end,
38-
Span<PointF>
39-
intersections,
40-
Span<PointOrientation> orientations,
41-
IntersectionRule intersectionRule)
42-
{
43-
Assert.Equal(this.TestYToScan, start.Y);
44-
Assert.Equal(this.TestYToScan, end.Y);
45-
Assert.True(start.X < this.Bounds.Left);
46-
Assert.True(end.X > this.Bounds.Right);
47-
48-
this.TestFindIntersectionsInvocationCounter++;
49-
50-
return this.TestFindIntersectionsResult;
51-
}
52-
5332
public int TestFindIntersectionsInvocationCounter { get; private set; }
5433

5534
public virtual int TestYToScan => 10;

tests/ImageSharp.Drawing.Tests/Issues/Issues_48.cs

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

0 commit comments

Comments
 (0)