NativePlane.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 UnityEngine;
  15. /// <summary> 6-dof Plane Tracking's Native API . </summary>
  16. public partial class NativePlane
  17. {
  18. /// <summary> The native interface. </summary>
  19. private NativeInterface m_NativeInterface;
  20. /// <summary> The maximum size of the polygon. </summary>
  21. private const int m_MaxPolygonSize = 1024;
  22. /// <summary> The points. </summary>
  23. private float[] m_Points;
  24. /// <summary> Handle of the temporary points. </summary>
  25. private GCHandle m_TmpPointsHandle;
  26. /// <summary> Constructor. </summary>
  27. /// <param name="nativeInterface"> The native interface.</param>
  28. public NativePlane(NativeInterface nativeInterface)
  29. {
  30. m_NativeInterface = nativeInterface;
  31. m_Points = new float[m_MaxPolygonSize * 2];
  32. m_TmpPointsHandle = GCHandle.Alloc(m_Points, GCHandleType.Pinned);
  33. }
  34. /// <summary> Finalizer. </summary>
  35. ~NativePlane()
  36. {
  37. m_TmpPointsHandle.Free();
  38. }
  39. /// <summary> Gets plane type. </summary>
  40. /// <param name="trackable_handle"> Handle of the trackable.</param>
  41. /// <returns> The plane type. </returns>
  42. public TrackablePlaneType GetPlaneType(UInt64 trackable_handle)
  43. {
  44. TrackablePlaneType plane_type = TrackablePlaneType.INVALID;
  45. NativeApi.NRTrackablePlaneGetType(m_NativeInterface.TrackingHandle, trackable_handle, ref plane_type);
  46. return plane_type;
  47. }
  48. /// <summary> Gets center pose. </summary>
  49. /// <param name="trackable_handle"> Handle of the trackble.</param>
  50. /// <returns> The center pose. </returns>
  51. public Pose GetCenterPose(UInt64 trackable_handle)
  52. {
  53. Pose pose = Pose.identity;
  54. NativeMat4f center_native_pos = NativeMat4f.identity;
  55. NativeApi.NRTrackablePlaneGetCenterPose(m_NativeInterface.TrackingHandle, trackable_handle, ref center_native_pos);
  56. ConversionUtility.ApiPoseToUnityPose(center_native_pos, out pose);
  57. return pose;
  58. }
  59. /// <summary> Gets extent x coordinate. </summary>
  60. /// <param name="trackable_handle"> Handle of the trackable.</param>
  61. /// <returns> The extent x coordinate. </returns>
  62. public float GetExtentX(UInt64 trackable_handle)
  63. {
  64. float extent_x = 0;
  65. NativeApi.NRTrackablePlaneGetExtentX(m_NativeInterface.TrackingHandle, trackable_handle, ref extent_x);
  66. return extent_x;
  67. }
  68. /// <summary> Gets extent z coordinate. </summary>
  69. /// <param name="trackable_handle"> Handle of the trackable.</param>
  70. /// <returns> The extent z coordinate. </returns>
  71. public float GetExtentZ(UInt64 trackable_handle)
  72. {
  73. float extent_z = 0;
  74. NativeApi.NRTrackablePlaneGetExtentZ(m_NativeInterface.TrackingHandle, trackable_handle, ref extent_z);
  75. return extent_z;
  76. }
  77. public void GetBoundaryPolygon(UInt64 trackable_handle, List<Vector3> polygonList)
  78. {
  79. polygonList.Clear();
  80. int polygon_size = 0;
  81. NativeApi.NRTrackablePlaneGetPolygonSize(m_NativeInterface.TrackingHandle, trackable_handle, ref polygon_size);
  82. int size = polygon_size / 2;
  83. NativeApi.NRTrackablePlaneGetPolygon(m_NativeInterface.TrackingHandle, trackable_handle, (m_TmpPointsHandle.AddrOfPinnedObject()));
  84. float[] point_data = new float[size * 2];
  85. Array.Copy(m_Points, point_data, size * 2);
  86. Pose centerPos = GetCenterPose(trackable_handle);
  87. var unityWorldTPlane = Matrix4x4.TRS(centerPos.position, centerPos.rotation, Vector3.one);
  88. var plane_type = GetPlaneType(trackable_handle);
  89. for (int i = 2 * size - 2; i >= 0; i -= 2)
  90. {
  91. Vector3 localpos = new Vector3(point_data[i], 0, -point_data[i + 1]);
  92. polygonList.Add(unityWorldTPlane.MultiplyPoint3x4(localpos));
  93. }
  94. if (plane_type == TrackablePlaneType.VERTICAL)
  95. {
  96. polygonList.Reverse();
  97. }
  98. }
  99. private partial struct NativeApi
  100. {
  101. /// <summary> Nr trackable plane get type. </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_plane_type"> [in,out] Type of the out plane.</param>
  105. /// <returns> A NativeResult. </returns>
  106. [DllImport(NativeConstants.NRNativeLibrary)]
  107. public static extern NativeResult NRTrackablePlaneGetType(UInt64 session_handle,
  108. UInt64 trackable_handle, ref TrackablePlaneType out_plane_type);
  109. /// <summary> Nr trackable plane get center pose. </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_center_pose"> [in,out] The out center pose.</param>
  113. /// <returns> A NativeResult. </returns>
  114. [DllImport(NativeConstants.NRNativeLibrary)]
  115. public static extern NativeResult NRTrackablePlaneGetCenterPose(UInt64 session_handle,
  116. UInt64 trackable_handle, ref NativeMat4f out_center_pose);
  117. /// <summary> Nr trackable plane get extent x 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_x"> [in,out] The out extent x coordinate.</param>
  121. /// <returns> A NativeResult. </returns>
  122. [DllImport(NativeConstants.NRNativeLibrary)]
  123. public static extern NativeResult NRTrackablePlaneGetExtentX(UInt64 session_handle,
  124. UInt64 trackable_handle, ref float out_extent_x);
  125. /// <summary> Nr trackable plane get extent z coordinate. </summary>
  126. /// <param name="session_handle"> Handle of the session.</param>
  127. /// <param name="trackable_handle"> Handle of the trackable.</param>
  128. /// <param name="out_extent_z"> [in,out] The out extent z coordinate.</param>
  129. /// <returns> A NativeResult. </returns>
  130. [DllImport(NativeConstants.NRNativeLibrary)]
  131. public static extern NativeResult NRTrackablePlaneGetExtentZ(UInt64 session_handle,
  132. UInt64 trackable_handle, ref float out_extent_z);
  133. /// <summary> Nr trackable plane get polygon size. </summary>
  134. /// <param name="session_handle"> Handle of the session.</param>
  135. /// <param name="trackable_handle"> Handle of the trackable.</param>
  136. /// <param name="out_polygon_size"> [in,out] Size of the out polygon.</param>
  137. /// <returns> A NativeResult. </returns>
  138. [DllImport(NativeConstants.NRNativeLibrary)]
  139. public static extern NativeResult NRTrackablePlaneGetPolygonSize(UInt64 session_handle,
  140. UInt64 trackable_handle, ref int out_polygon_size);
  141. /// <summary> Nr trackable plane get polygon. </summary>
  142. /// <param name="session_handle"> Handle of the session.</param>
  143. /// <param name="trackable_handle"> Handle of the trackable.</param>
  144. /// <param name="out_polygon"> The out polygon.</param>
  145. /// <returns> A NativeResult. </returns>
  146. [DllImport(NativeConstants.NRNativeLibrary)]
  147. public static extern NativeResult NRTrackablePlaneGetPolygon(UInt64 session_handle,
  148. UInt64 trackable_handle, IntPtr out_polygon);
  149. };
  150. }
  151. }