GlassesWindow.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using bbiwarg.Input.InputHandling;
  11. using bbiwarg.Input.InputProviding;
  12. using bbiwarg.Utility;
  13. using bbiwarg.Recognition.FingerRecognition;
  14. using bbiwarg.Recognition.PalmRecognition;
  15. using Emgu.CV.UI;
  16. namespace bbiwarg.Output.GlassesOutput
  17. {
  18. /// <summary>
  19. /// A Windows Form which displays a fullscreen window to be shown on augmented reality glasses.
  20. /// </summary>
  21. public partial class GlassesWindow : Form
  22. {
  23. /// <summary>
  24. /// the input provider
  25. /// </summary>
  26. private InputProvider inputProvider;
  27. /// <summary>
  28. /// the input handler
  29. /// </summary>
  30. private InputHandler inputHandler;
  31. /// <summary>
  32. /// timer to periodically update the window
  33. /// </summary>
  34. private System.Windows.Forms.Timer timer;
  35. /// <summary>
  36. /// id of the current frame
  37. /// </summary>
  38. private int currentFrameID;
  39. /// <summary>
  40. /// true iff the window is showing the latest data
  41. /// </summary>
  42. private bool guiUpToDate;
  43. /// <summary>
  44. /// size of the input images
  45. /// </summary>
  46. private ImageSize inputSize;
  47. /// <summary>
  48. /// size of the image show on the glasses
  49. /// </summary>
  50. private ImageSize outputSize;
  51. /// <summary>
  52. /// the image shown on the glasses
  53. /// </summary>
  54. private OutputImage image;
  55. /// <summary>
  56. /// projection from the input image plane to a plane which lies in front of the wearer of the glasses (output plane)
  57. /// </summary>
  58. private Projection2DTo2D projection;
  59. /// <summary>
  60. /// position of the current calibration point in the output plane
  61. /// </summary>
  62. private Vector2D currentCalibrationPoint;
  63. /// <summary>
  64. /// true iff the calibration image has the latest data
  65. /// </summary>
  66. private bool calibrationImageUpToDate;
  67. /// <summary>
  68. /// random number generator
  69. /// </summary>
  70. private Random rand;
  71. /// <summary>
  72. /// Creates a GlassesWindow.
  73. /// </summary>
  74. /// <param name="inputProvider">input provider</param>
  75. /// <param name="inputHandler">input handler</param>
  76. /// <param name="name">title of the window</param>
  77. /// <param name="screen">the screen this window is shown on</param>
  78. /// <param name="updateInterval">the update interval for the window in ms</param>
  79. public GlassesWindow(InputProvider inputProvider, InputHandler inputHandler, String name, Screen screen, int updateInterval)
  80. {
  81. InitializeComponent();
  82. this.inputProvider = inputProvider;
  83. this.inputHandler = inputHandler;
  84. this.inputSize = inputHandler.ImageSize;
  85. this.outputSize = new ImageSize(screen.Bounds.Width, screen.Bounds.Height);
  86. guiUpToDate = false;
  87. calibrationImageUpToDate = false;
  88. rand = new Random();
  89. currentCalibrationPoint = getRandomOutputPoint();
  90. projection = new Projection2DTo2D(inputSize, outputSize, Parameters.GlassesWindowNumCalibrationPoints);
  91. Name = name;
  92. Text = name;
  93. timer = new System.Windows.Forms.Timer();
  94. timer.Interval = updateInterval;
  95. timer.Tick += update;
  96. timer.Start();
  97. KeyPreview = true;
  98. //fullscreen
  99. FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  100. Location = screen.Bounds.Location;
  101. Size = screen.Bounds.Size;
  102. }
  103. /// <summary>
  104. /// Stops the input provider when closing the window.
  105. /// </summary>
  106. /// <param name="e">event arguments</param>
  107. protected override void OnClosing(CancelEventArgs e)
  108. {
  109. base.OnClosing(e);
  110. inputProvider.stop();
  111. }
  112. /// <summary>
  113. /// Updates the window.
  114. /// </summary>
  115. /// <param name="sender">the event sender</param>
  116. /// <param name="e">the event arguments</param>
  117. private void update(object sender, EventArgs e)
  118. {
  119. Utility.Timer.start("GlassesWindow.update");
  120. if (!inputProvider.IsActive)
  121. Close();
  122. if (projection.IsCalibrated)
  123. {
  124. FrameData frameData = inputHandler.FrameData;
  125. if (frameData != null)
  126. {
  127. lock (frameData)
  128. {
  129. if (currentFrameID != frameData.FrameID)
  130. {
  131. currentFrameID = frameData.FrameID;
  132. Utility.Timer.start("GlassesWindow.update::updateImage");
  133. updateImage(frameData);
  134. Utility.Timer.stop("GlassesWindow.update::updateImage");
  135. }
  136. }
  137. }
  138. }
  139. else
  140. {
  141. if (!calibrationImageUpToDate)
  142. updateCalibrationImage();
  143. }
  144. if (!guiUpToDate)
  145. {
  146. Utility.Timer.start("GlassesWindow.update::updateGUI");
  147. updateGUI();
  148. Utility.Timer.stop("GlassesWindow.update::updateGUI");
  149. }
  150. Utility.Timer.stop("GlassesWindow.update");
  151. }
  152. /// <summary>
  153. /// Updates the glasses image.
  154. /// </summary>
  155. /// <param name="frameData">data for the new frame</param>
  156. private void updateImage(FrameData frameData)
  157. {
  158. guiUpToDate = false;
  159. if (image != null)
  160. image.Dispose();
  161. image = new OutputImage(outputSize);
  162. foreach (Palm palm in frameData.TrackedPalms)
  163. {
  164. Quadrangle quadInput = palm.Quad;
  165. Vector2D a = projection.projectPoint(quadInput.TopLeft);
  166. Vector2D b = projection.projectPoint(quadInput.TopRight);
  167. Vector2D c = projection.projectPoint(quadInput.BottomRight);
  168. Vector2D d = projection.projectPoint(quadInput.BottomLeft);
  169. Quadrangle quadOutput = new Quadrangle(a, b, c, d);
  170. image.drawQuadrangleGrid(quadOutput, Color.Yellow, Color.Orange, 3, 4);
  171. }
  172. foreach (Finger finger in frameData.TrackedFingers)
  173. {
  174. Vector2D tipProjected = projection.projectPoint(finger.TipPoint);
  175. image.fillCircle(tipProjected, 10, Color.Yellow);
  176. }
  177. }
  178. /// <summary>
  179. /// Updates the calibration image.
  180. /// </summary>
  181. private void updateCalibrationImage()
  182. {
  183. guiUpToDate = false;
  184. if (image != null)
  185. image.Dispose();
  186. image = new OutputImage(outputSize);
  187. image.fillCircle(currentCalibrationPoint, 25, Color.Orange);
  188. }
  189. /// <summary>
  190. /// Updates the gui elements.
  191. /// </summary>
  192. private void updateGUI()
  193. {
  194. // update image boxes
  195. imageBox.Image = image;
  196. guiUpToDate = true;
  197. }
  198. /// <summary>
  199. /// Handle key down events by showing the next point or finishing or resetting the calibration.
  200. /// </summary>
  201. /// <param name="sender">event sender</param>
  202. /// <param name="e">event arguments</param>
  203. private void GlassesWindow_OnKeyDown(object sender, KeyEventArgs e)
  204. {
  205. if (e.KeyCode == Keys.K)
  206. {
  207. FrameData frameData = inputHandler.FrameData;
  208. if (frameData != null)
  209. {
  210. lock (frameData)
  211. {
  212. if (frameData.TrackedFingers.Count == 1)
  213. {
  214. Vector2D pointOutput = currentCalibrationPoint;
  215. Vector2D pointInput = frameData.TrackedFingers[0].TipPoint;
  216. projection.addCalibrationPoints(pointInput, pointOutput);
  217. currentCalibrationPoint = getRandomOutputPoint();
  218. }
  219. }
  220. }
  221. }
  222. else if (e.KeyCode == Keys.R)
  223. {
  224. projection.reset();
  225. currentCalibrationPoint = getRandomOutputPoint();
  226. }
  227. }
  228. /// <summary>
  229. /// Returns a random point in the glasses image.
  230. /// </summary>
  231. /// <returns></returns>
  232. private Vector2D getRandomOutputPoint()
  233. {
  234. return outputSize.getAbsolutePoint(new Vector2D((float)rand.NextDouble(), (float)rand.NextDouble()));
  235. }
  236. }
  237. }