Skip to content

Commit 7f9098b

Browse files
Merge commit from fork
The flatness tolerance of each cubic never goes below the single-precision rounding error of its flatness test, and a span whose deviation is NaN is accepted. Curves far from the origin, and curves with a control point that is not finite, no longer subdivide to the depth limit.
1 parent 94ff3a8 commit 7f9098b

3 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎src/ImageSharp.Drawing/CubicBezierLineSegment.cs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,18 @@ private static PointF[] FlattenCurve(PointF[] controlPoints, Vector2 scale)
173173
output.Add((PointF)p0);
174174
}
175175

176-
SubdivideAndAppend(0F, 1F, p0, p3, p0, p1, p2, p3, ref output, 0);
176+
// The flatness test compares the curve midpoint with the chord midpoint. Both are sums of products of
177+
// the control points, so in single precision each carries a rounding error of a few units in the last
178+
// place of the largest coordinate m (one unit is at most m * 2^-23). The measured deviation can then be
179+
// wrong by about m * 2^-21, which is more than the constant tolerance when m is more than 2^15. With a
180+
// tolerance less than this error, flat spans fail the test at almost every depth and the subdivision runs
181+
// to its depth limit, so the tolerance never goes below the error. Up to m = 32768 the constant tolerance
182+
// applies unchanged. When the squared error overflows to infinity, every span after the root is accepted.
183+
Vector2 extent = Vector2.Max(Vector2.Max(Vector2.Abs(p0), Vector2.Abs(p1)), Vector2.Max(Vector2.Abs(p2), Vector2.Abs(p3)));
184+
float roundingError = MathF.Max(extent.X, extent.Y) * (1F / (1 << 21));
185+
float flatnessSqrTolerance = MathF.Max(FlatnessSqrTolerance, roundingError * roundingError);
186+
187+
SubdivideAndAppend(0F, 1F, p0, p3, p0, p1, p2, p3, flatnessSqrTolerance, ref output, 0);
177188
output.Add((PointF)p3);
178189
}
179190

@@ -191,6 +202,7 @@ private static PointF[] FlattenCurve(PointF[] controlPoints, Vector2 scale)
191202
/// <param name="p1">The first control point of the cubic.</param>
192203
/// <param name="p2">The second control point of the cubic.</param>
193204
/// <param name="p3">The end point of the cubic.</param>
205+
/// <param name="flatnessSqrTolerance">The squared midpoint-to-chord error accepted for this cubic.</param>
194206
/// <param name="output">The builder receiving the appended points.</param>
195207
/// <param name="depth">The current recursion depth; used to bound the subdivision.</param>
196208
private static void SubdivideAndAppend(
@@ -202,6 +214,7 @@ private static void SubdivideAndAppend(
202214
Vector2 p1,
203215
Vector2 p2,
204216
Vector2 p3,
217+
float flatnessSqrTolerance,
205218
ref FlattenedPointBuilder output,
206219
int depth)
207220
{
@@ -218,15 +231,17 @@ private static void SubdivideAndAppend(
218231
// Compare the curve midpoint with the midpoint of the candidate line. An S-shaped cubic
219232
// can cross its chord at the root midpoint while still bending on both sides. Subdivide the
220233
// root once before trusting this test so the two bends are tested separately.
234+
// A point that is not finite gives a NaN deviation or tolerance. Such a span cannot be placed,
235+
// so the negated comparison accepts it instead of subdividing it to the depth limit.
221236
Vector2 chordMid = (left + right) * 0.5F;
222-
if (depth > 0 && (mid - chordMid).LengthSquared() <= FlatnessSqrTolerance)
237+
if (depth > 0 && !((mid - chordMid).LengthSquared() > flatnessSqrTolerance))
223238
{
224239
return;
225240
}
226241

227-
SubdivideAndAppend(t0, midT, left, mid, p0, p1, p2, p3, ref output, depth + 1);
242+
SubdivideAndAppend(t0, midT, left, mid, p0, p1, p2, p3, flatnessSqrTolerance, ref output, depth + 1);
228243
output.Add((PointF)mid);
229-
SubdivideAndAppend(midT, t1, mid, right, p0, p1, p2, p3, ref output, depth + 1);
244+
SubdivideAndAppend(midT, t1, mid, right, p0, p1, p2, p3, flatnessSqrTolerance, ref output, depth + 1);
230245
}
231246

232247
/// <summary>

‎tests/ImageSharp.Drawing.Tests/Shapes/ArcLineSegmentTest.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public void CheckZeroRadii()
4141
}
4242
}
4343

