MVP_Model.cs 30 KB

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