IisuDataSource.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Iisu;
  4. using MathNet.Numerics.LinearAlgebra.Single;
  5. namespace bbiwarg.DataSource
  6. {
  7. class IIsuDataSource: IVideoDataSource
  8. {
  9. private IHandle handle;
  10. private IDevice device;
  11. private string moviePath;
  12. bool active;
  13. // parameters
  14. private IParameterHandle<float> frameRate;
  15. // data
  16. private IDataHandle<bool>[] handOpen = new IDataHandle<bool>[2];
  17. private IDataHandle<Iisu.Data.Vector3>[] palmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  18. private IDataHandle<Iisu.Data.Vector3>[] tipPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  19. private IDataHandle<Iisu.Data.Vector3>[] forearmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  20. private IDataHandle<Iisu.Data.Vector3>[] palmNormals3D = new IDataHandle<Iisu.Data.Vector3>[2];
  21. private IDataHandle<int[]>[] fingerStatus = new IDataHandle<int[]>[2];
  22. private IDataHandle<Iisu.Data.Vector3[]>[] fingerTipPositions3D = new IDataHandle<Iisu.Data.Vector3[]>[2];
  23. private IDataHandle<int>[] handSides = new IDataHandle<int>[2];
  24. private IDataHandle<Iisu.Data.IImageData> depthImage;
  25. private IDataHandle<Iisu.Data.IImageData> colorImage;
  26. /*
  27. * Creates an Iisu data source.
  28. * params:
  29. * moviePath: path to movie to be used as source
  30. * if empty the camera is used
  31. */
  32. public IIsuDataSource(string moviePath = "")
  33. {
  34. this.moviePath = moviePath;
  35. active = false;
  36. }
  37. public void init()
  38. {
  39. handle = Iisu.Iisu.Context.CreateHandle();
  40. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  41. if (moviePath.Length != 0)
  42. conf.MoviePath = moviePath;
  43. device = handle.InitializeDevice(conf);
  44. // parameters
  45. device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
  46. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  47. // events
  48. device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
  49. // data
  50. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  51. colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
  52. handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
  53. handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
  54. palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
  55. palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
  56. tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
  57. tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
  58. forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
  59. forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
  60. palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
  61. palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
  62. fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
  63. fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
  64. fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
  65. fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
  66. handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  67. handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  68. }
  69. private void onDeviceStatusChanged(string eventName, DeviceStatus status)
  70. {
  71. active = status.HasFlag(Iisu.DeviceStatus.Playing);
  72. }
  73. public void start()
  74. {
  75. device.Start();
  76. }
  77. public void stop()
  78. {
  79. device.Stop(true);
  80. }
  81. public void updateFrame()
  82. {
  83. device.UpdateFrame(true);
  84. }
  85. public void releaseFrame()
  86. {
  87. device.ReleaseFrame();
  88. }
  89. public bool isActive()
  90. {
  91. return active;
  92. }
  93. public int getFrameRate()
  94. {
  95. return (int) frameRate.Value;
  96. }
  97. public DepthImage getDepthImage()
  98. {
  99. Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
  100. int width = (int) imageInfos.Width;
  101. int height = (int) imageInfos.Height;
  102. int numBytes = (int) imageInfos.BytesRaw;
  103. IntPtr imageData = depthImage.Value.Raw;
  104. short[] depthData = new short[width * height];
  105. Marshal.Copy(imageData, depthData, 0, width * height);
  106. return new DepthImage(width, height, depthData);
  107. }
  108. public ColorImage getColorImage()
  109. {
  110. Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
  111. int width = (int)imageInfos.Width;
  112. int height = (int)imageInfos.Height;
  113. int numBytes = (int)imageInfos.BytesRaw;
  114. Console.WriteLine("width: " + width + " height: " + height + " numBytes: " + numBytes);
  115. return new ColorImage();
  116. }
  117. private void checkHandIndex(uint handIndex)
  118. {
  119. if (handIndex < 1 || handIndex > 2)
  120. throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
  121. }
  122. public bool isHandOpen(uint handIndex)
  123. {
  124. checkHandIndex(handIndex);
  125. return handOpen[handIndex - 1].Value;
  126. }
  127. public Vector getPalmPosition3D(uint handIndex)
  128. {
  129. checkHandIndex(handIndex);
  130. return new DenseVector(palmPositions3D[handIndex - 1].Value.ToArray());
  131. }
  132. public Vector getTipPosition3D(uint handIndex)
  133. {
  134. checkHandIndex(handIndex);
  135. return new DenseVector(tipPositions3D[handIndex - 1].Value.ToArray());
  136. }
  137. public Vector getForearmPosition3D(uint handIndex)
  138. {
  139. checkHandIndex(handIndex);
  140. return new DenseVector(forearmPositions3D[handIndex - 1].Value.ToArray());
  141. }
  142. public Vector getPalmNormal3D(uint handIndex)
  143. {
  144. checkHandIndex(handIndex);
  145. return new DenseVector(palmNormals3D[handIndex - 1].Value.ToArray());
  146. }
  147. public FingerStatus[] getFingerStatus(uint handIndex)
  148. {
  149. checkHandIndex(handIndex);
  150. int[] status = fingerStatus[handIndex - 1].Value;
  151. FingerStatus[] result = new FingerStatus[status.Length];
  152. for (int i = 0; i < status.Length; ++i)
  153. {
  154. switch (status[i])
  155. {
  156. case 0:
  157. result[i] = FingerStatus.Inactive;
  158. break;
  159. case 1:
  160. result[i] = FingerStatus.Detected;
  161. break;
  162. case 2:
  163. result[i] = FingerStatus.Tracked;
  164. break;
  165. }
  166. }
  167. return result;
  168. }
  169. public Vector[] getFingerTipPositions3D(uint handIndex)
  170. {
  171. checkHandIndex(handIndex);
  172. Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
  173. Vector[] results = new DenseVector[positions.Length];
  174. for (int i = 0; i < positions.Length; ++i)
  175. results[i] = new DenseVector(positions[i].ToArray());
  176. return results;
  177. }
  178. public HandSide getHandSide(uint handIndex)
  179. {
  180. checkHandIndex(handIndex);
  181. int side = handSides[handIndex - 1].Value;
  182. switch (side)
  183. {
  184. case 1:
  185. return HandSide.Left;
  186. case 2:
  187. return HandSide.Right;
  188. default:
  189. return HandSide.Unknown;
  190. }
  191. }
  192. }
  193. }