EllipseOperation.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using ChunkyImageLib.DataHolders;
  2. using Drawie.Backend.Core.ColorsImpl;
  3. using Drawie.Backend.Core.ColorsImpl.Paintables;
  4. using Drawie.Backend.Core.Numerics;
  5. using Drawie.Backend.Core.Surfaces;
  6. using Drawie.Backend.Core.Surfaces.PaintImpl;
  7. using Drawie.Backend.Core.Vector;
  8. using Drawie.Numerics;
  9. namespace ChunkyImageLib.Operations;
  10. internal class EllipseOperation : IMirroredDrawOperation
  11. {
  12. public bool IgnoreEmptyChunks => false;
  13. private readonly RectD location;
  14. private readonly Paintable strokePaintable;
  15. private readonly Paintable fillPaintable;
  16. private readonly float strokeWidth;
  17. private readonly double rotation;
  18. private readonly Paint paint;
  19. private bool init = false;
  20. private VectorPath? outerPath;
  21. private VectorPath? innerPath;
  22. private VectorPath? ellipseOutline;
  23. private VecF[]? ellipse;
  24. private VecF[]? ellipseFill;
  25. private RectI? ellipseFillRect;
  26. private bool antialiased;
  27. public EllipseOperation(RectD location, Paintable strokePaintable, Paintable fillPaintable, float strokeWidth, double rotationRad,
  28. bool antiAliased, Paint? paint = null)
  29. {
  30. this.location = location;
  31. this.strokePaintable = strokePaintable;
  32. this.fillPaintable = fillPaintable;
  33. this.strokeWidth = strokeWidth;
  34. this.rotation = rotationRad;
  35. this.paint = paint?.Clone() ?? new Paint();
  36. antialiased = antiAliased;
  37. }
  38. private void Init()
  39. {
  40. init = true;
  41. if (strokeWidth - 1 < 0.01)
  42. {
  43. if (Math.Abs(rotation) < 0.001)
  44. {
  45. if (strokeWidth == 0)
  46. {
  47. ellipseOutline = EllipseHelper.ConstructEllipseOutline((RectI)location);
  48. }
  49. else
  50. {
  51. var ellipseList = EllipseHelper.GenerateEllipseFromRect((RectI)location);
  52. ellipse = ellipseList.Select(a => new VecF(a)).ToArray();
  53. if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
  54. {
  55. (var fill, ellipseFillRect) =
  56. EllipseHelper.SplitEllipseFillIntoRegions(ellipseList.ToList(), (RectI)location);
  57. ellipseFill = fill.Select(a => new VecF(a)).ToArray();
  58. }
  59. }
  60. }
  61. else
  62. {
  63. ellipseOutline = EllipseHelper.GenerateEllipseVectorFromRect(location);
  64. }
  65. }
  66. else
  67. {
  68. outerPath = new VectorPath();
  69. outerPath.ArcTo(location, 0, 359, true);
  70. innerPath = new VectorPath();
  71. innerPath.ArcTo(location.Inflate(-strokeWidth), 0, 359, true);
  72. }
  73. }
  74. public void DrawOnChunk(Chunk targetChunk, VecI chunkPos)
  75. {
  76. if (!init)
  77. Init();
  78. var surf = targetChunk.Surface.DrawingSurface;
  79. surf.Canvas.Save();
  80. surf.Canvas.Scale((float)targetChunk.Resolution.Multiplier());
  81. surf.Canvas.Translate(-chunkPos * ChunkyImage.FullChunkSize);
  82. paint.IsAntiAliased = antialiased || targetChunk.Resolution != ChunkResolution.Full;
  83. if (antialiased)
  84. {
  85. DrawAntiAliased(surf);
  86. }
  87. else
  88. {
  89. DrawAliased(surf);
  90. }
  91. surf.Canvas.Restore();
  92. }
  93. private void DrawAliased(DrawingSurface surf)
  94. {
  95. paint.IsAntiAliased = false;
  96. if (strokeWidth - 1 < 0.01)
  97. {
  98. if (Math.Abs(rotation) < 0.001 && strokeWidth > 0)
  99. {
  100. if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
  101. {
  102. paint.SetPaintable(fillPaintable);
  103. surf.Canvas.DrawPoints(PointMode.Lines, ellipseFill!, paint);
  104. surf.Canvas.DrawRect((RectD)ellipseFillRect!.Value, paint);
  105. }
  106. paint.SetPaintable(strokeWidth <= 0 ? fillPaintable : strokePaintable);
  107. paint.StrokeWidth = 1f;
  108. surf.Canvas.DrawPoints(PointMode.Points, ellipse!, paint);
  109. }
  110. else
  111. {
  112. surf.Canvas.Save();
  113. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  114. if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
  115. {
  116. paint.SetPaintable(fillPaintable);
  117. paint.Style = PaintStyle.Fill;
  118. surf.Canvas.DrawPath(ellipseOutline!, paint);
  119. }
  120. if (strokeWidth > 0)
  121. {
  122. paint.SetPaintable(strokePaintable);
  123. paint.Style = PaintStyle.Stroke;
  124. paint.StrokeWidth = 1;
  125. surf.Canvas.DrawPath(ellipseOutline!, paint);
  126. }
  127. surf.Canvas.Restore();
  128. }
  129. }
  130. else
  131. {
  132. if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
  133. {
  134. surf.Canvas.Save();
  135. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  136. surf.Canvas.ClipPath(innerPath!);
  137. surf.Canvas.DrawPaintable(fillPaintable, paint.BlendMode);
  138. surf.Canvas.Restore();
  139. }
  140. surf.Canvas.Save();
  141. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  142. surf.Canvas.ClipPath(outerPath!);
  143. surf.Canvas.ClipPath(innerPath!, ClipOperation.Difference);
  144. surf.Canvas.DrawPaintable(strokePaintable, paint.BlendMode);
  145. surf.Canvas.Restore();
  146. }
  147. }
  148. private void DrawAntiAliased(DrawingSurface surf)
  149. {
  150. surf.Canvas.Save();
  151. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  152. paint.IsAntiAliased = false;
  153. paint.SetPaintable(fillPaintable);
  154. paint.Style = PaintStyle.Fill;
  155. RectD fillRect = ((RectD)location).Inflate(-strokeWidth / 2f);
  156. surf.Canvas.DrawOval(fillRect.Center, fillRect.Size / 2f, paint);
  157. paint.IsAntiAliased = true;
  158. paint.SetPaintable(strokeWidth <= 0 ? fillPaintable : strokePaintable);
  159. paint.Style = PaintStyle.Stroke;
  160. paint.StrokeWidth = strokeWidth <= 0 ? 1f : strokeWidth;
  161. RectD strokeRect = ((RectD)location).Inflate((-strokeWidth / 2f));
  162. surf.Canvas.DrawOval(strokeRect.Center, strokeRect.Size / 2f, paint);
  163. surf.Canvas.Restore();
  164. }
  165. public AffectedArea FindAffectedArea(VecI imageSize)
  166. {
  167. ShapeCorners corners = new((RectD)location);
  168. corners = corners.AsRotated(rotation, (VecD)location.Center);
  169. RectI bounds = (RectI)corners.AABBBounds.RoundOutwards();
  170. var chunks = OperationHelper.FindChunksTouchingRectangle(bounds, ChunkyImage.FullChunkSize);
  171. if (!fillPaintable?.AnythingVisible ?? false)
  172. {
  173. chunks.ExceptWith(OperationHelper.FindChunksFullyInsideEllipse
  174. (location.Center, location.Width / 2.0 - strokeWidth * 2, location.Height / 2.0 - strokeWidth * 2, ChunkyImage.FullChunkSize, rotation));
  175. }
  176. return new AffectedArea(chunks, bounds);
  177. }
  178. public IDrawOperation AsMirrored(double? verAxisX, double? horAxisY)
  179. {
  180. RectD newLocation = location;
  181. if (verAxisX is not null)
  182. newLocation = newLocation.ReflectX((double)verAxisX).Round();
  183. if (horAxisY is not null)
  184. newLocation = newLocation.ReflectY((double)horAxisY).Round();
  185. return new EllipseOperation(newLocation, strokePaintable, fillPaintable, strokeWidth, rotation, antialiased, paint);
  186. }
  187. public void Dispose()
  188. {
  189. paint.Dispose();
  190. outerPath?.Dispose();
  191. innerPath?.Dispose();
  192. ellipseOutline?.Dispose();
  193. }
  194. }