IisuDataSource.cs 8.0 KB

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