MVP_Model.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using OptiTrack;
  11. namespace SketchAssistantWPF
  12. {
  13. public class MVP_Model
  14. {
  15. /// <summary>
  16. /// The Presenter of the MVP-Model.
  17. /// </summary>
  18. MVP_Presenter programPresenter;
  19. /// <summary>
  20. /// History of Actions
  21. /// </summary>
  22. ActionHistory historyOfActions;
  23. /// <summary>
  24. /// The assistant responsible for the redraw mode
  25. /// </summary>
  26. //RedrawAssistant redrawAss;
  27. OptiTrackConnector connector;
  28. OptiTrackConnector.OnFrameReady frameReady;
  29. /***********************/
  30. /*** CLASS VARIABLES ***/
  31. /***********************/
  32. /// <summary>
  33. /// If the program is in drawing mode.
  34. /// </summary>
  35. bool inDrawingMode;
  36. /// <summary>
  37. /// Size of deletion area
  38. /// </summary>
  39. int deletionRadius = 5;
  40. /// <summary>
  41. /// Size of areas marking endpoints of lines in the redraw mode.
  42. /// </summary>
  43. int markerRadius = 10;
  44. /// <summary>
  45. /// The Position of the Cursor in the right picture box
  46. /// </summary>
  47. Point currentCursorPosition;
  48. /// <summary>
  49. /// The Previous Cursor Position in the right picture box
  50. /// </summary>
  51. Point previousCursorPosition;
  52. /// <summary>
  53. /// Queue for the cursorPositions
  54. /// </summary>
  55. Queue<Point> cursorPositions = new Queue<Point>();
  56. /// <summary>
  57. /// Lookup Matrix for checking postions of lines in the image
  58. /// </summary>
  59. bool[,] isFilledMatrix;
  60. /// <summary>
  61. /// Lookup Matrix for getting line ids at a certain postions of the image
  62. /// </summary>
  63. HashSet<int>[,] linesMatrix;
  64. /// <summary>
  65. /// List of items which will be overlayed over the right canvas.
  66. /// </summary>
  67. List<Tuple<bool, HashSet<Point>>> overlayItems;
  68. /// <summary>
  69. /// Width of the LeftImageBox.
  70. /// </summary>
  71. public int leftImageBoxWidth;
  72. /// <summary>
  73. /// Height of the LeftImageBox.
  74. /// </summary>
  75. public int leftImageBoxHeight;
  76. /// <summary>
  77. /// Width of the RightImageBox.
  78. /// </summary>
  79. public int rightImageBoxWidth;
  80. /// <summary>
  81. /// Height of the RightImageBox.
  82. /// </summary>
  83. public int rightImageBoxHeight;
  84. public ImageDimension leftImageSize { get; private set; }
  85. public ImageDimension rightImageSize { get; private set; }
  86. /// <summary>
  87. /// Indicates whether or not the canvas on the right side is active.
  88. /// </summary>
  89. public bool canvasActive {get; set;}
  90. /// <summary>
  91. /// Indicates if there is a graphic loaded in the left canvas.
  92. /// </summary>
  93. public bool graphicLoaded { get; set; }
  94. Image rightImageWithoutOverlay;
  95. List<InternalLine> leftLineList;
  96. List<Tuple<bool, InternalLine>> rightLineList;
  97. List<Point> currentLine = new List<Point>();
  98. public MVP_Model(MVP_Presenter presenter)
  99. {
  100. programPresenter = presenter;
  101. historyOfActions = new ActionHistory();
  102. //redrawAss = new RedrawAssistant();
  103. //overlayItems = new List<Tuple<bool, HashSet<Point>>>();
  104. rightLineList = new List<Tuple<bool, InternalLine>>();
  105. canvasActive = false;
  106. UpdateUI();
  107. rightImageSize = new ImageDimension(0, 0);
  108. leftImageSize = new ImageDimension(0, 0);
  109. connector = new OptiTrackConnector();
  110. }
  111. /**************************/
  112. /*** INTERNAL FUNCTIONS ***/
  113. /**************************/
  114. /// <summary>
  115. /// Change the status of whether or not the lines are shown.
  116. /// </summary>
  117. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  118. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  119. private void ChangeLines(HashSet<int> lines, bool shown)
  120. {
  121. foreach (int lineId in lines)
  122. {
  123. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  124. {
  125. rightLineList[lineId] = new Tuple<bool, InternalLine>(shown, rightLineList[lineId].Item2);
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// A function that populates the matrixes needed for deletion detection with line data.
  131. /// </summary>
  132. private void RepopulateDeletionMatrixes()
  133. {
  134. if (canvasActive)
  135. {
  136. isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
  137. linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
  138. foreach (Tuple<bool, InternalLine> lineTuple in rightLineList)
  139. {
  140. if (lineTuple.Item1)
  141. {
  142. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  143. }
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Tells the Presenter to Update the UI
  149. /// </summary>
  150. private void UpdateUI()
  151. {
  152. programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded);
  153. }
  154. /// <summary>
  155. /// A function that checks the deletion matrixes at a certain point
  156. /// and returns all Line ids at that point and in a square around it in a certain range.
  157. /// </summary>
  158. /// <param name="p">The point around which to check.</param>
  159. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  160. /// <returns>A List of all lines.</returns>
  161. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
  162. {
  163. HashSet<int> returnSet = new HashSet<int>();
  164. foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
  165. {
  166. if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImageSize.Width && pnt.Y < rightImageSize.Height)
  167. {
  168. if (isFilledMatrix[(int)pnt.X, (int)pnt.Y])
  169. {
  170. returnSet.UnionWith(linesMatrix[(int)pnt.X, (int)pnt.Y]);
  171. }
  172. }
  173. }
  174. return returnSet;
  175. }
  176. /********************************************/
  177. /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
  178. /********************************************/
  179. /// <summary>
  180. /// A function to update the dimensions of the left and right canvas when the window is resized.
  181. /// </summary>
  182. /// <param name="LeftCanvas">The size of the left canvas.</param>
  183. /// <param name="RightCanvas">The size of the right canvas.</param>
  184. public void ResizeEvent(ImageDimension LeftCanvas, ImageDimension RightCanvas)
  185. {
  186. if(LeftCanvas.Height >= 0 && LeftCanvas.Width>= 0) { leftImageSize = LeftCanvas; }
  187. if(RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
  188. RepopulateDeletionMatrixes();
  189. }
  190. /// <summary>
  191. /// A function to reset the right image.
  192. /// </summary>
  193. public void ResetRightImage()
  194. {
  195. rightLineList.Clear();
  196. programPresenter.PassLastActionTaken(historyOfActions.Reset());
  197. programPresenter.ClearRightLines();
  198. }
  199. /// <summary>
  200. /// The function to set the left image.
  201. /// </summary>
  202. /// <param name="width">The width of the left image.</param>
  203. /// <param name="height">The height of the left image.</param>
  204. /// <param name="listOfLines">The List of Lines to be displayed in the left image.</param>
  205. public void SetLeftLineList(int width, int height, List<InternalLine> listOfLines)
  206. {
  207. leftImageSize = new ImageDimension(width, height);
  208. rightImageSize = new ImageDimension(width, height);
  209. leftLineList = listOfLines;
  210. graphicLoaded = true;
  211. programPresenter.UpdateLeftLines(leftLineList);
  212. CanvasActivated();
  213. /*
  214. var workingCanvas = GetEmptyCanvas(width, height);
  215. var workingGraph = Graphics.FromImage(workingCanvas);
  216. leftLineList = listOfLines;
  217. //redrawAss = new RedrawAssistant(leftLineList);
  218. //overlayItems = redrawAss.Initialize(markerRadius);
  219. //Lines
  220. foreach (InternalLine line in leftLineList)
  221. {
  222. line.DrawLine(workingGraph);
  223. }
  224. leftImage = workingCanvas;
  225. programPresenter.UpdateLeftImage(leftImage);
  226. //Set right image to same size as left image and delete linelist
  227. DrawEmptyCanvasRight();
  228. rightLineList = new List<Tuple<bool, InternalLine>>();
  229. */
  230. }
  231. /// <summary>
  232. /// A function to tell the model a new canvas was activated.
  233. /// </summary>
  234. public void CanvasActivated()
  235. {
  236. canvasActive = true;
  237. RepopulateDeletionMatrixes();
  238. UpdateUI();
  239. }
  240. /// <summary>
  241. /// Will undo the last action taken, if the action history allows it.
  242. /// </summary>
  243. public void Undo()
  244. {
  245. if (historyOfActions.CanUndo())
  246. {
  247. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  248. SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
  249. switch (undoAction)
  250. {
  251. case SketchAction.ActionType.Delete:
  252. //Deleted Lines need to be shown
  253. ChangeLines(affectedLines, true);
  254. break;
  255. case SketchAction.ActionType.Draw:
  256. //Drawn lines need to be hidden
  257. ChangeLines(affectedLines, false);
  258. break;
  259. default:
  260. break;
  261. }
  262. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  263. programPresenter.UpdateRightLines(rightLineList);
  264. }
  265. RepopulateDeletionMatrixes();
  266. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(true));
  267. UpdateUI();
  268. }
  269. /// <summary>
  270. /// Will redo the last action undone, if the action history allows it.
  271. /// </summary>
  272. public void Redo()
  273. {
  274. if (historyOfActions.CanRedo())
  275. {
  276. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(false));
  277. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  278. SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
  279. switch (redoAction)
  280. {
  281. case SketchAction.ActionType.Delete:
  282. //Deleted Lines need to be redeleted
  283. ChangeLines(affectedLines, false);
  284. break;
  285. case SketchAction.ActionType.Draw:
  286. //Drawn lines need to be redrawn
  287. ChangeLines(affectedLines, true);
  288. break;
  289. default:
  290. break;
  291. }
  292. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  293. programPresenter.UpdateRightLines(rightLineList);
  294. RepopulateDeletionMatrixes();
  295. }
  296. UpdateUI();
  297. }
  298. /// <summary>
  299. /// The function called by the Presenter to change the drawing state of the program.
  300. /// </summary>
  301. /// <param name="nowDrawing">The new drawingstate of the program</param>
  302. public void ChangeState(bool nowDrawing)
  303. {
  304. inDrawingMode = nowDrawing;
  305. UpdateUI();
  306. }
  307. /// <summary>
  308. /// Updates the current cursor position of the model.
  309. /// </summary>
  310. /// <param name="p">The new cursor position</param>
  311. public void SetCurrentCursorPosition(Point p)
  312. {
  313. currentCursorPosition = p;
  314. }
  315. /// <summary>
  316. /// Start a new Line, when the Mouse is pressed down.
  317. /// </summary>
  318. public void MouseDown()
  319. {
  320. if (inDrawingMode)
  321. {
  322. currentLine.Clear();
  323. currentLine.Add(currentCursorPosition);
  324. }
  325. }
  326. /// <summary>
  327. /// Finish the current Line, when the pressed Mouse is released.
  328. /// </summary>
  329. public void MouseUp()
  330. {
  331. if (inDrawingMode && currentLine.Count > 0)
  332. {
  333. InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
  334. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  335. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  336. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  337. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  338. programPresenter.UpdateRightLines(rightLineList);
  339. currentLine.Clear();
  340. programPresenter.UpdateCurrentLine(currentLine);
  341. }
  342. UpdateUI();
  343. }
  344. /// <summary>
  345. /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
  346. /// </summary>
  347. public void Tick()
  348. {
  349. if(inDrawingMode)
  350. {
  351. if (connector.Init(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
  352. {
  353. connector.StartTracking(programPresenter.PassOptiTrackMessage);
  354. }
  355. }
  356. if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
  357. else { previousCursorPosition = currentCursorPosition; }
  358. cursorPositions.Enqueue(currentCursorPosition);
  359. //Drawing
  360. if (inDrawingMode && programPresenter.IsMousePressed())
  361. {
  362. currentLine.Add(currentCursorPosition);
  363. programPresenter.UpdateCurrentLine(currentLine);
  364. }
  365. //Deleting
  366. if (!inDrawingMode && programPresenter.IsMousePressed())
  367. {
  368. List<Point> uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  369. foreach (Point currPoint in uncheckedPoints)
  370. {
  371. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
  372. if (linesToDelete.Count > 0)
  373. {
  374. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
  375. foreach (int lineID in linesToDelete)
  376. {
  377. rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
  378. }
  379. RepopulateDeletionMatrixes();
  380. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  381. programPresenter.UpdateRightLines(rightLineList);
  382. }
  383. }
  384. }
  385. }
  386. /// <summary>
  387. /// A helper Function that updates the markerRadius & deletionRadius, considering the size of the canvas.
  388. /// </summary>
  389. /// <param name="CanvasSize">The size of the canvas</param>
  390. public void UpdateSizes(ImageDimension CanvasSize)
  391. {
  392. if (rightImageWithoutOverlay != null)
  393. {
  394. int widthImage = rightImageSize.Width;
  395. int heightImage = rightImageSize.Height;
  396. int widthBox = CanvasSize.Width;
  397. int heightBox = CanvasSize.Height;
  398. float imageRatio = (float)widthImage / (float)heightImage;
  399. float containerRatio = (float)widthBox / (float)heightBox;
  400. float zoomFactor = 0;
  401. if (imageRatio >= containerRatio)
  402. {
  403. //Image is wider than it is high
  404. zoomFactor = (float)widthImage / (float)widthBox;
  405. }
  406. else
  407. {
  408. //Image is higher than it is wide
  409. zoomFactor = (float)heightImage / (float)heightBox;
  410. }
  411. markerRadius = (int)(10 * zoomFactor);
  412. deletionRadius = (int)(5 * zoomFactor);
  413. }
  414. }
  415. /// <summary>
  416. /// If there is unsaved progress.
  417. /// </summary>
  418. /// <returns>True if there is progress that has not been saved.</returns>
  419. public bool HasUnsavedProgress()
  420. {
  421. return !historyOfActions.IsEmpty();
  422. }
  423. }
  424. }