InputProvider.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using Iisu;
  8. namespace bbiwarg.Input.InputProviding
  9. {
  10. /// <summary>
  11. /// signature for the event that the device started
  12. /// </summary>
  13. /// <param name="sender">sender of the event</param>
  14. /// <param name="e">arguments of the event</param>
  15. public delegate void DeviceStartedEventHandler(object sender, EventArgs e);
  16. /// <summary>
  17. /// signature for the event that a new frame is available
  18. /// </summary>
  19. /// <param name="sender">sender of the event</param>
  20. /// <param name="e">arguments of the event</param>
  21. public delegate void NewFrameEventHandler(object sender, NewFrameEventArgs e);
  22. /// <summary>
  23. /// Encapsulates the arguments of the event that a new frame is available.
  24. /// </summary>
  25. public class NewFrameEventArgs : EventArgs
  26. {
  27. /// <summary>
  28. /// the id of the frame
  29. /// </summary>
  30. public int FrameID { get; private set; }
  31. /// <summary>
  32. /// the with of all images in the frame
  33. /// </summary>
  34. public int Width { get; private set; }
  35. /// <summary>
  36. /// the height of all images in the frame
  37. /// </summary>
  38. public int Height { get; private set; }
  39. /// <summary>
  40. /// pointer to the raw depth data for the frame
  41. /// </summary>
  42. public IntPtr RawDepthData { get; private set; }
  43. /// <summary>
  44. /// pointer to the raw confidence data for the frame
  45. /// </summary>
  46. public IntPtr RawConfidenceData { get; private set; }
  47. /// <summary>
  48. /// Constructs a NewFrameEventArgs.
  49. /// </summary>
  50. /// <param name="frameID">frame id</param>
  51. /// <param name="width">width of all images</param>
  52. /// <param name="height">height of all images</param>
  53. /// <param name="rawDepthData">pointer to raw depth data</param>
  54. /// <param name="rawConfidenceData">pointer to raw confidence data</param>
  55. public NewFrameEventArgs(int frameID, int width, int height, IntPtr rawDepthData, IntPtr rawConfidenceData)
  56. {
  57. FrameID = frameID;
  58. Width = width;
  59. Height = height;
  60. RawDepthData = rawDepthData;
  61. RawConfidenceData = rawConfidenceData;
  62. }
  63. }
  64. /// <summary>
  65. /// InputProvider provides the raw depth and confidence data through an event.
  66. /// </summary>
  67. public class InputProvider
  68. {
  69. /// <summary>
  70. /// iisu handle
  71. /// </summary>
  72. protected IHandle handle;
  73. /// <summary>
  74. /// iisu device from which the data is read
  75. /// </summary>
  76. protected IDevice device;
  77. /// <summary>
  78. /// paramter handle for the frame rate
  79. /// </summary>
  80. protected IParameterHandle<float> frameRate;
  81. /// <summary>
  82. /// parameter handle for the image width
  83. /// </summary>
  84. protected IParameterHandle<int> width;
  85. /// <summary>
  86. /// parameter handle for the image height
  87. /// </summary>
  88. protected IParameterHandle<int> height;
  89. /// <summary>
  90. /// parameter handle for the horizontal field of view angle
  91. /// </summary>
  92. protected IParameterHandle<float> hfov;
  93. /// <summary>
  94. /// parameter handle for the vertical field of view angle
  95. /// </summary>
  96. protected IParameterHandle<float> vfov;
  97. /// <summary>
  98. /// data handle for the raw depth data
  99. /// </summary>
  100. protected IDataHandle<Iisu.Data.IImageData> depthImage;
  101. /// <summary>
  102. /// data handle for the raw confidence data
  103. /// </summary>
  104. protected IDataHandle<Iisu.Data.IImageData> confidenceImage;
  105. /// <summary>
  106. /// the width of all images
  107. /// </summary>
  108. public int ImageWidth { get { return width.Value; } }
  109. /// <summary>
  110. /// the height of all images
  111. /// </summary>
  112. public int ImageHeight { get { return height.Value; } }
  113. /// <summary>
  114. /// the horizontal field of view angle
  115. /// </summary>
  116. public float HFOV { get { return hfov.Value; } }
  117. /// <summary>
  118. /// the vertical field of view angle
  119. /// </summary>
  120. public float VFOV { get { return vfov.Value; } }
  121. /// <summary>
  122. /// true iff the input source provides data
  123. /// </summary>
  124. public bool IsActive { get; private set; }
  125. /// <summary>
  126. /// the id of the current frame
  127. /// </summary>
  128. public virtual int CurrentFrameID { get { return device.FrameId; } }
  129. /// <summary>
  130. /// event that the device started
  131. /// </summary>
  132. public event DeviceStartedEventHandler DeviceStartedEvent;
  133. /// <summary>
  134. /// event that a new frame is available
  135. /// </summary>
  136. public event NewFrameEventHandler NewFrameEvent;
  137. /// <summary>
  138. /// Constructs an InputProvider.
  139. /// </summary>
  140. public InputProvider()
  141. {
  142. IsActive = false;
  143. }
  144. /// <summary>
  145. /// Initializes to device and data handles.
  146. /// </summary>
  147. public void initialize()
  148. {
  149. createDevice();
  150. registerHandles();
  151. }
  152. /// <summary>
  153. /// Starts the device.
  154. /// </summary>
  155. public void start()
  156. {
  157. device.Start();
  158. IsActive = true;
  159. if (DeviceStartedEvent != null)
  160. DeviceStartedEvent(this, new EventArgs());
  161. run();
  162. }
  163. /// <summary>
  164. /// Stops the device.
  165. /// </summary>
  166. public void stop()
  167. {
  168. IsActive = false;
  169. device.Stop(true);
  170. }
  171. /// <summary>
  172. /// Creates an iisu device which provides the data.
  173. /// </summary>
  174. protected void createDevice()
  175. {
  176. handle = Iisu.Iisu.Context.CreateHandle();
  177. IDeviceConfiguration conf = createDeviceConfiguration();
  178. device = handle.InitializeDevice(conf);
  179. }
  180. /// <summary>
  181. /// Returns an iisu device configuration.
  182. /// </summary>
  183. /// <returns>iisu device configuration</returns>
  184. protected virtual IDeviceConfiguration createDeviceConfiguration()
  185. {
  186. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  187. conf.IsAsynchronous = false;
  188. return conf;
  189. }
  190. /// <summary>
  191. /// Registers all parameter and data handles.
  192. /// </summary>
  193. protected virtual void registerHandles()
  194. {
  195. width = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Width");
  196. height = device.RegisterParameterHandle<int>("SOURCE.CAMERA.DEPTH.Height");
  197. hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
  198. vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
  199. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  200. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  201. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  202. }
  203. /// <summary>
  204. /// Provides the main loop for reading data from the device.
  205. /// </summary>
  206. protected virtual void run()
  207. {
  208. while (IsActive)
  209. nextFrame();
  210. }
  211. /// <summary>
  212. /// Gets the next frame from the device.
  213. /// </summary>
  214. protected virtual void nextFrame()
  215. {
  216. device.UpdateFrame(true);
  217. provideNewFrame();
  218. device.ReleaseFrame();
  219. }
  220. /// <summary>
  221. /// Triggers the new frame event.
  222. /// </summary>
  223. protected void provideNewFrame()
  224. {
  225. if (NewFrameEvent != null)
  226. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, ImageWidth, ImageHeight, depthImage.Value.Raw, confidenceImage.Value.Raw));
  227. }
  228. }
  229. }