NativeTrackable.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /// <summary> 6-dof Trackable's Native API . </summary>
  15. public partial class NativeTrackable
  16. {
  17. /// <summary> The native interface. </summary>
  18. private NativeInterface m_NativeInterface;
  19. /// <summary> Constructor. </summary>
  20. /// <param name="nativeInterface"> The native interface.</param>
  21. public NativeTrackable(NativeInterface nativeInterface)
  22. {
  23. m_NativeInterface = nativeInterface;
  24. }
  25. public bool UpdateTrackables(TrackableType trackable_type, List<UInt64> trackables)
  26. {
  27. if (m_NativeInterface == null || m_NativeInterface.TrackingHandle == 0)
  28. {
  29. return false;
  30. }
  31. trackables.Clear();
  32. UInt64 trackable_list_handle = 0;
  33. var create_result = NativeApi.NRTrackableListCreate(m_NativeInterface.TrackingHandle, ref trackable_list_handle);
  34. var update_result = NativeApi.NRTrackingUpdateTrackables(m_NativeInterface.TrackingHandle, trackable_type, trackable_list_handle);
  35. int list_size = 0;
  36. var getsize_result = NativeApi.NRTrackableListGetSize(m_NativeInterface.TrackingHandle, trackable_list_handle, ref list_size);
  37. for (int i = 0; i < list_size; i++)
  38. {
  39. UInt64 trackable_handle = 0;
  40. var acquireitem_result = NativeApi.NRTrackableListAcquireItem(m_NativeInterface.TrackingHandle, trackable_list_handle, i, ref trackable_handle);
  41. if (acquireitem_result == NativeResult.Success) trackables.Add(trackable_handle);
  42. }
  43. var destroy_result = NativeApi.NRTrackableListDestroy(m_NativeInterface.TrackingHandle, trackable_list_handle);
  44. return (create_result == NativeResult.Success) && (update_result == NativeResult.Success)
  45. && (getsize_result == NativeResult.Success) && (destroy_result == NativeResult.Success);
  46. }
  47. /// <summary> Gets an identify. </summary>
  48. /// <param name="trackable_handle"> Handle of the trackable.</param>
  49. /// <returns> The identify. </returns>
  50. public UInt32 GetIdentify(UInt64 trackable_handle)
  51. {
  52. if (m_NativeInterface.TrackingHandle == 0)
  53. {
  54. return 0;
  55. }
  56. UInt32 identify = NativeConstants.IllegalInt;
  57. NativeApi.NRTrackableGetIdentifier(m_NativeInterface.TrackingHandle, trackable_handle, ref identify);
  58. return identify;
  59. }
  60. /// <summary> Gets trackable type. </summary>
  61. /// <param name="trackable_handle"> Handle of the trackable.</param>
  62. /// <returns> The trackable type. </returns>
  63. public TrackableType GetTrackableType(UInt64 trackable_handle)
  64. {
  65. if (m_NativeInterface.TrackingHandle == 0)
  66. {
  67. return TrackableType.TRACKABLE_BASE;
  68. }
  69. TrackableType trackble_type = TrackableType.TRACKABLE_BASE;
  70. NativeApi.NRTrackableGetType(m_NativeInterface.TrackingHandle, trackable_handle, ref trackble_type);
  71. return trackble_type;
  72. }
  73. /// <summary> Gets tracking state. </summary>
  74. /// <param name="trackable_handle"> Handle of the trackable.</param>
  75. /// <returns> The tracking state. </returns>
  76. public TrackingState GetTrackingState(UInt64 trackable_handle)
  77. {
  78. if (m_NativeInterface.TrackingHandle == 0)
  79. {
  80. return TrackingState.Stopped;
  81. }
  82. TrackingState status = TrackingState.Stopped;
  83. NativeApi.NRTrackableGetTrackingState(m_NativeInterface.TrackingHandle, trackable_handle, ref status);
  84. return status;
  85. }
  86. private partial struct NativeApi
  87. {
  88. /// <summary> Nr trackable list create. </summary>
  89. /// <param name="session_handle"> Handle of the session.</param>
  90. /// <param name="out_trackable_list_handle"> [in,out] Handle of the out trackable list.</param>
  91. /// <returns> A NativeResult. </returns>
  92. [DllImport(NativeConstants.NRNativeLibrary)]
  93. public static extern NativeResult NRTrackableListCreate(UInt64 session_handle,
  94. ref UInt64 out_trackable_list_handle);
  95. /// <summary> Nr trackable list destroy. </summary>
  96. /// <param name="session_handle"> Handle of the session.</param>
  97. /// <param name="trackable_list_handle"> Handle of the out trackable list.</param>
  98. /// <returns> A NativeResult. </returns>
  99. [DllImport(NativeConstants.NRNativeLibrary)]
  100. public static extern NativeResult NRTrackableListDestroy(UInt64 session_handle,
  101. UInt64 trackable_list_handle);
  102. /// <summary> Nr tracking update trackables. </summary>
  103. /// <param name="tracking_handle"> Handle of the tracking.</param>
  104. /// <param name="trackable_type"> Type of the trackable.</param>
  105. /// <param name="trackable_list_handle"> Handle of the out trackable list.</param>
  106. /// <returns> A NativeResult. </returns>
  107. [DllImport(NativeConstants.NRNativeLibrary)]
  108. public static extern NativeResult NRTrackingUpdateTrackables(UInt64 tracking_handle,
  109. TrackableType trackable_type, UInt64 trackable_list_handle);
  110. /// <summary> Nr trackable list get size. </summary>
  111. /// <param name="session_handle"> Handle of the session.</param>
  112. /// <param name="trackable_list_handle"> Handle of the trackable list.</param>
  113. /// <param name="out_list_size"> [in,out] Size of the out list.</param>
  114. /// <returns> A NativeResult. </returns>
  115. [DllImport(NativeConstants.NRNativeLibrary)]
  116. public static extern NativeResult NRTrackableListGetSize(UInt64 session_handle,
  117. UInt64 trackable_list_handle, ref int out_list_size);
  118. /// <summary> Nr trackable list acquire item. </summary>
  119. /// <param name="session_handle"> Handle of the session.</param>
  120. /// <param name="trackable_list_handle"> Handle of the trackable list.</param>
  121. /// <param name="index"> Zero-based index of the.</param>
  122. /// <param name="out_trackable_item_handle"> [in,out] The out trackable.</param>
  123. /// <returns> A NativeResult. </returns>
  124. [DllImport(NativeConstants.NRNativeLibrary)]
  125. public static extern NativeResult NRTrackableListAcquireItem(UInt64 session_handle,
  126. UInt64 trackable_list_handle, int index, ref UInt64 out_trackable_item_handle);
  127. /// <summary> Nr trackable get identifier. </summary>
  128. /// <param name="session_handle"> Handle of the session.</param>
  129. /// <param name="trackable_handle"> Handle of the trackable.</param>
  130. /// <param name="out_identifier"> [in,out] Identifier for the out.</param>
  131. /// <returns> A NativeResult. </returns>
  132. [DllImport(NativeConstants.NRNativeLibrary)]
  133. public static extern NativeResult NRTrackableGetIdentifier(UInt64 session_handle,
  134. UInt64 trackable_handle, ref UInt32 out_identifier);
  135. /// <summary> Nr trackable get type. </summary>
  136. /// <param name="session_handle"> Handle of the session.</param>
  137. /// <param name="trackable_handle"> Handle of the trackable.</param>
  138. /// <param name="out_trackable_type"> [in,out] Type of the out trackable.</param>
  139. /// <returns> A NativeResult. </returns>
  140. [DllImport(NativeConstants.NRNativeLibrary)]
  141. public static extern NativeResult NRTrackableGetType(UInt64 session_handle,
  142. UInt64 trackable_handle, ref TrackableType out_trackable_type);
  143. /// <summary> Nr trackable get tracking state. </summary>
  144. /// <param name="session_handle"> Handle of the session.</param>
  145. /// <param name="trackable_handle"> Handle of the trackable.</param>
  146. /// <param name="out_tracking_state"> [in,out] State of the out tracking.</param>
  147. /// <returns> A NativeResult. </returns>
  148. [DllImport(NativeConstants.NRNativeLibrary)]
  149. public static extern NativeResult NRTrackableGetTrackingState(UInt64 session_handle,
  150. UInt64 trackable_handle, ref TrackingState out_tracking_state);
  151. };
  152. }
  153. }