NativeMapping.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental
  10. {
  11. using System;
  12. using System.Runtime.InteropServices;
  13. using UnityEngine;
  14. /// <summary> Session Native API. </summary>
  15. public class NativeMapping
  16. {
  17. /// <summary> Handle of the database. </summary>
  18. private UInt64 m_DatabaseHandle;
  19. /// <summary> The native interface. </summary>
  20. private static NativeInterface m_NativeInterface;
  21. /// <summary> Constructor. </summary>
  22. /// <param name="nativeInterface"> The native interface.</param>
  23. public NativeMapping(NativeInterface nativeInterface)
  24. {
  25. m_NativeInterface = nativeInterface;
  26. }
  27. /// <summary> Creates data base. </summary>
  28. /// <returns> True if it succeeds, false if it fails. </returns>
  29. public bool CreateDataBase()
  30. {
  31. var result = NativeApi.NRWorldMapDatabaseCreate(m_NativeInterface.TrackingHandle, ref m_DatabaseHandle);
  32. NativeErrorListener.Check(result, this, "CreateDataBase");
  33. return result == NativeResult.Success;
  34. }
  35. /// <summary> Destroys the data base. </summary>
  36. /// <returns> True if it succeeds, false if it fails. </returns>
  37. public bool DestroyDataBase()
  38. {
  39. var result = NativeApi.NRWorldMapDatabaseDestroy(m_NativeInterface.TrackingHandle, m_DatabaseHandle);
  40. NativeErrorListener.Check(result, this, "DestroyDataBase");
  41. return result == NativeResult.Success;
  42. }
  43. /// <summary> Loads a map. </summary>
  44. /// <param name="path"> Full pathname of the file.</param>
  45. /// <returns> True if it succeeds, false if it fails. </returns>
  46. public bool LoadMap(string path)
  47. {
  48. var result = NativeApi.NRWorldMapDatabaseLoadFile(m_NativeInterface.TrackingHandle, m_DatabaseHandle, path);
  49. NativeErrorListener.Check(result, this, "LoadMap");
  50. return result == NativeResult.Success;
  51. }
  52. /// <summary> Saves a map. </summary>
  53. /// <param name="path"> Full pathname of the file.</param>
  54. /// <returns> True if it succeeds, false if it fails. </returns>
  55. public bool SaveMap(string path)
  56. {
  57. var result = NativeApi.NRWorldMapDatabaseSaveFile(m_NativeInterface.TrackingHandle, m_DatabaseHandle, path);
  58. NativeErrorListener.Check(result, this, "SaveMap");
  59. return result == NativeResult.Success;
  60. }
  61. /// <summary> Reset Map </summary>
  62. /// <returns> True if it succeeds, false if it fails. </returns>
  63. public bool Reset()
  64. {
  65. var result = NativeApi.NRMappingReset(m_NativeInterface.TrackingHandle);
  66. NativeErrorListener.Check(result, this, "Reset");
  67. return result == NativeResult.Success;
  68. }
  69. /// <summary> Adds an anchor. </summary>
  70. /// <param name="pose"> The pose.</param>
  71. /// <returns> An UInt64. </returns>
  72. public UInt64 AddAnchor(Pose pose)
  73. {
  74. UInt64 anchorHandle = 0;
  75. NativeMat4f nativePose;
  76. ConversionUtility.UnityPoseToApiPose(pose, out nativePose);
  77. var result = NativeApi.NRTrackingAcquireNewAnchor(m_NativeInterface.TrackingHandle, ref nativePose, ref anchorHandle);
  78. NativeErrorListener.Check(result, this, "AddAnchor");
  79. return anchorHandle;
  80. }
  81. /// <summary> Creates anchor list. </summary>
  82. /// <returns> The new anchor list. </returns>
  83. public UInt64 CreateAnchorList()
  84. {
  85. UInt64 anchorlisthandle = 0;
  86. var result = NativeApi.NRAnchorListCreate(m_NativeInterface.TrackingHandle, ref anchorlisthandle);
  87. NativeErrorListener.Check(result, this, "CreateAnchorList");
  88. return anchorlisthandle;
  89. }
  90. /// <summary> Updates the anchor described by anchorlisthandle. </summary>
  91. /// <param name="anchorlisthandle"> The anchorlisthandle.</param>
  92. /// <returns> True if it succeeds, false if it fails. </returns>
  93. public bool UpdateAnchor(UInt64 anchorlisthandle)
  94. {
  95. var result = NativeApi.NRTrackingUpdateAnchors(m_NativeInterface.TrackingHandle, anchorlisthandle);
  96. NativeErrorListener.Check(result, this, "UpdateAnchor");
  97. return result == NativeResult.Success;
  98. }
  99. /// <summary> Destroys the anchor list described by anchorlisthandle. </summary>
  100. /// <param name="anchorlisthandle"> The anchorlisthandle.</param>
  101. /// <returns> True if it succeeds, false if it fails. </returns>
  102. public bool DestroyAnchorList(UInt64 anchorlisthandle)
  103. {
  104. //NRDebugger.Info("Start to destroy anchor list...");
  105. var result = NativeApi.NRAnchorListDestroy(m_NativeInterface.TrackingHandle, anchorlisthandle);
  106. NativeErrorListener.Check(result, this, "DestroyAnchorList");
  107. return result == NativeResult.Success;
  108. }
  109. /// <summary> Gets anchor list size. </summary>
  110. /// <param name="anchor_list_handle"> Handle of the anchor list.</param>
  111. /// <returns> The anchor list size. </returns>
  112. public int GetAnchorListSize(UInt64 anchor_list_handle)
  113. {
  114. int size = 0;
  115. var result = NativeApi.NRAnchorListGetSize(m_NativeInterface.TrackingHandle, anchor_list_handle, ref size);
  116. NativeErrorListener.Check(result, this, "GetAnchorListSize");
  117. return size;
  118. }
  119. /// <summary> Acquires the item. </summary>
  120. /// <param name="anchor_list_handle"> Handle of the anchor list.</param>
  121. /// <param name="index"> Zero-based index of the.</param>
  122. /// <returns> An UInt64. </returns>
  123. public UInt64 AcquireItem(UInt64 anchor_list_handle, int index)
  124. {
  125. UInt64 anchorHandle = 0;
  126. var result = NativeApi.NRAnchorListAcquireItem(m_NativeInterface.TrackingHandle, anchor_list_handle, index, ref anchorHandle);
  127. NativeErrorListener.Check(result, this, "AcquireItem");
  128. return anchorHandle;
  129. }
  130. /// <summary> Gets tracking state. </summary>
  131. /// <param name="anchor_handle"> Handle of the anchor.</param>
  132. /// <returns> The tracking state. </returns>
  133. public TrackingState GetTrackingState(UInt64 anchor_handle)
  134. {
  135. TrackingState trackingState = TrackingState.Stopped;
  136. var result = NativeApi.NRAnchorGetTrackingState(m_NativeInterface.TrackingHandle, anchor_handle, ref trackingState);
  137. NativeErrorListener.Check(result, this, "GetTrackingState");
  138. return trackingState;
  139. }
  140. /// <summary> Gets anchor native identifier. </summary>
  141. /// <param name="anchor_handle"> Handle of the anchor.</param>
  142. /// <returns> The anchor native identifier. </returns>
  143. public static int GetAnchorNativeID(UInt64 anchor_handle)
  144. {
  145. int anchorID = -1;
  146. NativeApi.NRAnchorGetID(m_NativeInterface.TrackingHandle, anchor_handle, ref anchorID);
  147. return anchorID;
  148. }
  149. /// <summary> Gets anchor pose. </summary>
  150. /// <param name="anchor_handle"> Handle of the anchor.</param>
  151. /// <returns> The anchor pose. </returns>
  152. public Pose GetAnchorPose(UInt64 anchor_handle)
  153. {
  154. NativeMat4f nativePose = NativeMat4f.identity;
  155. NativeApi.NRAnchorGetPose(m_NativeInterface.TrackingHandle, anchor_handle, ref nativePose);
  156. Pose unitypose;
  157. ConversionUtility.ApiPoseToUnityPose(nativePose, out unitypose);
  158. return unitypose;
  159. }
  160. /// <summary> Destroys the anchor described by anchor_handle. </summary>
  161. /// <param name="anchor_handle"> Handle of the anchor.</param>
  162. /// <returns> True if it succeeds, false if it fails. </returns>
  163. public bool DestroyAnchor(UInt64 anchor_handle)
  164. {
  165. var result = NativeApi.NRAnchorDestroy(m_NativeInterface.TrackingHandle, anchor_handle);
  166. NativeErrorListener.Check(result, this, "DestroyAnchor");
  167. return result == NativeResult.Success;
  168. }
  169. /// <summary> A native api. </summary>
  170. private struct NativeApi
  171. {
  172. /// <summary> Nr world map database create. </summary>
  173. /// <param name="tracking_handle"> Handle of the tracking.</param>
  174. /// <param name="out_world_map_database_handle"> [in,out] Handle of the out world map database.</param>
  175. /// <returns> A NativeResult. </returns>
  176. [DllImport(NativeConstants.NRNativeLibrary)]
  177. public static extern NativeResult NRWorldMapDatabaseCreate(UInt64 tracking_handle,
  178. ref UInt64 out_world_map_database_handle);
  179. /// <summary> Nr world map database destroy. </summary>
  180. /// <param name="tracking_handle"> Handle of the tracking.</param>
  181. /// <param name="world_map_database_handle"> Handle of the world map database.</param>
  182. /// <returns> A NativeResult. </returns>
  183. [DllImport(NativeConstants.NRNativeLibrary)]
  184. public static extern NativeResult NRWorldMapDatabaseDestroy(UInt64 tracking_handle,
  185. UInt64 world_map_database_handle);
  186. /// <summary> Nr world map database load file. </summary>
  187. /// <param name="tracking_handle"> Handle of the tracking.</param>
  188. /// <param name="world_map_database_handle"> Handle of the world map database.</param>
  189. /// <param name="world_map_database_file_path"> Full pathname of the world map database file.</param>
  190. /// <returns> A NativeResult. </returns>
  191. [DllImport(NativeConstants.NRNativeLibrary)]
  192. public static extern NativeResult NRWorldMapDatabaseLoadFile(UInt64 tracking_handle,
  193. UInt64 world_map_database_handle, string world_map_database_file_path);
  194. /// <summary> Nr world map database save file. </summary>
  195. /// <param name="tracking_handle"> Handle of the tracking.</param>
  196. /// <param name="world_map_database_handle"> Handle of the world map database.</param>
  197. /// <param name="world_map_database_file_path"> Full pathname of the world map database file.</param>
  198. /// <returns> A NativeResult. </returns>
  199. [DllImport(NativeConstants.NRNativeLibrary)]
  200. public static extern NativeResult NRWorldMapDatabaseSaveFile(UInt64 tracking_handle,
  201. UInt64 world_map_database_handle, string world_map_database_file_path);
  202. /// <summary> Reset Map </summary>
  203. /// <param name="tracking_handle"> Handle of the tracking.</param>
  204. /// <returns> A NativeResult. </returns>
  205. [DllImport(NativeConstants.NRNativeLibrary)]
  206. public static extern NativeResult NRMappingReset(UInt64 tracking_handle);
  207. /// <summary> NRTracking. </summary>
  208. /// <param name="tracking_handle"> Handle of the tracking.</param>
  209. /// <param name="pose"> [in,out] The pose.</param>
  210. /// <param name="out_anchor_handle"> [in,out] Handle of the out anchor.</param>
  211. /// <returns> A NativeResult. </returns>
  212. [DllImport(NativeConstants.NRNativeLibrary)]
  213. public static extern NativeResult NRTrackingAcquireNewAnchor(
  214. UInt64 tracking_handle, ref NativeMat4f pose, ref UInt64 out_anchor_handle);
  215. /// <summary> Nr tracking update anchors. </summary>
  216. /// <param name="tracking_handle"> Handle of the tracking.</param>
  217. /// <param name="out_anchor_list_handle"> Handle of the out anchor list.</param>
  218. /// <returns> A NativeResult. </returns>
  219. [DllImport(NativeConstants.NRNativeLibrary)]
  220. public static extern NativeResult NRTrackingUpdateAnchors(
  221. UInt64 tracking_handle, UInt64 out_anchor_list_handle);
  222. /// <summary> NRAnchorList. </summary>
  223. /// <param name="tracking_handle"> Handle of the tracking.</param>
  224. /// <param name="out_anchor_list_handle"> [in,out] Handle of the out anchor list.</param>
  225. /// <returns> A NativeResult. </returns>
  226. [DllImport(NativeConstants.NRNativeLibrary)]
  227. public static extern NativeResult NRAnchorListCreate(
  228. UInt64 tracking_handle, ref UInt64 out_anchor_list_handle);
  229. /// <summary> Nr anchor list destroy. </summary>
  230. /// <param name="tracking_handle"> Handle of the tracking.</param>
  231. /// <param name="anchor_list_handle"> Handle of the anchor list.</param>
  232. /// <returns> A NativeResult. </returns>
  233. [DllImport(NativeConstants.NRNativeLibrary)]
  234. public static extern NativeResult NRAnchorListDestroy(
  235. UInt64 tracking_handle, UInt64 anchor_list_handle);
  236. /// <summary> Nr anchor list get size. </summary>
  237. /// <param name="tracking_handle"> Handle of the tracking.</param>
  238. /// <param name="anchor_list_handle"> Handle of the anchor list.</param>
  239. /// <param name="out_list_size"> [in,out] Size of the out list.</param>
  240. /// <returns> A NativeResult. </returns>
  241. [DllImport(NativeConstants.NRNativeLibrary)]
  242. public static extern NativeResult NRAnchorListGetSize(UInt64 tracking_handle,
  243. UInt64 anchor_list_handle, ref int out_list_size);
  244. /// <summary> Nr anchor list acquire item. </summary>
  245. /// <param name="tracking_handle"> Handle of the tracking.</param>
  246. /// <param name="anchor_list_handle"> Handle of the anchor list.</param>
  247. /// <param name="index"> Zero-based index of the.</param>
  248. /// <param name="out_anchor"> [in,out] The out anchor.</param>
  249. /// <returns> A NativeResult. </returns>
  250. [DllImport(NativeConstants.NRNativeLibrary)]
  251. public static extern NativeResult NRAnchorListAcquireItem(UInt64 tracking_handle,
  252. UInt64 anchor_list_handle, int index, ref UInt64 out_anchor);
  253. /// <summary> NRAnchor. </summary>
  254. /// <param name="tracking_handle"> Handle of the tracking.</param>
  255. /// <param name="anchor_handle"> Handle of the anchor.</param>
  256. /// <param name="out_tracking_state"> [in,out] State of the out tracking.</param>
  257. /// <returns> A NativeResult. </returns>
  258. [DllImport(NativeConstants.NRNativeLibrary)]
  259. public static extern NativeResult NRAnchorGetTrackingState(UInt64 tracking_handle,
  260. UInt64 anchor_handle, ref TrackingState out_tracking_state);
  261. /// <summary> Nr anchor get identifier. </summary>
  262. /// <param name="tracking_handle"> Handle of the tracking.</param>
  263. /// <param name="anchor_handle"> Handle of the anchor.</param>
  264. /// <param name="out_anchor_id"> [in,out] Identifier for the out anchor.</param>
  265. /// <returns> A NativeResult. </returns>
  266. [DllImport(NativeConstants.NRNativeLibrary)]
  267. public static extern NativeResult NRAnchorGetID(UInt64 tracking_handle,
  268. UInt64 anchor_handle, ref int out_anchor_id);
  269. /// <summary> Nr anchor get pose. </summary>
  270. /// <param name="tracking_handle"> Handle of the tracking.</param>
  271. /// <param name="anchor_handle"> Handle of the anchor.</param>
  272. /// <param name="out_pose"> [in,out] The out pose.</param>
  273. /// <returns> A NativeResult. </returns>
  274. [DllImport(NativeConstants.NRNativeLibrary)]
  275. public static extern NativeResult NRAnchorGetPose(UInt64 tracking_handle,
  276. UInt64 anchor_handle, ref NativeMat4f out_pose);
  277. /// <summary> Nr anchor destroy. </summary>
  278. /// <param name="tracking_handle"> Handle of the tracking.</param>
  279. /// <param name="anchor_handle"> Handle of the anchor.</param>
  280. /// <returns> A NativeResult. </returns>
  281. [DllImport(NativeConstants.NRNativeLibrary)]
  282. public static extern NativeResult NRAnchorDestroy(UInt64 tracking_handle,
  283. UInt64 anchor_handle);
  284. }
  285. }
  286. }