MVP_Model.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. using System.Runtime.InteropServices;
  12. using System.IO;
  13. namespace SketchAssistantWPF
  14. {
  15. public class MVP_Model
  16. {
  17. /// <summary>
  18. /// The Presenter of the MVP-Model.
  19. /// </summary>
  20. MVP_Presenter programPresenter;
  21. /// <summary>
  22. /// History of Actions
  23. /// </summary>
  24. ActionHistory historyOfActions;
  25. OptiTrackConnector connector;
  26. /***********************/
  27. /*** CLASS VARIABLES ***/
  28. /***********************/
  29. /// <summary>
  30. /// If the program is in drawing mode.
  31. /// </summary>
  32. bool inDrawingMode;
  33. /// <summary>
  34. /// if the program is using OptiTrack
  35. /// </summary>
  36. public bool optiTrackInUse { get; private set; }
  37. /// <summary>
  38. /// Size of deletion area
  39. /// </summary>
  40. int deletionRadius = 5;
  41. /// <summary>
  42. /// Size of areas marking endpoints of lines in the redraw mode.
  43. /// </summary>
  44. int markerRadius = 10;
  45. /// <summary>
  46. /// The Position of the Cursor in the right picture box
  47. /// </summary>
  48. Point currentCursorPosition;
  49. /// <summary>
  50. /// The Previous Cursor Position in the right picture box
  51. /// </summary>
  52. Point previousCursorPosition;
  53. /// <summary>
  54. /// Queue for the cursorPositions
  55. /// </summary>
  56. Queue<Point> cursorPositions = new Queue<Point>();
  57. /// <summary>
  58. /// Lookup Matrix for checking postions of lines in the image
  59. /// </summary>
  60. bool[,] isFilledMatrix;
  61. /// <summary>
  62. /// Lookup Matrix for getting line ids at a certain postions of the image
  63. /// </summary>
  64. HashSet<int>[,] linesMatrix;
  65. /// <summary>
  66. /// List of items which will be overlayed over the right canvas.
  67. /// </summary>
  68. List<Tuple<bool, HashSet<Point>>> overlayItems;
  69. /// <summary>
  70. /// Width of the LeftImageBox.
  71. /// </summary>
  72. public int leftImageBoxWidth;
  73. /// <summary>
  74. /// Height of the LeftImageBox.
  75. /// </summary>
  76. public int leftImageBoxHeight;
  77. /// <summary>
  78. /// Width of the RightImageBox.
  79. /// </summary>
  80. public int rightImageBoxWidth;
  81. /// <summary>
  82. /// Height of the RightImageBox.
  83. /// </summary>
  84. public int rightImageBoxHeight;
  85. public ImageDimension leftImageSize { get; private set; }
  86. public ImageDimension rightImageSize { get; private set; }
  87. /// <summary>
  88. /// Indicates whether or not the canvas on the right side is active.
  89. /// </summary>
  90. public bool canvasActive { get; set; }
  91. /// <summary>
  92. /// Indicates if there is a graphic loaded in the left canvas.
  93. /// </summary>
  94. public bool graphicLoaded { get; set; }
  95. /// <summary>
  96. /// Whether or not an optitrack system is avaiable.
  97. /// </summary>
  98. public bool optitrackAvailable { get; private set; }
  99. //TODO: calibrate
  100. double OPTITRACK_X_OFFSET = 0.7878;
  101. double OPTITRACK_Y_OFFSET = 0.7977;
  102. double OPTITRACK_CANVAS_HEIGHT = 1.29;
  103. double OPTITRACK_X_SCALE = -0.254 * (((1.816 / 0.0254) * 96) / (1.816));
  104. double OPTITRACK_Y_SCALE = 0.254 * (((1.360 / 0.0254) * 96) / (1.360));
  105. Image rightImageWithoutOverlay;
  106. /// Whether or not the mouse is pressed.
  107. /// </summary>
  108. private bool mouseDown;
  109. List<InternalLine> leftLineList;
  110. List<Tuple<bool, InternalLine>> rightLineList;
  111. List<Point> currentLine = new List<Point>();
  112. public MVP_Model(MVP_Presenter presenter)
  113. {
  114. programPresenter = presenter;
  115. historyOfActions = new ActionHistory();
  116. //redrawAss = new RedrawAssistant();
  117. //overlayItems = new List<Tuple<bool, HashSet<Point>>>();
  118. rightLineList = new List<Tuple<bool, InternalLine>>();
  119. canvasActive = false;
  120. UpdateUI();
  121. rightImageSize = new ImageDimension(0, 0);
  122. leftImageSize = new ImageDimension(0, 0);
  123. connector = new OptiTrackConnector();
  124. armband = new Armband();
  125. optitrackAvailable = false;
  126. if (File.Exists(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
  127. {
  128. if (connector.Init(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
  129. {
  130. optitrackAvailable = true;
  131. connector.StartTracking(getOptiTrackPosition);
  132. }
  133. }
  134. }
  135. [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
  136. private static extern bool SetCursorPos(int X, int Y);
  137. //[DllImport("user32.dll")]
  138. //public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
  139. public enum MouseEventType : int
  140. {
  141. LeftDown = 0x02,
  142. LeftUp = 0x04,
  143. RightDown = 0x08,
  144. RightUp = 0x10
  145. }
  146. /**************************/
  147. /*** INTERNAL FUNCTIONS ***/
  148. /**************************/
  149. /// <summary>
  150. /// Change the status of whether or not the lines are shown.
  151. /// </summary>
  152. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  153. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  154. private void ChangeLines(HashSet<int> lines, bool shown)
  155. {
  156. foreach (int lineId in lines)
  157. {
  158. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  159. {
  160. rightLineList[lineId] = new Tuple<bool, InternalLine>(shown, rightLineList[lineId].Item2);
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// A function that populates the matrixes needed for deletion detection with line data.
  166. /// </summary>
  167. private void RepopulateDeletionMatrixes()
  168. {
  169. if (canvasActive)
  170. {
  171. isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
  172. linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
  173. foreach (Tuple<bool, InternalLine> lineTuple in rightLineList)
  174. {
  175. if (lineTuple.Item1)
  176. {
  177. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  178. }
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// Tells the Presenter to Update the UI
  184. /// </summary>
  185. private void UpdateUI()
  186. {
  187. programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded, optitrackAvailable, optiTrackInUse);
  188. }
  189. /// <summary>
  190. /// A function that checks the deletion matrixes at a certain point
  191. /// and returns all Line ids at that point and in a square around it in a certain range.
  192. /// </summary>
  193. /// <param name="p">The point around which to check.</param>
  194. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  195. /// <returns>A List of all lines.</returns>
  196. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
  197. {
  198. HashSet<int> returnSet = new HashSet<int>();
  199. foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
  200. {
  201. if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImageSize.Width && pnt.Y < rightImageSize.Height)
  202. {
  203. if (isFilledMatrix[(int)pnt.X, (int)pnt.Y])
  204. {
  205. returnSet.UnionWith(linesMatrix[(int)pnt.X, (int)pnt.Y]);
  206. }
  207. }
  208. }
  209. return returnSet;
  210. }
  211. /********************************************/
  212. /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
  213. /********************************************/
  214. /// <summary>
  215. /// A function to update the dimensions of the left and right canvas when the window is resized.
  216. /// </summary>
  217. /// <param name="LeftCanvas">The size of the left canvas.</param>
  218. /// <param name="RightCanvas">The size of the right canvas.</param>
  219. public void ResizeEvent(ImageDimension LeftCanvas, ImageDimension RightCanvas)
  220. {
  221. if (LeftCanvas.Height >= 0 && LeftCanvas.Width >= 0) { leftImageSize = LeftCanvas; }
  222. if (RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
  223. RepopulateDeletionMatrixes();
  224. }
  225. /// <summary>
  226. /// A function to reset the right image.
  227. /// </summary>
  228. public void ResetRightImage()
  229. {
  230. rightLineList.Clear();
  231. programPresenter.PassLastActionTaken(historyOfActions.Reset());
  232. programPresenter.ClearRightLines();
  233. }
  234. /// <summary>
  235. /// The function to set the left image.
  236. /// </summary>
  237. /// <param name="width">The width of the left image.</param>
  238. /// <param name="height">The height of the left image.</param>
  239. /// <param name="listOfLines">The List of Lines to be displayed in the left image.</param>
  240. public void SetLeftLineList(int width, int height, List<InternalLine> listOfLines)
  241. {
  242. leftImageSize = new ImageDimension(width, height);
  243. rightImageSize = new ImageDimension(width, height);
  244. leftLineList = listOfLines;
  245. graphicLoaded = true;
  246. programPresenter.UpdateLeftLines(leftLineList);
  247. CanvasActivated();
  248. /*
  249. var workingCanvas = GetEmptyCanvas(width, height);
  250. var workingGraph = Graphics.FromImage(workingCanvas);
  251. leftLineList = listOfLines;
  252. //redrawAss = new RedrawAssistant(leftLineList);
  253. //overlayItems = redrawAss.Initialize(markerRadius);
  254. //Lines
  255. foreach (InternalLine line in leftLineList)
  256. {
  257. line.DrawLine(workingGraph);
  258. }
  259. leftImage = workingCanvas;
  260. programPresenter.UpdateLeftImage(leftImage);
  261. //Set right image to same size as left image and delete linelist
  262. DrawEmptyCanvasRight();
  263. rightLineList = new List<Tuple<bool, InternalLine>>();
  264. */
  265. }
  266. /// <summary>
  267. /// A function to tell the model a new canvas was activated.
  268. /// </summary>
  269. public void CanvasActivated()
  270. {
  271. canvasActive = true;
  272. RepopulateDeletionMatrixes();
  273. UpdateUI();
  274. }
  275. /// <summary>
  276. /// Will undo the last action taken, if the action history allows it.
  277. /// </summary>
  278. public void Undo()
  279. {
  280. if (historyOfActions.CanUndo())
  281. {
  282. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  283. SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
  284. switch (undoAction)
  285. {
  286. case SketchAction.ActionType.Delete:
  287. //Deleted Lines need to be shown
  288. ChangeLines(affectedLines, true);
  289. break;
  290. case SketchAction.ActionType.Draw:
  291. //Drawn lines need to be hidden
  292. ChangeLines(affectedLines, false);
  293. break;
  294. default:
  295. break;
  296. }
  297. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  298. programPresenter.UpdateRightLines(rightLineList);
  299. }
  300. RepopulateDeletionMatrixes();
  301. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(true));
  302. UpdateUI();
  303. }
  304. /// <summary>
  305. /// Will redo the last action undone, if the action history allows it.
  306. /// </summary>
  307. public void Redo()
  308. {
  309. if (historyOfActions.CanRedo())
  310. {
  311. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(false));
  312. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  313. SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
  314. switch (redoAction)
  315. {
  316. case SketchAction.ActionType.Delete:
  317. //Deleted Lines need to be redeleted
  318. ChangeLines(affectedLines, false);
  319. break;
  320. case SketchAction.ActionType.Draw:
  321. //Drawn lines need to be redrawn
  322. ChangeLines(affectedLines, true);
  323. break;
  324. default:
  325. break;
  326. }
  327. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  328. programPresenter.UpdateRightLines(rightLineList);
  329. RepopulateDeletionMatrixes();
  330. }
  331. UpdateUI();
  332. }
  333. /// <summary>
  334. /// The function called by the Presenter to change the drawing state of the program.
  335. /// </summary>
  336. /// <param name="nowDrawing">The new drawingstate of the program</param>
  337. public void ChangeState(bool nowDrawing)
  338. {
  339. inDrawingMode = nowDrawing;
  340. UpdateUI();
  341. }
  342. public void ChangeOptiTrack(bool usingOptiTrack)
  343. {
  344. optiTrackInUse = usingOptiTrack;
  345. }
  346. /// <summary>
  347. /// Updates the current cursor position of the model.
  348. /// </summary>
  349. /// <param name="p">The new cursor position</param>
  350. public void SetCurrentCursorPosition(Point p)
  351. {
  352. if (!optiTrackInUse) currentCursorPosition = p;
  353. mouseDown = programPresenter.IsMousePressed();
  354. }
  355. /// <summary>
  356. /// Updates the current cursor position of the model.
  357. /// </summary>
  358. /// <param name="p">The new cursor position</param>
  359. public void SetCurrentFingerPosition(Point p)
  360. {
  361. Console.WriteLine("raw coordinates: " + p.X + ";" + p.Y);
  362. Point correctedPoint = ConvertTo96thsOfInch(p);
  363. Console.WriteLine(correctedPoint.X + "," + correctedPoint.Y);
  364. currentCursorPosition = correctedPoint;
  365. }
  366. private Point ConvertTo96thsOfInch(Point p)
  367. {
  368. double xCoordinate = (p.X - OPTITRACK_X_OFFSET) * OPTITRACK_X_SCALE;
  369. double yCoordinate = (OPTITRACK_CANVAS_HEIGHT - (p.Y - OPTITRACK_Y_OFFSET)) * OPTITRACK_Y_SCALE;
  370. return new Point(xCoordinate, yCoordinate);
  371. }
  372. private Point ConvertToPixel(Point p)
  373. {
  374. double xCoordinate = (p.X - OPTITRACK_X_OFFSET) * -1 * (/*Anzahl Pixel X-Richtung*/0 / (1.816)) + 0/*1/2 * x-richtung pixel*/; //TODO
  375. double yCoordinate = (((OPTITRACK_CANVAS_HEIGHT + 0/*meter von oberer Rand Leinwand zu oberer Rand Bildschirm*/) - (p.Y - OPTITRACK_Y_OFFSET))) * (/*Anzahl Pixel Y-Richtung*/0 / (1.360));
  376. return new Point(xCoordinate, yCoordinate);
  377. }
  378. /// <summary>
  379. /// Start a new Line, when the Mouse is pressed down.
  380. /// </summary>
  381. public void StartNewLine()
  382. {
  383. if (optiTrackInUse || programPresenter.IsMousePressed())
  384. {
  385. if (inDrawingMode)
  386. {
  387. currentLine.Clear();
  388. currentLine.Add(currentCursorPosition);
  389. }
  390. }
  391. }
  392. /// <summary>
  393. /// Finish the current Line, when the pressed Mouse is released.
  394. /// </summary>
  395. /// <param name="valid">Whether the up event is valid or not</param>
  396. public void FinishCurrentLine(bool valid)
  397. {
  398. foreach (Point p in currentLine)
  399. {
  400. Console.WriteLine(p.X + ";" + p.Y);
  401. }
  402. if (valid)
  403. {
  404. if (inDrawingMode && currentLine.Count > 0)
  405. {
  406. InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
  407. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  408. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  409. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  410. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  411. programPresenter.UpdateRightLines(rightLineList);
  412. currentLine.Clear();
  413. programPresenter.UpdateCurrentLine(currentLine);
  414. }
  415. }
  416. else
  417. {
  418. currentLine.Clear();
  419. }
  420. UpdateUI();
  421. }
  422. public float optiTrackX;
  423. public float optiTrackY;
  424. public float optiTrackZ;
  425. private bool optiTrackInsideDrawingZone = false;
  426. private double WARNING_ZONE_BOUNDARY = 0.10; //5cm
  427. private Armband armband;
  428. void getOptiTrackPosition(OptiTrack.Frame frame)
  429. {
  430. optiTrackX = frame.Trackables[0].X;
  431. optiTrackY = frame.Trackables[0].Y;
  432. optiTrackZ = frame.Trackables[0].Z;
  433. }
  434. /// <summary>
  435. /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
  436. /// </summary>
  437. public void Tick()
  438. {
  439. if (optiTrackInUse)
  440. {
  441. if (CheckInsideDrawingZone(optiTrackZ))
  442. {
  443. SetCurrentFingerPosition(new Point(optiTrackX, optiTrackY));
  444. if (!optiTrackInsideDrawingZone)
  445. {
  446. optiTrackInsideDrawingZone = true;
  447. StartNewLine();
  448. Console.WriteLine("new line begun");
  449. }
  450. if (optiTrackZ > WARNING_ZONE_BOUNDARY)
  451. {
  452. armband.pushForward();
  453. }
  454. else if (optiTrackZ < -1 * WARNING_ZONE_BOUNDARY)
  455. {
  456. armband.pushBackward();
  457. }
  458. }
  459. else
  460. {
  461. if (optiTrackInsideDrawingZone)
  462. {
  463. optiTrackInsideDrawingZone = false;
  464. FinishCurrentLine(true);
  465. Console.WriteLine("line finished");
  466. }
  467. }
  468. projectPointOntoScreen(optiTrackX, optiTrackY);
  469. }
  470. if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
  471. else { previousCursorPosition = currentCursorPosition; }
  472. cursorPositions.Enqueue(currentCursorPosition);
  473. //Drawing
  474. if (optiTrackInUse)
  475. {
  476. if (CheckInsideDrawingZone(optiTrackZ))
  477. {
  478. Console.WriteLine("point added");
  479. currentLine.Add(currentCursorPosition);
  480. programPresenter.UpdateCurrentLine(currentLine);
  481. }
  482. }
  483. else if (inDrawingMode && programPresenter.IsMousePressed())
  484. {
  485. currentLine.Add(currentCursorPosition);
  486. //programPresenter.UpdateCurrentLine(currentLine);
  487. }
  488. //Deleting
  489. if (!inDrawingMode && programPresenter.IsMousePressed())
  490. {
  491. List<Point> uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  492. foreach (Point currPoint in uncheckedPoints)
  493. {
  494. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
  495. if (linesToDelete.Count > 0)
  496. {
  497. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
  498. foreach (int lineID in linesToDelete)
  499. {
  500. rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
  501. }
  502. RepopulateDeletionMatrixes();
  503. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  504. programPresenter.UpdateRightLines(rightLineList);
  505. }
  506. }
  507. }
  508. }
  509. private void projectPointOntoScreen(float optiTrackX, float optiTrackY)
  510. {
  511. Point auxiliaryPoint = ConvertToPixel(new Point(optiTrackX, optiTrackY));
  512. SetCursorPos((int)auxiliaryPoint.X, (int)auxiliaryPoint.Y);
  513. }
  514. private bool CheckInsideDrawingZone(float optiTrackZ)
  515. {
  516. if (Math.Abs(optiTrackZ) > WARNING_ZONE_BOUNDARY * 2) return false;
  517. return true;
  518. }
  519. /*
  520. /// <summary>
  521. /// A helper Function that updates the markerRadius & deletionRadius, considering the size of the canvas.
  522. /// </summary>
  523. /// <param name="CanvasSize">The size of the canvas</param>
  524. public void UpdateSizes(ImageDimension CanvasSize)
  525. {
  526. if (rightImageWithoutOverlay != null)
  527. {
  528. int widthImage = rightImageSize.Width;
  529. int heightImage = rightImageSize.Height;
  530. int widthBox = CanvasSize.Width;
  531. int heightBox = CanvasSize.Height;
  532. float imageRatio = (float)widthImage / (float)heightImage;
  533. float containerRatio = (float)widthBox / (float)heightBox;
  534. float zoomFactor = 0;
  535. if (imageRatio >= containerRatio)
  536. {
  537. //Image is wider than it is high
  538. zoomFactor = (float)widthImage / (float)widthBox;
  539. }
  540. else
  541. {
  542. //Image is higher than it is wide
  543. zoomFactor = (float)heightImage / (float)heightBox;
  544. }
  545. markerRadius = (int)(10 * zoomFactor);
  546. deletionRadius = (int)(5 * zoomFactor);
  547. }
  548. }
  549. */
  550. /// <summary>
  551. /// If there is unsaved progress.
  552. /// </summary>
  553. /// <returns>True if there is progress that has not been saved.</returns>
  554. public bool HasUnsavedProgress()
  555. {
  556. return !historyOfActions.IsEmpty();
  557. }
  558. }
  559. }