IisuDataSource.cs 16 KB

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