EllipseOperation.cs 8.7 KB

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