MVP_Model.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. if (connector.Init(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
  111. {
  112. connector.StartTracking(InnerMethod);
  113. }
  114. }
  115. /**************************/
  116. /*** INTERNAL FUNCTIONS ***/
  117. /**************************/
  118. /// <summary>
  119. /// Change the status of whether or not the lines are shown.
  120. /// </summary>
  121. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  122. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  123. private void ChangeLines(HashSet<int> lines, bool shown)
  124. {
  125. foreach (int lineId in lines)
  126. {
  127. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  128. {
  129. rightLineList[lineId] = new Tuple<bool, InternalLine>(shown, rightLineList[lineId].Item2);
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// A function that populates the matrixes needed for deletion detection with line data.
  135. /// </summary>
  136. private void RepopulateDeletionMatrixes()
  137. {
  138. if (canvasActive)
  139. {
  140. isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
  141. linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
  142. foreach (Tuple<bool, InternalLine> lineTuple in rightLineList)
  143. {
  144. if (lineTuple.Item1)
  145. {
  146. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  147. }
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// Tells the Presenter to Update the UI
  153. /// </summary>
  154. private void UpdateUI()
  155. {
  156. programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded);
  157. }
  158. /// <summary>
  159. /// A function that checks the deletion matrixes at a certain point
  160. /// and returns all Line ids at that point and in a square around it in a certain range.
  161. /// </summary>
  162. /// <param name="p">The point around which to check.</param>
  163. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  164. /// <returns>A List of all lines.</returns>
  165. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
  166. {
  167. HashSet<int> returnSet = new HashSet<int>();
  168. foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
  169. {
  170. if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImageSize.Width && pnt.Y < rightImageSize.Height)
  171. {
  172. if (isFilledMatrix[(int)pnt.X, (int)pnt.Y])
  173. {
  174. returnSet.UnionWith(linesMatrix[(int)pnt.X, (int)pnt.Y]);
  175. }
  176. }
  177. }
  178. return returnSet;
  179. }
  180. /********************************************/
  181. /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
  182. /********************************************/
  183. /// <summary>
  184. /// A function to update the dimensions of the left and right canvas when the window is resized.
  185. /// </summary>
  186. /// <param name="LeftCanvas">The size of the left canvas.</param>
  187. /// <param name="RightCanvas">The size of the right canvas.</param>
  188. public void ResizeEvent(ImageDimension LeftCanvas, ImageDimension RightCanvas)
  189. {
  190. if(LeftCanvas.Height >= 0 && LeftCanvas.Width>= 0) { leftImageSize = LeftCanvas; }
  191. if(RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
  192. RepopulateDeletionMatrixes();
  193. }
  194. /// <summary>
  195. /// A function to reset the right image.
  196. /// </summary>
  197. public void ResetRightImage()
  198. {
  199. rightLineList.Clear();
  200. programPresenter.PassLastActionTaken(historyOfActions.Reset());
  201. programPresenter.ClearRightLines();
  202. }
  203. /// <summary>
  204. /// The function to set the left image.
  205. /// </summary>
  206. /// <param name="width">The width of the left image.</param>
  207. /// <param name="height">The height of the left image.</param>
  208. /// <param name="listOfLines">The List of Lines to be displayed in the left image.</param>
  209. public void SetLeftLineList(int width, int height, List<InternalLine> listOfLines)
  210. {
  211. leftImageSize = new ImageDimension(width, height);
  212. rightImageSize = new ImageDimension(width, height);
  213. leftLineList = listOfLines;
  214. graphicLoaded = true;
  215. programPresenter.UpdateLeftLines(leftLineList);
  216. CanvasActivated();
  217. /*
  218. var workingCanvas = GetEmptyCanvas(width, height);
  219. var workingGraph = Graphics.FromImage(workingCanvas);
  220. leftLineList = listOfLines;
  221. //redrawAss = new RedrawAssistant(leftLineList);
  222. //overlayItems = redrawAss.Initialize(markerRadius);
  223. //Lines
  224. foreach (InternalLine line in leftLineList)
  225. {
  226. line.DrawLine(workingGraph);
  227. }
  228. leftImage = workingCanvas;
  229. programPresenter.UpdateLeftImage(leftImage);
  230. //Set right image to same size as left image and delete linelist
  231. DrawEmptyCanvasRight();
  232. rightLineList = new List<Tuple<bool, InternalLine>>();
  233. */
  234. }
  235. /// <summary>
  236. /// A function to tell the model a new canvas was activated.
  237. /// </summary>
  238. public void CanvasActivated()
  239. {
  240. canvasActive = true;
  241. RepopulateDeletionMatrixes();
  242. UpdateUI();
  243. }
  244. /// <summary>
  245. /// Will undo the last action taken, if the action history allows it.
  246. /// </summary>
  247. public void Undo()
  248. {
  249. if (historyOfActions.CanUndo())
  250. {
  251. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  252. SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
  253. switch (undoAction)
  254. {
  255. case SketchAction.ActionType.Delete:
  256. //Deleted Lines need to be shown
  257. ChangeLines(affectedLines, true);
  258. break;
  259. case SketchAction.ActionType.Draw:
  260. //Drawn lines need to be hidden
  261. ChangeLines(affectedLines, false);
  262. break;
  263. default:
  264. break;
  265. }
  266. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  267. programPresenter.UpdateRightLines(rightLineList);
  268. }
  269. RepopulateDeletionMatrixes();
  270. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(true));
  271. UpdateUI();
  272. }
  273. /// <summary>
  274. /// Will redo the last action undone, if the action history allows it.
  275. /// </summary>
  276. public void Redo()
  277. {
  278. if (historyOfActions.CanRedo())
  279. {
  280. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(false));
  281. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  282. SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
  283. switch (redoAction)
  284. {
  285. case SketchAction.ActionType.Delete:
  286. //Deleted Lines need to be redeleted
  287. ChangeLines(affectedLines, false);
  288. break;
  289. case SketchAction.ActionType.Draw:
  290. //Drawn lines need to be redrawn
  291. ChangeLines(affectedLines, true);
  292. break;
  293. default:
  294. break;
  295. }
  296. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  297. programPresenter.UpdateRightLines(rightLineList);
  298. RepopulateDeletionMatrixes();
  299. }
  300. UpdateUI();
  301. }
  302. /// <summary>
  303. /// The function called by the Presenter to change the drawing state of the program.
  304. /// </summary>
  305. /// <param name="nowDrawing">The new drawingstate of the program</param>
  306. public void ChangeState(bool nowDrawing)
  307. {
  308. inDrawingMode = nowDrawing;
  309. UpdateUI();
  310. }
  311. /// <summary>
  312. /// Updates the current cursor position of the model.
  313. /// </summary>
  314. /// <param name="p">The new cursor position</param>
  315. public void SetCurrentCursorPosition(Point p)
  316. {
  317. currentCursorPosition = p;
  318. }
  319. /// <summary>
  320. /// Start a new Line, when the Mouse is pressed down.
  321. /// </summary>
  322. public void MouseDown()
  323. {
  324. if (inDrawingMode)
  325. {
  326. currentLine.Clear();
  327. currentLine.Add(currentCursorPosition);
  328. }
  329. }
  330. /// <summary>
  331. /// Finish the current Line, when the pressed Mouse is released.
  332. /// </summary>
  333. public void MouseUp()
  334. {
  335. if (inDrawingMode && currentLine.Count > 0)
  336. {
  337. InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
  338. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  339. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  340. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  341. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  342. programPresenter.UpdateRightLines(rightLineList);
  343. currentLine.Clear();
  344. programPresenter.UpdateCurrentLine(currentLine);
  345. }
  346. UpdateUI();
  347. }
  348. String returnString = null;
  349. void InnerMethod(OptiTrack.Frame frame)
  350. {
  351. Console.WriteLine(frame.Trackables.Length);
  352. float x = frame.Trackables[0].X;
  353. float y = frame.Trackables[0].Y;
  354. float z = frame.Trackables[0].Z;
  355. returnString = ("X: " + x + "Y: " + y + "Z: " + z);
  356. }
  357. /// <summary>
  358. /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
  359. /// </summary>
  360. public void Tick()
  361. {
  362. programPresenter.PassOptiTrackMessage(returnString);
  363. if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
  364. else { previousCursorPosition = currentCursorPosition; }
  365. cursorPositions.Enqueue(currentCursorPosition);
  366. //Drawing
  367. if (inDrawingMode && programPresenter.IsMousePressed())
  368. {
  369. currentLine.Add(currentCursorPosition);
  370. programPresenter.UpdateCurrentLine(currentLine);
  371. }
  372. //Deleting
  373. if (!inDrawingMode && programPresenter.IsMousePressed())
  374. {
  375. List<Point> uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  376. foreach (Point currPoint in uncheckedPoints)
  377. {
  378. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
  379. if (linesToDelete.Count > 0)
  380. {
  381. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
  382. foreach (int lineID in linesToDelete)
  383. {
  384. rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
  385. }
  386. RepopulateDeletionMatrixes();
  387. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  388. programPresenter.UpdateRightLines(rightLineList);
  389. }
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// A helper Function that updates the markerRadius & deletionRadius, considering the size of the canvas.
  395. /// </summary>
  396. /// <param name="CanvasSize">The size of the canvas</param>
  397. public void UpdateSizes(ImageDimension CanvasSize)
  398. {
  399. if (rightImageWithoutOverlay != null)
  400. {
  401. int widthImage = rightImageSize.Width;
  402. int heightImage = rightImageSize.Height;
  403. int widthBox = CanvasSize.Width;
  404. int heightBox = CanvasSize.Height;
  405. float imageRatio = (float)widthImage / (float)heightImage;
  406. float containerRatio = (float)widthBox / (float)heightBox;
  407. float zoomFactor = 0;
  408. if (imageRatio >= containerRatio)
  409. {
  410. //Image is wider than it is high
  411. zoomFactor = (float)widthImage / (float)widthBox;
  412. }
  413. else
  414. {
  415. //Image is higher than it is wide
  416. zoomFactor = (float)heightImage / (float)heightBox;
  417. }
  418. markerRadius = (int)(10 * zoomFactor);
  419. deletionRadius = (int)(5 * zoomFactor);
  420. }
  421. }
  422. /// <summary>
  423. /// If there is unsaved progress.
  424. /// </summary>
  425. /// <returns>True if there is progress that has not been saved.</returns>
  426. public bool HasUnsavedProgress()
  427. {
  428. return !historyOfActions.IsEmpty();
  429. }
  430. }
  431. }