Browse Source

Improve affected chunk finding for quadrilaterals

Equbuxu 3 years ago
parent
commit
79d144b24b

+ 4 - 0
src/ChunkyImageLib/Operations/OperationHelper.cs

@@ -102,6 +102,8 @@ public static class OperationHelper
 
     public static HashSet<VecI> FindChunksTouchingQuadrilateral(ShapeCorners corners, int chunkSize)
     {
+        if (corners.IsRect && Math.Abs(corners.RectRotation) < 0.0001)
+            return FindChunksTouchingRectangle((RectI)RectD.FromCenterAndSize(corners.RectCenter, corners.RectSize).RoundOutwards(), chunkSize);
         if (corners.HasNaNOrInfinity ||
             (corners.BottomLeft - corners.TopRight).Length > chunkSize * 40 * 20 ||
             (corners.TopLeft - corners.BottomRight).Length > chunkSize * 40 * 20)
@@ -119,6 +121,8 @@ public static class OperationHelper
 
     public static HashSet<VecI> FindChunksFullyInsideQuadrilateral(ShapeCorners corners, int chunkSize)
     {
+        if (corners.IsRect && Math.Abs(corners.RectRotation) < 0.0001)
+            return FindChunksFullyInsideRectangle((RectI)RectD.FromCenterAndSize(corners.RectCenter, corners.RectSize).RoundOutwards(), chunkSize);
         if (corners.HasNaNOrInfinity ||
             (corners.BottomLeft - corners.TopRight).Length > chunkSize * 40 * 20 ||
             (corners.TopLeft - corners.BottomRight).Length > chunkSize * 40 * 20)

+ 18 - 0
src/ChunkyImageLibTest/ImageOperationTests.cs

@@ -0,0 +1,18 @@
+using System.Collections.Generic;
+using ChunkyImageLib;
+using ChunkyImageLib.DataHolders;
+using ChunkyImageLib.Operations;
+using Xunit;
+
+namespace ChunkyImageLibTest;
+public class ImageOperationTests
+{
+    [Fact]
+    public void FindAffectedChunks_SingleChunk_ReturnsSingleChunk()
+    {
+        using Surface testImage = new Surface((ChunkyImage.FullChunkSize, ChunkyImage.FullChunkSize));
+        using ImageOperation operation = new((ChunkyImage.FullChunkSize, ChunkyImage.FullChunkSize), testImage);
+        var chunks = operation.FindAffectedChunks();
+        Assert.Equal(new HashSet<VecI>() { new(1, 1) }, chunks);
+    }
+}