IisuDataSource.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. private IDataHandle<Iisu.Data.IImageData> confidenceImage;
  27. private IDataHandle<Iisu.Data.Vertex[]> vertexArray;
  28. /*
  29. * Creates an Iisu data source.
  30. * params:
  31. * moviePath: path to movie to be used as source
  32. * if empty the camera is used
  33. */
  34. public IIsuDataSource(string moviePath = "")
  35. {
  36. this.moviePath = moviePath;
  37. active = false;
  38. }
  39. public void init()
  40. {
  41. handle = Iisu.Iisu.Context.CreateHandle();
  42. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  43. if (moviePath.Length != 0)
  44. conf.MoviePath = moviePath;
  45. device = handle.InitializeDevice(conf);
  46. // parameters
  47. if (moviePath.Length != 0)
  48. device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
  49. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  50. // events
  51. device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
  52. // data
  53. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  54. colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
  55. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  56. vertexArray = device.RegisterDataHandle<Iisu.Data.Vertex[]>("SCENE.VertexArray");
  57. handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
  58. handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
  59. palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
  60. palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
  61. tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
  62. tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
  63. forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
  64. forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
  65. palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
  66. palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
  67. fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
  68. fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
  69. fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
  70. fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
  71. handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  72. handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  73. }
  74. private void onDeviceStatusChanged(string eventName, DeviceStatus status)
  75. {
  76. active = status.HasFlag(Iisu.DeviceStatus.Playing);
  77. }
  78. public void start()
  79. {
  80. device.Start();
  81. }
  82. public void stop()
  83. {
  84. device.Stop(true);
  85. }
  86. public void updateFrame()
  87. {
  88. device.UpdateFrame(true);
  89. }
  90. public void releaseFrame()
  91. {
  92. device.ReleaseFrame();
  93. }
  94. public bool isActive()
  95. {
  96. return active;
  97. }
  98. public int getFrameRate()
  99. {
  100. return (int) frameRate.Value;
  101. }
  102. public DepthImage getDepthImage()
  103. {
  104. Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
  105. int width = (int) imageInfos.Width;
  106. int height = (int) imageInfos.Height;
  107. int numBytes = (int) imageInfos.BytesRaw;
  108. IntPtr imageData = depthImage.Value.Raw;
  109. short[] depthData = new short[width * height];
  110. Marshal.Copy(imageData, depthData, 0, width * height);
  111. return new DepthImage(width, height, depthData);
  112. }
  113. public ColorImage getColorImage()
  114. {
  115. Iisu.Data.IImageInfos imageInfos = colorImage.Value.ImageInfos;
  116. int width = (int)imageInfos.Width;
  117. int height = (int)imageInfos.Height;
  118. int numBytes = (int)imageInfos.BytesRaw;
  119. IntPtr imageData = colorImage.Value.Raw;
  120. byte[] colorData = new byte[numBytes];
  121. Marshal.Copy(imageData, colorData, 0, numBytes);
  122. return new ColorImage(width, height, colorData);
  123. }
  124. public ConfidenceImage getConfidenceImage()
  125. {
  126. Iisu.Data.IImageInfos imageInfos = confidenceImage.Value.ImageInfos;
  127. int width = (int)imageInfos.Width;
  128. int height = (int)imageInfos.Height;
  129. int numBytes = (int)imageInfos.BytesRaw;
  130. IntPtr imageData = confidenceImage.Value.Raw;
  131. short[] confidenceData = new short[width * height];
  132. Marshal.Copy(imageData, confidenceData, 0, width * height);
  133. return new ConfidenceImage(width, height, confidenceData);
  134. }
  135. public VertexArray getVertexArray()
  136. {
  137. int numVertices = vertexArray.Value.Length;
  138. Vector[] positions = new Vector[numVertices];
  139. Color4[] colors = new Color4[numVertices];
  140. for (int i = 0; i < numVertices; ++i)
  141. {
  142. positions[i] = new DenseVector(vertexArray.Value[i].Position.ToArray());
  143. Iisu.Data.Color4C color = vertexArray.Value[i].Color;
  144. colors[i] = new Color4(color.A, color.R, color.G, color.B);
  145. }
  146. return new VertexArray(positions, colors);
  147. }
  148. private void checkHandIndex(uint handIndex)
  149. {
  150. if (handIndex < 1 || handIndex > 2)
  151. throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
  152. }
  153. public bool isHandOpen(uint handIndex)
  154. {
  155. checkHandIndex(handIndex);
  156. return handOpen[handIndex - 1].Value;
  157. }
  158. public Vector getPalmPosition3D(uint handIndex)
  159. {
  160. checkHandIndex(handIndex);
  161. return new DenseVector(palmPositions3D[handIndex - 1].Value.ToArray());
  162. }
  163. public Vector getTipPosition3D(uint handIndex)
  164. {
  165. checkHandIndex(handIndex);
  166. return new DenseVector(tipPositions3D[handIndex - 1].Value.ToArray());
  167. }
  168. public Vector getForearmPosition3D(uint handIndex)
  169. {
  170. checkHandIndex(handIndex);
  171. return new DenseVector(forearmPositions3D[handIndex - 1].Value.ToArray());
  172. }
  173. public Vector getPalmNormal3D(uint handIndex)
  174. {
  175. checkHandIndex(handIndex);
  176. return new DenseVector(palmNormals3D[handIndex - 1].Value.ToArray());
  177. }
  178. public FingerStatus[] getFingerStatus(uint handIndex)
  179. {
  180. checkHandIndex(handIndex);
  181. int[] status = fingerStatus[handIndex - 1].Value;
  182. FingerStatus[] result = new FingerStatus[status.Length];
  183. for (int i = 0; i < status.Length; ++i)
  184. {
  185. switch (status[i])
  186. {
  187. case 0:
  188. result[i] = FingerStatus.Inactive;
  189. break;
  190. case 1:
  191. result[i] = FingerStatus.Detected;
  192. break;
  193. case 2:
  194. result[i] = FingerStatus.Tracked;
  195. break;
  196. }
  197. }
  198. return result;
  199. }
  200. public Vector[] getFingerTipPositions3D(uint handIndex)
  201. {
  202. checkHandIndex(handIndex);
  203. Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
  204. Vector[] results = new DenseVector[positions.Length];
  205. for (int i = 0; i < positions.Length; ++i)
  206. results[i] = new DenseVector(positions[i].ToArray());
  207. return results;
  208. }
  209. public HandSide getHandSide(uint handIndex)
  210. {
  211. checkHandIndex(handIndex);
  212. int side = handSides[handIndex - 1].Value;
  213. switch (side)
  214. {
  215. case 1:
  216. return HandSide.Left;
  217. case 2:
  218. return HandSide.Right;
  219. default:
  220. return HandSide.Unknown;
  221. }
  222. }
  223. }
  224. }