NativeConfigration.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Runtime.InteropServices;
  14. using System.Threading.Tasks;
  15. /// <summary> A native configration. </summary>
  16. public partial class NativeConfigration
  17. {
  18. /// <summary> The native interface. </summary>
  19. private NativeInterface m_NativeInterface;
  20. /// <summary> Dictionary of trackable image databases. </summary>
  21. private Dictionary<string, UInt64> m_TrackableImageDatabaseDict;
  22. /// <summary> Handle of the configuration. </summary>
  23. private UInt64 m_ConfigHandle = 0;
  24. /// <summary> Handle of the database. </summary>
  25. private UInt64 m_DatabaseHandle = 0;
  26. /// <summary> The last session configuration. </summary>
  27. private NRSessionConfig m_LastSessionConfig;
  28. /// <summary> The native trackable image. </summary>
  29. private NativeTrackableImage m_NativeTrackableImage;
  30. /// <summary> True if is update configuration lock, false if not. </summary>
  31. private bool m_IsUpdateConfigLock = false;
  32. /// <summary> Constructor. </summary>
  33. /// <param name="nativeInterface"> The native interface.</param>
  34. public NativeConfigration(NativeInterface nativeInterface)
  35. {
  36. m_NativeInterface = nativeInterface;
  37. m_LastSessionConfig = NRSessionConfig.CreateInstance(typeof(NRSessionConfig)) as NRSessionConfig;
  38. m_NativeTrackableImage = m_NativeInterface.NativeTrackableImage;
  39. m_TrackableImageDatabaseDict = new Dictionary<string, ulong>();
  40. }
  41. /// <summary> Updates the configuration described by config. </summary>
  42. /// <param name="config"> The configuration.</param>
  43. /// <returns> True if it succeeds, false if it fails. </returns>
  44. public async Task<bool> UpdateConfig(NRSessionConfig config)
  45. {
  46. if (m_IsUpdateConfigLock)
  47. {
  48. return false;
  49. }
  50. m_IsUpdateConfigLock = true;
  51. if (m_ConfigHandle == 0)
  52. {
  53. m_ConfigHandle = this.Create();
  54. }
  55. if (m_ConfigHandle == 0 || m_LastSessionConfig.Equals(config))
  56. {
  57. NRDebugger.Info("[NativeConfigration] Faild to Update NRSessionConfig!!!");
  58. m_IsUpdateConfigLock = false;
  59. return false;
  60. }
  61. await UpdatePlaneFindMode(config);
  62. await UpdateImageTrackingConfig(config);
  63. m_LastSessionConfig.CopyFrom(config);
  64. m_IsUpdateConfigLock = false;
  65. return true;
  66. }
  67. /// <summary> Updates the plane find mode described by config. </summary>
  68. /// <param name="config"> The configuration.</param>
  69. /// <returns> An asynchronous result. </returns>
  70. private Task UpdatePlaneFindMode(NRSessionConfig config)
  71. {
  72. return Task.Run(() =>
  73. {
  74. var currentmode = this.GetPlaneFindMode(m_ConfigHandle);
  75. if (currentmode != config.PlaneFindingMode)
  76. {
  77. SetPlaneFindMode(m_ConfigHandle, config.PlaneFindingMode);
  78. }
  79. });
  80. }
  81. /// <summary> Updates the image tracking configuration described by config. </summary>
  82. /// <param name="config"> The configuration.</param>
  83. /// <returns> An asynchronous result. </returns>
  84. private Task UpdateImageTrackingConfig(NRSessionConfig config)
  85. {
  86. return Task.Run(() =>
  87. {
  88. switch (config.ImageTrackingMode)
  89. {
  90. case TrackableImageFindingMode.DISABLE:
  91. var result = SetTrackableImageDataBase(m_ConfigHandle, 0);
  92. if (result)
  93. {
  94. m_TrackableImageDatabaseDict.Clear();
  95. }
  96. NRDebugger.Info("[NativeConfigration] Disable trackable image result : " + result);
  97. break;
  98. case TrackableImageFindingMode.ENABLE:
  99. if (config.TrackingImageDatabase == null)
  100. {
  101. return;
  102. }
  103. if (!m_TrackableImageDatabaseDict.TryGetValue(config.TrackingImageDatabase.GUID, out m_DatabaseHandle))
  104. {
  105. DeployData(config.TrackingImageDatabase);
  106. m_DatabaseHandle = m_NativeTrackableImage.CreateDataBase();
  107. m_TrackableImageDatabaseDict.Add(config.TrackingImageDatabase.GUID, m_DatabaseHandle);
  108. }
  109. result = m_NativeTrackableImage.LoadDataBase(m_DatabaseHandle, config.TrackingImageDatabase.TrackingImageDataPath);
  110. NRDebugger.Info("[NativeConfigration] LoadDataBase path:{0} result:{1} ", config.TrackingImageDatabase.TrackingImageDataPath, result);
  111. result = SetTrackableImageDataBase(m_ConfigHandle, m_DatabaseHandle);
  112. NRDebugger.Info("[NativeConfigration] SetTrackableImageDataBase result : " + result);
  113. break;
  114. default:
  115. break;
  116. }
  117. });
  118. }
  119. /// <summary> Deploy data. </summary>
  120. /// <param name="database"> The database.</param>
  121. private void DeployData(NRTrackingImageDatabase database)
  122. {
  123. string deploy_path = database.TrackingImageDataOutPutPath;
  124. NRDebugger.Info("[TrackingImageDatabase] DeployData to path :" + deploy_path);
  125. ZipUtility.UnzipFile(database.RawData, deploy_path, NativeConstants.ZipKey);
  126. }
  127. /// <summary> Creates a new UInt64. </summary>
  128. /// <returns> An UInt64. </returns>
  129. private UInt64 Create()
  130. {
  131. UInt64 config_handle = 0;
  132. var result = NativeApi.NRConfigCreate(m_NativeInterface.TrackingHandle, ref config_handle);
  133. NativeErrorListener.Check(result, this, "Create");
  134. return config_handle;
  135. }
  136. /// <summary> Gets plane find mode. </summary>
  137. /// <param name="config_handle"> The Configuration handle to destroy.</param>
  138. /// <returns> The plane find mode. </returns>
  139. public TrackablePlaneFindingMode GetPlaneFindMode(UInt64 config_handle)
  140. {
  141. TrackablePlaneFindingMode mode = TrackablePlaneFindingMode.DISABLE;
  142. var result = NativeApi.NRConfigGetTrackablePlaneFindingMode(m_NativeInterface.TrackingHandle, config_handle, ref mode);
  143. NativeErrorListener.Check(result, this, "GetPlaneFindMode");
  144. return mode;
  145. }
  146. /// <summary> Sets plane find mode. </summary>
  147. /// <param name="config_handle"> The Configuration handle to destroy.</param>
  148. /// <param name="mode"> The mode.</param>
  149. /// <returns> True if it succeeds, false if it fails. </returns>
  150. public bool SetPlaneFindMode(UInt64 config_handle, TrackablePlaneFindingMode mode)
  151. {
  152. int mode_value;
  153. switch (mode)
  154. {
  155. case TrackablePlaneFindingMode.DISABLE:
  156. case TrackablePlaneFindingMode.HORIZONTAL:
  157. case TrackablePlaneFindingMode.VERTICLE:
  158. mode_value = (int)mode;
  159. break;
  160. case TrackablePlaneFindingMode.BOTH:
  161. mode_value = ((int)TrackablePlaneFindingMode.HORIZONTAL) | ((int)TrackablePlaneFindingMode.VERTICLE);
  162. break;
  163. default:
  164. mode_value = (int)TrackablePlaneFindingMode.DISABLE;
  165. break;
  166. }
  167. var result = NativeApi.NRConfigSetTrackablePlaneFindingMode(m_NativeInterface.TrackingHandle, config_handle, mode_value);
  168. NativeErrorListener.Check(result, this, "SetPlaneFindMode");
  169. return result == NativeResult.Success;
  170. }
  171. /// <summary> Gets trackable image data base. </summary>
  172. /// <param name="config_handle"> The Configuration handle to destroy.</param>
  173. /// <returns> The trackable image data base. </returns>
  174. public UInt64 GetTrackableImageDataBase(UInt64 config_handle)
  175. {
  176. UInt64 database_handle = 0;
  177. var result = NativeApi.NRConfigGetTrackableImageDatabase(m_NativeInterface.TrackingHandle, config_handle, ref database_handle);
  178. NativeErrorListener.Check(result, this, "GetTrackableImageDataBase");
  179. return database_handle;
  180. }
  181. /// <summary> Sets trackable image data base. </summary>
  182. /// <param name="config_handle"> The Configuration handle to destroy.</param>
  183. /// <param name="database_handle"> Handle of the database.</param>
  184. /// <returns> True if it succeeds, false if it fails. </returns>
  185. public bool SetTrackableImageDataBase(UInt64 config_handle, UInt64 database_handle)
  186. {
  187. var result = NativeApi.NRConfigSetTrackableImageDatabase(m_NativeInterface.TrackingHandle, config_handle, database_handle);
  188. NativeErrorListener.Check(result, this, "SetTrackableImageDataBase");
  189. return result == NativeResult.Success;
  190. }
  191. /// <summary> Destroys the given config_handle. </summary>
  192. /// <param name="config_handle"> The Configuration handle to destroy.</param>
  193. /// <returns> True if it succeeds, false if it fails. </returns>
  194. public bool Destroy(UInt64 config_handle)
  195. {
  196. var result = NativeApi.NRConfigDestroy(m_NativeInterface.TrackingHandle, config_handle);
  197. NativeErrorListener.Check(result, this, "Destroy");
  198. return result == NativeResult.Success;
  199. }
  200. /// <summary> A native api. </summary>
  201. private struct NativeApi
  202. {
  203. /// <summary> Nr configuration create. </summary>
  204. /// <param name="session_handle"> Handle of the session.</param>
  205. /// <param name="out_config_handle"> [in,out] Handle of the out configuration.</param>
  206. /// <returns> A NativeResult. </returns>
  207. [DllImport(NativeConstants.NRNativeLibrary)]
  208. public static extern NativeResult NRConfigCreate(UInt64 session_handle, ref UInt64 out_config_handle);
  209. /// <summary> Nr configuration destroy. </summary>
  210. /// <param name="session_handle"> Handle of the session.</param>
  211. /// <param name="config_handle"> Handle of the configuration.</param>
  212. /// <returns> A NativeResult. </returns>
  213. [DllImport(NativeConstants.NRNativeLibrary)]
  214. public static extern NativeResult NRConfigDestroy(UInt64 session_handle, UInt64 config_handle);
  215. /// <summary> Nr configuration get trackable plane finding mode. </summary>
  216. /// <param name="session_handle"> Handle of the session.</param>
  217. /// <param name="config_handle"> Handle of the configuration.</param>
  218. /// <param name="out_trackable_plane_finding_mode"> [in,out] The out trackable plane finding mode.</param>
  219. /// <returns> A NativeResult. </returns>
  220. [DllImport(NativeConstants.NRNativeLibrary)]
  221. public static extern NativeResult NRConfigGetTrackablePlaneFindingMode(UInt64 session_handle,
  222. UInt64 config_handle, ref TrackablePlaneFindingMode out_trackable_plane_finding_mode);
  223. /// <summary> Nr configuration set trackable plane finding mode. </summary>
  224. /// <param name="session_handle"> Handle of the session.</param>
  225. /// <param name="config_handle"> Handle of the configuration.</param>
  226. /// <param name="trackable_plane_finding_mode"> The trackable plane finding mode.</param>
  227. /// <returns> A NativeResult. </returns>
  228. [DllImport(NativeConstants.NRNativeLibrary)]
  229. public static extern NativeResult NRConfigSetTrackablePlaneFindingMode(UInt64 session_handle,
  230. UInt64 config_handle, int trackable_plane_finding_mode);
  231. /// <summary> Nr configuration get trackable image database. </summary>
  232. /// <param name="session_handle"> Handle of the session.</param>
  233. /// <param name="config_handle"> Handle of the configuration.</param>
  234. /// <param name="out_trackable_image_database_handle"> [in,out] Handle of the out trackable
  235. /// image database.</param>
  236. /// <returns> A NativeResult. </returns>
  237. [DllImport(NativeConstants.NRNativeLibrary)]
  238. public static extern NativeResult NRConfigGetTrackableImageDatabase(UInt64 session_handle,
  239. UInt64 config_handle, ref UInt64 out_trackable_image_database_handle);
  240. /// <summary> Nr configuration set trackable image database. </summary>
  241. /// <param name="session_handle"> Handle of the session.</param>
  242. /// <param name="config_handle"> Handle of the configuration.</param>
  243. /// <param name="trackable_image_database_handle"> Handle of the trackable image database.</param>
  244. /// <returns> A NativeResult. </returns>
  245. [DllImport(NativeConstants.NRNativeLibrary)]
  246. public static extern NativeResult NRConfigSetTrackableImageDatabase(UInt64 session_handle,
  247. UInt64 config_handle, UInt64 trackable_image_database_handle);
  248. };
  249. }
  250. }