MVP_Model.cs 27 KB

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