RedrawAssistant.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing;
  8. namespace SketchAssistant
  9. {
  10. public class RedrawAssistant
  11. {
  12. /// <summary>
  13. /// The lines of the left image, with a boolean indicating if they have been redrawn
  14. /// and an integer that is the same as the line id of the respective line in the right image.
  15. /// </summary>
  16. List<Tuple<Line, bool, int>> linesToRedraw;
  17. /// <summary>
  18. /// The Start and End points of all lines in linesToRedraw in the same order.
  19. /// </summary>
  20. List<Tuple<HashSet<Point>, HashSet<Point>>> startAndEndPoints;
  21. /// <summary>
  22. /// A Hashtable for quick lookup for a line id and its respective tuple in linesToRedraw
  23. /// </summary>
  24. Hashtable redrawnLineLookupTable;
  25. /// <summary>
  26. /// The position of the line currently being redrawn in the startAndEndPoints
  27. /// & linesToRedraw lists. -1 if no line is being redrawn.
  28. /// </summary>
  29. int lineBeingRedrawn;
  30. /// <summary>
  31. /// The id of the line being drawn on the right side. -1 if no line is being drawn.
  32. /// </summary>
  33. int currentLineID;
  34. /// <summary>
  35. /// Whether or not the user is currently redrawing a line.
  36. /// </summary>
  37. bool currentlyRedrawing;
  38. /// <summary>
  39. /// Whether or not the RedrawAssistant is active.
  40. /// </summary>
  41. bool isActive;
  42. /// <summary>
  43. /// The radius of the markers for redrawing.
  44. /// </summary>
  45. int markerRadius = 5;
  46. /// <summary>
  47. /// The Constructor for an inactive RedrawAssistant.
  48. /// </summary>
  49. public RedrawAssistant()
  50. {
  51. isActive = false;
  52. }
  53. /// <summary>
  54. /// The constructor for an active RedrawAssistant
  55. /// </summary>
  56. /// <param name="redrawItem">The lines that shall be redrawn</param>
  57. public RedrawAssistant(List<Line> redrawItem)
  58. {
  59. linesToRedraw = new List<Tuple<Line, bool, int>>();
  60. startAndEndPoints = new List<Tuple<HashSet<Point>, HashSet<Point>>>();
  61. isActive = true;
  62. currentlyRedrawing = false;
  63. lineBeingRedrawn = -1;
  64. redrawnLineLookupTable = new Hashtable();
  65. foreach (Line line in redrawItem)
  66. {
  67. linesToRedraw.Add(new Tuple<Line, bool, int>(line, false, -1));
  68. }
  69. SetMarkerRadius(5);
  70. }
  71. /// <summary>
  72. /// Initialization function that returns the initial list of overlay points.
  73. /// </summary>
  74. /// <param name="mRad">The radius of the points.</param>
  75. /// <returns>The list of overlay points.</returns>
  76. public List<Tuple<bool, HashSet<Point>>> Initialize(int mRad)
  77. {
  78. if (isActive)
  79. {
  80. List<Tuple<bool, HashSet<Point>>> retList = new List<Tuple<bool, HashSet<Point>>>();
  81. SetMarkerRadius(mRad);
  82. foreach(Tuple<HashSet<Point>, HashSet<Point>> tup in startAndEndPoints)
  83. {
  84. retList.Add(new Tuple<bool, HashSet<Point>>(false, tup.Item1));
  85. retList.Add(new Tuple<bool, HashSet<Point>>(false, tup.Item2));
  86. }
  87. return retList;
  88. }
  89. return null;
  90. }
  91. /// <summary>
  92. /// The main functionality of the RedrawAssistant, which updates the Assistant according to the inputs given.
  93. /// </summary>
  94. /// <param name="currentPoint">The current position of the cursor, as a point</param>
  95. /// <param name="rightLines">The lines on the right canvas</param>
  96. /// <param name="currLineID">The id of the line currently being drawn.</param>
  97. /// <param name="lineFinished">A boolean to indicate that the line is finished</param>
  98. /// <param name="overlayItems">A list containing the overlay items and if they should be drawn.</param>
  99. /// <returns>The updated List of overlay items, or the same list if no changes need to be done,
  100. /// along with a boolean indicating if something was changed</returns>
  101. public Tuple<bool, List<Tuple<bool, HashSet<Point>>>> Tick(Point currentPoint, List<Tuple<bool, Line>> rightLines, int currLineID, bool lineFinished,
  102. List<Tuple<bool, HashSet<Point>>> overlayItems)
  103. {
  104. if (!isActive) { return new Tuple<bool, List<Tuple<bool, HashSet<Point>>>>(false, overlayItems); }
  105. if (!currentlyRedrawing)
  106. {
  107. }
  108. /*
  109. Tuple<Line, bool, int> newLineTuple = null;
  110. var returnAllStartPoints = true;
  111. CheckForUndrawnLines(rightLines);
  112. // Checking if a startpoint is intersected
  113. if (!currentlyRedrawing)
  114. {
  115. for (int i = 0; i < linesToRedraw.Count; i++)
  116. {
  117. Tuple<Line, bool, int> tup = linesToRedraw[i];
  118. if (!tup.Item2)
  119. {
  120. if (startAndEndPoints[i].Item1.Contains(currentPoint))
  121. {
  122. currentlyRedrawing = true;
  123. lineBeingRedrawn = i;
  124. currentLineID = currLineID;
  125. returnList.Add(startAndEndPoints[i].Item1);
  126. returnList.Add(startAndEndPoints[i].Item2);
  127. returnAllStartPoints = false;
  128. }
  129. }
  130. }
  131. }
  132. //Currently redrawing a line, but a line hasn't been finished drawing.
  133. else if (!lineFinished)
  134. {
  135. returnList.Add(startAndEndPoints[lineBeingRedrawn].Item1);
  136. returnList.Add(startAndEndPoints[lineBeingRedrawn].Item2);
  137. returnAllStartPoints = false;
  138. }
  139. //Line is finished, check if it is in the correct endpoint
  140. else if (currLineID == currentLineID && startAndEndPoints[lineBeingRedrawn].Item2.Contains(currentPoint))
  141. {
  142. newLineTuple = new Tuple<Line, bool, int>(linesToRedraw[lineBeingRedrawn].Item1, true, currLineID);
  143. currentlyRedrawing = false;
  144. lineBeingRedrawn = -1;
  145. currentLineID = -1;
  146. }
  147. //Line is finished, but not in the correct endpoint
  148. else
  149. {
  150. currentlyRedrawing = false;
  151. lineBeingRedrawn = -1;
  152. currentLineID = -1;
  153. }
  154. //Replace the changed line tuple in linesToRedraw
  155. if(newLineTuple != null)
  156. {
  157. var newLine = newLineTuple.Item1;
  158. for (int i = 0; i < linesToRedraw.Count; i++)
  159. {
  160. var redrawLine = linesToRedraw[i].Item1;
  161. if (redrawLine.GetID() == newLine.GetID()
  162. && redrawLine.GetStartPoint().Equals(newLine.GetStartPoint())
  163. && redrawLine.GetEndPoint().Equals(newLine.GetEndPoint()))
  164. {
  165. redrawnLineLookupTable.Add(currLineID, i);
  166. linesToRedraw[i] = newLineTuple;
  167. }
  168. }
  169. }
  170. //Add all the startpoints to the list being returned
  171. if (returnAllStartPoints)
  172. {
  173. for (int i = 0; i < linesToRedraw.Count; i++)
  174. {
  175. if (!linesToRedraw[i].Item2)
  176. {
  177. returnList.Add(startAndEndPoints[i].Item1);
  178. }
  179. }
  180. }
  181. return returnList;
  182. */
  183. return new Tuple<bool, List<Tuple<bool, HashSet<Point>>>>(false, overlayItems);
  184. }
  185. /// <summary>
  186. /// A helping function which checks for lines where previously redrawn, but were removed from the image again.
  187. /// </summary>
  188. /// <param name="rightLines">The lines in the right image.</param>
  189. private void CheckForUndrawnLines(List<Tuple<bool, Line>> rightLines)
  190. {
  191. for (int i = 0; i < rightLines.Count; i++)
  192. {
  193. if (redrawnLineLookupTable.ContainsKey(rightLines[i].Item2.GetID()))
  194. {
  195. if (!rightLines[i].Item1)
  196. {
  197. int listPos = (int)redrawnLineLookupTable[rightLines[i].Item2.GetID()];
  198. var oldTup = linesToRedraw[listPos];
  199. linesToRedraw[listPos] = new Tuple<Line, bool, int>(oldTup.Item1, false, -1);
  200. }
  201. else
  202. {
  203. int listPos = (int)redrawnLineLookupTable[rightLines[i].Item2.GetID()];
  204. var oldTup = linesToRedraw[listPos];
  205. linesToRedraw[listPos] = new Tuple<Line, bool, int>(oldTup.Item1, true, rightLines[i].Item2.GetID());
  206. }
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// A function to set the marker radius for the markers returned by the RedrawAssistant
  212. /// </summary>
  213. /// <param name="markerRad">The Radius of the markers.</param>
  214. public void SetMarkerRadius(int markerRad)
  215. {
  216. markerRadius = markerRad;
  217. if (isActive)
  218. {
  219. startAndEndPoints = new List<Tuple<HashSet<Point>, HashSet<Point>>>();
  220. foreach (Tuple<Line, bool, int> tup in linesToRedraw)
  221. {
  222. startAndEndPoints.Add(CalculateStartAndEnd(tup.Item1));
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// Will calculate the start and endpoints of the given line.
  228. /// </summary>
  229. /// <param name="line">The given line.</param>
  230. private Tuple<HashSet<Point>, HashSet<Point>> CalculateStartAndEnd(Line line)
  231. {
  232. var circle0 = GeometryCalculator.FilledCircleAlgorithm(line.GetStartPoint(), markerRadius);
  233. var circle1 = GeometryCalculator.FilledCircleAlgorithm(line.GetEndPoint(), markerRadius);
  234. var currentLineEndings = new Tuple<HashSet<Point>, HashSet<Point>>(circle0, circle1);
  235. return currentLineEndings;
  236. }
  237. }
  238. }