NRPointCloudCreator.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.Experimental.Persistence
  10. {
  11. using System;
  12. using UnityEngine;
  13. using System.Collections;
  14. public class NRPointCloudCreator
  15. {
  16. #if !UNITY_EDITOR
  17. private NativePointCloud NativePointCloud { get; set; }
  18. #endif
  19. private IPointCloudDrawer PointCloudDrawer { get; set; }
  20. private static NRPointCloudCreator instance;
  21. public bool IsUpdatedThisFrame
  22. {
  23. get
  24. {
  25. #if !UNITY_EDITOR
  26. return NativePointCloud.IsUpdatedThisFrame();
  27. #else
  28. return true;
  29. #endif
  30. }
  31. }
  32. private bool isInited = false;
  33. public static NRPointCloudCreator Create(IPointCloudDrawer drawer)
  34. {
  35. if (instance == null)
  36. {
  37. instance = new NRPointCloudCreator();
  38. #if !UNITY_EDITOR
  39. instance.NativePointCloud = new NativePointCloud();
  40. instance.NativePointCloud.Create();
  41. #endif
  42. instance.PointCloudDrawer = drawer;
  43. instance.Init();
  44. }
  45. return instance;
  46. }
  47. public IEnumerator UpdatePoints()
  48. {
  49. #if UNITY_EDITOR
  50. PointCloudPoint[] points = new PointCloudPoint[1000];
  51. for (int i = 0; i < points.Length; i++)
  52. {
  53. points[i] = new PointCloudPoint();
  54. points[i].Id = i;
  55. points[i].Position = new NativeVector3f(UnityEngine.Random.insideUnitSphere);
  56. points[i].Confidence = UnityEngine.Random.Range(0f, 1f);
  57. }
  58. #endif
  59. while (true)
  60. {
  61. yield return new WaitForSeconds(0.5f);
  62. // Do the points update.
  63. if (IsUpdatedThisFrame)
  64. {
  65. #if !UNITY_EDITOR
  66. var listhandle = NativePointCloud.CreatPointCloudList();
  67. NativePointCloud.UpdatePointCloudList(listhandle);
  68. int len = NativePointCloud.GetSize(listhandle);
  69. #else
  70. int len = points.Length;
  71. #endif
  72. if (len > 0)
  73. {
  74. for (int i = 0; i < len; i++)
  75. {
  76. #if !UNITY_EDITOR
  77. var point = NativePointCloud.AquireItem(listhandle, i);
  78. #else
  79. var point = points[i];
  80. #endif
  81. PointCloudDrawer.Update(point);
  82. }
  83. PointCloudDrawer.Draw();
  84. }
  85. #if !UNITY_EDITOR
  86. NativePointCloud.DestroyPointCloudList(listhandle);
  87. #endif
  88. }
  89. }
  90. }
  91. public void Init()
  92. {
  93. if (isInited)
  94. {
  95. return;
  96. }
  97. PointCloudCoroutine.Instance.StartCoroutine(UpdatePoints());
  98. isInited = true;
  99. }
  100. public void Save(string path)
  101. {
  102. #if !UNITY_EDITOR
  103. NativePointCloud.SaveMap(path);
  104. #endif
  105. }
  106. public static int confidence
  107. {
  108. get
  109. {
  110. #if !UNITY_EDITOR
  111. return NativePointCloud.GetConfidence();
  112. #else
  113. return 0;
  114. #endif
  115. }
  116. }
  117. public void Destroy()
  118. {
  119. #if !UNITY_EDITOR
  120. NativePointCloud.Destroy();
  121. #endif
  122. instance = null;
  123. isInited = false;
  124. }
  125. }
  126. }