MVP_Model.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. /// <summary>
  26. /// The connector class used to get frames from the Optitrack system.
  27. /// </summary>
  28. OptiTrackConnector connector;
  29. /***********************/
  30. /*** CLASS VARIABLES ***/
  31. /***********************/
  32. /// <summary>
  33. /// If the program is in drawing mode.
  34. /// </summary>
  35. public bool inDrawingMode { get; private set; }
  36. /// <summary>
  37. /// if the program is using OptiTrack
  38. /// </summary>
  39. public bool optiTrackInUse { get; private set; }
  40. /// <summary>
  41. /// Size of deletion area
  42. /// </summary>
  43. int deletionRadius = 5;
  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. /// The Position of the Cursor of opti track
  58. /// </summary>
  59. Point currentOptiCursorPosition;
  60. /// <summary>
  61. /// The Previous Cursor Position of opti track
  62. /// </summary>
  63. Point previousOptiCursorPosition;
  64. /// <summary>
  65. /// Queue for the cursorPositions of opti track
  66. /// </summary>
  67. Queue<Point> optiCursorPositions = new Queue<Point>();
  68. /// <summary>
  69. /// Lookup Matrix for checking postions of lines in the image
  70. /// </summary>
  71. bool[,] isFilledMatrix;
  72. /// <summary>
  73. /// Lookup Matrix for getting line ids at a certain postions of the image
  74. /// </summary>
  75. HashSet<int>[,] linesMatrix;
  76. /// <summary>
  77. /// Width of the LeftImageBox.
  78. /// </summary>
  79. public int leftImageBoxWidth;
  80. /// <summary>
  81. /// Height of the LeftImageBox.
  82. /// </summary>
  83. public int leftImageBoxHeight;
  84. /// <summary>
  85. /// Width of the RightImageBox.
  86. /// </summary>
  87. public int rightImageBoxWidth;
  88. /// <summary>
  89. /// Height of the RightImageBox.
  90. /// </summary>
  91. public int rightImageBoxHeight;
  92. /// <summary>
  93. /// The size of the right canvas.
  94. /// </summary>
  95. public ImageDimension rightImageSize { get; private set; }
  96. /// <summary>
  97. /// Indicates whether or not the canvas on the right side is active.
  98. /// </summary>
  99. public bool canvasActive { get; set; }
  100. /// <summary>
  101. /// Indicates if there is a graphic loaded in the left canvas.
  102. /// </summary>
  103. public bool graphicLoaded { get; set; }
  104. /// <summary>
  105. /// Whether or not an optitrack system is avaiable.
  106. /// </summary>
  107. public bool optitrackAvailable { get; private set; }
  108. /// <summary>
  109. /// x coordinate in real world. one unit is one meter. If standing in front of video wall facing it, moving left results in incrementation of x.
  110. /// </summary>
  111. public float optiTrackX;
  112. /// <summary>
  113. /// y coordinate in real world. one unit is one meter. If standing in front of video wall, moving up results in incrementation of y.
  114. /// </summary>
  115. public float optiTrackY;
  116. /// <summary>
  117. /// z coordinate in real world. one unit is one meter. If standing in front of video wall, moving back results in incrementation of y.
  118. /// </summary>
  119. public float optiTrackZ;
  120. /// <summary>
  121. /// keeps track of whether last tick the trackable was inside drawing zone or not.
  122. /// </summary>
  123. private bool optiTrackInsideDrawingZone = false;
  124. /// <summary>
  125. /// this is a variable used for detecting whether the tracker is in the warning zone (0 +- variable), no drawing zone (0 +- 2 * variable) or normal drawing zone
  126. /// </summary>
  127. private double WARNING_ZONE_BOUNDARY = 0.10; //10cm
  128. /// <summary>
  129. /// object of class wristband used for controlling the vibrotactile wristband
  130. /// </summary>
  131. private Wristband wristband;
  132. /// <summary>
  133. /// Is set to true when the trackable has passed to the backside of the drawing surface,
  134. /// invalidating all inputs on its way back.
  135. /// </summary>
  136. bool OptiMovingBack = false;
  137. /// <summary>
  138. /// The Layer in which the optitrack system was. 0 is drawing layer, -1 is in front, 1 is behind
  139. /// </summary>
  140. int OptiLayer = 0;
  141. /// <summary>
  142. /// The path traveled since the last tick
  143. /// </summary>
  144. double PathTraveled = 0;
  145. /// <summary>
  146. /// Whether or not the mouse is pressed.
  147. /// </summary>
  148. private bool mouseDown;
  149. /// <summary>
  150. /// A List of lines in the left canvas.
  151. /// </summary>
  152. List<InternalLine> leftLineList;
  153. /// <summary>
  154. /// A list of lines in the right canvas along with a boolean indicating if they should be drawn.
  155. /// </summary>
  156. List<Tuple<bool, InternalLine>> rightLineList;
  157. /// <summary>
  158. /// The line currently being drawin with optitrack.
  159. /// </summary>
  160. List<Point> currentLine = new List<Point>();
  161. public MVP_Model(MVP_Presenter presenter)
  162. {
  163. programPresenter = presenter;
  164. historyOfActions = new ActionHistory();
  165. rightLineList = new List<Tuple<bool, InternalLine>>();
  166. canvasActive = false;
  167. UpdateUI();
  168. rightImageSize = new ImageDimension(0, 0);
  169. connector = new OptiTrackConnector();
  170. wristband = new Wristband();
  171. //Set up Optitrack
  172. optitrackAvailable = false;
  173. if (File.Exists(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
  174. {
  175. if (connector.Init(@"C:\Users\videowall-pc-user\Documents\BP-SketchAssistant\SketchAssistant\optitrack_setup.ttp"))
  176. {
  177. optitrackAvailable = true;
  178. connector.StartTracking(GetOptiTrackPosition);
  179. }
  180. }
  181. }
  182. /**************************/
  183. /*** INTERNAL FUNCTIONS ***/
  184. /**************************/
  185. /// <summary>
  186. /// Check if the Optitrack trackable is in the drawing plane.
  187. /// </summary>
  188. /// <param name="optiTrackZ">The real world z coordinates of the trackable.</param>
  189. /// <returns>If the trackable is in front of the drawing plane</returns>
  190. private bool CheckInsideDrawingZone(float optiTrackZ)
  191. {
  192. if (Math.Abs(optiTrackZ) > WARNING_ZONE_BOUNDARY * 2) return false;
  193. return true;
  194. }
  195. /// <summary>
  196. /// Function that is called by the OptitrackController to pass frames to the model.
  197. /// </summary>
  198. /// <param name="frame">An Optitrack Frame</param>
  199. void GetOptiTrackPosition(OptiTrack.Frame frame)
  200. {
  201. if (frame.Trackables.Length >= 1)
  202. {
  203. optiTrackX = frame.Trackables[0].X;
  204. optiTrackY = frame.Trackables[0].Y;
  205. optiTrackZ = frame.Trackables[0].Z;
  206. }
  207. }
  208. /// <summary>
  209. /// Change the status of whether or not the lines are shown.
  210. /// </summary>
  211. /// <param name="lines">The HashSet containing the affected Line IDs.</param>
  212. /// <param name="shown">True if the lines should be shown, false if they should be hidden.</param>
  213. private void ChangeLines(HashSet<int> lines, bool shown)
  214. {
  215. foreach (int lineId in lines)
  216. {
  217. if (lineId <= rightLineList.Count - 1 && lineId >= 0)
  218. {
  219. rightLineList[lineId] = new Tuple<bool, InternalLine>(shown, rightLineList[lineId].Item2);
  220. }
  221. }
  222. }
  223. /// <summary>
  224. /// Check if enough distance has been travelled to warrant a vibration.
  225. /// </summary>
  226. private void CheckPathTraveled()
  227. {
  228. var a = Math.Abs(previousOptiCursorPosition.X - currentOptiCursorPosition.X);
  229. var b = Math.Abs(previousOptiCursorPosition.Y - currentOptiCursorPosition.Y);
  230. PathTraveled += Math.Sqrt(Math.Pow(a,2) + Math.Pow(b,2));
  231. //Set the Interval of vibrations here
  232. if(PathTraveled > 2)
  233. {
  234. PathTraveled = 0;
  235. //Activate vibration here
  236. }
  237. }
  238. /// <summary>
  239. /// A function that populates the matrixes needed for deletion detection with line data.
  240. /// </summary>
  241. private void RepopulateDeletionMatrixes()
  242. {
  243. if (canvasActive)
  244. {
  245. isFilledMatrix = new bool[rightImageSize.Width, rightImageSize.Height];
  246. linesMatrix = new HashSet<int>[rightImageSize.Width, rightImageSize.Height];
  247. foreach (Tuple<bool, InternalLine> lineTuple in rightLineList)
  248. {
  249. if (lineTuple.Item1)
  250. {
  251. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  252. }
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// Tells the Presenter to Update the UI
  258. /// </summary>
  259. private void UpdateUI()
  260. {
  261. programPresenter.UpdateUIState(inDrawingMode, historyOfActions.CanUndo(), historyOfActions.CanRedo(), canvasActive, graphicLoaded, optitrackAvailable, optiTrackInUse);
  262. }
  263. /// <summary>
  264. /// A function that checks the deletion matrixes at a certain point
  265. /// and returns all Line ids at that point and in a square around it in a certain range.
  266. /// </summary>
  267. /// <param name="p">The point around which to check.</param>
  268. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  269. /// <returns>A List of all lines.</returns>
  270. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, int range)
  271. {
  272. HashSet<int> returnSet = new HashSet<int>();
  273. foreach (Point pnt in GeometryCalculator.FilledCircleAlgorithm(p, (int)range))
  274. {
  275. if (pnt.X >= 0 && pnt.Y >= 0 && pnt.X < rightImageSize.Width && pnt.Y < rightImageSize.Height)
  276. {
  277. if (isFilledMatrix[(int)pnt.X, (int)pnt.Y])
  278. {
  279. returnSet.UnionWith(linesMatrix[(int)pnt.X, (int)pnt.Y]);
  280. }
  281. }
  282. }
  283. return returnSet;
  284. }
  285. /// <summary>
  286. /// Converts given point to device-independent pixel.
  287. /// </summary>
  288. /// <param name="p">real world coordinate</param>
  289. /// <returns>The given Point converted to device-independent pixel </returns>
  290. private Point ConvertTo96thsOfInch(Point p)
  291. {
  292. //The position of the optitrack coordinate system
  293. // and sizes of the optitrack tracking area relative to the canvas.
  294. double OPTITRACK_X_OFFSET = -0.4854;
  295. double OPTITRACK_Y_OFFSET = 0.9;
  296. double OPTITRACK_CANVAS_HEIGHT = 1.2;
  297. double OPTITRACK_X_SCALE = 0.21 * (((1.8316/*size of canvas*/ / 0.0254) * 96) / (1.8316));
  298. double OPTITRACK_Y_SCALE = 0.205 * (((1.2 / 0.0254) * 96) / (1.2));
  299. //The coordinates on the display
  300. double xCoordinate = (p.X - OPTITRACK_X_OFFSET) * OPTITRACK_X_SCALE;
  301. double yCoordinate = (OPTITRACK_CANVAS_HEIGHT - (p.Y - OPTITRACK_Y_OFFSET)) * OPTITRACK_Y_SCALE;
  302. return new Point(xCoordinate, yCoordinate);
  303. }
  304. /// <summary>
  305. /// Updates the Optitrack coordiantes, aswell as the marker for optitrack.
  306. /// </summary>
  307. /// <param name="p">The new cursor position</param>
  308. private void SetCurrentFingerPosition(Point p)
  309. {
  310. Point correctedPoint = ConvertTo96thsOfInch(p);
  311. if (optiTrackZ < -2.2 * WARNING_ZONE_BOUNDARY && OptiLayer > -1)
  312. {
  313. OptiLayer = -1; OptiMovingBack = false;
  314. programPresenter.SetOverlayColor("optipoint", Brushes.Yellow);
  315. }
  316. else if (optiTrackZ > 2 * WARNING_ZONE_BOUNDARY && OptiLayer < 1)
  317. {
  318. programPresenter.SetOverlayColor("optipoint", Brushes.Red);
  319. OptiLayer = 1;
  320. }
  321. else if(optiTrackZ <= 2 * WARNING_ZONE_BOUNDARY && optiTrackZ >= -2.2 * WARNING_ZONE_BOUNDARY){
  322. if(OptiLayer > 0)
  323. OptiMovingBack = true;
  324. programPresenter.SetOverlayColor("optipoint", Brushes.Green);
  325. OptiLayer = 0;
  326. }
  327. currentOptiCursorPosition = correctedPoint;
  328. programPresenter.MoveOptiPoint(currentOptiCursorPosition);
  329. if (optiCursorPositions.Count > 0) { previousOptiCursorPosition = optiCursorPositions.Dequeue(); }
  330. else { previousOptiCursorPosition = currentOptiCursorPosition; }
  331. }
  332. /********************************************/
  333. /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
  334. /********************************************/
  335. /// <summary>
  336. /// A function to update the dimensions of the left and right canvas when the window is resized.
  337. /// </summary>
  338. /// <param name="LeftCanvas">The size of the left canvas.</param>
  339. /// <param name="RightCanvas">The size of the right canvas.</param>
  340. public void ResizeEvent(ImageDimension LeftCanvas, ImageDimension RightCanvas)
  341. {
  342. if (RightCanvas.Height >= 0 && RightCanvas.Width >= 0) { rightImageSize = RightCanvas; }
  343. RepopulateDeletionMatrixes();
  344. }
  345. /// <summary>
  346. /// A function to reset the right image.
  347. /// </summary>
  348. public void ResetRightImage()
  349. {
  350. if(currentLine.Count > 0)
  351. FinishCurrentLine(true);
  352. rightLineList.Clear();
  353. programPresenter.PassLastActionTaken(historyOfActions.Reset());
  354. programPresenter.ClearRightLines();
  355. }
  356. /// <summary>
  357. /// The function to set the left image.
  358. /// </summary>
  359. /// <param name="width">The width of the left image.</param>
  360. /// <param name="height">The height of the left image.</param>
  361. /// <param name="listOfLines">The List of Lines to be displayed in the left image.</param>
  362. public void SetLeftLineList(int width, int height, List<InternalLine> listOfLines)
  363. {
  364. rightImageSize = new ImageDimension(width, height);
  365. leftLineList = listOfLines;
  366. graphicLoaded = true;
  367. programPresenter.UpdateLeftLines(leftLineList);
  368. CanvasActivated();
  369. }
  370. /// <summary>
  371. /// A function to tell the model a new canvas was activated.
  372. /// </summary>
  373. public void CanvasActivated()
  374. {
  375. canvasActive = true;
  376. RepopulateDeletionMatrixes();
  377. UpdateUI();
  378. }
  379. /// <summary>
  380. /// Will undo the last action taken, if the action history allows it.
  381. /// </summary>
  382. public void Undo()
  383. {
  384. if (historyOfActions.CanUndo())
  385. {
  386. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  387. SketchAction.ActionType undoAction = historyOfActions.GetCurrentAction().GetActionType();
  388. switch (undoAction)
  389. {
  390. case SketchAction.ActionType.Delete:
  391. //Deleted Lines need to be shown
  392. ChangeLines(affectedLines, true);
  393. break;
  394. case SketchAction.ActionType.Draw:
  395. //Drawn lines need to be hidden
  396. ChangeLines(affectedLines, false);
  397. break;
  398. default:
  399. break;
  400. }
  401. programPresenter.UpdateRightLines(rightLineList);
  402. }
  403. RepopulateDeletionMatrixes();
  404. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(true));
  405. UpdateUI();
  406. }
  407. /// <summary>
  408. /// Will redo the last action undone, if the action history allows it.
  409. /// </summary>
  410. public void Redo()
  411. {
  412. if (historyOfActions.CanRedo())
  413. {
  414. programPresenter.PassLastActionTaken(historyOfActions.MoveAction(false));
  415. HashSet<int> affectedLines = historyOfActions.GetCurrentAction().GetLineIDs();
  416. SketchAction.ActionType redoAction = historyOfActions.GetCurrentAction().GetActionType();
  417. switch (redoAction)
  418. {
  419. case SketchAction.ActionType.Delete:
  420. //Deleted Lines need to be redeleted
  421. ChangeLines(affectedLines, false);
  422. break;
  423. case SketchAction.ActionType.Draw:
  424. //Drawn lines need to be redrawn
  425. ChangeLines(affectedLines, true);
  426. break;
  427. default:
  428. break;
  429. }
  430. programPresenter.UpdateRightLines(rightLineList);
  431. RepopulateDeletionMatrixes();
  432. }
  433. UpdateUI();
  434. }
  435. /// <summary>
  436. /// The function called by the Presenter to change the drawing state of the program.
  437. /// </summary>
  438. /// <param name="nowDrawing">The new drawingstate of the program</param>
  439. public void ChangeState(bool nowDrawing)
  440. {
  441. if(inDrawingMode && !nowDrawing && currentLine.Count > 0 && optiTrackInUse)
  442. FinishCurrentLine(true);
  443. inDrawingMode = nowDrawing;
  444. UpdateUI();
  445. }
  446. /// <summary>
  447. /// The function called by the Presenter to set a variable which describes if OptiTrack is in use
  448. /// </summary>
  449. /// <param name="usingOptiTrack"></param>
  450. public void SetOptiTrack(bool usingOptiTrack)
  451. {
  452. optiTrackInUse = usingOptiTrack;
  453. if (usingOptiTrack && optiTrackX == 0 && optiTrackY == 0 && optiTrackZ == 0)
  454. {
  455. programPresenter.PassWarning("Trackable not detected, please check if OptiTrack is activated and Trackable is recognized");
  456. optiTrackInUse = false;
  457. //Disable optipoint
  458. programPresenter.SetOverlayStatus("optipoint", false, currentCursorPosition);
  459. Console.WriteLine("Point disabled");
  460. }
  461. else
  462. {
  463. Console.WriteLine("Point enabled: {0}",optiTrackInUse);
  464. //Enable optipoint
  465. programPresenter.SetOverlayStatus("optipoint", true, currentCursorPosition);
  466. }
  467. }
  468. /// <summary>
  469. /// Updates the current cursor position of the model.
  470. /// </summary>
  471. /// <param name="p">The new cursor position</param>
  472. public void SetCurrentCursorPosition(Point p)
  473. {
  474. currentCursorPosition = p;
  475. mouseDown = programPresenter.IsMousePressed();
  476. }
  477. /// <summary>
  478. /// Start a new Line, when the Mouse is pressed down.
  479. /// </summary>
  480. public void StartNewLine()
  481. {
  482. mouseDown = true;
  483. if (inDrawingMode)
  484. {
  485. if(optiTrackInUse)
  486. {
  487. currentLine.Clear();
  488. currentLine.Add(currentOptiCursorPosition);
  489. }
  490. else if (programPresenter.IsMousePressed())
  491. {
  492. currentLine.Clear();
  493. currentLine.Add(currentCursorPosition);
  494. }
  495. }
  496. }
  497. /// <summary>
  498. /// Finish the current Line, when the pressed Mouse is released.
  499. /// </summary>
  500. /// <param name="valid">Whether the up event is valid or not</param>
  501. public void FinishCurrentLine(bool valid)
  502. {
  503. mouseDown = false;
  504. if (valid)
  505. {
  506. if (inDrawingMode && currentLine.Count > 0)
  507. {
  508. InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
  509. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  510. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  511. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  512. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  513. programPresenter.UpdateRightLines(rightLineList);
  514. currentLine.Clear();
  515. programPresenter.UpdateCurrentLine(currentLine);
  516. }
  517. }
  518. else
  519. {
  520. currentLine.Clear();
  521. }
  522. UpdateUI();
  523. }
  524. /// <summary>
  525. /// Finish the current Line, when the pressed Mouse is released.
  526. /// Overload that is used to pass a list of points to be used when one is available.
  527. /// </summary>
  528. /// <param name="p">The list of points</param>
  529. public void FinishCurrentLine(List<Point> p)
  530. {
  531. mouseDown = false;
  532. if (inDrawingMode && currentLine.Count > 0)
  533. {
  534. InternalLine newLine = new InternalLine(p, rightLineList.Count);
  535. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  536. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  537. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  538. programPresenter.UpdateRightLines(rightLineList);
  539. currentLine.Clear();
  540. }
  541. UpdateUI();
  542. }
  543. /// <summary>
  544. /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
  545. /// </summary>
  546. public void Tick()
  547. {
  548. if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
  549. else { previousCursorPosition = currentCursorPosition; }
  550. if(optitrackAvailable)
  551. SetCurrentFingerPosition(new Point(optiTrackX, optiTrackY));
  552. if (optiTrackInUse && inDrawingMode && !OptiMovingBack) // optitrack is being used
  553. {
  554. //outside of drawing zone
  555. if (!CheckInsideDrawingZone(optiTrackZ))
  556. {
  557. //Check if trackable was in drawing zone last tick & program is in drawing mode-> finish line
  558. if (optiTrackInsideDrawingZone && inDrawingMode)
  559. {
  560. optiTrackInsideDrawingZone = false;
  561. FinishCurrentLine(true);
  562. }
  563. }
  564. //Draw with optitrack, when in drawing zone
  565. if (CheckInsideDrawingZone(optiTrackZ))
  566. {
  567. //Optitrack wasn't in the drawing zone last tick -> start a new line
  568. if (!optiTrackInsideDrawingZone)
  569. {
  570. optiTrackInsideDrawingZone = true;
  571. StartNewLine();
  572. }
  573. else currentLine.Add(currentOptiCursorPosition);
  574. programPresenter.UpdateCurrentLine(currentLine);
  575. if (optiTrackZ > WARNING_ZONE_BOUNDARY)
  576. {
  577. wristband.pushForward();
  578. }
  579. else if (optiTrackZ < -1 * WARNING_ZONE_BOUNDARY)
  580. {
  581. wristband.pushBackward();
  582. }
  583. }
  584. }
  585. else if( !optiTrackInUse && inDrawingMode)
  586. {
  587. //drawing without optitrack
  588. cursorPositions.Enqueue(currentCursorPosition);
  589. if (inDrawingMode && programPresenter.IsMousePressed())
  590. {
  591. currentLine.Add(currentCursorPosition);
  592. //programPresenter.UpdateCurrentLine(currentLine);
  593. }
  594. }
  595. //Deletion mode for optitrack and regular use
  596. if (!inDrawingMode)
  597. {
  598. List<Point> uncheckedPoints = new List<Point>();
  599. if (programPresenter.IsMousePressed() && !optiTrackInUse) //without optitrack
  600. uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  601. if(optiTrackInUse && CheckInsideDrawingZone(optiTrackZ) && !OptiMovingBack) //with optitrack
  602. uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousOptiCursorPosition, currentOptiCursorPosition);
  603. foreach (Point currPoint in uncheckedPoints)
  604. {
  605. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
  606. if (linesToDelete.Count > 0)
  607. {
  608. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
  609. foreach (int lineID in linesToDelete)
  610. {
  611. rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
  612. }
  613. RepopulateDeletionMatrixes();
  614. programPresenter.UpdateRightLines(rightLineList);
  615. }
  616. }
  617. }
  618. }
  619. /// <summary>
  620. /// If there is unsaved progress.
  621. /// </summary>
  622. /// <returns>True if there is progress that has not been saved.</returns>
  623. public bool HasUnsavedProgress()
  624. {
  625. return !historyOfActions.IsEmpty();
  626. }
  627. }
  628. }