InputProvider.cs 6.6 KB

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