IisuDataSource.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using Iisu;
  5. using MathNet.Numerics.LinearAlgebra.Single;
  6. using bbiwarg.Graphics;
  7. namespace bbiwarg.DataSource
  8. {
  9. class IIsuDataSource: IVideoDataSource
  10. {
  11. private IHandle handle;
  12. private IDevice device;
  13. private string moviePath;
  14. bool active;
  15. // parameters
  16. private IParameterHandle<float> frameRate;
  17. // data
  18. private IDataHandle<bool>[] handOpen = new IDataHandle<bool>[2];
  19. private IDataHandle<Iisu.Data.Vector3>[] palmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  20. private IDataHandle<Iisu.Data.Vector2>[] palmPositions2D = new IDataHandle<Iisu.Data.Vector2>[2];
  21. private IDataHandle<Iisu.Data.Vector3>[] tipPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  22. private IDataHandle<Iisu.Data.Vector3>[] forearmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  23. private IDataHandle<Iisu.Data.Vector3>[] palmNormals3D = new IDataHandle<Iisu.Data.Vector3>[2];
  24. private IDataHandle<int[]>[] fingerStatus = new IDataHandle<int[]>[2];
  25. private IDataHandle<int>[] handStatus = new IDataHandle<int>[2];
  26. private IDataHandle<Iisu.Data.Vector3[]>[] fingerTipPositions3D = new IDataHandle<Iisu.Data.Vector3[]>[2];
  27. private IDataHandle<Iisu.Data.Vector2[]>[] fingerTipPositions2D = new IDataHandle<Iisu.Data.Vector2[]>[2];
  28. private IDataHandle<int>[] handSides = new IDataHandle<int>[2];
  29. private IDataHandle<Iisu.Data.IImageData> depthImage;
  30. private IDataHandle<Iisu.Data.IImageData> colorImage;
  31. private IDataHandle<Iisu.Data.IImageData> confidenceImage;
  32. private IDataHandle<Iisu.Data.IImageData> uvImage;
  33. private IParameterHandle<float> hfov;
  34. private IParameterHandle<float> vfov;
  35. private ImageData currentImage;
  36. /*
  37. * Creates an Iisu data source.
  38. * params:
  39. * moviePath: path to movie to be used as source
  40. * if empty the camera is used
  41. */
  42. public IIsuDataSource(string moviePath = "")
  43. {
  44. this.moviePath = moviePath;
  45. active = false;
  46. }
  47. public void init()
  48. {
  49. handle = Iisu.Iisu.Context.CreateHandle();
  50. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  51. if (moviePath.Length != 0)
  52. conf.MoviePath = moviePath;
  53. device = handle.InitializeDevice(conf);
  54. // parameters
  55. if (moviePath.Length != 0)
  56. device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
  57. else
  58. device.RegisterParameterHandle<int>("SOURCE.DEPTHSENSE.AmplitudeThreshold").Value = 100; // confidence-threshhold
  59. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  60. hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
  61. vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
  62. // events
  63. device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
  64. // data
  65. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  66. colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
  67. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  68. uvImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.REGISTRATION.UV.Image");
  69. handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
  70. handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
  71. palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
  72. palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
  73. palmPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND1.PalmPosition2D");
  74. palmPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND2.PalmPosition2D");
  75. tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
  76. tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
  77. forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
  78. forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
  79. palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
  80. palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
  81. fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
  82. fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
  83. handStatus[0] = device.RegisterDataHandle<int>("CI.HAND1.Status");
  84. handStatus[1] = device.RegisterDataHandle<int>("CI.HAND2.Status");
  85. fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
  86. fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
  87. fingerTipPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND1.FingerTipPositions2D");
  88. fingerTipPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND2.FingerTipPositions2D");
  89. handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  90. handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  91. }
  92. private void onDeviceStatusChanged(string eventName, DeviceStatus status)
  93. {
  94. active = status.HasFlag(Iisu.DeviceStatus.Playing);
  95. }
  96. public void start()
  97. {
  98. device.Start();
  99. updateFrame();
  100. }
  101. public void stop()
  102. {
  103. device.Stop(true);
  104. }
  105. public void updateFrame()
  106. {
  107. device.UpdateFrame(true);
  108. createImageData();
  109. }
  110. public void releaseFrame()
  111. {
  112. device.ReleaseFrame();
  113. }
  114. public bool isActive()
  115. {
  116. return active;
  117. }
  118. public int getFrameRate()
  119. {
  120. return (int) frameRate.Value;
  121. }
  122. public float getHFOV() {
  123. return hfov.Value;
  124. }
  125. public float getVFOV() {
  126. return vfov.Value;
  127. }
  128. public DepthImage getDepthImage()
  129. {
  130. Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
  131. int width = (int) imageInfos.Width;
  132. int height = (int) imageInfos.Height;
  133. int numBytes = (int) imageInfos.BytesRaw;
  134. IntPtr imageData = depthImage.Value.Raw;
  135. short[] depthData = new short[width * height];
  136. Marshal.Copy(imageData, depthData, 0, width * height);
  137. return new DepthImage(width, height, depthData);
  138. }
  139. public ColorImage getColorImage()
  140. {
  141. Iisu.Data.IImageInfos imageInfos = colorImage.Value.ImageInfos;
  142. int width = (int)imageInfos.Width;
  143. int height = (int)imageInfos.Height;
  144. int numBytes = (int)imageInfos.BytesRaw;
  145. IntPtr imageData = colorImage.Value.Raw;
  146. byte[] colorData = new byte[numBytes];
  147. Marshal.Copy(imageData, colorData, 0, numBytes);
  148. return new ColorImage(width, height, colorData);
  149. }
  150. public ConfidenceImage getConfidenceImage()
  151. {
  152. Iisu.Data.IImageInfos imageInfos = confidenceImage.Value.ImageInfos;
  153. int width = (int)imageInfos.Width;
  154. int height = (int)imageInfos.Height;
  155. int numBytes = (int)imageInfos.BytesRaw;
  156. IntPtr imageData = confidenceImage.Value.Raw;
  157. short[] confidenceData = new short[width * height];
  158. Marshal.Copy(imageData, confidenceData, 0, width * height);
  159. return new ConfidenceImage(width, height, confidenceData);
  160. }
  161. public UVImage getUVImage()
  162. {
  163. Iisu.Data.IImageInfos imageInfos = uvImage.Value.ImageInfos;
  164. int width = (int)imageInfos.Width;
  165. int height = (int)imageInfos.Height;
  166. int numBytes = (int)imageInfos.BytesRaw;
  167. IntPtr imageData = uvImage.Value.Raw;
  168. float[] uvData = new float[2 * width * height];
  169. Marshal.Copy(imageData, uvData, 0, 2 * width * height);
  170. return new UVImage(width, height, uvData);
  171. }
  172. public ImageData getImageData()
  173. {
  174. return currentImage;
  175. }
  176. private void createImageData() {
  177. currentImage = new ImageData(getDepthImage(), getConfidenceImage(), getColorImage(), getUVImage());
  178. }
  179. private void checkHandIndex(uint handIndex)
  180. {
  181. if (handIndex < 1 || handIndex > 2)
  182. throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
  183. }
  184. public bool isHandOpen(uint handIndex)
  185. {
  186. checkHandIndex(handIndex);
  187. return handOpen[handIndex - 1].Value;
  188. }
  189. public Vector getPalmPosition3D(uint handIndex)
  190. {
  191. checkHandIndex(handIndex);
  192. return new DenseVector(palmPositions3D[handIndex - 1].Value.ToArray());
  193. }
  194. public Vector getPalmPosition2D(uint handIndex)
  195. {
  196. checkHandIndex(handIndex);
  197. return new DenseVector(palmPositions2D[handIndex - 1].Value.ToArray());
  198. }
  199. public Vector getTipPosition3D(uint handIndex)
  200. {
  201. checkHandIndex(handIndex);
  202. return new DenseVector(tipPositions3D[handIndex - 1].Value.ToArray());
  203. }
  204. public Vector getForearmPosition3D(uint handIndex)
  205. {
  206. checkHandIndex(handIndex);
  207. return new DenseVector(forearmPositions3D[handIndex - 1].Value.ToArray());
  208. }
  209. public Vector getPalmNormal3D(uint handIndex)
  210. {
  211. checkHandIndex(handIndex);
  212. return new DenseVector(palmNormals3D[handIndex - 1].Value.ToArray());
  213. }
  214. public DetectionStatus[] getFingerStatus(uint handIndex)
  215. {
  216. checkHandIndex(handIndex);
  217. int[] status = fingerStatus[handIndex - 1].Value;
  218. DetectionStatus[] result = new DetectionStatus[status.Length];
  219. for (int i = 0; i < status.Length; ++i)
  220. {
  221. switch (status[i])
  222. {
  223. case 0:
  224. result[i] = DetectionStatus.Inactive;
  225. break;
  226. case 1:
  227. result[i] = DetectionStatus.Detected;
  228. break;
  229. case 2:
  230. result[i] = DetectionStatus.Tracked;
  231. break;
  232. }
  233. }
  234. return result;
  235. }
  236. public DetectionStatus getHandStatus(uint handIndex)
  237. {
  238. checkHandIndex(handIndex);
  239. int status = handStatus[handIndex - 1].Value;
  240. DetectionStatus result = new DetectionStatus();
  241. switch (status)
  242. {
  243. case 0:
  244. result = DetectionStatus.Inactive;
  245. break;
  246. case 1:
  247. result = DetectionStatus.Detected;
  248. break;
  249. case 2:
  250. result = DetectionStatus.Tracked;
  251. break;
  252. }
  253. return result;
  254. }
  255. public Vector[] getFingerTipPositions3D(uint handIndex)
  256. {
  257. checkHandIndex(handIndex);
  258. Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
  259. Vector[] results = new DenseVector[positions.Length];
  260. for (int i = 0; i < positions.Length; ++i)
  261. results[i] = new DenseVector(positions[i].ToArray());
  262. return results;
  263. }
  264. public Vector[] getFingerTipPositions2D(uint handIndex)
  265. {
  266. checkHandIndex(handIndex);
  267. Iisu.Data.Vector2[] positions = fingerTipPositions2D[handIndex - 1].Value;
  268. Vector[] results = new DenseVector[positions.Length];
  269. for (int i = 0; i < positions.Length; ++i)
  270. results[i] = new DenseVector(positions[i].ToArray());
  271. return results;
  272. }
  273. public HandSide getHandSide(uint handIndex)
  274. {
  275. checkHandIndex(handIndex);
  276. int side = handSides[handIndex - 1].Value;
  277. switch (side)
  278. {
  279. case 1:
  280. return HandSide.Left;
  281. case 2:
  282. return HandSide.Right;
  283. default:
  284. return HandSide.Unknown;
  285. }
  286. }
  287. public VertexArray getVertexArray()
  288. {
  289. int width = currentImage.getWidth();
  290. int height = currentImage.getHeight();
  291. Vertex[] vertices = new Vertex[width * height];
  292. Color[] colors = new Color[width * height];
  293. int index = 0;
  294. for (int x = 0; x < width; x++)
  295. {
  296. for (int y = 0; y < height; y++)
  297. {
  298. int depth = currentImage.getDepth(x, y);
  299. vertices[index] = create3DVertexFrom2D(x, y, depth);
  300. colors[index] = currentImage.getColor(x, y);
  301. index++;
  302. }
  303. }
  304. return new VertexArray(vertices, colors);
  305. }
  306. private Vertex create3DVertexFrom2D(float pixelX, float pixelY, int depth)
  307. {
  308. int width = currentImage.getWidth();
  309. int height = currentImage.getHeight();
  310. float alphaX = hfov.Value;
  311. float alphaY = vfov.Value;
  312. float convertedDepth = depth / 1000f; // mm into m
  313. float u = (pixelX / (float)width - 0.5f) * 2f;
  314. float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
  315. float relX = (float)(u * Math.Tan(alphaX / 2f));
  316. float relY = (float)(v * Math.Tan(alphaY / 2f));
  317. float z = (float)(convertedDepth / Math.Sqrt(1 + relX * relX + relY * relY));
  318. float x = relX * z;
  319. float y = relY * z;
  320. return new Vertex(x, y, z );
  321. }
  322. }
  323. }