SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
8.6 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace UnityEngine.XR.ARSubsystems
  5. {
  6. /// <summary>
  7. /// Holds arrays of changes (added, updated, and removed) to trackables.
  8. /// This is typically used by subsystems to report changes each frame.
  9. /// </summary>
  10. /// <typeparam name="T">The <see cref="ITrackable"/> that can be added and updated.</typeparam>
  11. public struct TrackableChanges<T> : IDisposable where T : struct, ITrackable
  12. {
  13. /// <summary>
  14. /// An array of added trackables.
  15. /// </summary>
  16. public NativeArray<T> added { get { return m_Added; } }
  17. /// <summary>
  18. /// An array of updated trackables.
  19. /// </summary>
  20. public NativeArray<T> updated { get { return m_Updated; } }
  21. /// <summary>
  22. /// An array of <see cref="TrackableId"/>s that have been removed.
  23. /// </summary>
  24. public NativeArray<TrackableId> removed { get { return m_Removed; } }
  25. /// <summary>
  26. /// Whether the <c>NativeArray</c>s have been created.
  27. /// </summary>
  28. public bool isCreated { get; private set; }
  29. /// <summary>
  30. /// Constructs a <see cref="TrackableChanges{T}"/> from the number of added, updated, and removed trackables.
  31. /// This creates the <c>NativeArray</c>s <see cref="added"/>, <see cref="updated"/>, and <see cref="removed"/>
  32. /// and should be disposed by calling <see cref="Dispose"/> on this object.
  33. /// </summary>
  34. /// <param name="addedCount">The number of added trackables.</param>
  35. /// <param name="updatedCount">The number of updated trackables.</param>
  36. /// <param name="removedCount">The number of removed trackables.</param>
  37. /// <param name="allocator">
  38. /// An allocator to use for each of
  39. /// <see cref="added"/>, <see cref="updated"/>, and <see cref="removed"/> arrays.
  40. /// </param>
  41. public TrackableChanges(
  42. int addedCount,
  43. int updatedCount,
  44. int removedCount,
  45. Allocator allocator)
  46. {
  47. m_Added = new NativeArray<T>(addedCount, allocator);
  48. m_Updated = new NativeArray<T>(updatedCount, allocator);
  49. m_Removed = new NativeArray<TrackableId>(removedCount, allocator);
  50. isCreated = true;
  51. }
  52. /// <summary>
  53. /// Constructor which creates all three arrays and fills the
  54. /// <see cref="added"/> and <see cref="updated"/> arrays with repeated copies of
  55. /// <paramref name="defaultValue"/>.
  56. /// </summary>
  57. /// <param name="addedCount">The number of added trackables.</param>
  58. /// <param name="updatedCount">The number of updated trackables.</param>
  59. /// <param name="removedCount">The number of removed trackables.</param>
  60. /// <param name="allocator">The allocator to use for each of <see cref="added"/>, <see cref="updated"/>, and <see cref="removed"/> arrays.</param>
  61. /// <param name="defaultValue">The value with which to fill the <see cref="added"/> and <see cref="updated"/> arrays.</param>
  62. public TrackableChanges(
  63. int addedCount,
  64. int updatedCount,
  65. int removedCount,
  66. Allocator allocator,
  67. T defaultValue)
  68. {
  69. m_Added = NativeCopyUtility.CreateArrayFilledWithValue(defaultValue, addedCount, allocator);
  70. m_Updated = NativeCopyUtility.CreateArrayFilledWithValue(defaultValue, updatedCount, allocator);
  71. m_Removed = new NativeArray<TrackableId>(removedCount, allocator);
  72. isCreated = true;
  73. }
  74. /// <summary>
  75. /// Constructs a <see cref="TrackableChanges{T}"/> from native memory.
  76. /// </summary>
  77. /// <remarks>
  78. /// Because native code may be using an older version of <typeparamref name="T"/>,
  79. /// this constructor first fills the <see cref="added"/> and <see cref="updated"/>
  80. /// arrays with copies of <paramref name="defaultT"/> before copying the data from
  81. /// the <paramref name="addedPtr"/> and <paramref name="updatedPtr"/> pointers.
  82. /// This ensures that the addition of new fields to <typeparamref name="T"/> will have
  83. /// appropriate default values.
  84. /// </remarks>
  85. /// <param name="addedPtr">A pointer to a block of memory continaing <paramref name="addedCount"/> elements each of size <paramref name="stride"/>.</param>
  86. /// <param name="addedCount">The number of added elements.</param>
  87. /// <param name="updatedPtr">A pointer to a block of memory continaing <paramref name="updatedCount"/> elements each of size <paramref name="stride"/>.</param>
  88. /// <param name="updatedCount">The number of updated elements.</param>
  89. /// <param name="removedPtr">A pointer to a block of memory containing <paramref name="removedCount"/> <see cref="TrackableId"/>s.</param>
  90. /// <param name="removedCount">The number of removed elements.</param>
  91. /// <param name="defaultT">A default <typeparamref name="T"/> which should be used to fill the <see cref="added"/> and <see cref="updated"/>
  92. /// arrays before copying data from <paramref name="addedPtr"/> and <paramref name="updatedPtr"/>, respectively.</param>
  93. /// <param name="stride">The number of bytes for each element in the <paramref name="addedPtr"/> and <paramref name="updatedPtr"/> arrays.</param>
  94. /// <param name="allocator">An allocator to use when creating the <see cref="added"/>, <see cref="updated"/>, and <see cref="removed"/> arrays.</param>
  95. public unsafe TrackableChanges(
  96. void* addedPtr, int addedCount,
  97. void* updatedPtr, int updatedCount,
  98. void* removedPtr, int removedCount,
  99. T defaultT, int stride,
  100. Allocator allocator)
  101. {
  102. m_Added = NativeCopyUtility.PtrToNativeArrayWithDefault<T>(defaultT, addedPtr, stride, addedCount, allocator);
  103. m_Updated = NativeCopyUtility.PtrToNativeArrayWithDefault<T>(defaultT, updatedPtr, stride, updatedCount, allocator);
  104. m_Removed = new NativeArray<TrackableId>(removedCount, allocator);
  105. if (removedCount > 0)
  106. {
  107. m_Removed.CopyFrom(NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<TrackableId>(
  108. removedPtr, removedCount, Allocator.None));
  109. }
  110. isCreated = true;
  111. }
  112. /// <summary>
  113. /// Creates a new <see cref="TrackableChanges{T}"/> from existing <c>NativeArrays</c>.
  114. /// </summary>
  115. /// <param name="added">An array of added elements.</param>
  116. /// <param name="updated">An array of updated elements.</param>
  117. /// <param name="removed">An array of removed elements.</param>
  118. /// <param name="allocator">The allocator to use for each of the <see cref="added"/>, <see cref="updated"/>, and <see cref="removed"/> arrays.</param>
  119. /// <returns>a new <see cref="TrackableChanges{T}"/> from existing <c>NativeArrays</c>. The caller must <see cref="Dispose"/>
  120. /// the returned <see cref="TrackableChanges{T}"/> to avoid memory leaks.</returns>
  121. public static TrackableChanges<T> CopyFrom(
  122. NativeArray<T> added,
  123. NativeArray<T> updated,
  124. NativeArray<TrackableId> removed,
  125. Allocator allocator)
  126. {
  127. var addedCopy = new NativeArray<T>(added.Length, allocator);
  128. addedCopy.CopyFrom(added);
  129. var updatedCopy = new NativeArray<T>(updated.Length, allocator);
  130. updatedCopy.CopyFrom(updated);
  131. var removedCopy = new NativeArray<TrackableId>(removed.Length, allocator);
  132. removedCopy.CopyFrom(removed);
  133. return new TrackableChanges<T>(addedCopy, updatedCopy, removedCopy);
  134. }
  135. /// <summary>
  136. /// Disposes the <see cref="added"/>, <see cref="updated"/>, and <see cref="removed"/> arrays.
  137. /// Safe to call on a default-constructed <see cref="TrackableChanges{T}"/>.
  138. /// </summary>
  139. public void Dispose()
  140. {
  141. if (isCreated)
  142. {
  143. m_Added.Dispose();
  144. m_Updated.Dispose();
  145. m_Removed.Dispose();
  146. }
  147. isCreated = false;
  148. }
  149. TrackableChanges(
  150. NativeArray<T> added,
  151. NativeArray<T> updated,
  152. NativeArray<TrackableId> removed)
  153. {
  154. m_Added = added;
  155. m_Updated = updated;
  156. m_Removed = removed;
  157. isCreated = true;
  158. }
  159. NativeArray<T> m_Added;
  160. NativeArray<T> m_Updated;
  161. NativeArray<TrackableId> m_Removed;
  162. }
  163. }