Mapper.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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;
  9. using System.Runtime.InteropServices;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Net.Http;
  14. using System.Threading.Tasks;
  15. using UnityEngine;
  16. using UnityEngine.Networking;
  17. using UnityEngine.UI;
  18. using Immersal.AR;
  19. using Immersal.REST;
  20. using Immersal.Samples.Util;
  21. using UnityEngine.XR.ARFoundation;
  22. using UnityEngine.XR.ARSubsystems;
  23. namespace Immersal.Samples.Mapping
  24. {
  25. public class Mapper : MapperBase
  26. {
  27. private void SessionStateChanged(ARSessionStateChangedEventArgs args)
  28. {
  29. ImageRunUpdate();
  30. m_IsTracking = args.state == ARSessionState.SessionTracking;
  31. var captureButton = workspaceManager.captureButton.GetComponent<Button>();
  32. var localizeButton = visualizeManager.localizeButton.GetComponent<Button>();
  33. captureButton.interactable = m_IsTracking;
  34. localizeButton.interactable = m_IsTracking;
  35. }
  36. protected override void OnEnable()
  37. {
  38. #if !UNITY_EDITOR
  39. ARSession.stateChanged += SessionStateChanged;
  40. #endif
  41. base.OnEnable();
  42. }
  43. protected override void OnDisable()
  44. {
  45. #if !UNITY_EDITOR
  46. ARSession.stateChanged -= SessionStateChanged;
  47. #endif
  48. base.OnDisable();
  49. }
  50. protected override async void Capture(bool anchor)
  51. {
  52. await Task.Delay(250);
  53. if(this.stats.imageCount + this.stats.queueLen >= this.stats.imageMax)
  54. {
  55. ImageLimitExceeded();
  56. }
  57. m_bCaptureRunning = true;
  58. float captureStartTime = Time.realtimeSinceStartup;
  59. float uploadStartTime = Time.realtimeSinceStartup;
  60. #if PLATFORM_LUMIN && UNITY_2020_1
  61. XRCameraImage image;
  62. #else
  63. XRCpuImage image;
  64. #endif
  65. ARCameraManager cameraManager = m_Sdk.cameraManager;
  66. var cameraSubsystem = cameraManager.subsystem;
  67. #if PLATFORM_LUMIN && UNITY_2020_1
  68. if (cameraSubsystem != null && cameraSubsystem.TryGetLatestImage(out image))
  69. #else
  70. if (cameraSubsystem != null && cameraSubsystem.TryAcquireLatestCpuImage(out image))
  71. #endif
  72. {
  73. JobCaptureAsync j = new JobCaptureAsync();
  74. j.run = (int)(m_ImageRun & 0x7FFFFFFF);
  75. j.index = m_ImageIndex++;
  76. j.anchor = anchor;
  77. if (mapperSettings.useGps)
  78. {
  79. j.latitude = m_Latitude;
  80. j.longitude = m_Longitude;
  81. j.altitude = m_Altitude;
  82. }
  83. else
  84. {
  85. j.latitude = j.longitude = j.altitude = 0.0;
  86. }
  87. Camera cam = this.mainCamera;
  88. ARHelper.GetIntrinsics(out j.intrinsics);
  89. Quaternion rot = cam.transform.rotation;
  90. Vector3 pos = cam.transform.position;
  91. ARHelper.GetRotation(ref rot);
  92. j.rotation = ARHelper.SwitchHandedness(Matrix4x4.Rotate(rot));
  93. j.position = ARHelper.SwitchHandedness(pos);
  94. int width = image.width;
  95. int height = image.height;
  96. byte[] pixels;
  97. int channels = 1;
  98. if (mapperSettings.captureRgb)
  99. {
  100. ARHelper.GetPlaneDataRGB(out pixels, image);
  101. channels = 3;
  102. }
  103. else
  104. {
  105. ARHelper.GetPlaneData(out pixels, image);
  106. }
  107. byte[] capture = new byte[channels * width * height + 8192];
  108. int useMatching = mapperSettings.checkConnectivity ? 1 : 0;
  109. Task<icvCaptureInfo> captureTask = Task.Run(() =>
  110. {
  111. return Core.CaptureImage(capture, capture.Length, pixels, width, height, channels, useMatching);
  112. });
  113. await captureTask;
  114. string path = string.Format("{0}/{1}", this.tempImagePath, System.Guid.NewGuid());
  115. using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(path)))
  116. {
  117. writer.Write(capture, 0, captureTask.Result.captureSize);
  118. }
  119. j.imagePath = path;
  120. j.encodedImage = "";
  121. if (mapperSettings.checkConnectivity)
  122. {
  123. NotifyIfConnected(captureTask.Result);
  124. }
  125. if (m_SessionFirstImage)
  126. m_SessionFirstImage = false;
  127. j.OnStart += () =>
  128. {
  129. uploadStartTime = Time.realtimeSinceStartup;
  130. mappingUIManager.SetProgress(0);
  131. mappingUIManager.ShowProgressBar();
  132. };
  133. j.OnResult += (SDKImageResult result) =>
  134. {
  135. float et = Time.realtimeSinceStartup - uploadStartTime;
  136. Debug.Log(string.Format("Image uploaded successfully in {0} seconds", et));
  137. mappingUIManager.HideProgressBar();
  138. };
  139. j.Progress.ProgressChanged += (s, progress) =>
  140. {
  141. int value = (int)(100f * progress);
  142. //Debug.Log(string.Format("Upload progress: {0}%", value));
  143. mappingUIManager.SetProgress(value);
  144. };
  145. j.OnError += (e) =>
  146. {
  147. mappingUIManager.HideProgressBar();
  148. };
  149. m_Jobs.Add(j);
  150. image.Dispose();
  151. float elapsedTime = Time.realtimeSinceStartup - captureStartTime;
  152. Debug.Log(string.Format("Capture in {0} seconds", elapsedTime));
  153. }
  154. m_bCaptureRunning = false;
  155. var captureButton = workspaceManager.captureButton.GetComponent<Button>();
  156. captureButton.interactable = true;
  157. }
  158. public void TryLocalize()
  159. {
  160. Debug.Log("TryLocalize===>start");
  161. if (mapperSettings.useServerLocalizer)
  162. {
  163. Debug.Log("TryLocalize===>useServerLocalizer");
  164. int n = ARSpace.mapIdToMap.Count;
  165. SDKMapId[] mapIds = new SDKMapId[n];
  166. int count = 0;
  167. foreach (int id in ARSpace.mapIdToMap.Keys)
  168. {
  169. mapIds[count] = new SDKMapId();
  170. mapIds[count++].id = id;
  171. }
  172. if (m_UseGeoPose)
  173. {
  174. Debug.Log("TryLocalize===>m_UseGeoPose");
  175. m_Sdk.Localizer.LocalizeGeoPose(mapIds);
  176. }
  177. else
  178. {
  179. Debug.Log("TryLocalize===>!m_UseGeoPose");
  180. m_Sdk.Localizer.LocalizeServer(mapIds);
  181. }
  182. }
  183. else
  184. {
  185. Debug.Log("TryLocalize===>!useServerLocalizer");
  186. m_Sdk.Localizer.Localize();
  187. }
  188. }
  189. }
  190. }