MVP_Model.cs 31 KB

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