EditableVectorPathTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using Drawie.Backend.Core.Bridge;
  2. using Drawie.Backend.Core.Vector;
  3. using Drawie.Numerics;
  4. using Drawie.Skia;
  5. using DrawiEngine;
  6. using PixiEditor.Views.Overlays.PathOverlay;
  7. namespace PixiEditor.Tests;
  8. public class EditableVectorPathTests
  9. {
  10. public EditableVectorPathTests()
  11. {
  12. if (DrawingBackendApi.HasBackend)
  13. {
  14. return;
  15. }
  16. SkiaDrawingBackend skiaDrawingBackend = new SkiaDrawingBackend();
  17. DrawingBackendApi.SetupBackend(skiaDrawingBackend, new DrawieRenderingDispatcher());
  18. }
  19. [Fact]
  20. public void TestThatRectVectorShapeReturnsCorrectSubShapes()
  21. {
  22. VectorPath path = new VectorPath();
  23. path.AddRect(new RectD(0, 0, 10, 10));
  24. EditableVectorPath editablePath = new EditableVectorPath(path);
  25. Assert.Single(editablePath.SubShapes);
  26. Assert.True(editablePath.SubShapes[0].IsClosed);
  27. Assert.Equal(4, editablePath.SubShapes[0].Points.Count);
  28. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Position);
  29. Assert.Equal(new VecF(10, 0), editablePath.SubShapes[0].Points[1].Position);
  30. Assert.Equal(new VecF(10, 10), editablePath.SubShapes[0].Points[2].Position);
  31. Assert.Equal(new VecF(0, 10), editablePath.SubShapes[0].Points[3].Position);
  32. }
  33. [Fact]
  34. public void TestThatOvalVectorShapeReturnsCorrectSubShapes()
  35. {
  36. VectorPath path = new VectorPath();
  37. path.AddOval(RectD.FromCenterAndSize(new VecD(5, 5), new VecD(10, 10)));
  38. EditableVectorPath editablePath = new EditableVectorPath(path);
  39. Assert.Single(editablePath.SubShapes);
  40. Assert.True(editablePath.SubShapes[0].IsClosed);
  41. Assert.Equal(4, editablePath.SubShapes[0].Points.Count);
  42. Assert.Equal(new VecF(10, 5), editablePath.SubShapes[0].Points[0].Position);
  43. Assert.Equal(new VecF(5, 10), editablePath.SubShapes[0].Points[1].Position);
  44. Assert.Equal(new VecF(0, 5), editablePath.SubShapes[0].Points[2].Position);
  45. Assert.Equal(new VecF(5, 0), editablePath.SubShapes[0].Points[3].Position);
  46. }
  47. [Fact]
  48. public void TestThatLineVectorShapeReturnsCorrectSubShapes()
  49. {
  50. VectorPath path = new VectorPath();
  51. path.LineTo(new VecF(2, 2));
  52. path.Close();
  53. EditableVectorPath editablePath = new EditableVectorPath(path);
  54. Assert.Single(editablePath.SubShapes);
  55. Assert.True(editablePath.SubShapes[0].IsClosed);
  56. Assert.Equal(2, editablePath.SubShapes[0].Points.Count);
  57. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Position);
  58. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[1].Position);
  59. }
  60. [Fact]
  61. public void TestThatNotClosedPolyReturnsCorrectSubShape()
  62. {
  63. VectorPath path = new VectorPath();
  64. path.MoveTo(new VecF(0, 0));
  65. path.LineTo(new VecF(2, 2));
  66. path.LineTo(new VecF(4, 4));
  67. EditableVectorPath editablePath = new EditableVectorPath(path);
  68. Assert.Single(editablePath.SubShapes);
  69. Assert.False(editablePath.SubShapes[0].IsClosed);
  70. Assert.Equal(3, editablePath.SubShapes[0].Points.Count);
  71. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Position);
  72. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[1].Position);
  73. Assert.Equal(new VecF(4, 4), editablePath.SubShapes[0].Points[2].Position);
  74. }
  75. [Fact]
  76. public void TestThatMultipleRectsReturnCorrectSubShapes()
  77. {
  78. VectorPath path = new VectorPath();
  79. path.AddRect(new RectD(0, 0, 10, 10));
  80. path.AddRect(new RectD(10, 10, 20, 20));
  81. EditableVectorPath editablePath = new EditableVectorPath(path);
  82. Assert.Equal(2, editablePath.SubShapes.Count);
  83. Assert.True(editablePath.SubShapes[0].IsClosed);
  84. Assert.Equal(4, editablePath.SubShapes[0].Points.Count);
  85. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Position);
  86. Assert.Equal(new VecF(10, 0), editablePath.SubShapes[0].Points[1].Position);
  87. Assert.Equal(new VecF(10, 10), editablePath.SubShapes[0].Points[2].Position);
  88. Assert.Equal(new VecF(0, 10), editablePath.SubShapes[0].Points[3].Position);
  89. Assert.True(editablePath.SubShapes[1].IsClosed);
  90. Assert.Equal(4, editablePath.SubShapes[1].Points.Count);
  91. Assert.Equal(new VecF(10, 10), editablePath.SubShapes[1].Points[0].Position);
  92. Assert.Equal(new VecF(30, 10), editablePath.SubShapes[1].Points[1].Position);
  93. Assert.Equal(new VecF(30, 30), editablePath.SubShapes[1].Points[2].Position);
  94. Assert.Equal(new VecF(10, 30), editablePath.SubShapes[1].Points[3].Position);
  95. }
  96. [Fact]
  97. public void TestThatTwoPolysWithSecondUnclosedReturnsCorrectShapeData()
  98. {
  99. VectorPath path = new VectorPath();
  100. path.MoveTo(new VecF(0, 0));
  101. path.LineTo(new VecF(2, 2));
  102. path.LineTo(new VecF(4, 4));
  103. path.Close();
  104. path.MoveTo(new VecF(10, 10));
  105. path.LineTo(new VecF(12, 12));
  106. path.LineTo(new VecF(14, 14));
  107. EditableVectorPath editablePath = new EditableVectorPath(path);
  108. Assert.Equal(2, editablePath.SubShapes.Count);
  109. Assert.True(editablePath.SubShapes[0].IsClosed);
  110. Assert.Equal(3, editablePath.SubShapes[0].Points.Count);
  111. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Position);
  112. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[1].Position);
  113. Assert.Equal(new VecF(4, 4), editablePath.SubShapes[0].Points[2].Position);
  114. Assert.False(editablePath.SubShapes[1].IsClosed);
  115. Assert.Equal(3, editablePath.SubShapes[1].Points.Count);
  116. Assert.Equal(new VecF(10, 10), editablePath.SubShapes[1].Points[0].Position);
  117. Assert.Equal(new VecF(12, 12), editablePath.SubShapes[1].Points[1].Position);
  118. Assert.Equal(new VecF(14, 14), editablePath.SubShapes[1].Points[2].Position);
  119. }
  120. [Fact]
  121. public void TestThatStartAndEndIndexesForMultipleShapesAreCorrect()
  122. {
  123. VectorPath path = new VectorPath();
  124. path.MoveTo(new VecF(0, 0));
  125. path.LineTo(new VecF(2, 2));
  126. path.LineTo(new VecF(4, 4));
  127. path.Close();
  128. path.MoveTo(new VecF(10, 10));
  129. path.LineTo(new VecF(12, 12));
  130. path.Close();
  131. int expectedFirstShapeStartIndex = 0;
  132. int expectedFirstShapeEndIndex = 2;
  133. int expectedSecondShapeStartIndex = 3;
  134. int expectedSecondShapeEndIndex = 5;
  135. EditableVectorPath editablePath = new EditableVectorPath(path);
  136. Assert.Equal(2, editablePath.SubShapes.Count);
  137. }
  138. [Fact]
  139. public void TestThatGetNextPointInTriangleShapeReturnsCorrectPoint()
  140. {
  141. VectorPath path = new VectorPath();
  142. path.MoveTo(new VecF(0, 0));
  143. path.LineTo(new VecF(2, 2));
  144. path.LineTo(new VecF(4, 4));
  145. path.Close();
  146. EditableVectorPath editablePath = new EditableVectorPath(path);
  147. ShapePoint nextPoint = editablePath.SubShapes[0].GetNextPoint(0);
  148. Assert.Equal(new VecF(2, 2), nextPoint.Position);
  149. Assert.Equal(1, nextPoint.Index);
  150. nextPoint = editablePath.SubShapes[0].GetNextPoint(1);
  151. Assert.Equal(new VecF(4, 4), nextPoint.Position);
  152. Assert.Equal(2, nextPoint.Index);
  153. nextPoint = editablePath.SubShapes[0].GetNextPoint(2);
  154. Assert.Equal(new VecF(0, 0), nextPoint.Position);
  155. Assert.Equal(0, nextPoint.Index);
  156. }
  157. [Fact]
  158. public void TestThatGetPreviousPointInTriangleShapeReturnsCorrectPoint()
  159. {
  160. VectorPath path = new VectorPath();
  161. path.MoveTo(new VecF(0, 0));
  162. path.LineTo(new VecF(2, 2));
  163. path.LineTo(new VecF(4, 4));
  164. path.Close();
  165. EditableVectorPath editablePath = new EditableVectorPath(path);
  166. ShapePoint previousPoint = editablePath.SubShapes[0].GetPreviousPoint(0);
  167. Assert.Equal(new VecF(4, 4), previousPoint.Position);
  168. Assert.Equal(2, previousPoint.Index);
  169. previousPoint = editablePath.SubShapes[0].GetPreviousPoint(1);
  170. Assert.Equal(new VecF(0, 0), previousPoint.Position);
  171. Assert.Equal(0, previousPoint.Index);
  172. previousPoint = editablePath.SubShapes[0].GetPreviousPoint(2);
  173. Assert.Equal(new VecF(2, 2), previousPoint.Position);
  174. Assert.Equal(1, previousPoint.Index);
  175. }
  176. [Fact]
  177. public void TestThatVerbsInTriangleAreCorrect()
  178. {
  179. VectorPath path = new VectorPath();
  180. path.MoveTo(new VecF(0, 0));
  181. path.LineTo(new VecF(2, 2));
  182. path.LineTo(new VecF(4, 4));
  183. path.Close();
  184. EditableVectorPath editablePath = new EditableVectorPath(path);
  185. Assert.Equal(3, editablePath.SubShapes[0].Points.Count);
  186. Assert.Equal(PathVerb.Line, editablePath.SubShapes[0].Points[0].Verb.VerbType);
  187. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Verb.From);
  188. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[0].Verb.To);
  189. Assert.Equal(PathVerb.Line, editablePath.SubShapes[0].Points[1].Verb.VerbType);
  190. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[1].Verb.From);
  191. Assert.Equal(new VecF(4, 4), editablePath.SubShapes[0].Points[1].Verb.To);
  192. Assert.Equal(PathVerb.Line, editablePath.SubShapes[0].Points[2].Verb.VerbType);
  193. Assert.Equal(new VecF(4, 4), editablePath.SubShapes[0].Points[2].Verb.From);
  194. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[2].Verb.To);
  195. }
  196. [Fact]
  197. public void TestThatVerbsInOvalAreCorrect()
  198. {
  199. const float conic = 0.70710769f;
  200. const float rangeLower = conic - 0.001f;
  201. const float rangeUpper = conic + 0.001f;
  202. VectorPath path = new VectorPath();
  203. path.AddOval(RectD.FromCenterAndSize(new VecD(5, 5), new VecD(10, 10)));
  204. EditableVectorPath editablePath = new EditableVectorPath(path);
  205. Assert.Equal(4, editablePath.SubShapes[0].Points.Count);
  206. Assert.Equal(PathVerb.Conic, editablePath.SubShapes[0].Points[0].Verb.VerbType);
  207. Assert.Equal(new VecF(10, 5), editablePath.SubShapes[0].Points[0].Verb.From);
  208. Assert.Equal(new VecF(5, 10), editablePath.SubShapes[0].Points[0].Verb.To);
  209. Assert.InRange(editablePath.SubShapes[0].Points[0].Verb.ConicWeight, rangeLower, rangeUpper);
  210. Assert.Equal(PathVerb.Conic, editablePath.SubShapes[0].Points[1].Verb.VerbType);
  211. Assert.Equal(new VecF(5, 10), editablePath.SubShapes[0].Points[1].Verb.From);
  212. Assert.Equal(new VecF(0, 5), editablePath.SubShapes[0].Points[1].Verb.To);
  213. Assert.InRange(editablePath.SubShapes[0].Points[1].Verb.ConicWeight, rangeLower, rangeUpper);
  214. Assert.Equal(PathVerb.Conic, editablePath.SubShapes[0].Points[2].Verb.VerbType);
  215. Assert.Equal(new VecF(0, 5), editablePath.SubShapes[0].Points[2].Verb.From);
  216. Assert.Equal(new VecF(5, 0), editablePath.SubShapes[0].Points[2].Verb.To);
  217. Assert.InRange(editablePath.SubShapes[0].Points[2].Verb.ConicWeight, rangeLower, rangeUpper);
  218. Assert.Equal(PathVerb.Conic, editablePath.SubShapes[0].Points[3].Verb.VerbType);
  219. Assert.Equal(new VecF(5, 0), editablePath.SubShapes[0].Points[3].Verb.From);
  220. Assert.Equal(new VecF(10, 5), editablePath.SubShapes[0].Points[3].Verb.To);
  221. Assert.InRange(editablePath.SubShapes[0].Points[3].Verb.ConicWeight, rangeLower, rangeUpper);
  222. }
  223. [Fact]
  224. public void TestThatOverlappingPolyPointsReturnCorrectSubShapePoints()
  225. {
  226. VectorPath path = new VectorPath();
  227. /*
  228. * |\
  229. * |_\
  230. * |
  231. */
  232. path.MoveTo(new VecF(0, 0));
  233. path.LineTo(new VecF(0, 2));
  234. path.LineTo(new VecF(0, 4));
  235. path.LineTo(new VecF(2, 2));
  236. path.LineTo(new VecF(0, 2));
  237. path.Close();
  238. EditableVectorPath editablePath = new EditableVectorPath(path);
  239. Assert.Equal(5, editablePath.SubShapes[0].Points.Count);
  240. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Verb.From);
  241. Assert.Equal(new VecF(0, 2), editablePath.SubShapes[0].Points[0].Verb.To);
  242. Assert.Equal(new VecF(0, 2), editablePath.SubShapes[0].Points[1].Verb.From);
  243. Assert.Equal(new VecF(0, 4), editablePath.SubShapes[0].Points[1].Verb.To);
  244. Assert.Equal(new VecF(0, 4), editablePath.SubShapes[0].Points[2].Verb.From);
  245. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[2].Verb.To);
  246. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[3].Verb.From);
  247. Assert.Equal(new VecF(0, 2), editablePath.SubShapes[0].Points[3].Verb.To);
  248. Assert.Equal(new VecF(0, 2), editablePath.SubShapes[0].Points[4].Verb.From);
  249. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[4].Verb.To);
  250. }
  251. [Fact]
  252. public void TestThatMultiSubShapesWithUnclosedReturnsCorrectPoints()
  253. {
  254. VectorPath path = new VectorPath();
  255. path.LineTo(new VecF(2, 2));
  256. path.LineTo(new VecF(0, 4));
  257. path.AddOval(RectD.FromCenterAndSize(new VecD(5, 5), new VecD(10, 10)));
  258. EditableVectorPath editablePath = new EditableVectorPath(path);
  259. Assert.Equal(2, editablePath.SubShapes.Count);
  260. Assert.Equal(3, editablePath.SubShapes[0].Points.Count);
  261. Assert.Equal(new VecF(0, 0), editablePath.SubShapes[0].Points[0].Position);
  262. Assert.Equal(new VecF(2, 2), editablePath.SubShapes[0].Points[1].Position);
  263. Assert.Equal(new VecF(0, 4), editablePath.SubShapes[0].Points[2].Position);
  264. Assert.False(editablePath.SubShapes[0].IsClosed);
  265. Assert.Equal(4, editablePath.SubShapes[1].Points.Count);
  266. Assert.Equal(new VecF(10, 5), editablePath.SubShapes[1].Points[0].Position);
  267. Assert.Equal(new VecF(5, 10), editablePath.SubShapes[1].Points[1].Position);
  268. Assert.Equal(new VecF(0, 5), editablePath.SubShapes[1].Points[2].Position);
  269. Assert.Equal(new VecF(5, 0), editablePath.SubShapes[1].Points[3].Position);
  270. Assert.True(editablePath.SubShapes[1].IsClosed);
  271. }
  272. [Fact]
  273. public void TestThatMoveToProducesEmptyVerb()
  274. {
  275. VectorPath path = new VectorPath();
  276. path.MoveTo(new VecF(0, 0));
  277. EditableVectorPath editablePath = new EditableVectorPath(path);
  278. Assert.Single(editablePath.SubShapes);
  279. Assert.Single(editablePath.SubShapes[0].Points);
  280. Assert.Equal(null, editablePath.SubShapes[0].Points[0].Verb.VerbType);
  281. }
  282. [Fact]
  283. public void TestThatMultipleMoveToProduceEmptyVerbs()
  284. {
  285. VectorPath path = new VectorPath();
  286. path.MoveTo(new VecF(0, 0));
  287. path.MoveTo(new VecF(2, 2));
  288. EditableVectorPath editablePath = new EditableVectorPath(path);
  289. Assert.Equal(2, editablePath.SubShapes.Count);
  290. Assert.Single(editablePath.SubShapes[0].Points);
  291. Assert.Single(editablePath.SubShapes[1].Points);
  292. Assert.Null(editablePath.SubShapes[0].Points[0].Verb.VerbType);
  293. Assert.Null(editablePath.SubShapes[1].Points[0].Verb.VerbType);
  294. }
  295. [Fact]
  296. public void TestThatEditingPointResultsInCorrectVectorPath()
  297. {
  298. VectorPath path = new VectorPath();
  299. path.MoveTo(new VecF(0, 0));
  300. path.LineTo(new VecF(2, 2));
  301. path.LineTo(new VecF(4, 4));
  302. path.Close();
  303. EditableVectorPath editablePath = new EditableVectorPath(path);
  304. editablePath.SubShapes[0].SetPointPosition(1, new VecF(3, 3), true);
  305. VectorPath newPath = editablePath.ToVectorPath();
  306. PathVerb[] sequence = [ PathVerb.Move, PathVerb.Line, PathVerb.Line, PathVerb.Line, PathVerb.Close, PathVerb.Done ];
  307. VecF[] points = [ new VecF(0, 0), new VecF(3, 3), new VecF(4, 4), new VecF(0, 0) ];
  308. int i = 0;
  309. foreach (var data in newPath)
  310. {
  311. Assert.Equal(sequence[i], data.verb);
  312. if(data.verb != PathVerb.Close && data.verb != PathVerb.Done)
  313. {
  314. Assert.Equal(points[i], Verb.GetPointFromVerb(data));
  315. }
  316. i++;
  317. }
  318. }
  319. [Theory]
  320. [InlineData(0, 0)]
  321. [InlineData(1, 0)]
  322. [InlineData(2, 0)]
  323. [InlineData(3, 0)]
  324. [InlineData(4, 1)]
  325. [InlineData(5, 1)]
  326. [InlineData(6, 1)]
  327. [InlineData(7, 1)]
  328. public void TestThatGetSubShapeByPointIndexReturnsCorrectSubShapeIndex(int index, int expected)
  329. {
  330. VectorPath path = new VectorPath();
  331. path.AddOval(RectD.FromCenterAndSize(new VecD(5, 5), new VecD(10, 10)));
  332. path.AddOval(RectD.FromCenterAndSize(new VecD(15, 15), new VecD(20, 20)));
  333. EditableVectorPath editablePath = new EditableVectorPath(path);
  334. Assert.Equal(expected, editablePath.SubShapes.ToList().IndexOf(editablePath.GetSubShapeContainingIndex(index)));
  335. }
  336. }