44+
[Theory]
45+
[InlineData(100000F)]
46+
[InlineData(10000000F)]
47+
public void FarFromOrigin_HasNoMorePointsThanNearOrigin(float offset)
48+
{
49+
// Far from the origin, single precision places points less exactly than near the origin,
50+
// so the same arc cannot need more points there.
51+
ArcLineSegment near = new(new PointF(0, 0), new SizeF(100, 50), 15, 0, 270);
52+
ArcLineSegment far = new(new PointF(offset, offset), new SizeF(100, 50), 15, 0, 270);
53+
54+
Assert.InRange(far.LinearVertexCount(Vector2.One), 2, near.LinearVertexCount(Vector2.One));
55+
}
56+
4457
private static PointF[] Flatten(ArcLineSegment segment)
4558
{
4659
PointF[] points = new PointF[segment.LinearVertexCount(Vector2.One)];

‎tests/ImageSharp.Drawing.Tests/Shapes/BezierLineSegmentTests.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,46 @@ public void MustHaveAtLeast4Points()
2424
ArgumentOutOfRangeException error = Assert.Throws<ArgumentOutOfRangeException>(() => new CubicBezierLineSegment(
2525
[new PointF(0, 0)]));
2626
}
27+
28+
[Theory]
29+
[InlineData(100000F)]
30+
[InlineData(10000000F)]
31+
public void FarFromOrigin_HasNoMorePointsThanNearOrigin(float offset)
32+
{
33+
// Far from the origin, single precision places points less exactly than near the origin,
34+
// so the same curve cannot need more points there.
35+
CubicBezierLineSegment near = QuarterCircle(Vector2.Zero);
36+
CubicBezierLineSegment far = QuarterCircle(new Vector2(offset));
37+
38+
Assert.InRange(far.LinearVertexCount(Vector2.One), 2, near.LinearVertexCount(Vector2.One));
39+
}
40+
41+
[Theory]
42+
[InlineData(float.PositiveInfinity)]
43+
[InlineData(float.NaN)]
44+
public void ControlPointNotFinite_KeepsEndPointsAndRootMidpoint(float value)
45+
{
46+
// The root span is always split once. The deviation of a span with a point that is not finite
47+
// cannot be measured, so each half is accepted as it is.
48+
CubicBezierLineSegment segment = new(new Vector2(0, 0), new Vector2(value, 0), new Vector2(10, 10), new Vector2(20, 0));
49+
50+
Assert.Equal(3, segment.LinearVertexCount(Vector2.One));
51+
}
52+
53+
/// <summary>
54+
/// Creates a cubic that approximates a quarter circle of radius 100 around <paramref name="center"/>.
55+
/// </summary>
56+
/// <param name="center">The center of the circle.</param>
57+
/// <returns>The cubic.</returns>
58+
private static CubicBezierLineSegment QuarterCircle(Vector2 center)
59+
{
60+
const float radius = 100F;
61+
const float handle = radius * 0.5522847F;
62+
63+
return new CubicBezierLineSegment(
64+
center + new Vector2(radius, 0),
65+
center + new Vector2(radius, handle),
66+
center + new Vector2(handle, radius),
67+
center + new Vector2(0, radius));
68+
}
2769
}

0 commit comments

Comments
 (0)