MVP_Model.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. /// 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
  34. /// </summary>
  35. readonly double WARNING_ZONE_BOUNDARY = 0.10; //10cm
  36. /// <summary>
  37. /// If the program is in drawing mode.
  38. /// </summary>
  39. public bool inDrawingMode { get; private set; }
  40. /// <summary>
  41. /// if the program is using OptiTrack
  42. /// </summary>
  43. public bool optiTrackInUse { get; private set; }
  44. /// <summary>
  45. /// Size of deletion area
  46. /// </summary>
  47. int deletionRadius = 5;
  48. /// <summary>
  49. /// The Position of the Cursor in the right picture box
  50. /// </summary>
  51. Point currentCursorPosition;
  52. /// <summary>
  53. /// The Previous Cursor Position in the right picture box
  54. /// </summary>
  55. Point previousCursorPosition;
  56. /// <summary>
  57. /// Queue for the cursorPositions
  58. /// </summary>
  59. Queue<Point> cursorPositions = new Queue<Point>();
  60. /// <summary>
  61. /// The Position of the Cursor of opti track
  62. /// </summary>
  63. Point currentOptiCursorPosition;
  64. /// <summary>
  65. /// The Previous Cursor Position of opti track
  66. /// </summary>
  67. Point previousOptiCursorPosition;
  68. /// <summary>
  69. /// Queue for the cursorPositions of opti track
  70. /// </summary>
  71. Queue<Point> optiCursorPositions = new Queue<Point>();
  72. /// <summary>
  73. /// Lookup Matrix for checking postions of lines in the image
  74. /// </summary>
  75. bool[,] isFilledMatrix;
  76. /// <summary>
  77. /// Lookup Matrix for getting line ids at a certain postions of the image
  78. /// </summary>
  79. HashSet<int>[,] linesMatrix;
  80. /// <summary>
  81. /// Width of the LeftImageBox.
  82. /// </summary>
  83. public int leftImageBoxWidth;
  84. /// <summary>
  85. /// Height of the LeftImageBox.
  86. /// </summary>
  87. public int leftImageBoxHeight;
  88. /// <summary>
  89. /// Width of the RightImageBox.
  90. /// </summary>
  91. public int rightImageBoxWidth;
  92. /// <summary>
  93. /// Height of the RightImageBox.
  94. /// </summary>
  95. public int rightImageBoxHeight;
  96. /// <summary>
  97. /// The size of the right canvas.
  98. /// </summary>
  99. public ImageDimension rightImageSize { get; private set; }
  100. /// <summary>
  101. /// Indicates whether or not the canvas on the right side is active.
  102. /// </summary>
  103. public bool canvasActive { get; set; }
  104. /// <summary>
  105. /// Indicates if there is a graphic loaded in the left canvas.
  106. /// </summary>
  107. public bool graphicLoaded { get; set; }
  108. /// <summary>
  109. /// Whether or not an optitrack system is avaiable.
  110. /// </summary>
  111. public bool optitrackAvailable { get; private set; }
  112. /// <summary>
  113. /// 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.
  114. /// </summary>
  115. public float optiTrackX;
  116. /// <summary>
  117. /// y coordinate in real world. one unit is one meter. If standing in front of video wall, moving up results in incrementation of y.
  118. /// </summary>
  119. public float optiTrackY;
  120. /// <summary>
  121. /// z coordinate in real world. one unit is one meter. If standing in front of video wall, moving back results in incrementation of y.
  122. /// </summary>
  123. public float optiTrackZ;
  124. /// <summary>
  125. /// keeps track of whether last tick the trackable was inside drawing zone or not.
  126. /// </summary>
  127. private bool optiTrackInsideDrawingZone = false;
  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. //TODO: 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. optiCursorPositions.Enqueue(currentOptiCursorPosition);
  332. }
  333. /********************************************/
  334. /*** FUNCTIONS TO INTERACT WITH PRESENTER ***/
  335. /********************************************/
  336. /// <summary>
  337. /// A function to update the dimensions of the left and right canvas when the window is resized.
  338. /// </summary>
  339. /// <param name="RightCanvas">The size of the right canvas.</param>
  340. public void ResizeEvent(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">The status of optitrack button</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. }
  460. else
  461. {
  462. //Enable optipoint
  463. programPresenter.SetOverlayStatus("optipoint", true, currentCursorPosition);
  464. }
  465. }
  466. /// <summary>
  467. /// Updates the current cursor position of the model.
  468. /// </summary>
  469. /// <param name="p">The new cursor position</param>
  470. public void SetCurrentCursorPosition(Point p)
  471. {
  472. currentCursorPosition = p;
  473. mouseDown = programPresenter.IsMousePressed();
  474. }
  475. /// <summary>
  476. /// Start a new Line, when the Mouse is pressed down.
  477. /// </summary>
  478. public void StartNewLine()
  479. {
  480. mouseDown = true;
  481. if (inDrawingMode)
  482. {
  483. if(optiTrackInUse)
  484. {
  485. currentLine.Clear();
  486. currentLine.Add(currentOptiCursorPosition);
  487. }
  488. else if (programPresenter.IsMousePressed())
  489. {
  490. currentLine.Clear();
  491. currentLine.Add(currentCursorPosition);
  492. }
  493. }
  494. }
  495. /// <summary>
  496. /// Finish the current Line, when the pressed Mouse is released.
  497. /// </summary>
  498. /// <param name="valid">Whether the up event is valid or not</param>
  499. public void FinishCurrentLine(bool valid)
  500. {
  501. mouseDown = false;
  502. if (valid)
  503. {
  504. if (inDrawingMode && currentLine.Count > 0)
  505. {
  506. InternalLine newLine = new InternalLine(currentLine, rightLineList.Count);
  507. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  508. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  509. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  510. //TODO: For the person implementing overlay: Add check if overlay needs to be added
  511. programPresenter.UpdateRightLines(rightLineList);
  512. currentLine.Clear();
  513. programPresenter.UpdateCurrentLine(currentLine);
  514. }
  515. }
  516. else
  517. {
  518. currentLine.Clear();
  519. }
  520. UpdateUI();
  521. }
  522. /// <summary>
  523. /// Finish the current Line, when the pressed Mouse is released.
  524. /// Overload that is used to pass a list of points to be used when one is available.
  525. /// </summary>
  526. /// <param name="p">The list of points</param>
  527. public void FinishCurrentLine(List<Point> p)
  528. {
  529. mouseDown = false;
  530. if (inDrawingMode && currentLine.Count > 0)
  531. {
  532. InternalLine newLine = new InternalLine(p, rightLineList.Count);
  533. rightLineList.Add(new Tuple<bool, InternalLine>(true, newLine));
  534. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  535. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Draw, newLine.GetID())));
  536. programPresenter.UpdateRightLines(rightLineList);
  537. currentLine.Clear();
  538. }
  539. UpdateUI();
  540. }
  541. /// <summary>
  542. /// Method to be called every tick. Updates the current Line, or checks for Lines to delete, depending on the drawing mode.
  543. /// </summary>
  544. public void Tick()
  545. {
  546. if (cursorPositions.Count > 0) { previousCursorPosition = cursorPositions.Dequeue(); }
  547. else { previousCursorPosition = currentCursorPosition; }
  548. if(optitrackAvailable)
  549. SetCurrentFingerPosition(new Point(optiTrackX, optiTrackY));
  550. if (optiTrackInUse && inDrawingMode && !OptiMovingBack) // optitrack is being used
  551. {
  552. //outside of drawing zone
  553. if (!CheckInsideDrawingZone(optiTrackZ))
  554. {
  555. //Check if trackable was in drawing zone last tick & program is in drawing mode-> finish line
  556. if (optiTrackInsideDrawingZone && inDrawingMode)
  557. {
  558. optiTrackInsideDrawingZone = false;
  559. FinishCurrentLine(true);
  560. }
  561. }
  562. else //Draw with optitrack, when in drawing zone
  563. {
  564. //Optitrack wasn't in the drawing zone last tick -> start a new line
  565. if (!optiTrackInsideDrawingZone)
  566. {
  567. optiTrackInsideDrawingZone = true;
  568. StartNewLine();
  569. }
  570. else currentLine.Add(currentOptiCursorPosition);
  571. programPresenter.UpdateCurrentLine(currentLine);
  572. if (optiTrackZ > WARNING_ZONE_BOUNDARY)
  573. {
  574. wristband.PushForward();
  575. }
  576. else if (optiTrackZ < -1 * WARNING_ZONE_BOUNDARY)
  577. {
  578. wristband.PushBackward();
  579. }
  580. }
  581. }
  582. else if( !optiTrackInUse && inDrawingMode)
  583. {
  584. //drawing without optitrack
  585. cursorPositions.Enqueue(currentCursorPosition);
  586. if (inDrawingMode && programPresenter.IsMousePressed())
  587. {
  588. currentLine.Add(currentCursorPosition);
  589. //programPresenter.UpdateCurrentLine(currentLine);
  590. }
  591. }
  592. //Deletion mode for optitrack and regular use
  593. if (!inDrawingMode)
  594. {
  595. List<Point> uncheckedPoints = new List<Point>();
  596. if (programPresenter.IsMousePressed() && !optiTrackInUse) //without optitrack
  597. uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  598. if(optiTrackInUse && CheckInsideDrawingZone(optiTrackZ) && !OptiMovingBack) //with optitrack
  599. uncheckedPoints = GeometryCalculator.BresenhamLineAlgorithm(previousOptiCursorPosition, currentOptiCursorPosition);
  600. foreach (Point currPoint in uncheckedPoints)
  601. {
  602. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionRadius);
  603. if (linesToDelete.Count > 0)
  604. {
  605. programPresenter.PassLastActionTaken(historyOfActions.AddNewAction(new SketchAction(SketchAction.ActionType.Delete, linesToDelete)));
  606. foreach (int lineID in linesToDelete)
  607. {
  608. rightLineList[lineID] = new Tuple<bool, InternalLine>(false, rightLineList[lineID].Item2);
  609. }
  610. RepopulateDeletionMatrixes();
  611. programPresenter.UpdateRightLines(rightLineList);
  612. }
  613. }
  614. }
  615. }
  616. /// <summary>
  617. /// If there is unsaved progress.
  618. /// </summary>
  619. /// <returns>True if there is progress that has not been saved.</returns>
  620. public bool HasUnsavedProgress()
  621. {
  622. return !historyOfActions.IsEmpty();
  623. }
  624. }
  625. }