Core.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using UnityEngine;
  9. using System;
  10. using System.Runtime.InteropServices;
  11. #if UNITY_EDITOR || UNITY_STANDALONE
  12. using System.Diagnostics;
  13. #endif
  14. namespace Immersal
  15. {
  16. [StructLayout(LayoutKind.Sequential)]
  17. public struct icvCaptureInfo
  18. {
  19. public int captureSize;
  20. public int connected;
  21. }
  22. public static class Core
  23. {
  24. /// <summary>
  25. /// Get a Vector3 point cloud representation of the map data.
  26. /// </summary>
  27. /// <param name="mapHandle">An integer map handle</param>
  28. /// <param name="points">A preallocated Vector3 array for the points</param>
  29. /// <returns>Returns the number of points if succeeded, 0 otherwise.</returns>
  30. public static int GetPointCloud(int mapHandle, Vector3[] points)
  31. {
  32. GCHandle vector3ArrayHandle = GCHandle.Alloc(points, GCHandleType.Pinned);
  33. int n = Native.icvPointsGet(mapHandle, vector3ArrayHandle.AddrOfPinnedObject(), points.Length);
  34. vector3ArrayHandle.Free();
  35. return n;
  36. }
  37. /// <summary>
  38. /// Get point count of the map's point cloud.
  39. /// </summary>
  40. /// <param name="mapHandle">An integer map handle</param>
  41. /// <returns>Returns the number of points.</returns>
  42. public static int GetPointCloudSize(int mapHandle) => Native.icvPointsGetCount(mapHandle);
  43. /// <summary>
  44. /// Load map data from a .bytes file.
  45. /// </summary>
  46. /// <param name="buffer">Map data as a byte array</param>
  47. /// <returns>An integer map handle.</returns>
  48. public static int LoadMap(byte[] buffer) => Native.icvLoadMap(buffer);
  49. /// <summary>
  50. /// Free the map data from memory.
  51. /// </summary>
  52. /// <param name="mapHandle">An integer map handle</param>
  53. /// <returns>Returns 1 if succeeded, 0 otherwise.</returns>
  54. public static int FreeMap(int mapHandle) => Native.icvFreeMap(mapHandle);
  55. /// <summary>
  56. /// Capture image into the current map.
  57. /// </summary>
  58. /// <param name="capture">A preallocated byte array for the captured PNG image</param>
  59. /// <param name="captureSizeMax">Int size of the array</param>
  60. /// <param name="pixels">Raw pixel buffer data from the camera</param>
  61. /// <param name="width">Image width</param>
  62. /// <param name="height">Image height</param>
  63. /// <param name="channels">1 or 3, monochromatic or RGB capture</param>
  64. /// <returns>Int size of the captured PNG bytes</returns>
  65. public static icvCaptureInfo CaptureImage(byte[] capture, int captureSizeMax, byte[] pixels, int width,
  66. int height, int channels, int useMatching = 0)
  67. {
  68. GCHandle captureHandle = GCHandle.Alloc(capture, GCHandleType.Pinned);
  69. GCHandle pixelsHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);
  70. icvCaptureInfo info = Native.icvCaptureImage(captureHandle.AddrOfPinnedObject(), captureSizeMax,
  71. pixelsHandle.AddrOfPinnedObject(), width, height, channels, useMatching);
  72. captureHandle.Free();
  73. pixelsHandle.Free();
  74. return info;
  75. }
  76. /// <summary>
  77. /// Gets the position and orientation of the image within the map.
  78. /// </summary>
  79. /// <param name="pos">Output Vector3 for the position</param>
  80. /// <param name="rot">Output Quaternion for the orientation</param>
  81. /// <param name="width">Image width</param>
  82. /// <param name="height">Image height</param>
  83. /// <param name="intrinsics">Camera intrinsics</param>
  84. /// <param name="pixels">Raw pixel buffer data from the camera</param>
  85. /// <returns>An integer map ID if succeeded, -1 otherwise</returns>
  86. public static int LocalizeImage(out Vector3 pos, out Quaternion rot, int n, int[] handles, int width,
  87. int height, ref Vector4 intrinsics, IntPtr pixels, int param1 = 0, int param2 = 12, float param3 = 0.0f,
  88. float param4 = 2.0f, int method = 1)
  89. {
  90. GCHandle intHandle = GCHandle.Alloc(handles, GCHandleType.Pinned);
  91. int result = Native.icvLocalize(out pos, out rot, n, intHandle.AddrOfPinnedObject(), width, height,
  92. ref intrinsics, pixels, param1, param2, param3, param4, method);
  93. intHandle.Free();
  94. return result;
  95. }
  96. /// <summary>
  97. /// Gets the position and orientation of the image within the map.
  98. /// </summary>
  99. /// <param name="pos">Output Vector3 for the position</param>
  100. /// <param name="rot">Output Quaternion for the orientation</param>
  101. /// <param name="width">Image width</param>
  102. /// <param name="height">Image height</param>
  103. /// <param name="intrinsics">Camera intrinsics</param>
  104. /// <param name="pixels">Raw pixel buffer data from the camera</param>
  105. /// <returns>An integer map ID if succeeded, -1 otherwise</returns>
  106. public static int LocalizeImage(out Vector3 pos, out Quaternion rot, int width, int height,
  107. ref Vector4 intrinsics, IntPtr pixels, int param1 = 0, int param2 = 12, float param3 = 0.0f,
  108. float param4 = 2.0f, int method = 1)
  109. {
  110. int n = 0;
  111. int[] handles = new int[1];
  112. return LocalizeImage(out pos, out rot, n, handles, width, height, ref intrinsics, pixels, param1, param2,
  113. param3, param4, method);
  114. }
  115. /// <summary>
  116. ///
  117. /// </summary>
  118. /// <param name="ecef"></param>
  119. /// <param name="map"></param>
  120. /// <param name="mapToEcef"></param>
  121. /// <returns></returns>
  122. public static int PosMapToEcef(double[] ecef, Vector3 map, double[] mapToEcef)
  123. {
  124. GCHandle ecefHandle = GCHandle.Alloc(ecef, GCHandleType.Pinned);
  125. GCHandle mapToEcefHandle = GCHandle.Alloc(mapToEcef, GCHandleType.Pinned);
  126. int r = Native.icvPosMapToEcef(ecefHandle.AddrOfPinnedObject(), ref map,
  127. mapToEcefHandle.AddrOfPinnedObject());
  128. mapToEcefHandle.Free();
  129. ecefHandle.Free();
  130. return r;
  131. }
  132. /// <summary>
  133. ///
  134. /// </summary>
  135. /// <param name="wgs84"></param>
  136. /// <param name="ecef"></param>
  137. /// <returns></returns>
  138. public static int PosEcefToWgs84(double[] wgs84, double[] ecef)
  139. {
  140. GCHandle wgs84Handle = GCHandle.Alloc(wgs84, GCHandleType.Pinned);
  141. GCHandle ecefHandle = GCHandle.Alloc(ecef, GCHandleType.Pinned);
  142. int r = Native.icvPosEcefToWgs84(wgs84Handle.AddrOfPinnedObject(), ecefHandle.AddrOfPinnedObject());
  143. ecefHandle.Free();
  144. wgs84Handle.Free();
  145. return r;
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. /// <param name="ecef"></param>
  151. /// <param name="wgs84"></param>
  152. /// <returns></returns>
  153. public static int PosWgs84ToEcef(double[] ecef, double[] wgs84)
  154. {
  155. GCHandle ecefHandle = GCHandle.Alloc(ecef, GCHandleType.Pinned);
  156. GCHandle wgs84Handle = GCHandle.Alloc(wgs84, GCHandleType.Pinned);
  157. int r = Native.icvPosWgs84ToEcef(ecefHandle.AddrOfPinnedObject(), wgs84Handle.AddrOfPinnedObject());
  158. wgs84Handle.Free();
  159. ecefHandle.Free();
  160. return r;
  161. }
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. /// <param name="map"></param>
  166. /// <param name="ecef"></param>
  167. /// <param name="mapToEcef"></param>
  168. /// <returns></returns>
  169. public static int PosEcefToMap(out Vector3 map, double[] ecef, double[] mapToEcef)
  170. {
  171. GCHandle ecefHandle = GCHandle.Alloc(ecef, GCHandleType.Pinned);
  172. GCHandle mapToEcefHandle = GCHandle.Alloc(mapToEcef, GCHandleType.Pinned);
  173. int r = Native.icvPosEcefToMap(out map, ecefHandle.AddrOfPinnedObject(),
  174. mapToEcefHandle.AddrOfPinnedObject());
  175. mapToEcefHandle.Free();
  176. ecefHandle.Free();
  177. return r;
  178. }
  179. /// <summary>
  180. ///
  181. /// </summary>
  182. /// <param name="wgs84"></param>
  183. /// <param name="map"></param>
  184. /// <param name="mapToEcef"></param>
  185. /// <returns></returns>
  186. public static int PosMapToWgs84(double[] wgs84, Vector3 map, double[] mapToEcef)
  187. {
  188. double[] ecef = new double[3];
  189. int err = PosMapToEcef(ecef, map, mapToEcef);
  190. if (err != 0)
  191. return err;
  192. return PosEcefToWgs84(wgs84, ecef);
  193. }
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. /// <param name="ecef"></param>
  198. /// <param name="map"></param>
  199. /// <param name="mapToEcef"></param>
  200. /// <returns></returns>
  201. public static int RotMapToEcef(out Quaternion ecef, Quaternion map, double[] mapToEcef)
  202. {
  203. GCHandle mapToEcefHandle = GCHandle.Alloc(mapToEcef, GCHandleType.Pinned);
  204. int r = Native.icvRotMapToEcef(out ecef, ref map, mapToEcefHandle.AddrOfPinnedObject());
  205. mapToEcefHandle.Free();
  206. return r;
  207. }
  208. /// <summary>
  209. ///
  210. /// </summary>
  211. /// <param name="map"></param>
  212. /// <param name="ecef"></param>
  213. /// <param name="mapToEcef"></param>
  214. /// <returns></returns>
  215. public static int RotEcefToMap(out Quaternion map, Quaternion ecef, double[] mapToEcef)
  216. {
  217. GCHandle mapToEcefHandle = GCHandle.Alloc(mapToEcef, GCHandleType.Pinned);
  218. int r = Native.icvRotEcefToMap(out map, ref ecef, mapToEcefHandle.AddrOfPinnedObject());
  219. mapToEcefHandle.Free();
  220. return r;
  221. }
  222. /// <summary>
  223. /// Get internal plugin parameters.
  224. /// </summary>
  225. /// <param name="parameter">Parameter name</param>
  226. /// <returns>Returns an integer value if set, -1 otherwise.</returns>
  227. public static int GetInteger(string parameter) => Native.icvGetInteger(parameter);
  228. /// <summary>
  229. /// Set internal plugin parameters.
  230. ///
  231. /// Available parameters:
  232. /// "LocalizationMaxPixels" - 0 is no limit (the default), 1280*720 or higher.
  233. /// "NumThreads" - how many CPU cores to use; -1 (system default) or a positive integer.
  234. /// "ImageCompressionLevel" - 0 (no compression, fastest) to 9 (slowest). Defaults to 4.
  235. /// </summary>
  236. /// <param name="parameter">Parameter name</param>
  237. /// <param name="value">An integer parameter value</param>
  238. /// <returns>Returns 1 if succeeded, -1 otherwise.</returns>
  239. public static int SetInteger(string parameter, int value) => Native.icvSetInteger(parameter, value);
  240. }
  241. public static class Native
  242. {
  243. private const string Assembly =
  244. #if UNITY_IOS && !UNITY_EDITOR
  245. "__Internal";
  246. #else
  247. "PosePlugin";
  248. #endif
  249. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  250. public static extern int icvPointsGet(int mapHandle, IntPtr array, int maxCount);
  251. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  252. public static extern int icvPointsGetCount(int mapHandle);
  253. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  254. public static extern int icvLoadMap(byte[] buffer);
  255. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  256. public static extern int icvFreeMap(int mapHandle);
  257. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  258. public static extern icvCaptureInfo icvCaptureImage(IntPtr capture, int captureSizeMax, IntPtr pixels,
  259. int width, int height, int channels, int useMatching);
  260. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  261. public static extern int icvLocalize(out Vector3 pos, out Quaternion rot, int n, IntPtr handles, int width,
  262. int height, ref Vector4 intrinsics, IntPtr pixels, int param1, int param2, float param3, float param4,
  263. int method);
  264. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  265. public static extern int icvMapToEcefGet(IntPtr mapToEcef, int handle);
  266. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  267. public static extern int icvPosMapToEcef(IntPtr ecef, ref Vector3 map, IntPtr mapToEcef);
  268. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  269. public static extern int icvPosEcefToWgs84(IntPtr wgs84, IntPtr ecef);
  270. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  271. public static extern int icvPosWgs84ToEcef(IntPtr ecef, IntPtr wgs84);
  272. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  273. public static extern int icvPosEcefToMap(out Vector3 map, IntPtr ecef, IntPtr mapToEcef);
  274. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  275. public static extern int icvRotMapToEcef(out Quaternion ecef, ref Quaternion map, IntPtr mapToEcef);
  276. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  277. public static extern int icvRotEcefToMap(out Quaternion map, ref Quaternion ecef, IntPtr mapToEcef);
  278. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  279. public static extern int icvSetInteger([MarshalAs(UnmanagedType.LPStr)] string parameter, int value);
  280. [DllImport(Assembly, CallingConvention = CallingConvention.Cdecl)]
  281. public static extern int icvGetInteger([MarshalAs(UnmanagedType.LPStr)] string parameter);
  282. }
  283. }