NRTrackableManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /// <summary>
  14. /// Manages AR system state and handles the session lifecycle. this class, application can create
  15. /// a session, configure it, start/pause or stop it. </summary>
  16. public class NRTrackableManager : ILifecycle
  17. {
  18. /// <summary> Dictionary of trackables. </summary>
  19. private Dictionary<UInt64, NRTrackable> m_TrackableDict = new Dictionary<UInt64, NRTrackable>();
  20. /// <summary> Dictionary of trackable types. </summary>
  21. private Dictionary<TrackableType, Dictionary<UInt64, NRTrackable>> m_TrackableTypeDict = new Dictionary<TrackableType, Dictionary<ulong, NRTrackable>>();
  22. /// <summary> all trackables. </summary>
  23. private List<NRTrackable> m_AllTrackables = new List<NRTrackable>();
  24. /// <summary> The new trackables. </summary>
  25. private List<NRTrackable> m_NewTrackables = new List<NRTrackable>();
  26. /// <summary> The old trackables. </summary>
  27. private HashSet<NRTrackable> m_OldTrackables = new HashSet<NRTrackable>();
  28. /// <summary> Temp trackable handle list. </summary>
  29. private List<UInt64> m_TempTrackableHandles = new List<UInt64>();
  30. private NRTrackableSubsystem m_TrackableSubsystem;
  31. public NRTrackableSubsystem TrackableSubsystem
  32. {
  33. get
  34. {
  35. if (m_TrackableSubsystem == null)
  36. {
  37. string trackable_match = NRTrackableSubsystemDescriptor.Name;
  38. List<NRTrackableSubsystemDescriptor> trackableDes = new List<NRTrackableSubsystemDescriptor>();
  39. NRSubsystemManager.GetSubsystemDescriptors(trackableDes);
  40. foreach (var descripe in trackableDes)
  41. {
  42. if (descripe.id.Equals(trackable_match))
  43. {
  44. m_TrackableSubsystem = descripe.Create();
  45. }
  46. }
  47. }
  48. return m_TrackableSubsystem;
  49. }
  50. }
  51. private NRPlaneSubsystem m_PlaneSubsystem;
  52. public NRPlaneSubsystem PlaneSubsystem
  53. {
  54. get
  55. {
  56. if (m_PlaneSubsystem == null)
  57. {
  58. string trackable_match = NRPlaneSubsystemDescriptor.Name;
  59. List<NRPlaneSubsystemDescriptor> trackableDes = new List<NRPlaneSubsystemDescriptor>();
  60. NRSubsystemManager.GetSubsystemDescriptors(trackableDes);
  61. foreach (var descripe in trackableDes)
  62. {
  63. if (descripe.id.Equals(trackable_match))
  64. {
  65. m_PlaneSubsystem = descripe.Create();
  66. }
  67. }
  68. }
  69. return m_PlaneSubsystem;
  70. }
  71. }
  72. private NRTrackableImageSubsystem m_TrackableImagesSubsystem;
  73. public NRTrackableImageSubsystem TrackableImageSubsystem
  74. {
  75. get
  76. {
  77. if (m_TrackableImagesSubsystem == null)
  78. {
  79. string trackable_match = NRTrackableImageSubsystemDescriptor.Name;
  80. List<NRTrackableImageSubsystemDescriptor> trackableDes = new List<NRTrackableImageSubsystemDescriptor>();
  81. NRSubsystemManager.GetSubsystemDescriptors(trackableDes);
  82. foreach (var descripe in trackableDes)
  83. {
  84. if (descripe.id.Equals(trackable_match))
  85. {
  86. m_TrackableImagesSubsystem = descripe.Create();
  87. }
  88. }
  89. }
  90. return m_TrackableImagesSubsystem;
  91. }
  92. }
  93. /// <summary> Constructor. </summary>
  94. /// <param name="nativeInterface"> The native interface.</param>
  95. public NRTrackableManager() { }
  96. /// <summary> Creates a new NRTrackable. </summary>
  97. /// <param name="trackable_handle"> Handle of the trackable.</param>
  98. /// <param name="nativeInterface"> The native interface.</param>
  99. /// <returns> A NRTrackable. </returns>
  100. private NRTrackable Create(UInt64 trackable_handle)
  101. {
  102. if (trackable_handle == 0 || !TrackableSubsystem.running)
  103. {
  104. return null;
  105. }
  106. NRTrackable result;
  107. if (m_TrackableDict.TryGetValue(trackable_handle, out result))
  108. {
  109. return result;
  110. }
  111. TrackableType trackableType = TrackableSubsystem.GetTrackableType(trackable_handle);
  112. if (trackableType == TrackableType.TRACKABLE_PLANE)
  113. {
  114. result = new NRTrackablePlane(trackable_handle);
  115. }
  116. else if (trackableType == TrackableType.TRACKABLE_IMAGE)
  117. {
  118. result = new NRTrackableImage(trackable_handle);
  119. }
  120. else
  121. {
  122. throw new NotImplementedException(
  123. "TrackableFactory::No constructor for requested trackable type.");
  124. }
  125. if (result != null)
  126. {
  127. m_TrackableDict.Add(trackable_handle, result);
  128. if (!m_TrackableTypeDict.ContainsKey(trackableType))
  129. {
  130. m_TrackableTypeDict.Add(trackableType, new Dictionary<ulong, NRTrackable>());
  131. }
  132. Dictionary<ulong, NRTrackable> trackbletype_dict = null;
  133. m_TrackableTypeDict.TryGetValue(trackableType, out trackbletype_dict);
  134. if (!trackbletype_dict.ContainsKey(trackable_handle))
  135. {
  136. trackbletype_dict.Add(trackable_handle, result);
  137. m_AllTrackables.Add(result);
  138. }
  139. }
  140. return result;
  141. }
  142. /// <summary> Updates the trackables described by trackable_type. </summary>
  143. /// <param name="trackable_type"> Type of the trackable.</param>
  144. private void UpdateTrackables(TrackableType trackable_type)
  145. {
  146. if (!TrackableSubsystem.running)
  147. {
  148. return;
  149. }
  150. if (TrackableSubsystem.UpdateTrackables(trackable_type, m_TempTrackableHandles))
  151. {
  152. for (int i = 0; i < m_TempTrackableHandles.Count; i++)
  153. {
  154. Create(m_TempTrackableHandles[i]);
  155. }
  156. }
  157. }
  158. /// <summary> Get the list of trackables with specified filter. </summary>
  159. /// <typeparam name="T"> Generic type parameter.</typeparam>
  160. /// <param name="trackables"> trackableList A list where the returned trackable stored. The
  161. /// previous values will be cleared.</param>
  162. /// <param name="filter"> Query filter.</param>
  163. public void GetTrackables<T>(List<T> trackables, NRTrackableQueryFilter filter) where T : NRTrackable
  164. {
  165. TrackableType t_type = GetTrackableType<T>();
  166. // Update trackable by type
  167. UpdateTrackables(t_type);
  168. // Find the new trackable in this frame
  169. m_NewTrackables.Clear();
  170. for (int i = 0; i < m_AllTrackables.Count; i++)
  171. {
  172. NRTrackable trackable = m_AllTrackables[i];
  173. if (!m_OldTrackables.Contains(trackable))
  174. {
  175. m_NewTrackables.Add(trackable);
  176. m_OldTrackables.Add(trackable);
  177. }
  178. }
  179. trackables.Clear();
  180. if (filter == NRTrackableQueryFilter.All)
  181. {
  182. for (int i = 0; i < m_AllTrackables.Count; i++)
  183. {
  184. SafeAdd<T>(m_AllTrackables[i], trackables);
  185. }
  186. }
  187. else if (filter == NRTrackableQueryFilter.New)
  188. {
  189. for (int i = 0; i < m_NewTrackables.Count; i++)
  190. {
  191. SafeAdd<T>(m_NewTrackables[i], trackables);
  192. }
  193. }
  194. }
  195. /// <summary> Safe add. </summary>
  196. /// <typeparam name="T"> Generic type parameter.</typeparam>
  197. /// <param name="trackable"> The trackable.</param>
  198. /// <param name="trackables"> trackableList A list where the returned trackable stored. The
  199. /// previous values will be cleared.</param>
  200. private void SafeAdd<T>(NRTrackable trackable, List<T> trackables) where T : NRTrackable
  201. {
  202. if (trackable is T)
  203. {
  204. trackables.Add(trackable as T);
  205. }
  206. }
  207. /// <summary> Gets trackable type. </summary>
  208. /// <typeparam name="T"> Generic type parameter.</typeparam>
  209. /// <returns> The trackable type. </returns>
  210. private TrackableType GetTrackableType<T>() where T : NRTrackable
  211. {
  212. if (typeof(NRTrackablePlane).Equals(typeof(T)))
  213. {
  214. return TrackableType.TRACKABLE_PLANE;
  215. }
  216. else if (typeof(NRTrackableImage).Equals(typeof(T)))
  217. {
  218. return TrackableType.TRACKABLE_IMAGE;
  219. }
  220. return TrackableType.TRACKABLE_BASE;
  221. }
  222. public void Start()
  223. {
  224. TrackableSubsystem.Start();
  225. PlaneSubsystem.Start();
  226. TrackableImageSubsystem.Start();
  227. }
  228. public void Pause()
  229. {
  230. TrackableSubsystem.Pause();
  231. PlaneSubsystem.Pause();
  232. TrackableImageSubsystem.Pause();
  233. }
  234. public void Resume()
  235. {
  236. TrackableSubsystem.Resume();
  237. PlaneSubsystem.Resume();
  238. TrackableImageSubsystem.Resume();
  239. }
  240. public void Stop()
  241. {
  242. TrackableSubsystem.Stop();
  243. PlaneSubsystem.Stop();
  244. TrackableImageSubsystem.Stop();
  245. }
  246. }
  247. }