NativeTrackableImage.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.Runtime.InteropServices;
  13. using System.Text;
  14. using UnityEngine;
  15. /// <summary> 6-dof Trackable Image Tracking's Native API . </summary>
  16. public partial class NativeTrackableImage
  17. {
  18. /// <summary> The native interface. </summary>
  19. private NativeInterface m_NativeInterface;
  20. /// <summary> Constructor. </summary>
  21. /// <param name="nativeInterface"> The native interface.</param>
  22. public NativeTrackableImage(NativeInterface nativeInterface)
  23. {
  24. m_NativeInterface = nativeInterface;
  25. }
  26. /// <summary> Creates data base. </summary>
  27. /// <returns> The new data base. </returns>
  28. public UInt64 CreateDataBase()
  29. {
  30. UInt64 database_handle = 0;
  31. var result = NativeApi.NRTrackableImageDatabaseCreate(m_NativeInterface.TrackingHandle, ref database_handle);
  32. NativeErrorListener.Check(result, this, "CreateDataBase");
  33. return database_handle;
  34. }
  35. /// <summary> Destroys the data base described by database_handle. </summary>
  36. /// <param name="database_handle"> Handle of the database.</param>
  37. /// <returns> True if it succeeds, false if it fails. </returns>
  38. public bool DestroyDataBase(UInt64 database_handle)
  39. {
  40. var result = NativeApi.NRTrackableImageDatabaseDestroy(m_NativeInterface.TrackingHandle, database_handle);
  41. NativeErrorListener.Check(result, this, "DestroyDataBase");
  42. return result == NativeResult.Success;
  43. }
  44. /// <summary> Loads data base. </summary>
  45. /// <param name="database_handle"> Handle of the database.</param>
  46. /// <param name="path"> Full pathname of the file.</param>
  47. /// <returns> True if it succeeds, false if it fails. </returns>
  48. public bool LoadDataBase(UInt64 database_handle, string path)
  49. {
  50. var result = NativeApi.NRTrackableImageDatabaseLoadDirectory(m_NativeInterface.TrackingHandle, database_handle, path);
  51. return result == NativeResult.Success;
  52. }
  53. /// <summary> Gets center pose. </summary>
  54. /// <param name="trackable_handle"> Handle of the trackable.</param>
  55. /// <returns> The center pose. </returns>
  56. public Pose GetCenterPose(UInt64 trackable_handle)
  57. {
  58. Pose pose = Pose.identity;
  59. NativeMat4f center_pose_native = NativeMat4f.identity;
  60. NativeApi.NRTrackableImageGetCenterPose(m_NativeInterface.TrackingHandle, trackable_handle, ref center_pose_native);
  61. ConversionUtility.ApiPoseToUnityPose(center_pose_native, out pose);
  62. return pose;
  63. }
  64. /// <summary> Gets a size. </summary>
  65. /// <param name="trackable_handle"> Handle of the trackable.</param>
  66. /// <returns> The size. </returns>
  67. public Vector2 GetSize(UInt64 trackable_handle)
  68. {
  69. float extent_x, extent_z;
  70. extent_x = extent_z = 0;
  71. NativeApi.NRTrackableImageGetExtentX(m_NativeInterface.TrackingHandle, trackable_handle, ref extent_x);
  72. NativeApi.NRTrackableImageGetExtentZ(m_NativeInterface.TrackingHandle, trackable_handle, ref extent_z);
  73. return new Vector2(extent_x * 0.001f, extent_z * 0.001f);
  74. }
  75. private partial struct NativeApi
  76. {
  77. /// <summary> Nr trackable image database create. </summary>
  78. /// <param name="session_handle"> Handle of the session.</param>
  79. /// <param name="out_trackable_image_database_handle"> [in,out] Handle of the out trackable
  80. /// image database.</param>
  81. /// <returns> A NativeResult. </returns>
  82. [DllImport(NativeConstants.NRNativeLibrary)]
  83. public static extern NativeResult NRTrackableImageDatabaseCreate(UInt64 session_handle,
  84. ref UInt64 out_trackable_image_database_handle);
  85. /// <summary> Nr trackable image database destroy. </summary>
  86. /// <param name="session_handle"> Handle of the session.</param>
  87. /// <param name="trackable_image_database_handle"> Handle of the trackable image database.</param>
  88. /// <returns> A NativeResult. </returns>
  89. [DllImport(NativeConstants.NRNativeLibrary)]
  90. public static extern NativeResult NRTrackableImageDatabaseDestroy(UInt64 session_handle,
  91. UInt64 trackable_image_database_handle);
  92. /// <summary> Nr trackable image database load directory. </summary>
  93. /// <param name="session_handle"> Handle of the session.</param>
  94. /// <param name="trackable_image_database_handle"> Handle of the trackable image database.</param>
  95. /// <param name="trackable_image_database_directory"> Pathname of the trackable image database
  96. /// directory.</param>
  97. /// <returns> A NativeResult. </returns>
  98. [DllImport(NativeConstants.NRNativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  99. public static extern NativeResult NRTrackableImageDatabaseLoadDirectory(UInt64 session_handle,
  100. UInt64 trackable_image_database_handle, string trackable_image_database_directory);
  101. /// <summary> Nr trackable image get center pose. </summary>
  102. /// <param name="session_handle"> Handle of the session.</param>
  103. /// <param name="trackable_handle"> Handle of the trackable.</param>
  104. /// <param name="out_center_pose"> [in,out] The out center pose.</param>
  105. /// <returns> A NativeResult. </returns>
  106. [DllImport(NativeConstants.NRNativeLibrary)]
  107. public static extern NativeResult NRTrackableImageGetCenterPose(UInt64 session_handle,
  108. UInt64 trackable_handle, ref NativeMat4f out_center_pose);
  109. /// <summary> Nr trackable image get extent x coordinate. </summary>
  110. /// <param name="session_handle"> Handle of the session.</param>
  111. /// <param name="trackable_handle"> Handle of the trackable.</param>
  112. /// <param name="out_extent_x"> [in,out] The out extent x coordinate.</param>
  113. /// <returns> A NativeResult. </returns>
  114. [DllImport(NativeConstants.NRNativeLibrary)]
  115. public static extern NativeResult NRTrackableImageGetExtentX(UInt64 session_handle,
  116. UInt64 trackable_handle, ref float out_extent_x);
  117. /// <summary> Nr trackable image get extent z coordinate. </summary>
  118. /// <param name="session_handle"> Handle of the session.</param>
  119. /// <param name="trackable_handle"> Handle of the trackable.</param>
  120. /// <param name="out_extent_z"> [in,out] The out extent z coordinate.</param>
  121. /// <returns> A NativeResult. </returns>
  122. [DllImport(NativeConstants.NRNativeLibrary)]
  123. public static extern NativeResult NRTrackableImageGetExtentZ(UInt64 session_handle,
  124. UInt64 trackable_handle, ref float out_extent_z);
  125. };
  126. }
  127. }