Skip to content

Commit 481479d

Browse files
Merge commit from fork
* Keep the cubic subdivision test finite near float.MaxValue The direction test of the cubic subdivision scales the differences of a cubic with coordinates more than 1E18 before it normalizes them. Their squared lengths no longer overflow, so such a cubic no longer subdivides to millions of points. * Correct the comment on the cubic direction scale
1 parent 6b07a29 commit 481479d

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎src/ImageSharp.Drawing/Shapes/CubicBezierLineSegment.cs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,25 @@ private static List<PointF> FindDrawingPoints(int curveIndex, PointF[] controlPo
123123
pointList.Add(left);
124124
pointList.Add(right);
125125

126-
FindDrawingPoints(curveIndex, 0, 1, pointList, 1, controlPoints, 0);
126+
// The subdivision test normalizes the difference of two curve points. The curve stays inside the hull of
127+
// its control points, so each component of a difference is at most twice their largest coordinate. The
128+
// squared length of a difference overflows to infinity when its length is more than about 1.8E19.
129+
// Normalize then gives a zero vector, the test always subdivides, and one cubic near float.MaxValue
130+
// flattens to millions of points. A direction does not depend on the length of its vector, so the
131+
// differences of a cubic with coordinates more than 1E18 are scaled down before they are normalized.
132+
// Other cubics use a scale of one, which keeps their direction test unchanged. A difference of two
133+
// coordinates with opposite signs near float.MaxValue overflows before the scale. Its direction is NaN,
134+
// so only the root span of the cubic subdivides there.
135+
int nodeIndex = curveIndex * 3;
136+
float extent = 0;
137+
for (int i = nodeIndex; i < nodeIndex + 4; i++)
138+
{
139+
extent = MathF.Max(extent, MathF.Max(MathF.Abs(controlPoints[i].X), MathF.Abs(controlPoints[i].Y)));
140+
}
141+
142+
float directionScale = extent > 1E18F ? 1F / extent : 1F;
143+
144+
FindDrawingPoints(curveIndex, 0, 1, pointList, 1, controlPoints, directionScale, 0);
127145

128146
return pointList;
129147
}
@@ -135,6 +153,7 @@ private static int FindDrawingPoints(
135153
List<PointF> pointList,
136154
int insertionIndex,
137155
PointF[] controlPoints,
156+
float directionScale,
138157
int depth)
139158
{
140159
// max recursive depth for control points, means this is approx the max number of points discoverable
@@ -154,17 +173,17 @@ private static int FindDrawingPoints(
154173
float midT = (t0 + t1) / 2;
155174
Vector2 mid = CalculateBezierPoint(curveIndex, midT, controlPoints);
156175

157-
var leftDirection = Vector2.Normalize(left - mid);
158-
var rightDirection = Vector2.Normalize(right - mid);
176+
var leftDirection = Vector2.Normalize((left - mid) * directionScale);
177+
var rightDirection = Vector2.Normalize((right - mid) * directionScale);
159178

160179
if (Vector2.Dot(leftDirection, rightDirection) > DivisionThreshold || Math.Abs(midT - 0.5f) < 0.0001f)
161180
{
162181
int pointsAddedCount = 0;
163182

164-
pointsAddedCount += FindDrawingPoints(curveIndex, t0, midT, pointList, insertionIndex, controlPoints, depth + 1);
183+
pointsAddedCount += FindDrawingPoints(curveIndex, t0, midT, pointList, insertionIndex, controlPoints, directionScale, depth + 1);
165184
pointList.Insert(insertionIndex + pointsAddedCount, mid);
166185
pointsAddedCount++;
167-
pointsAddedCount += FindDrawingPoints(curveIndex, midT, t1, pointList, insertionIndex + pointsAddedCount, controlPoints, depth + 1);
186+
pointsAddedCount += FindDrawingPoints(curveIndex, midT, t1, pointList, insertionIndex + pointsAddedCount, controlPoints, directionScale, depth + 1);
168187

169188
return pointsAddedCount;
170189
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,15 @@ public void CheckZeroRadii()
3838
Assert.Equal(10, point.Y);
3939
}
4040
}
41+
42+
[Fact]
43+
public void NearFloatMax_HasNoMorePointsThanAtSmallScale()
44+
{
45+
// The arc is drawn as cubics whose subdivision compares directions, and directions do not depend
46+
// on the scale of the arc. The same arc near float.MaxValue cannot need more points.
47+
int small = new ArcLineSegment(new(0, 0), new(1000, 500), 15, 0, 270).Flatten().Length;
48+
int huge = new ArcLineSegment(new(0, 0), new(1E37F, 5E36F), 15, 0, 270).Flatten().Length;
49+
50+
Assert.InRange(huge, 2, small);
51+
}
4152
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,32 @@ public void MustHaveAtLeast4Points()
2222
{
2323
ArgumentOutOfRangeException error = Assert.Throws<ArgumentOutOfRangeException>(() => new CubicBezierLineSegment(new[] { new PointF(0, 0) }));
2424
}
25+
26+
[Fact]
27+
public void NearFloatMax_HasNoMorePointsThanAtSmallScale()
28+
{
29+
// The subdivision compares the directions of the two halves of a span, and directions do not depend
30+
// on the scale of the curve. A radius of 1000 keeps every span longer than the minimum span length,
31+
// so the same curve near float.MaxValue cannot need more points.
32+
int small = QuarterCircle(1000F).Flatten().Length;
33+
int huge = QuarterCircle(1E37F).Flatten().Length;
34+
35+
Assert.InRange(huge, 2, small);
36+
}
37+
38+
/// <summary>
39+
/// Creates a cubic that approximates a quarter circle around the origin.
40+
/// </summary>
41+
/// <param name="radius">The radius of the circle.</param>
42+
/// <returns>The cubic.</returns>
43+
private static CubicBezierLineSegment QuarterCircle(float radius)
44+
{
45+
float handle = radius * 0.5522847F;
46+
47+
return new CubicBezierLineSegment(
48+
new Vector2(radius, 0),
49+
new Vector2(radius, handle),
50+
new Vector2(handle, radius),
51+
new Vector2(0, radius));
52+
}
2553
}

0 commit comments

Comments
 (0)