MVP_Model.cs 29 KB

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