RedrawAssistant.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /// The main functionality of the RedrawAssistant, which updates the Assistant according to the inputs given.
  73. /// </summary>
  74. /// <param name="currentPoint">The current position of the cursor, as a point</param>
  75. /// <param name="rightLines">The lines on the right canvas</param>
  76. /// <param name="currLineID">The id of the line currently being drawn.</param>
  77. /// <param name="lineFinished">A boolean to indicate that the line is finished</param>
  78. /// <returns>A List of HashSets of Points, which are markers for the user to redraw lines.</returns>
  79. public List<HashSet<Point>> Tick(Point currentPoint, List<Tuple<bool, Line>> rightLines, int currLineID, bool lineFinished)
  80. {
  81. List<HashSet<Point>> returnList = new List<HashSet<Point>>();
  82. if (!isActive) { return returnList; }
  83. Tuple<Line, bool, int> newLineTuple = null;
  84. var returnAllStartPoints = true;
  85. CheckForUndrawnLines(rightLines);
  86. // Checking if a startpoint is intersected
  87. if (!currentlyRedrawing)
  88. {
  89. for (int i = 0; i < linesToRedraw.Count; i++)
  90. {
  91. Tuple<Line, bool, int> tup = linesToRedraw[i];
  92. if (!tup.Item2)
  93. {
  94. if (startAndEndPoints[i].Item1.Contains(currentPoint))
  95. {
  96. currentlyRedrawing = true;
  97. lineBeingRedrawn = i;
  98. currentLineID = currLineID;
  99. returnList.Add(startAndEndPoints[i].Item1);
  100. returnList.Add(startAndEndPoints[i].Item2);
  101. returnAllStartPoints = false;
  102. }
  103. }
  104. }
  105. }
  106. //Currently redrawing a line, but a line hasn't been finished drawing.
  107. else if (!lineFinished)
  108. {
  109. returnList.Add(startAndEndPoints[lineBeingRedrawn].Item1);
  110. returnList.Add(startAndEndPoints[lineBeingRedrawn].Item2);
  111. returnAllStartPoints = false;
  112. }
  113. //Line is finished, check if it is in the correct endpoint
  114. else if (currLineID == currentLineID && startAndEndPoints[lineBeingRedrawn].Item2.Contains(currentPoint))
  115. {
  116. newLineTuple = new Tuple<Line, bool, int>(linesToRedraw[lineBeingRedrawn].Item1, true, currLineID);
  117. currentlyRedrawing = false;
  118. lineBeingRedrawn = -1;
  119. currentLineID = -1;
  120. }
  121. //Line is finished, but not in the correct endpoint
  122. else
  123. {
  124. currentlyRedrawing = false;
  125. lineBeingRedrawn = -1;
  126. currentLineID = -1;
  127. }
  128. //Replace the changed line tuple in linesToRedraw
  129. if(newLineTuple != null)
  130. {
  131. var newLine = newLineTuple.Item1;
  132. for (int i = 0; i < linesToRedraw.Count; i++)
  133. {
  134. var redrawLine = linesToRedraw[i].Item1;
  135. if (redrawLine.GetID() == newLine.GetID()
  136. && redrawLine.GetStartPoint().Equals(newLine.GetStartPoint())
  137. && redrawLine.GetEndPoint().Equals(newLine.GetEndPoint()))
  138. {
  139. redrawnLineLookupTable.Add(currLineID, i);
  140. linesToRedraw[i] = newLineTuple;
  141. }
  142. }
  143. }
  144. //Add all the startpoints to the list being returned
  145. if (returnAllStartPoints)
  146. {
  147. for (int i = 0; i < linesToRedraw.Count; i++)
  148. {
  149. if (!linesToRedraw[i].Item2)
  150. {
  151. returnList.Add(startAndEndPoints[i].Item1);
  152. }
  153. }
  154. }
  155. return returnList;
  156. }
  157. /// <summary>
  158. /// A helping function which checks for lines where previously redrawn, but were removed from the image again.
  159. /// </summary>
  160. /// <param name="rightLines">The lines in the right image.</param>
  161. private void CheckForUndrawnLines(List<Tuple<bool, Line>> rightLines)
  162. {
  163. for (int i = 0; i < rightLines.Count; i++)
  164. {
  165. if (redrawnLineLookupTable.ContainsKey(rightLines[i].Item2.GetID()))
  166. {
  167. if (!rightLines[i].Item1)
  168. {
  169. int listPos = (int)redrawnLineLookupTable[rightLines[i].Item2.GetID()];
  170. var oldTup = linesToRedraw[listPos];
  171. linesToRedraw[listPos] = new Tuple<Line, bool, int>(oldTup.Item1, false, -1);
  172. }
  173. else
  174. {
  175. int listPos = (int)redrawnLineLookupTable[rightLines[i].Item2.GetID()];
  176. var oldTup = linesToRedraw[listPos];
  177. linesToRedraw[listPos] = new Tuple<Line, bool, int>(oldTup.Item1, true, rightLines[i].Item2.GetID());
  178. }
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// A function to set the marker radius for the markers returned by the RedrawAssistant
  184. /// </summary>
  185. /// <param name="markerRad">The Radius of the markers.</param>
  186. public void SetMarkerRadius(int markerRad)
  187. {
  188. markerRadius = markerRad;
  189. if (isActive)
  190. {
  191. startAndEndPoints = new List<Tuple<HashSet<Point>, HashSet<Point>>>();
  192. foreach (Tuple<Line, bool, int> tup in linesToRedraw)
  193. {
  194. startAndEndPoints.Add(CalculateStartAndEnd(tup.Item1));
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// Will calculate the start and endpoints of the given line.
  200. /// </summary>
  201. /// <param name="line">The given line.</param>
  202. private Tuple<HashSet<Point>, HashSet<Point>> CalculateStartAndEnd(Line line)
  203. {
  204. var circle0 = GeometryCalculator.FilledCircleAlgorithm(line.GetStartPoint(), markerRadius);
  205. var circle1 = GeometryCalculator.FilledCircleAlgorithm(line.GetEndPoint(), markerRadius);
  206. var currentLineEndings = new Tuple<HashSet<Point>, HashSet<Point>>(circle0, circle1);
  207. return currentLineEndings;
  208. }
  209. }
  210. }