PluginVioFusion.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //=============================================================================
  2. //
  3. // Copyright 2016 Ximmerse, LTD. All rights reserved.
  4. //
  5. //=============================================================================
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using UnityEngine;
  9. namespace Ximmerse.XR
  10. {
  11. /// <summary>
  12. /// Interface for PluginVIO fusion API.
  13. /// </summary>
  14. public class PluginVioFusion
  15. {
  16. private const string kPlugin_FusionAPI = "fusion_api";
  17. public enum ParamType {
  18. TYPE_POSE_VELOCITY_FLOAT = 0,
  19. TYPE_POSE_QUALITY_FLOAT = 1,
  20. TYPE_SENSOR_QUALITY_FLOAT = 2,
  21. TYPE_CAMERA_QUALITY_FLOAT = 3,
  22. TYPE_TRACKING_WARNING_INT = 4,
  23. TYPE_ANGLE_WITH_GROUND_FLOAT = 5
  24. };
  25. public enum TrackingWarningType
  26. {
  27. VIO_LOW_FEATURE_COUNT_ERROR = 0x0001,
  28. VIO_LOW_LIGHT_ERROR = 0x0002,
  29. VIO_BRIGHT_LIGHT_ERROR = 0x0004,
  30. VIO_STEREO_CAMERA_CALIBRATION_ERROR = 0x0008,
  31. VIO_NOT_READY_ERROR = 0x1000,
  32. };
  33. /////////////////////////////////////////////
  34. /// @struct XAttrVIOWorldToBeaconInfo
  35. /// @brief 6DOF Information of VIO world-origin in beacon coordinate.
  36. [StructLayout(LayoutKind.Sequential)]
  37. public struct XAttrVIOWorldInBeaconInfo
  38. {
  39. public int beacon_id;
  40. public UInt64 timestamp;
  41. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  42. public float[] position ;
  43. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  44. public float[] rotation;
  45. public float tracking_confidence;
  46. public float min_distance;
  47. public float correct_weight;
  48. public static XAttrVIOWorldInBeaconInfo Obtain()
  49. {
  50. return new XAttrVIOWorldInBeaconInfo(-1);
  51. }
  52. public XAttrVIOWorldInBeaconInfo(int beacon_id)
  53. {
  54. this.beacon_id = beacon_id;
  55. this.timestamp = 0;
  56. this.position = new float[3] { 0, 0, 0 } ;
  57. this.rotation = new float[4] { 0, 0, 0, 0 };
  58. this.tracking_confidence = 0;
  59. this.min_distance = 0;
  60. this.correct_weight = 0;
  61. }
  62. };
  63. [StructLayout(LayoutKind.Sequential)]
  64. public struct XAttrVIOInfo
  65. {
  66. public UInt64 timestamp; // timestamp of current frame
  67. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  68. public float[] position;
  69. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  70. public float[] rotation;
  71. public float pose_quality;
  72. public float sensor_quality;
  73. public float camera_quality;
  74. public static XAttrVIOInfo Obtain()
  75. {
  76. return new XAttrVIOInfo(-1);
  77. }
  78. public XAttrVIOInfo(int index)
  79. {
  80. this.timestamp = 0;
  81. this.position = new float[3] { 0, 0, 0 };
  82. this.rotation = new float[4] { 0, 0, 0, 0 };
  83. this.pose_quality = 0;
  84. this.sensor_quality = 0;
  85. this.camera_quality = 0;
  86. }
  87. };
  88. /////////////////////////////////////////////
  89. /// @struct XAttrTrackingInfo
  90. /// @brief VPU cammera tracking object pose info.
  91. [StructLayout(LayoutKind.Sequential)]
  92. public struct XAttrTrackingInfo
  93. {
  94. public int index;
  95. public int state;
  96. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  97. public float[] position;
  98. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  99. public float[] rotation;
  100. public UInt64 timestamp;
  101. public UInt64 recognized_markers_mask;
  102. public double confidence;
  103. public double marker_distance;
  104. public static XAttrTrackingInfo Obtain()
  105. {
  106. return new XAttrTrackingInfo(-1);
  107. }
  108. public XAttrTrackingInfo(int index)
  109. {
  110. this.timestamp = 0;
  111. this.index = index;
  112. this.state = 0;
  113. this.position = new float[3] { 0, 0, 0 };
  114. this.rotation = new float[4] { 0, 0, 0, 0 };
  115. this.recognized_markers_mask = 0;
  116. this.confidence = 0;
  117. this.marker_distance = -1.0;
  118. }
  119. };
  120. [StructLayout(LayoutKind.Sequential)]
  121. public struct XAttrBeaconInWorldInfo
  122. {
  123. public float drift_recenter_distance_threshold; // distance threshold for detect vio drift and recenter, default: 1.0f
  124. public float drift_recenter_angle_threshold; // angle threshold for detect vio drift and recenter, default: 1.0f
  125. public int beacon_id; // beacon id
  126. public int group_id; // beacon group id, = -1 when this beacon is not belong to a group
  127. public int coord_system_flag; // true/false means left/right hand coordinates system
  128. public float confidence_thresh; // beacon tracking confidence threshold
  129. public float min_distance_thresh; // min beacon tracking distance threshold
  130. public float max_distance_thresh; // max beacon tracking distance threshold
  131. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  132. public float[] position; // beacon position in world
  133. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  134. public float[] rotation; // beacon rotation int world x, y, z, w. Since beacon can only be horizontal or vertical, the rotation can only be happened around y axis
  135. public static XAttrBeaconInWorldInfo Obtain()
  136. {
  137. return new XAttrBeaconInWorldInfo(0);
  138. }
  139. public XAttrBeaconInWorldInfo(int beacon_id)
  140. {
  141. this.drift_recenter_distance_threshold = 1.0f;
  142. this.drift_recenter_angle_threshold = 1.0f;
  143. this.beacon_id = 0;
  144. this.group_id = 0;
  145. this.coord_system_flag = 1;
  146. this.confidence_thresh = 0;
  147. this.min_distance_thresh = 0;
  148. this.max_distance_thresh = 0;
  149. this.position = new float[3] { 0, 0, 0 };
  150. this.rotation = new float[4] { 0, 0, 0, 1 };
  151. }
  152. };
  153. public static bool plugin_vio_fusion_get_frame(UInt64 predictedTimeNs,
  154. ref XAttrVIOWorldInBeaconInfo beacon_Info,
  155. ref XAttrTrackingInfo marker_info,
  156. ref XAttrVIOInfo vio_info)
  157. {
  158. int beacon_id = 0;
  159. UInt64 beacon_timestamp = 0;
  160. float beacon_pos0 = 0;
  161. float beacon_pos1 = 0;
  162. float beacon_pos2 = 0;
  163. float beacon_rot0 = 0;
  164. float beacon_rot1 = 0;
  165. float beacon_rot2 = 0;
  166. float beacon_rot3 = 0;
  167. float beacon_tracking_confidence = 0;
  168. float beacon_min_distance = 0;
  169. float beacon_correct_weight = 0;
  170. int track_index = 0;
  171. int track_state = 0;
  172. UInt64 track_timestamp = 0;
  173. float track_pos0 = 0;
  174. float track_pos1 = 0;
  175. float track_pos2 = 0;
  176. float track_rot0 = 0;
  177. float track_rot1 = 0;
  178. float track_rot2 = 0;
  179. float track_rot3 = 0;
  180. UInt64 track_recognized_markers_mask = 0;
  181. double track_confidence = 0;
  182. double track_marker_distance = 0;
  183. UInt64 vio_timestamp = 0;
  184. float vio_pos0 = 0;
  185. float vio_pos1 = 0;
  186. float vio_pos2 = 0;
  187. float vio_rot0 = 0;
  188. float vio_rot1 = 0;
  189. float vio_rot2 = 0;
  190. float vio_rot3 = 0;
  191. float vio_pose_quality = 0;
  192. float vio_sensor_quality = 0;
  193. float vio_camera_quality = 0;
  194. bool ret = plugin_vio_fusion_get2(predictedTimeNs, ref beacon_id,
  195. ref beacon_timestamp,
  196. ref beacon_pos0, ref beacon_pos1, ref beacon_pos2,
  197. ref beacon_rot0, ref beacon_rot1, ref beacon_rot2, ref beacon_rot3,
  198. ref beacon_tracking_confidence,
  199. ref beacon_min_distance,
  200. ref beacon_correct_weight,
  201. ref track_index,
  202. ref track_state,
  203. ref track_timestamp,
  204. ref track_pos0,
  205. ref track_pos1,
  206. ref track_pos2,
  207. ref track_rot0,
  208. ref track_rot1,
  209. ref track_rot2,
  210. ref track_rot3,
  211. ref track_recognized_markers_mask,
  212. ref track_confidence,
  213. ref track_marker_distance,
  214. ref vio_timestamp,
  215. ref vio_pos0,
  216. ref vio_pos1,
  217. ref vio_pos2,
  218. ref vio_rot0, ref vio_rot1, ref vio_rot2, ref vio_rot3,
  219. ref vio_pose_quality,
  220. ref vio_sensor_quality,
  221. ref vio_camera_quality);
  222. if (ret) {
  223. beacon_Info.beacon_id = beacon_id;
  224. beacon_Info.timestamp = beacon_timestamp;
  225. beacon_Info.position[0] = beacon_pos0;
  226. beacon_Info.position[1] = beacon_pos1;
  227. beacon_Info.position[2] = beacon_pos2;
  228. beacon_Info.rotation[0] = beacon_rot0;
  229. beacon_Info.rotation[1] = beacon_rot1;
  230. beacon_Info.rotation[2] = beacon_rot2;
  231. beacon_Info.rotation[3] = beacon_rot3;
  232. beacon_Info.tracking_confidence = beacon_tracking_confidence;
  233. beacon_Info.min_distance = beacon_min_distance;
  234. beacon_Info.correct_weight = beacon_correct_weight;
  235. marker_info.index = track_index;
  236. marker_info.state = track_state;
  237. marker_info.timestamp = track_timestamp;
  238. marker_info.position[0] = track_pos0;
  239. marker_info.position[1] = track_pos1;
  240. marker_info.position[2] = track_pos2;
  241. marker_info.rotation[0] = track_rot0;
  242. marker_info.rotation[1] = track_rot1;
  243. marker_info.rotation[2] = track_rot2;
  244. marker_info.rotation[3] = track_rot3;
  245. marker_info.recognized_markers_mask = track_recognized_markers_mask;
  246. marker_info.confidence = track_confidence;
  247. marker_info.marker_distance = track_marker_distance;
  248. vio_info.timestamp = vio_timestamp;
  249. vio_info.position[0] = vio_pos0;
  250. vio_info.position[1] = vio_pos1;
  251. vio_info.position[2] = vio_pos2;
  252. vio_info.rotation[0] = vio_rot0;
  253. vio_info.rotation[1] = vio_rot1;
  254. vio_info.rotation[2] = vio_rot2;
  255. vio_info.rotation[3] = vio_rot3;
  256. vio_info.pose_quality = vio_pose_quality;
  257. vio_info.sensor_quality = vio_sensor_quality;
  258. vio_info.camera_quality = vio_camera_quality;
  259. }
  260. return ret;
  261. }
  262. public static bool plugin_vio_fusion_set_param(ref XAttrBeaconInWorldInfo beacon_in_world_info)
  263. {
  264. // beacon in world ptr
  265. System.IntPtr ptr_info = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(XAttrBeaconInWorldInfo)));
  266. Marshal.StructureToPtr(beacon_in_world_info, ptr_info, false);
  267. bool ret = plugin_vio_fusion_set(ptr_info);
  268. Marshal.FreeHGlobal(ptr_info);
  269. return ret;
  270. }
  271. public static void plugin_vio_fusion_set_off() {
  272. XAttrBeaconInWorldInfo beacon_in_world_info =new XAttrBeaconInWorldInfo();
  273. beacon_in_world_info.beacon_id = -1;
  274. plugin_vio_fusion_set_param(ref beacon_in_world_info);
  275. }
  276. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  277. public static extern bool plugin_vio_fusion_init(bool bQvrStart);
  278. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  279. public static extern void plugin_vio_fusion_exit();
  280. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  281. public static extern bool plugin_vio_fusion_set(IntPtr beacon_in_world_info);
  282. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  283. private static extern bool plugin_vio_fusion_get(UInt64 predictedTimeNs,
  284. IntPtr beacon_Info,
  285. IntPtr marker_info,
  286. IntPtr vio_info);
  287. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  288. public static extern bool plugin_vio_fusion_run(int unused);
  289. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  290. public static extern float plugin_get_vio_state_float(int data_type);
  291. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  292. public static extern int plugin_get_vio_state_int(int data_type);
  293. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  294. public static extern bool plugin_vio_fusion_reset(int param);
  295. [DllImport(kPlugin_FusionAPI, CallingConvention = CallingConvention.Cdecl)]
  296. private static extern bool plugin_vio_fusion_get2(UInt64 predictedTimeNs,
  297. ref int beacon_id,
  298. ref UInt64 beacon_timestamp,
  299. ref float beacon_pos0, ref float beacon_pos1, ref float beacon_pos2,
  300. ref float beacon_rot0, ref float beacon_rot1, ref float beacon_rot2, ref float beacon_rot3,
  301. ref float beacon_tracking_confidence,
  302. ref float beacon_min_distance,
  303. ref float beacon_correct_weight,
  304. ref int track_index,
  305. ref int track_state,
  306. ref UInt64 track_timestamp,
  307. ref float track_pos0, ref float track_pos1, ref float track_pos2,
  308. ref float track_rot0, ref float track_rot1, ref float track_rot2, ref float track_rot3,
  309. ref UInt64 track_recognized_markers_mask,
  310. ref double track_confidence,
  311. ref double track_marker_distance,
  312. ref UInt64 vio_timestamp,
  313. ref float vio_pos0, ref float vio_pos1, ref float vio_pos2,
  314. ref float vio_rot0, ref float vio_rot1, ref float vio_rot2, ref float vio_rot3,
  315. ref float vio_pose_quality,
  316. ref float vio_sensor_quality,
  317. ref float vio_camera_quality);
  318. }
  319. }