OffLineMappingTool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 UnityEngine;
  12. using UnityEngine.UI;
  13. using System.IO;
  14. public class OffLineMappingTool : MonoBehaviour
  15. {
  16. public Text m_ConfidenceTips;
  17. public Slider m_PointSizeSlider;
  18. public Button m_StartMapBtn;
  19. public Button m_SaveMapBtn;
  20. public Slider m_ConfidenceSlider;
  21. public GameObject m_AnchorLoaders;
  22. private NRPointCloudCreator creator;
  23. private NRPointCloudVisualizer visualizer;
  24. public GameObject m_MapCreator;
  25. public GameObject m_AnchorsCreator;
  26. public GameObject m_Menu;
  27. void Start()
  28. {
  29. m_PointSizeSlider.onValueChanged.AddListener(OnScaleChange);
  30. visualizer = new NRPointCloudVisualizer();
  31. m_AnchorLoaders.SetActive(false);
  32. m_StartMapBtn.onClick.AddListener(Open);
  33. m_SaveMapBtn.onClick.AddListener(Save);
  34. m_StartMapBtn.gameObject.SetActive(true);
  35. m_SaveMapBtn.gameObject.SetActive(false);
  36. m_MapCreator.SetActive(false);
  37. m_AnchorsCreator.SetActive(false);
  38. }
  39. public void SwitchToMapMode()
  40. {
  41. m_MapCreator.SetActive(true);
  42. m_AnchorsCreator.SetActive(false);
  43. m_Menu.SetActive(false);
  44. }
  45. public void SwitchToAnchorMode()
  46. {
  47. m_MapCreator.SetActive(false);
  48. m_AnchorsCreator.SetActive(true);
  49. m_Menu.SetActive(false);
  50. m_AnchorLoaders.SetActive(true);
  51. }
  52. void Update()
  53. {
  54. int confidence = NRPointCloudCreator.confidence;
  55. m_ConfidenceTips.text = confidence.ToString();
  56. m_ConfidenceSlider.value = confidence;
  57. }
  58. public void Open()
  59. {
  60. if (creator != null)
  61. {
  62. return;
  63. }
  64. creator = NRPointCloudCreator.Create(visualizer);
  65. m_StartMapBtn.gameObject.SetActive(false);
  66. m_SaveMapBtn.gameObject.SetActive(true);
  67. }
  68. public void Save()
  69. {
  70. if (creator == null)
  71. {
  72. return;
  73. }
  74. string MapSavedPath = Path.Combine(Application.persistentDataPath, NRWorldAnchorStore.MapFolder);
  75. ClearMapFiles(MapSavedPath);
  76. if (!Directory.Exists(MapSavedPath))
  77. {
  78. Directory.CreateDirectory(MapSavedPath);
  79. }
  80. // Delete old files
  81. string[] files = Directory.GetFiles(MapSavedPath);
  82. foreach (var file in files)
  83. {
  84. NRDebugger.Info("[PointCloudTool] Delete file:" + file);
  85. File.Delete(file);
  86. }
  87. string mapfile = Path.Combine(MapSavedPath, NRWorldAnchorStore.MapFile);
  88. creator.Save(mapfile);
  89. Invoke("AfterBuildMap", 2f);
  90. m_StartMapBtn.gameObject.SetActive(true);
  91. m_SaveMapBtn.gameObject.SetActive(false);
  92. }
  93. public void Load()
  94. {
  95. SwitchToAnchorMode();
  96. }
  97. private void ClearMapFiles(string path)
  98. {
  99. if (!Directory.Exists(path))
  100. {
  101. return;
  102. }
  103. string[] files = Directory.GetFiles(path);
  104. foreach (var file in files)
  105. {
  106. NRDebugger.Info("[PointCloudTool] Copy file:" + file);
  107. string destiny = Path.Combine(path, Path.GetFileName(file));
  108. File.Delete(destiny);
  109. }
  110. }
  111. private void AfterBuildMap()
  112. {
  113. NRDebugger.Info("[PointCloudTool] Destroy creator.");
  114. creator.Destroy();
  115. }
  116. public void OnScaleChange(float value)
  117. {
  118. visualizer?.AdjustPointSize(value * 20);
  119. }
  120. }
  121. }