MapperSettings.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.IO;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using TMPro;
  12. namespace Immersal.Samples.Mapping
  13. {
  14. public class MapperSettings : MonoBehaviour
  15. {
  16. public const int VERSION = 9;
  17. public bool useGps { get; private set; } = true;
  18. public bool captureRgb { get; private set; } = false;
  19. public bool checkConnectivity { get; private set; } = true;
  20. public bool showPointClouds { get; private set; } = true;
  21. public bool renderPointsAs3D { get; private set; } = true;
  22. public float pointSize { get; private set; } = 0.33f;
  23. public bool useServerLocalizer { get; private set; } = false;
  24. public bool listOnlyNearbyMaps { get; private set; } = false;
  25. public bool transformRootToOrigin { get; private set; } = true;
  26. public bool downsampleWhenLocalizing { get; private set; } = false;
  27. public int resolution { get; private set; } = 0;
  28. public int localizer { get; private set; } = 1;
  29. public int mapDetailLevel { get; private set; } = 1024;
  30. public bool serverLocalizationWithIds { get; private set; } = true;
  31. public bool useDifferentARSpaces { get; private set; } = true;
  32. public bool preservePoses { get; private set; } = false;
  33. public bool automaticCapture { get; private set; } = false;
  34. public int automaticCaptureMaxImages { get; private set; } = 40;
  35. public float automaticCaptureInterval { get; private set; } = 0.6f;
  36. public int windowSize { get; private set; } = 0;
  37. // workspace mode settings
  38. [SerializeField]
  39. private Toggle m_GpsCaptureToggle = null;
  40. [SerializeField]
  41. private Toggle m_RgbCaptureToggle = null;
  42. [SerializeField]
  43. private Toggle m_CheckConnectivityToggle = null;
  44. // visualize mode settings
  45. [SerializeField]
  46. private Toggle m_ShowPointCloudsToggle = null;
  47. [SerializeField]
  48. private Toggle m_OnServerLocalizationToggle = null;
  49. [SerializeField]
  50. private Toggle m_RenderPointsAs3DToggle = null;
  51. [SerializeField]
  52. private Slider m_PointSizeSlider = null;
  53. [SerializeField]
  54. private Toggle m_ListOnlyNearbyMapsToggle = null;
  55. [SerializeField]
  56. private Toggle m_DownsampleWhenLocalizingToggle = null;
  57. [SerializeField]
  58. private Toggle m_TransformRootToOrigin = null;
  59. // developer settings
  60. [SerializeField]
  61. private TMP_Dropdown m_ResolutionDropdown = null;
  62. /*[SerializeField]
  63. private TMP_Dropdown m_LocalizerDropdown = null;*/
  64. [SerializeField]
  65. private TMP_InputField m_MapDetailLevelInput = null;
  66. [SerializeField]
  67. private Toggle m_ServerLocalizationWithIdsToggle = null;
  68. [SerializeField]
  69. private Toggle m_UseDifferentARSpacesToggle = null;
  70. [SerializeField]
  71. private Toggle m_PreservePosesToggle = null;
  72. [SerializeField]
  73. private Toggle m_AutomaticCaptureToggle = null;
  74. [SerializeField]
  75. private GameObject m_AutomaticCaptureMaxImages = null;
  76. [SerializeField]
  77. private Slider m_AutomaticCaptureMaxImagesSlider = null;
  78. [SerializeField]
  79. private TMP_Text m_AutomaticCaptureMaxImagesText = null;
  80. [SerializeField]
  81. private GameObject m_AutomaticCaptureInterval = null;
  82. [SerializeField]
  83. private Slider m_AutomaticCaptureIntervalSlider = null;
  84. [SerializeField]
  85. private TMP_Text m_AutomaticCaptureIntervalText = null;
  86. [SerializeField]
  87. private GameObject m_ManualCaptureButton = null;
  88. [SerializeField]
  89. private GameObject m_AutomaticCaptureButton = null;
  90. [SerializeField]
  91. private TMP_InputField m_WindowSizeInput = null;
  92. [SerializeField]
  93. private string m_Filename = "settings.json";
  94. [System.Serializable]
  95. public struct MapperSettingsFile
  96. {
  97. public int version;
  98. public bool useGps;
  99. public bool captureRgb;
  100. public bool checkConnectivity;
  101. public bool showPointClouds;
  102. public bool useServerLocalizer;
  103. public bool listOnlyNearbyMaps;
  104. public bool transformRootToOrigin;
  105. public bool downsampleWhenLocalizing;
  106. public bool renderPointsAs3D;
  107. public float pointSize;
  108. public int resolution;
  109. public int localizer;
  110. public int mapDetailLevel;
  111. public bool serverLocalizationWithIds;
  112. public bool useDifferentARSpaces;
  113. public bool preservePoses;
  114. public bool automaticCapture;
  115. public int automaticCaptureMaxImages;
  116. public float automaticCaptureInterval;
  117. public int windowSize;
  118. }
  119. void Awake()
  120. {
  121. /*
  122. m_LocalizerDropdown.ClearOptions();
  123. m_LocalizerDropdown.AddOptions( new List<string>() { "v1.10", "v1.11" });
  124. m_LocalizerDropdown.SetValueWithoutNotify(localizer);
  125. */
  126. }
  127. public void SetUseGPS(bool value)
  128. {
  129. useGps = value;
  130. SaveSettingsToPrefs();
  131. }
  132. public void SetCaptureRGB(bool value)
  133. {
  134. captureRgb = value;
  135. SaveSettingsToPrefs();
  136. }
  137. public void SetConnectivityCheck(bool value)
  138. {
  139. checkConnectivity = value;
  140. SaveSettingsToPrefs();
  141. }
  142. public void SetShowPointClouds(bool value)
  143. {
  144. showPointClouds = value;
  145. SaveSettingsToPrefs();
  146. }
  147. public void SetRenderPointsAs3D(bool value)
  148. {
  149. renderPointsAs3D = value;
  150. SaveSettingsToPrefs();
  151. }
  152. public void SetPointSize(float value)
  153. {
  154. pointSize = value;
  155. SaveSettingsToPrefs();
  156. }
  157. public void SetUseServerLocalizer(bool value)
  158. {
  159. useServerLocalizer = value;
  160. SaveSettingsToPrefs();
  161. }
  162. public void SetListOnlyNearbyMaps(bool value)
  163. {
  164. listOnlyNearbyMaps = value;
  165. SaveSettingsToPrefs();
  166. }
  167. public void SetTransformRootToOrigin(bool value)
  168. {
  169. transformRootToOrigin = value;
  170. SaveSettingsToPrefs();
  171. }
  172. public void SetDownsampleWhenLocalizing(bool value)
  173. {
  174. downsampleWhenLocalizing = value;
  175. if (value)
  176. {
  177. Immersal.Core.SetInteger("LocalizationMaxPixels", 1280*720);
  178. }
  179. else
  180. {
  181. Immersal.Core.SetInteger("LocalizationMaxPixels", 0);
  182. }
  183. SaveSettingsToPrefs();
  184. }
  185. public void SetResolution(int value)
  186. {
  187. resolution = value;
  188. SaveSettingsToPrefs();
  189. }
  190. public void SetLocalizer(int value)
  191. {
  192. localizer = value;
  193. SaveSettingsToPrefs();
  194. }
  195. public void SetMapDetailLevel(string value)
  196. {
  197. int a;
  198. int.TryParse(value, out a);
  199. mapDetailLevel = a;
  200. SaveSettingsToPrefs();
  201. }
  202. public void SetServerLocalizationWithIds(bool value)
  203. {
  204. serverLocalizationWithIds = value;
  205. SaveSettingsToPrefs();
  206. }
  207. public void SetUseDifferentARSpaces(bool value)
  208. {
  209. useDifferentARSpaces = value;
  210. SaveSettingsToPrefs();
  211. }
  212. public void SetPreservePoses(bool value)
  213. {
  214. preservePoses = value;
  215. SaveSettingsToPrefs();
  216. }
  217. public void SetAutomaticCapture(bool value)
  218. {
  219. automaticCapture = value;
  220. EnableAutomaticCaptureUI(automaticCapture);
  221. SaveSettingsToPrefs();
  222. }
  223. public void SetAutomaticCaptureMaxImageValue(float maxImages)
  224. {
  225. automaticCaptureMaxImages = Mathf.RoundToInt(maxImages);
  226. m_AutomaticCaptureMaxImagesText.text = automaticCaptureMaxImages.ToString();
  227. SaveSettingsToPrefs();
  228. }
  229. public void SetAutomaticCaptureIntervalValue(float interval)
  230. {
  231. automaticCaptureInterval = interval;
  232. m_AutomaticCaptureIntervalText.text = automaticCaptureInterval.ToString("F2");
  233. SaveSettingsToPrefs();
  234. }
  235. public void SetWindowSize(string value)
  236. {
  237. int a;
  238. int.TryParse(value, out a);
  239. windowSize = a;
  240. SaveSettingsToPrefs();
  241. }
  242. private void Start()
  243. {
  244. LoadSettingsFromPrefs();
  245. }
  246. private void EnableAutomaticCaptureUI(bool value)
  247. {
  248. //enable sub-setting items for automatic capture
  249. m_AutomaticCaptureMaxImages.SetActive(value);
  250. m_AutomaticCaptureInterval.SetActive(value);
  251. //enable manual capture button
  252. m_ManualCaptureButton.SetActive(!value);
  253. m_AutomaticCaptureButton.SetActive(value);
  254. }
  255. private void LoadSettingsFromPrefs()
  256. {
  257. string dataPath = Path.Combine(Application.persistentDataPath, m_Filename);
  258. try
  259. {
  260. MapperSettingsFile loadFile = JsonUtility.FromJson<MapperSettingsFile>(File.ReadAllText(dataPath));
  261. // set defaults for old file versions
  262. if (loadFile.version < VERSION)
  263. {
  264. ResetDeveloperSettings();
  265. return;
  266. }
  267. m_GpsCaptureToggle.isOn = loadFile.useGps;
  268. useGps = loadFile.useGps;
  269. m_RgbCaptureToggle.SetIsOnWithoutNotify(loadFile.captureRgb);
  270. captureRgb = loadFile.captureRgb;
  271. m_CheckConnectivityToggle.SetIsOnWithoutNotify(loadFile.checkConnectivity);
  272. checkConnectivity = loadFile.checkConnectivity;
  273. m_ShowPointCloudsToggle.SetIsOnWithoutNotify(loadFile.showPointClouds);
  274. showPointClouds = loadFile.showPointClouds;
  275. m_RenderPointsAs3DToggle.SetIsOnWithoutNotify(loadFile.renderPointsAs3D);
  276. renderPointsAs3D = loadFile.renderPointsAs3D;
  277. Immersal.AR.ARMap.renderAs3dPoints = renderPointsAs3D;
  278. m_PointSizeSlider.SetValueWithoutNotify(loadFile.pointSize);
  279. pointSize = loadFile.pointSize;
  280. Immersal.AR.ARMap.pointSize = pointSize;
  281. m_OnServerLocalizationToggle.SetIsOnWithoutNotify(loadFile.useServerLocalizer);
  282. useServerLocalizer = loadFile.useServerLocalizer;
  283. m_ListOnlyNearbyMapsToggle.SetIsOnWithoutNotify(loadFile.listOnlyNearbyMaps);
  284. listOnlyNearbyMaps = loadFile.listOnlyNearbyMaps;
  285. m_TransformRootToOrigin.SetIsOnWithoutNotify(loadFile.transformRootToOrigin);
  286. transformRootToOrigin = loadFile.transformRootToOrigin;
  287. m_DownsampleWhenLocalizingToggle.SetIsOnWithoutNotify(loadFile.downsampleWhenLocalizing);
  288. downsampleWhenLocalizing = loadFile.downsampleWhenLocalizing;
  289. m_MapDetailLevelInput.SetTextWithoutNotify(loadFile.mapDetailLevel.ToString());
  290. mapDetailLevel = loadFile.mapDetailLevel;
  291. m_ServerLocalizationWithIdsToggle.SetIsOnWithoutNotify(loadFile.serverLocalizationWithIds);
  292. serverLocalizationWithIds = loadFile.serverLocalizationWithIds;
  293. m_UseDifferentARSpacesToggle.SetIsOnWithoutNotify(loadFile.useDifferentARSpaces);
  294. useDifferentARSpaces = loadFile.useDifferentARSpaces;
  295. m_PreservePosesToggle.SetIsOnWithoutNotify(loadFile.preservePoses);
  296. preservePoses = loadFile.preservePoses;
  297. m_AutomaticCaptureToggle.SetIsOnWithoutNotify(loadFile.automaticCapture);
  298. automaticCapture = loadFile.automaticCapture;
  299. EnableAutomaticCaptureUI(automaticCapture);
  300. automaticCaptureMaxImages = Mathf.Max(loadFile.automaticCaptureMaxImages, Mathf.RoundToInt(m_AutomaticCaptureMaxImagesSlider.minValue));
  301. m_AutomaticCaptureMaxImagesSlider.value = loadFile.automaticCaptureMaxImages;
  302. m_AutomaticCaptureMaxImagesText.text = automaticCaptureMaxImages.ToString();
  303. automaticCaptureInterval = Mathf.Max(loadFile.automaticCaptureInterval, m_AutomaticCaptureIntervalSlider.minValue);
  304. m_AutomaticCaptureIntervalSlider.value = loadFile.automaticCaptureInterval;
  305. m_AutomaticCaptureIntervalText.text = automaticCaptureInterval.ToString("F2");
  306. m_WindowSizeInput.SetTextWithoutNotify(loadFile.windowSize.ToString());
  307. windowSize = loadFile.windowSize;
  308. }
  309. catch (FileNotFoundException e)
  310. {
  311. Debug.Log(e.Message + "\nsettings.json file not found");
  312. SaveSettingsToPrefs();
  313. LoadSettingsFromPrefs();
  314. return;
  315. }
  316. SaveSettingsToPrefs();
  317. }
  318. public void SaveSettingsToPrefs()
  319. {
  320. MapperSettingsFile saveFile = new MapperSettingsFile();
  321. saveFile.version = VERSION;
  322. saveFile.useGps = useGps;
  323. saveFile.captureRgb = captureRgb;
  324. saveFile.checkConnectivity = checkConnectivity;
  325. saveFile.showPointClouds = showPointClouds;
  326. saveFile.useServerLocalizer = useServerLocalizer;
  327. saveFile.listOnlyNearbyMaps = listOnlyNearbyMaps;
  328. saveFile.transformRootToOrigin = transformRootToOrigin;
  329. saveFile.downsampleWhenLocalizing = downsampleWhenLocalizing;
  330. saveFile.renderPointsAs3D = renderPointsAs3D;
  331. saveFile.pointSize = pointSize;
  332. saveFile.resolution = resolution;
  333. saveFile.localizer = localizer;
  334. saveFile.mapDetailLevel = mapDetailLevel;
  335. saveFile.serverLocalizationWithIds = serverLocalizationWithIds;
  336. saveFile.useDifferentARSpaces = useDifferentARSpaces;
  337. saveFile.preservePoses = preservePoses;
  338. saveFile.automaticCapture = automaticCapture;
  339. saveFile.automaticCaptureMaxImages = automaticCaptureMaxImages;
  340. saveFile.automaticCaptureInterval = automaticCaptureInterval;
  341. saveFile.windowSize = windowSize;
  342. string jsonstring = JsonUtility.ToJson(saveFile, true);
  343. string dataPath = Path.Combine(Application.persistentDataPath, m_Filename);
  344. //Debug.Log(dataPath);
  345. File.WriteAllText(dataPath, jsonstring);
  346. }
  347. public void ResetDeveloperSettings()
  348. {
  349. m_ResolutionDropdown.SetValueWithoutNotify(0);
  350. resolution = 0;
  351. //m_LocalizerDropdown.SetValueWithoutNotify(1);
  352. //localizer = 1;
  353. m_MapDetailLevelInput.SetTextWithoutNotify(1024.ToString());
  354. mapDetailLevel = 1024;
  355. m_ServerLocalizationWithIdsToggle.SetIsOnWithoutNotify(true);
  356. serverLocalizationWithIds = true;
  357. m_ListOnlyNearbyMapsToggle.SetIsOnWithoutNotify(false);
  358. listOnlyNearbyMaps = false;
  359. m_DownsampleWhenLocalizingToggle.SetIsOnWithoutNotify(false);
  360. downsampleWhenLocalizing = false;
  361. m_UseDifferentARSpacesToggle.SetIsOnWithoutNotify(true);
  362. useDifferentARSpaces = true;
  363. m_PreservePosesToggle.SetIsOnWithoutNotify(false);
  364. preservePoses = false;
  365. m_AutomaticCaptureToggle.SetIsOnWithoutNotify(false);
  366. automaticCapture = false;
  367. EnableAutomaticCaptureUI(automaticCapture);
  368. automaticCaptureMaxImages = Mathf.RoundToInt(40);
  369. m_AutomaticCaptureMaxImagesSlider.value = automaticCaptureMaxImages;
  370. automaticCaptureInterval = 0.6f;
  371. m_AutomaticCaptureIntervalSlider.value = automaticCaptureInterval;
  372. SaveSettingsToPrefs();
  373. }
  374. }
  375. }