Browse Source

Added ellipse cache

flabbet 7 months ago
parent
commit
6170d9fd1c

+ 10 - 0
src/ChunkyImageLib/Operations/EllipseCache.cs

@@ -0,0 +1,10 @@
+using System.Collections.Concurrent;
+using Drawie.Backend.Core.Vector;
+using Drawie.Numerics;
+
+namespace ChunkyImageLib.Operations;
+
+public static class EllipseCache
+{
+    public static readonly ConcurrentDictionary<VecI, VectorPath> Ellipses = new();
+}

+ 15 - 3
src/ChunkyImageLib/Operations/EllipseHelper.cs

@@ -193,6 +193,14 @@ public class EllipseHelper
     /// <returns>A vector path that represents an ellipse outline.</returns>
     /// <returns>A vector path that represents an ellipse outline.</returns>
     public static VectorPath ConstructEllipseOutline(RectI rectangle)
     public static VectorPath ConstructEllipseOutline(RectI rectangle)
     {
     {
+        if (EllipseCache.Ellipses.TryGetValue(rectangle.Size, out var cachedPath))
+        {
+            VectorPath finalPath = new(cachedPath);
+            finalPath.Transform(Matrix3X3.CreateTranslation(rectangle.TopLeft.X, rectangle.TopLeft.Y));
+            
+            return finalPath;
+        }
+        
         if (rectangle.Width < 3 || rectangle.Height < 3)
         if (rectangle.Width < 3 || rectangle.Height < 3)
         {
         {
             VectorPath rectPath = new();
             VectorPath rectPath = new();
@@ -206,8 +214,9 @@ public class EllipseHelper
             return CreateThreePixelCircle((VecI)rectangle.Center);
             return CreateThreePixelCircle((VecI)rectangle.Center);
         }
         }
 
 
-        var center = rectangle.Center;
-        var points = GenerateEllipseFromRect(rectangle, 0).ToList();
+        var center = rectangle.Size / 2d;
+        RectI rect = new RectI(0, 0, rectangle.Width, rectangle.Height);
+        var points = GenerateEllipseFromRect(rect, 0).ToList();
         points.Sort((vec, vec2) => Math.Sign((vec - center).Angle - (vec2 - center).Angle));
         points.Sort((vec, vec2) => Math.Sign((vec - center).Angle - (vec2 - center).Angle));
         List<VecI> finalPoints = new();
         List<VecI> finalPoints = new();
         for (int i = 0; i < points.Count; i++)
         for (int i = 0; i < points.Count; i++)
@@ -268,7 +277,10 @@ public class EllipseHelper
         }
         }
 
 
         path.Close();
         path.Close();
-
+        
+        EllipseCache.Ellipses[rectangle.Size] = new VectorPath(path);
+        
+        path.Transform(Matrix3X3.CreateTranslation(rectangle.TopLeft.X, rectangle.TopLeft.Y));
         return path;
         return path;
     }
     }
 
 

+ 1 - 0
src/ChunkyImageLib/Operations/EllipseOperation.cs

@@ -218,5 +218,6 @@ internal class EllipseOperation : IMirroredDrawOperation
         paint.Dispose();
         paint.Dispose();
         outerPath?.Dispose();
         outerPath?.Dispose();
         innerPath?.Dispose();
         innerPath?.Dispose();
+        ellipseOutline?.Dispose();
     }
     }
 }
 }