IisuDataSource.cs 15 KB

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