ARSpace.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 UnityEngine;
  9. using System.Collections.Generic;
  10. using System.Threading.Tasks;
  11. using Immersal.REST;
  12. namespace Immersal.AR
  13. {
  14. public class SpaceContainer
  15. {
  16. public int mapCount = 0;
  17. public Vector3 targetPosition = Vector3.zero;
  18. public Quaternion targetRotation = Quaternion.identity;
  19. public PoseFilter filter = new PoseFilter();
  20. }
  21. public class MapOffset
  22. {
  23. public Vector3 position;
  24. public Quaternion rotation;
  25. public Vector3 scale;
  26. public SpaceContainer space;
  27. }
  28. public class ARSpace : MonoBehaviour
  29. {
  30. public static Dictionary<Transform, SpaceContainer> transformToSpace = new Dictionary<Transform, SpaceContainer>();
  31. public static Dictionary<SpaceContainer, Transform> spaceToTransform = new Dictionary<SpaceContainer, Transform>();
  32. public static Dictionary<int, MapOffset> mapIdToOffset = new Dictionary<int, MapOffset>();
  33. public static Dictionary<int, ARMap> mapIdToMap = new Dictionary<int, ARMap>();
  34. private Matrix4x4 m_InitialOffset = Matrix4x4.identity;
  35. public Matrix4x4 initialOffset
  36. {
  37. get { return m_InitialOffset; }
  38. }
  39. void Awake()
  40. {
  41. Vector3 pos = transform.position;
  42. Quaternion rot = transform.rotation;
  43. Matrix4x4 offset = Matrix4x4.TRS(pos, rot, Vector3.one);
  44. m_InitialOffset = offset;
  45. }
  46. public void OnDestroy()
  47. {
  48. transformToSpace.Clear();
  49. spaceToTransform.Clear();
  50. mapIdToOffset.Clear();
  51. mapIdToMap.Clear();
  52. }
  53. public Pose ToCloudSpace(Vector3 camPos, Quaternion camRot)
  54. {
  55. Matrix4x4 trackerSpace = Matrix4x4.TRS(camPos, camRot, Vector3.one);
  56. Matrix4x4 trackerToCloudSpace = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  57. Matrix4x4 cloudSpace = trackerToCloudSpace.inverse * trackerSpace;
  58. return new Pose(cloudSpace.GetColumn(3), cloudSpace.rotation);
  59. }
  60. public Pose FromCloudSpace(Vector3 camPos, Quaternion camRot)
  61. {
  62. Matrix4x4 cloudSpace = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  63. Matrix4x4 trackerSpace = Matrix4x4.TRS(camPos, camRot, Vector3.one);
  64. Matrix4x4 m = trackerSpace * (cloudSpace.inverse);
  65. return new Pose(m.GetColumn(3), m.rotation);
  66. }
  67. public static async Task<ARMap> LoadAndInstantiateARMap(Transform root, SDKMapResult map, ARMap.RenderMode renderMode = ARMap.RenderMode.DoNotRender, Color pointCloudColor = default, bool applyAlignment = false)
  68. {
  69. GameObject go = new GameObject(string.Format("AR Map {0}-{1}", map.metadata.id, map.metadata.name));
  70. if (root != null)
  71. {
  72. go.transform.SetParent(root, false);
  73. }
  74. if (applyAlignment)
  75. {
  76. Matrix4x4 b = Matrix4x4.TRS(new Vector3((float)map.metadata.tx, (float)map.metadata.ty, (float)map.metadata.tz),
  77. new Quaternion((float)map.metadata.qx, (float)map.metadata.qy, (float)map.metadata.qz, (float)map.metadata.qw),
  78. new Vector3((float)map.metadata.scale, (float)map.metadata.scale, (float)map.metadata.scale)
  79. );
  80. Matrix4x4 a = ARHelper.SwitchHandedness(b);
  81. go.transform.localPosition = a.GetColumn(3);
  82. go.transform.localRotation = a.rotation;
  83. go.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
  84. }
  85. ARMap arMap = go.AddComponent<ARMap>();
  86. arMap.mapName = map.metadata.name;
  87. arMap.privacy = map.metadata.privacy;
  88. arMap.mapAlignment.tx = map.metadata.tx;
  89. arMap.mapAlignment.ty = map.metadata.ty;
  90. arMap.mapAlignment.tz = map.metadata.tz;
  91. arMap.mapAlignment.qx = map.metadata.qx;
  92. arMap.mapAlignment.qy = map.metadata.qy;
  93. arMap.mapAlignment.qz = map.metadata.qz;
  94. arMap.mapAlignment.qw = map.metadata.qw;
  95. // TODO: Scale support
  96. arMap.mapAlignment.scale = 1.0;
  97. arMap.wgs84.latitude = map.metadata.latitude;
  98. arMap.wgs84.longitude = map.metadata.longitude;
  99. arMap.wgs84.altitude = map.metadata.altitude;
  100. arMap.pointColor = pointCloudColor;
  101. arMap.renderMode = renderMode;
  102. await arMap.LoadMap(map.mapData, map.metadata.id);
  103. return arMap;
  104. }
  105. public static async Task<ARMap> LoadAndInstantiateARMap(Transform root, SDKJob map, byte[] mapData = null, ARMap.RenderMode renderMode = ARMap.RenderMode.DoNotRender, Color pointCloudColor = default, bool applyAlignment = false)
  106. {
  107. GameObject go = new GameObject(string.Format("AR Map {0}-{1}", map.id, map.name));
  108. if (root != null)
  109. {
  110. go.transform.SetParent(root, false);
  111. }
  112. ARMap arMap = go.AddComponent<ARMap>();
  113. arMap.mapName = map.name;
  114. arMap.privacy = map.privacy;
  115. arMap.pointColor = pointCloudColor;
  116. arMap.renderMode = renderMode;
  117. JobMapMetadataGetAsync j = new JobMapMetadataGetAsync();
  118. j.id = map.id;
  119. j.token = map.privacy == 0 ? ImmersalSDK.Instance.developerToken : "";
  120. j.OnResult += (SDKMapMetadataGetResult metadata) =>
  121. {
  122. if (metadata.error == "none")
  123. {
  124. arMap.mapAlignment.tx = metadata.tx;
  125. arMap.mapAlignment.ty = metadata.ty;
  126. arMap.mapAlignment.tz = metadata.tz;
  127. arMap.mapAlignment.qx = metadata.qx;
  128. arMap.mapAlignment.qy = metadata.qy;
  129. arMap.mapAlignment.qz = metadata.qz;
  130. arMap.mapAlignment.qw = metadata.qw;
  131. // TODO: Scale support
  132. arMap.mapAlignment.scale = 1.0;
  133. arMap.wgs84.latitude = metadata.latitude;
  134. arMap.wgs84.longitude = metadata.longitude;
  135. arMap.wgs84.altitude = metadata.altitude;
  136. if (applyAlignment)
  137. {
  138. Matrix4x4 b = Matrix4x4.TRS(new Vector3((float)metadata.tx, (float)metadata.ty, (float)metadata.tz),
  139. new Quaternion((float)metadata.qx, (float)metadata.qy, (float)metadata.qz, (float)metadata.qw),
  140. new Vector3((float)metadata.scale, (float)metadata.scale, (float)metadata.scale)
  141. );
  142. Matrix4x4 a = ARHelper.SwitchHandedness(b);
  143. go.transform.localPosition = a.GetColumn(3);
  144. go.transform.localRotation = a.rotation;
  145. go.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
  146. }
  147. }
  148. };
  149. await j.RunJobAsync();
  150. await arMap.LoadMap(mapData, map.id);
  151. return arMap;
  152. }
  153. public static void RegisterSpace(Transform tr, ARMap map, Vector3 offsetPosition, Quaternion offsetRotation, Vector3 offsetScale)
  154. {
  155. if (tr == null)
  156. return;
  157. SpaceContainer sc;
  158. if (!transformToSpace.ContainsKey(tr))
  159. {
  160. sc = new SpaceContainer();
  161. transformToSpace[tr] = sc;
  162. }
  163. else
  164. {
  165. sc = transformToSpace[tr];
  166. }
  167. spaceToTransform[sc] = tr;
  168. sc.mapCount++;
  169. MapOffset mo = new MapOffset();
  170. mo.position = offsetPosition;
  171. mo.rotation = offsetRotation;
  172. mo.scale = offsetScale;
  173. mo.space = sc;
  174. mapIdToOffset[map.mapId] = mo;
  175. mapIdToMap[map.mapId] = map;
  176. }
  177. public static void RegisterSpace(Transform tr, ARMap map)
  178. {
  179. RegisterSpace(tr, map, Vector3.zero, Quaternion.identity, Vector3.one);
  180. }
  181. public static void UnregisterSpace(Transform tr, int mapId)
  182. {
  183. if (tr == null)
  184. return;
  185. if (transformToSpace.ContainsKey(tr))
  186. {
  187. SpaceContainer sc = transformToSpace[tr];
  188. if (--sc.mapCount == 0)
  189. {
  190. transformToSpace.Remove(tr);
  191. spaceToTransform.Remove(sc);
  192. }
  193. if (mapIdToOffset.ContainsKey(mapId))
  194. mapIdToOffset.Remove(mapId);
  195. if (mapIdToMap.ContainsKey(mapId))
  196. mapIdToMap.Remove(mapId);
  197. }
  198. }
  199. public static bool isFind;
  200. public static void UpdateSpace(SpaceContainer space, Vector3 pos, Quaternion rot)
  201. {
  202. if(pos!=Vector3.zero)
  203. {
  204. isFind = true;
  205. }
  206. if (space == null)
  207. return;
  208. if (spaceToTransform.ContainsKey(space))
  209. {
  210. Transform tr = spaceToTransform[space];
  211. tr.SetPositionAndRotation(pos, rot);
  212. //tr.eulerAngles = new Vector3(tr.eulerAngles.x-90,tr.eulerAngles.y,tr.eulerAngles.z);
  213. }
  214. }
  215. }
  216. }