|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | + |
| 6 | +namespace SixLabors.ImageSharp.Drawing.Tests.Shapes; |
| 7 | + |
| 8 | +public class LinearGeometryTests |
| 9 | +{ |
| 10 | + private static LinearGeometry CreateLPolyline() |
| 11 | + => LinearGeometry.CreateOpenPolyline( |
| 12 | + [ |
| 13 | + new PointF(0, 0), |
| 14 | + new PointF(10, 0), |
| 15 | + new PointF(10, 10), |
| 16 | + ]); |
| 17 | + |
| 18 | + private static LinearGeometry CreateClosedSquare() |
| 19 | + { |
| 20 | + PointF[] points = |
| 21 | + [ |
| 22 | + new PointF(0, 0), |
| 23 | + new PointF(10, 0), |
| 24 | + new PointF(10, 10), |
| 25 | + new PointF(0, 10), |
| 26 | + ]; |
| 27 | + RectangleF bounds = RectangleF.FromLTRB(0, 0, 10, 10); |
| 28 | + |
| 29 | + return new LinearGeometry( |
| 30 | + new LinearGeometryInfo |
| 31 | + { |
| 32 | + Bounds = bounds, |
| 33 | + ContourCount = 1, |
| 34 | + PointCount = points.Length, |
| 35 | + SegmentCount = points.Length, |
| 36 | + }, |
| 37 | + [new LinearContour |
| 38 | + { |
| 39 | + PointStart = 0, |
| 40 | + PointCount = points.Length, |
| 41 | + Bounds = bounds, |
| 42 | + SegmentStart = 0, |
| 43 | + SegmentCount = points.Length, |
| 44 | + IsClosed = true, |
| 45 | + } |
| 46 | + ], |
| 47 | + points); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void CreateOpenPolyline_PopulatesInfoAndContours() |
| 52 | + { |
| 53 | + LinearGeometry geometry = CreateLPolyline(); |
| 54 | + |
| 55 | + Assert.Equal(1, geometry.Info.ContourCount); |
| 56 | + Assert.Equal(3, geometry.Info.PointCount); |
| 57 | + Assert.Equal(2, geometry.Info.SegmentCount); |
| 58 | + Assert.Equal(RectangleF.FromLTRB(0, 0, 10, 10), geometry.Info.Bounds); |
| 59 | + |
| 60 | + LinearContour contour = Assert.Single(geometry.Contours); |
| 61 | + Assert.False(contour.IsClosed); |
| 62 | + Assert.Equal(3, geometry.Points.Count); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void CreateOpenPolyline_AppliesScale() |
| 67 | + { |
| 68 | + LinearGeometry geometry = LinearGeometry.CreateOpenPolyline( |
| 69 | + [new PointF(1, 2), new PointF(3, 4)], |
| 70 | + new Vector2(2, 10)); |
| 71 | + |
| 72 | + Assert.Equal(new PointF(2, 20), geometry.Points[0]); |
| 73 | + Assert.Equal(new PointF(6, 40), geometry.Points[1]); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void CreateOpenPolyline_FewerThanTwoPoints_Throws() |
| 78 | + => Assert.Throws<ArgumentOutOfRangeException>(() => LinearGeometry.CreateOpenPolyline([new PointF(0, 0)])); |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void GetSegments_YieldsEachSegmentInOrder() |
| 82 | + { |
| 83 | + LinearGeometry geometry = CreateLPolyline(); |
| 84 | + List<(PointF Start, PointF End)> segments = []; |
| 85 | + |
| 86 | + SegmentEnumerator enumerator = geometry.GetSegments(); |
| 87 | + while (enumerator.MoveNext()) |
| 88 | + { |
| 89 | + segments.Add((enumerator.Current.Start, enumerator.Current.End)); |
| 90 | + } |
| 91 | + |
| 92 | + Assert.Equal( |
| 93 | + [ |
| 94 | + (new PointF(0, 0), new PointF(10, 0)), |
| 95 | + (new PointF(10, 0), new PointF(10, 10)), |
| 96 | + ], |
| 97 | + segments); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void ComputeLength_SumsSegmentLengths() |
| 102 | + => Assert.Equal(20F, CreateLPolyline().ComputeLength()); |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void ComputeArea_OpenRunWithThreePoints_UsesShoelaceOfPointRun() |
| 106 | + => Assert.Equal(50F, CreateLPolyline().ComputeArea()); |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void ComputeArea_ClosedSquare_ReturnsEnclosedArea() |
| 110 | + => Assert.Equal(100F, CreateClosedSquare().ComputeArea()); |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void Contains_OpenContour_NeverContains() |
| 114 | + { |
| 115 | + LinearGeometry geometry = CreateLPolyline(); |
| 116 | + |
| 117 | + Assert.False(geometry.Contains(new PointF(9, 1), IntersectionRule.NonZero)); |
| 118 | + Assert.False(geometry.Contains(new PointF(9, 1), IntersectionRule.EvenOdd)); |
| 119 | + } |
| 120 | + |
| 121 | + [Theory] |
| 122 | + [InlineData(5, 5, true)] |
| 123 | + [InlineData(0, 0, true)] |
| 124 | + [InlineData(15, 5, false)] |
| 125 | + [InlineData(-1, 5, false)] |
| 126 | + public void Contains_ClosedContour_UsesWinding(float x, float y, bool expected) |
| 127 | + { |
| 128 | + LinearGeometry geometry = CreateClosedSquare(); |
| 129 | + |
| 130 | + Assert.Equal(expected, geometry.Contains(new PointF(x, y), IntersectionRule.NonZero)); |
| 131 | + Assert.Equal(expected, geometry.Contains(new PointF(x, y), IntersectionRule.EvenOdd)); |
| 132 | + } |
| 133 | + |
| 134 | + [Theory] |
| 135 | + [InlineData(0, 0, 0)] |
| 136 | + [InlineData(5, 5, 0)] |
| 137 | + [InlineData(15, 10, 5)] |
| 138 | + [InlineData(20, 10, 10)] |
| 139 | + public void TryGetPathPointAtDistance_WithinLength_ReturnsPointOnPolyline(float distance, float x, float y) |
| 140 | + { |
| 141 | + LinearGeometry geometry = CreateLPolyline(); |
| 142 | + |
| 143 | + Assert.True(geometry.TryGetPathPointAtDistance(distance, out PathPoint pathPoint)); |
| 144 | + Assert.Equal(new PointF(x, y), pathPoint.Point); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void TryGetPathPointAtDistance_BeyondOpenPolyline_ReturnsFalse() |
| 149 | + { |
| 150 | + LinearGeometry geometry = CreateLPolyline(); |
| 151 | + |
| 152 | + Assert.False(geometry.TryGetPathPointAtDistance(25, out _)); |
| 153 | + Assert.False(geometry.TryGetPathPointAtDistance(-1, out _)); |
| 154 | + Assert.False(geometry.TryGetPathPointAtDistance(float.NaN, out _)); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void TryGetPathPointAtDistanceUnbounded_ExtrapolatesAlongEndTangent() |
| 159 | + { |
| 160 | + LinearGeometry geometry = CreateLPolyline(); |
| 161 | + |
| 162 | + Assert.True(geometry.TryGetPathPointAtDistanceUnbounded(25, out PathPoint pathPoint)); |
| 163 | + Assert.Equal(new PointF(10, 15), pathPoint.Point); |
| 164 | + } |
| 165 | + |
| 166 | + [Fact] |
| 167 | + public void TryGetPathPointAtDistanceUnbounded_NegativeDistance_ExtrapolatesBeforeStart() |
| 168 | + { |
| 169 | + LinearGeometry geometry = CreateLPolyline(); |
| 170 | + |
| 171 | + Assert.True(geometry.TryGetPathPointAtDistanceUnbounded(-5, out PathPoint pathPoint)); |
| 172 | + Assert.Equal(new PointF(-5, 0), pathPoint.Point); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void TryGetSegment_ReturnsSubPathBetweenDistances() |
| 177 | + { |
| 178 | + LinearGeometry geometry = CreateLPolyline(); |
| 179 | + |
| 180 | + Assert.True(geometry.TryGetSegment(5, 15, false, out IPath segment)); |
| 181 | + Assert.Equal(RectangleF.FromLTRB(5, 0, 10, 5), segment.Bounds); |
| 182 | + } |
| 183 | +} |
0 commit comments