From 93634c2f97e4624749f9b036c099f1db4aa8323a Mon Sep 17 00:00:00 2001 From: jlacoche Date: Wed, 24 Apr 2024 16:37:13 +0200 Subject: [PATCH 1/3] Add empty arfoundation configured package --- Editor.meta | 8 + Editor/Scripts.meta | 8 + ...rf.worldanalysisarfoundation.Editor.asmdef | 18 ++ ...rldanalysisarfoundation.Editor.asmdef.meta | 7 + LICENSE | 201 ++++++++++++++++++ LICENSE.meta | 7 + README.md.meta | 7 + Runtime.meta | 8 + Runtime/Scripts.meta | 8 + Runtime/Scripts/WorldAnalysisARFoundation.cs | 104 +++++++++ .../Scripts/WorldAnalysisARFoundation.cs.meta | 11 + ...i.isg.arf.worldanalysisarfoundation.asmdef | 19 ++ ....arf.worldanalysisarfoundation.asmdef.meta | 7 + package.json | 17 ++ package.json.meta | 7 + 15 files changed, 437 insertions(+) create mode 100644 Editor.meta create mode 100644 Editor/Scripts.meta create mode 100644 Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef create mode 100644 Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef.meta create mode 100644 LICENSE create mode 100644 LICENSE.meta create mode 100644 README.md.meta create mode 100644 Runtime.meta create mode 100644 Runtime/Scripts.meta create mode 100644 Runtime/Scripts/WorldAnalysisARFoundation.cs create mode 100644 Runtime/Scripts/WorldAnalysisARFoundation.cs.meta create mode 100644 Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef create mode 100644 Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef.meta create mode 100644 package.json create mode 100644 package.json.meta diff --git a/Editor.meta b/Editor.meta new file mode 100644 index 0000000..14941c7 --- /dev/null +++ b/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9338e2b6380288045b5167877e3a24b7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Scripts.meta b/Editor/Scripts.meta new file mode 100644 index 0000000..ab70a34 --- /dev/null +++ b/Editor/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 726d9451c4fe5a940b288eddb6611383 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef b/Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef new file mode 100644 index 0000000..89ef738 --- /dev/null +++ b/Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef @@ -0,0 +1,18 @@ +{ + "name": "etsi.isg.arf.worldanalysisarfoundation.Editor", + "rootNamespace": "", + "references": [ + "GUID:9d88b659f8b06cc48af14c01b37bb935" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef.meta b/Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef.meta new file mode 100644 index 0000000..de036f5 --- /dev/null +++ b/Editor/etsi.isg.arf.worldanalysisarfoundation.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: f49755f5597b4d641aa3b3063f89548a +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..369a251 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2022 ETSI + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/LICENSE.meta b/LICENSE.meta new file mode 100644 index 0000000..4d2ca72 --- /dev/null +++ b/LICENSE.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: d11880e634753dc49a0f9b0fce9109fb +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md.meta b/README.md.meta new file mode 100644 index 0000000..0e2d421 --- /dev/null +++ b/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e56534d73c2ce764990791de74ed42ea +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime.meta b/Runtime.meta new file mode 100644 index 0000000..dcb1a78 --- /dev/null +++ b/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 38dc653a402f8a641a349282321c4f7b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts.meta b/Runtime/Scripts.meta new file mode 100644 index 0000000..f05741c --- /dev/null +++ b/Runtime/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6ae8956641f2d8743b571197185eda4e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/WorldAnalysisARFoundation.cs b/Runtime/Scripts/WorldAnalysisARFoundation.cs new file mode 100644 index 0000000..5016015 --- /dev/null +++ b/Runtime/Scripts/WorldAnalysisARFoundation.cs @@ -0,0 +1,104 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.XR.ARFoundation; +using UnityEngine.XR.ARSubsystems; +using ETSI.ARF.OpenAPI.WorldAnalysis; +using static WorldAnalysisInterface; + +//Implementation of the WorldAnalysis interface +public class WorldAnalysisARFoundation : MonoBehaviour, WorldAnalysisInterface +{ + + #region Unity_Methods + + /// + /// Unity Awake Method + /// + protected void Awake() + { + } + + /// + /// Unity Start Method + /// + protected void Start() + { + } + + /// + /// Unity Update Method + /// + protected void Update() + { + } + + #endregion + + + #region ARF_API + + public AskFrameRateResult SetPoseEstimationFramerate(string token, PoseConfigurationTrackableType type, EncodingInformationStructure encodingInformation, int minimumFramerate) + { + return AskFrameRateResult.NOT_SUPPORTED; ///We cannot set any framerate for tracking on ARKit and ARCore + } + + public PoseEstimationResult GetLastPose(string token, string uuid, Mode_WorldAnalysis mode, out ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose) + { + pose = null; + return PoseEstimationResult.OK; + } + + + public PoseEstimationResult[] GetLastPoses(string token, string[] uuids, Mode_WorldAnalysis [] modes, out ETSI.ARF.OpenAPI.WorldAnalysis.Pose[] poses) + { + poses = null; + return null; + } + + public InformationSubscriptionResult SubscribeToPose(string token, string uuid, Mode_WorldAnalysis mode, PoseCallback callback, ref int validity, out string subscriptionUUID) + { + subscriptionUUID = ""; + return InformationSubscriptionResult.OK; + } + + public InformationSubscriptionResult[] SubscribeToPoses(string token, string[] uuids, Mode_WorldAnalysis [] modes, PoseCallback callback, ref int validity, out string[] subscriptionUUIDs) + { + subscriptionUUIDs = null; + return null; + } + + public InformationSubscriptionResult GetSubsription(string token, string subscriptionUUID, out PoseCallback callback, out string target, out Mode_WorldAnalysis mode, out int validity) + { + callback = null; + target = ""; + mode = Mode_WorldAnalysis.TRACKABLES_TO_DEVICE; + validity = 0; + return InformationSubscriptionResult.OK; + } + + public InformationSubscriptionResult UpdateSubscription(string token, string subscriptionUUID, Mode_WorldAnalysis mode, int validity, PoseCallback callback) + { + return InformationSubscriptionResult.OK; + } + + public InformationSubscriptionResult UnSubscribeToPose(string subscriptionUUID) + { + return InformationSubscriptionResult.OK; + } + + public CapabilityResult GetCapabilities(string token, out Capability[] capabilities) + { + capabilities = null; + return CapabilityResult.OK; + } + + public CapabilityResult GetCapability(string token, string uuid, out bool isSupported, out TypeWorldStorage type, out Capability capability) + { + isSupported = false; + type = TypeWorldStorage.UNKNOWN; + capability = null; + return CapabilityResult.OK; + } + + #endregion +} \ No newline at end of file diff --git a/Runtime/Scripts/WorldAnalysisARFoundation.cs.meta b/Runtime/Scripts/WorldAnalysisARFoundation.cs.meta new file mode 100644 index 0000000..0001ac9 --- /dev/null +++ b/Runtime/Scripts/WorldAnalysisARFoundation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d98e9bf37856a194b98a2a1956e214d5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef b/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef new file mode 100644 index 0000000..084868a --- /dev/null +++ b/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef @@ -0,0 +1,19 @@ +{ + "name": "etsi.isg.arf.worldanalysisarfoundation", + "rootNamespace": "", + "references": [ + "GUID:99fdaa6f193b69346bfc8863615f98f0", + "GUID:065e856e9f9becb49abdf0cb6855a039", + "GUID:a9420e37d7990b54abdef6688edbe313", + "GUID:92703082f92b41ba80f0d6912de66115" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef.meta b/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef.meta new file mode 100644 index 0000000..834b803 --- /dev/null +++ b/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 9d88b659f8b06cc48af14c01b37bb935 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json new file mode 100644 index 0000000..ff8e8c7 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "author": "ETSI-ISG-ARF", + "name": "etsi.isg.arf.worldanalysisarfoundation", + "version": "0.0.1", + "displayName": "ISG-ARF World Analysis ARFoundation Wrapper Package ", + "description": "ARFoundation wrapper for the World Analysis of the Augmented Reality Framework", + "unity": "2021.3", + "dependencies": { + "etsi.isg.arf.worldstorage" : "0.0.2", + "etsi.isg.arf.worldanalysis" : "0.0.1", + "com.unity.xr.arfoundation": "5.1.3", + "com.unity.xr.arkit": "5.1.3", + "com.unity.xr.arcore": "5.1.3" + + }, + "keywords": [] +} \ No newline at end of file diff --git a/package.json.meta b/package.json.meta new file mode 100644 index 0000000..610cdc7 --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: b453818522f35e9449e390426cd17b9c +PackageManifestImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: -- GitLab From 1a50123932c3760560c3e8fd37d6317317a80ba1 Mon Sep 17 00:00:00 2001 From: Sylvain Buche Date: Fri, 17 May 2024 16:46:52 +0200 Subject: [PATCH 2/3] First version of ARFoundation wrapper working with image trackers --- Runtime/Scripts/WorldAnalysisARFoundation.cs | 568 +++++++++++++++++- .../WorldAnalysisARFoundationModule.cs | 37 ++ .../WorldAnalysisARFoundationModule.cs.meta | 11 + .../WorldAnalysisARFoundationModuleImage.cs | 208 +++++++ ...rldAnalysisARFoundationModuleImage.cs.meta | 11 + ...i.isg.arf.worldanalysisarfoundation.asmdef | 3 +- 6 files changed, 806 insertions(+), 32 deletions(-) create mode 100644 Runtime/Scripts/WorldAnalysisARFoundationModule.cs create mode 100644 Runtime/Scripts/WorldAnalysisARFoundationModule.cs.meta create mode 100644 Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs create mode 100644 Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs.meta diff --git a/Runtime/Scripts/WorldAnalysisARFoundation.cs b/Runtime/Scripts/WorldAnalysisARFoundation.cs index 5016015..470bc31 100644 --- a/Runtime/Scripts/WorldAnalysisARFoundation.cs +++ b/Runtime/Scripts/WorldAnalysisARFoundation.cs @@ -1,13 +1,65 @@ using System.Collections.Generic; +using System; using UnityEngine; using UnityEngine.XR.ARFoundation; -using UnityEngine.XR.ARSubsystems; using ETSI.ARF.OpenAPI.WorldAnalysis; using static WorldAnalysisInterface; +using System.Collections; +using System.Linq; +using ETSI.ARF.OpenAPI.WorldStorage; +using ETSI.ARF.WorldStorage.REST; //Implementation of the WorldAnalysis interface public class WorldAnalysisARFoundation : MonoBehaviour, WorldAnalysisInterface { + /// + /// Dictionnary of susbscription informations for poses, for each item, stored using the UUID of the item (anchor/trackable) + /// + private Dictionary m_subscriptionsPoses; + /// + /// Dictionnary of relocalization informations for each item, stored using their UUID + /// + private Dictionary m_relocalizationInformations; + /// + /// Dictionnary of poses for each item, stored using their UUID + /// + private Dictionary m_computedPoses; + + /// + /// Module Image + /// + private WorldAnalysisARFoundationModuleImage m_imageTrackableModule; + + /// + /// World Storage Info object + /// + private WorldStorageInfo m_storageInfo; + + /// + /// WorldStorageServer + /// + private ETSI.ARF.WorldStorage.WorldStorageServer m_worldStorageServer; + + /// + /// Manages the AR Camera system + /// + private ARCameraManager m_arCameraManager; + + /// + /// Is true if the ArSession.CheckAvailability method is running + /// + private bool isChekingAvailabilityRunning = false; + + /// + /// Informations regarding a subscription to a pose + /// + public struct SubscriptionInfo + { + public Guid uuidTarget; // id trackable or anchor + public float timeValidity; //The duration of the validity of the subscription + public ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose; + public PoseCallback callback; + } #region Unity_Methods @@ -16,13 +68,42 @@ public class WorldAnalysisARFoundation : MonoBehaviour, WorldAnalysisInterface /// protected void Awake() { + Instance = this; + m_relocalizationInformations = new Dictionary(); + m_computedPoses = new Dictionary(); + m_subscriptionsPoses = new Dictionary(); + m_imageTrackableModule = new WorldAnalysisARFoundationModuleImage(); + m_imageTrackableModule.Initialize(); + + m_storageInfo = FindObjectOfType(); + m_worldStorageServer = m_storageInfo.worldStorageServer; } /// /// Unity Start Method /// - protected void Start() + IEnumerator Start() { + isChekingAvailabilityRunning = true; + ///Wait until AR session is launched + yield return ARSession.CheckAvailability(); + + if (ARSession.state == ARSessionState.NeedsInstall) + { + Debug.Log("ETSI ARF : AR is supported on this device, but requires installation."); + } + else if (ARSession.state == ARSessionState.Unsupported) + { + Debug.Log("ETSI ARF : AR is not supported on this device."); + } + else + { + Debug.Log("ETSI ARF : AR is supported on this device."); + } + + m_arCameraManager = FindObjectOfType(); + + isChekingAvailabilityRunning = false; } /// @@ -30,74 +111,499 @@ public class WorldAnalysisARFoundation : MonoBehaviour, WorldAnalysisInterface /// protected void Update() { + if (!isChekingAvailabilityRunning) + { + UnityEngine.Vector3 cameraTransformPos = m_arCameraManager.transform.position; + + /// Then compute poses + foreach (KeyValuePair subPose in m_subscriptionsPoses) + { + RelocalizationInformation information = m_relocalizationInformations[subPose.Value.uuidTarget]; + RelocObjects firstRelocInfo = information.RelocObjects.First(); + Dictionary temp = information.RelocObjects.ToDictionary(relocObject => relocObject.Trackable.UUID, relocObject => relocObject); + + /// Trackable selection + + Guid bestTrackableCandidateID = Guid.Empty; + + List trackedCandidates = new List(); + List limitedTrackingCandidates = new List(); + List notTrackedCandidates = new List(); + + //Hierarchy of types + string[] types = { "MESH", "IMAGE_MARKER", "FIDUCIAL_MARKER", "MAP", "OTHER" }; //TODO : GEOPOSE + + //We fill in the confidence level lists. + foreach (KeyValuePair relocObject in temp) + { + WorldAnalysisARFoundationModule.TrackableInfo inf = m_imageTrackableModule.GetPoseTrackable(relocObject.Value.Trackable.Name); // for now only image module + if (inf != null) + { + if (inf.confidence == 100) + { + trackedCandidates.Add(relocObject.Value); + } + else if (inf.confidence == 50) + { + limitedTrackingCandidates.Add(relocObject.Value); + } + else + { + notTrackedCandidates.Add(relocObject.Value); + } + } + } + + //uses the types[] array indexes as key, and UUIDs as values + List> typesSortedList = new List>(); + foreach (var relocObject in temp) + { + typesSortedList.Add(new KeyValuePair(relocObject.Value.Trackable.TrackableType.ToString(), relocObject.Value.Trackable.UUID)); + } + var sortedItems = typesSortedList.OrderBy(item => Array.IndexOf(types, item.Key)).ToList(); + + + if (trackedCandidates.Count > 0) + { + bestTrackableCandidateID = SelectTrackableWithTypeAndDistance(trackedCandidates, typesSortedList, cameraTransformPos); + } + else if (limitedTrackingCandidates.Count > 0) + { + bestTrackableCandidateID = SelectTrackableWithTypeAndDistance(limitedTrackingCandidates, typesSortedList, cameraTransformPos); + } + else if (notTrackedCandidates.Count > 0) + { + bestTrackableCandidateID = SelectTrackableWithTypeAndDistance(notTrackedCandidates, typesSortedList, cameraTransformPos); + } + + if (bestTrackableCandidateID != Guid.Empty) + { + /// We have found the best trackable + WorldAnalysisARFoundationModule.TrackableInfo info = m_imageTrackableModule.GetPoseTrackable(temp[bestTrackableCandidateID].Trackable.Name); + if (info == null) + { + // For now we just ignore it : we could also send not tracked todo + continue; + } + + Matrix4x4 tr = WorldAnalysisUnityHelper.ConvertETSIARFTransform3DToUnity(firstRelocInfo.Transform3D); + UnityEngine.Vector3 tr_trans = WorldAnalysisUnityHelper.ExtractTranslationFromMatrix(tr); + UnityEngine.Quaternion tr_rot = WorldAnalysisUnityHelper.ExtractRotationFromMatrix(tr); + + UnityEngine.Vector3 newPos = info.position + info.rotation * tr_trans; + UnityEngine.Quaternion newRot = (info.rotation * tr_rot); + + if (subPose.Value.pose.Mode == Mode_WorldAnalysis.TRACKABLES_TO_DEVICE) + { + // Inverse + newPos = -newPos; + newRot = UnityEngine.Quaternion.Inverse(newRot); + } + + //Pose metadata + ETSI.ARF.OpenAPI.WorldAnalysis.Pose poseToUpdate = new ETSI.ARF.OpenAPI.WorldAnalysis.Pose(); + poseToUpdate.Confidence = info.confidence; + poseToUpdate.Uuid = subPose.Value.uuidTarget; + poseToUpdate.Timestamp = info.timeStamp; + poseToUpdate.EstimationState = info.state; + poseToUpdate.InstructionInfo = ""; // not supported for now + poseToUpdate.Mode = subPose.Value.pose.Mode; + + //Pose value + VectorQuaternionPoseValue newPoseValue = new VectorQuaternionPoseValue(); + newPoseValue.Type = PoseValueType.VECTOR_QUATERNION; + newPoseValue.Unit = ETSI.ARF.OpenAPI.WorldAnalysis.UnitSystem.M; + + newPoseValue.Position = new ETSI.ARF.OpenAPI.WorldAnalysis.Vector3(newPos.x, newPos.y, newPos.z); + newPoseValue.Rotation = new ETSI.ARF.OpenAPI.WorldAnalysis.Quaternion(newRot.x, newRot.y, newRot.z, newRot.w); + poseToUpdate.Value = newPoseValue; + + //Save pose + m_computedPoses[subPose.Value.uuidTarget] = poseToUpdate; // Todo : in case of multiple subscription on the same pose, we compute it multiple times (can be needed in case of multiple mode, units etc.), in that case here the last one overrides + + if (subPose.Value.callback != null) + { + subPose.Value.callback(PoseEstimationResult.OK, poseToUpdate); + } + } + } + ManageSubscriptionValidity(); + } + else + { + Debug.Log("ETSI ARF : Starting..."); + } } #endregion + #region Lifecycle + + /// + /// Check the validity of all subscriptions and delete one if needed + /// + protected void ManageSubscriptionValidity() + { + List subscriptionToDelete = new List(); + foreach (KeyValuePair sub in m_subscriptionsPoses) + { + float validity = sub.Value.timeValidity; + if (Time.time > validity) + { + subscriptionToDelete.Add(sub.Key); + } + } + foreach (Guid s in subscriptionToDelete) + { + Debug.Log("ETSI ARF : Subscription deleted " + s); + m_subscriptionsPoses.Remove(s); + } + } + + #endregion #region ARF_API - public AskFrameRateResult SetPoseEstimationFramerate(string token, PoseConfigurationTrackableType type, EncodingInformationStructure encodingInformation, int minimumFramerate) + public AskFrameRateResult SetPoseEstimationFramerate(string token, PoseConfigurationTrackableType type, ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructure encodingInformation, int minimumFramerate) { return AskFrameRateResult.NOT_SUPPORTED; ///We cannot set any framerate for tracking on ARKit and ARCore } - public PoseEstimationResult GetLastPose(string token, string uuid, Mode_WorldAnalysis mode, out ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose) + public PoseEstimationResult GetLastPose(string token, Guid uuid, Mode_WorldAnalysis mode, out ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose) { - pose = null; + pose = new ETSI.ARF.OpenAPI.WorldAnalysis.Pose(); + if (!m_computedPoses.ContainsKey(uuid)) + { + Debug.LogWarning("ETSI ARF : Ask for the pose for an uuid without associated subscription"); + return PoseEstimationResult.UNKNOWN_ID; + } + ETSI.ARF.OpenAPI.WorldAnalysis.Pose computedPose = m_computedPoses[uuid]; + pose = computedPose; + if (mode != pose.Mode) + { + // Inverse pose + VectorQuaternionPoseValue poseValue = (VectorQuaternionPoseValue)pose.Value; + UnityEngine.Vector3 toUnityPos = WorldAnalysisUnityHelper.ConvertETSIVector3ToUnity(poseValue.Position); + toUnityPos = -toUnityPos; + UnityEngine.Quaternion toUnityRot = WorldAnalysisUnityHelper.ConvertETSIARFQuaternionToUnity(poseValue.Rotation); + toUnityRot = UnityEngine.Quaternion.Inverse(toUnityRot); + + poseValue.Position = new ETSI.ARF.OpenAPI.WorldAnalysis.Vector3(toUnityPos.x, toUnityPos.y, toUnityPos.z); + poseValue.Rotation = new ETSI.ARF.OpenAPI.WorldAnalysis.Quaternion(toUnityRot.x, toUnityRot.y, toUnityRot.z, toUnityRot.w); + } return PoseEstimationResult.OK; } - public PoseEstimationResult[] GetLastPoses(string token, string[] uuids, Mode_WorldAnalysis [] modes, out ETSI.ARF.OpenAPI.WorldAnalysis.Pose[] poses) + public PoseEstimationResult[] GetLastPoses(string token, Guid[] uuids, Mode_WorldAnalysis[] modes, out ETSI.ARF.OpenAPI.WorldAnalysis.Pose[] poses) { - poses = null; - return null; + if (uuids.Length != modes.Length) + { + Debug.LogError("ETSI ARF : Get poses: uuids and modes array do no have the same length"); + poses = null; + return null; + } + PoseEstimationResult[] resul = new PoseEstimationResult[uuids.Length]; + poses = new ETSI.ARF.OpenAPI.WorldAnalysis.Pose[uuids.Length]; + for (int i = 0; i < uuids.Length; i++) + { + ETSI.ARF.OpenAPI.WorldAnalysis.Pose pose; + PoseEstimationResult poseResul = GetLastPose(token, uuids[i], modes[i], out pose); + resul[i] = poseResul; + poses[i] = pose; + } + return resul; } - public InformationSubscriptionResult SubscribeToPose(string token, string uuid, Mode_WorldAnalysis mode, PoseCallback callback, ref int validity, out string subscriptionUUID) + + public InformationSubscriptionResult SubscribeToPose(string token, Guid uuid, Mode_WorldAnalysis mode, PoseCallback callback, ref int validity, out Guid subscriptionUUID) { - subscriptionUUID = ""; - return InformationSubscriptionResult.OK; + RelocalizationInformation relocInfo = null; + if (!m_relocalizationInformations.ContainsKey(uuid)) + { + // In case of another subscription to the same uuid : we do not get the reloc information again + List uuids = new List + { + uuid + }; + + List modes = new List + { + Mode_WorldStorage.TRACKABLES_TO_REQUEST + }; + + ETSI.ARF.OpenAPI.WorldAnalysis.Capability[] currentCapabilities; + // Filter with capabilities of current world analysis + GetCapabilities(token, out currentCapabilities); + + ///Convert into world storage capabilities + List capabilities = new List(); + foreach (ETSI.ARF.OpenAPI.WorldAnalysis.Capability capability in currentCapabilities) + { + capabilities.Add(WorldAnalysisUnityHelper.ConvertWorldAnalysisCapability(capability)); + } + + /// Collect relocalization information + ETSI.ARF.OpenAPI.WorldStorage.Response response = RelocalizationInformationRequest.GetRelocalizationInformation(m_worldStorageServer, uuids, modes, capabilities); + relocInfo = response.RelocInfo.First(); //Only one uuid requested + } + else + { + relocInfo = m_relocalizationInformations[uuid]; + } + + if (relocInfo == null) + { + subscriptionUUID = Guid.Empty; + /// Subscription not possible + return InformationSubscriptionResult.NONE; + } + + return CreateSubscriptionWithRelocalizationInformation(relocInfo, uuid, mode, callback, ref validity, out subscriptionUUID); } - public InformationSubscriptionResult[] SubscribeToPoses(string token, string[] uuids, Mode_WorldAnalysis [] modes, PoseCallback callback, ref int validity, out string[] subscriptionUUIDs) + public InformationSubscriptionResult[] SubscribeToPoses(string token, Guid[] uuids, Mode_WorldAnalysis[] modes, PoseCallback callback, ref int validity, out Guid[] subscriptionUUIDs) { - subscriptionUUIDs = null; - return null; + InformationSubscriptionResult[] resul = new InformationSubscriptionResult[uuids.Length]; + for (int i = 0; i < uuids.Length; i++) + { + resul[i] = InformationSubscriptionResult.NONE; // Init with None in case we do not collect relocalization info for that id + } + subscriptionUUIDs = new Guid[uuids.Length]; + if (uuids.Length != modes.Length) + { + Debug.LogError("ETSI ARF : You tried to subscribe to poses with two arrray of different length uuids and modes"); + return resul; + } + + ETSI.ARF.OpenAPI.WorldAnalysis.Capability[] currentCapabilities; + // Filter with capabilities of current world analysis + GetCapabilities(token, out currentCapabilities); + + ///Convert into world storage capabilities + List capabilities = new List(); + foreach (ETSI.ARF.OpenAPI.WorldAnalysis.Capability capability in currentCapabilities) + { + capabilities.Add(WorldAnalysisUnityHelper.ConvertWorldAnalysisCapability(capability)); + } + + List l_uuids = new List(); + List l_modes = new List(); + foreach (Guid uuid in uuids) + { + l_uuids.Add(uuid); + l_modes.Add(Mode_WorldStorage.TRACKABLES_TO_REQUEST); + } + /// Collect relocalization information + ETSI.ARF.OpenAPI.WorldStorage.Response response = RelocalizationInformationRequest.GetRelocalizationInformation(m_worldStorageServer, l_uuids, l_modes, capabilities); + + /// Check every reloc information object + foreach (RelocalizationInformation relocInfo in response.RelocInfo) + { + int indexUUID = l_uuids.IndexOf(relocInfo.RequestUUID); + Guid subscriptionUUID; + /// Create a subscription for each found reloc information + resul[indexUUID] = CreateSubscriptionWithRelocalizationInformation(relocInfo, relocInfo.RequestUUID, modes[indexUUID], callback, ref validity, out subscriptionUUID); + subscriptionUUIDs[indexUUID] = subscriptionUUID; + } + return resul; } - public InformationSubscriptionResult GetSubsription(string token, string subscriptionUUID, out PoseCallback callback, out string target, out Mode_WorldAnalysis mode, out int validity) + public InformationSubscriptionResult GetSubsription(string token, Guid subscriptionUUID, out PoseCallback callback, out Guid target, out Mode_WorldAnalysis mode, out int validity) { - callback = null; - target = ""; - mode = Mode_WorldAnalysis.TRACKABLES_TO_DEVICE; - validity = 0; - return InformationSubscriptionResult.OK; + if (!m_subscriptionsPoses.ContainsKey(subscriptionUUID)) + { + subscriptionUUID = Guid.Empty; + callback = null; + target = Guid.Empty; + mode = Mode_WorldAnalysis.TRACKABLES_TO_DEVICE; + validity = 0; + return InformationSubscriptionResult.UNKNOWN_ID; + } + else + { + SubscriptionInfo subInfo = m_subscriptionsPoses[subscriptionUUID]; + callback = subInfo.callback; + target = subInfo.uuidTarget; + mode = subInfo.pose.Mode; + float validitySeconds = subInfo.timeValidity - Time.time; + validity = (int)validitySeconds * 1000;// conversion in ms + return InformationSubscriptionResult.OK; + } } - public InformationSubscriptionResult UpdateSubscription(string token, string subscriptionUUID, Mode_WorldAnalysis mode, int validity, PoseCallback callback) + public InformationSubscriptionResult UpdateSubscription(string token, Guid subscriptionUUID, Mode_WorldAnalysis mode, int validity, PoseCallback callback) { - return InformationSubscriptionResult.OK; + if (m_subscriptionsPoses.ContainsKey(subscriptionUUID)) + { + SubscriptionInfo currrent = m_subscriptionsPoses[subscriptionUUID]; + SubscriptionInfo newSubInfo = new SubscriptionInfo(); + newSubInfo.callback = callback; + newSubInfo.uuidTarget = currrent.uuidTarget; + newSubInfo.pose = new ETSI.ARF.OpenAPI.WorldAnalysis.Pose(); + newSubInfo.pose.Mode = mode; + newSubInfo.timeValidity = Time.time + (validity / 1000.0f); + m_subscriptionsPoses[subscriptionUUID] = newSubInfo; + return InformationSubscriptionResult.OK; + } + else + { + return InformationSubscriptionResult.UNKNOWN_ID; + } } - public InformationSubscriptionResult UnSubscribeToPose(string subscriptionUUID) + public InformationSubscriptionResult UnSubscribeToPose(Guid subscriptionUUID) { - return InformationSubscriptionResult.OK; + if (m_subscriptionsPoses.ContainsKey(subscriptionUUID)) + { + m_subscriptionsPoses.Remove(subscriptionUUID); + return InformationSubscriptionResult.OK; + } + else + { + return InformationSubscriptionResult.UNKNOWN_ID; + } } - public CapabilityResult GetCapabilities(string token, out Capability[] capabilities) + public CapabilityResult GetCapabilities(string token, out ETSI.ARF.OpenAPI.WorldAnalysis.Capability[] capabilities) { - capabilities = null; + List capabilitiesList = new List(); + + /// Image Tracking + if (m_imageTrackableModule != null) + { + ETSI.ARF.OpenAPI.WorldAnalysis.Capability capabilityImage = new ETSI.ARF.OpenAPI.WorldAnalysis.Capability(); + capabilityImage.TrackableType = ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType.IMAGE_MARKER; + ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructure encodingInformation = new ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructure(); + encodingInformation.DataFormat = ETSI.ARF.OpenAPI.WorldAnalysis.EncodingInformationStructureDataFormat.OTHER; + encodingInformation.Version = "1.01"; + capabilityImage.EncodingInformation = encodingInformation; + capabilityImage.Framerate = 30; // Not particularly specified on ARKit and ARCore + capabilityImage.Latency = 0; // Not particularly specified on ARKit and ARCore + capabilityImage.Accuracy = 1; // Not particularly specified on ARKit and ARCore + capabilitiesList.Add(capabilityImage); + } + + capabilities = capabilitiesList.ToArray(); return CapabilityResult.OK; } - public CapabilityResult GetCapability(string token, string uuid, out bool isSupported, out TypeWorldStorage type, out Capability capability) + public CapabilityResult GetCapability(string token, Guid uuid, out bool isSupported, out ETSI.ARF.OpenAPI.WorldAnalysis.TypeWorldStorage type, out ETSI.ARF.OpenAPI.WorldAnalysis.Capability[] capability) { - isSupported = false; - type = TypeWorldStorage.UNKNOWN; - capability = null; - return CapabilityResult.OK; + throw new NotImplementedException(); + /* isSupported = false; + type = ETSI.ARF.OpenAPI.WorldAnalysis.TypeWorldStorage.UNKNOWN; + capability = null; + return CapabilityResult.OK;*/ + } + + #endregion + + #region Helper + + /// + /// To select the best trackable + /// + /// + /// + /// + /// + private Guid SelectTrackableWithTypeAndDistance(List candidates, List> typesSortedList, UnityEngine.Vector3 cameraTransformPos) + { + float bestDistance = 100.0f; //valeur par défaut = grande distance (ŕ changer) + Guid selectedTrackableUUID = Guid.Empty; + + //if only one trackable is candidate + if (candidates.Count > 0) + { + if (candidates.Count == 1) + { + selectedTrackableUUID = candidates[0].Trackable.UUID; //selected candidate + } + else if (candidates.Count >= 2) + { + //if there is more than one candidate, whe must chose using the hierarchy of the trackables + //The list is already sorted, so whe just keep those who have the higher rank + List bestTypeCandidates = new List(); + + foreach (KeyValuePair kvp in typesSortedList) + { + if (kvp.Key == typesSortedList.First().Key) + { + bestTypeCandidates.Add(kvp.Value); + } + else + { + break; + } + } + + //Then if there is only one candidate with the higher rank + if (bestTypeCandidates.Count == 1) + { + selectedTrackableUUID = bestTypeCandidates[0]; //selected candidate + } + else + { + // Shortest distance among those in the confidence class with the same best trackable type rank + foreach (Guid distanceCandidate in bestTypeCandidates) + { + foreach (RelocObjects tmp in candidates) + { + // select the corresponding candidate from the list of candidates + if (tmp.Trackable.UUID == distanceCandidate) + { + WorldAnalysisARFoundationModule.TrackableInfo inf = m_imageTrackableModule.GetPoseTrackable(tmp.Trackable.Name); + + float distance = UnityEngine.Vector3.Distance(cameraTransformPos, inf.position); + + if (distance <= bestDistance) + { + bestDistance = distance; + selectedTrackableUUID = tmp.Trackable.UUID; + } + } + } + } + } + } + } + + return selectedTrackableUUID; + } + + private InformationSubscriptionResult CreateSubscriptionWithRelocalizationInformation(RelocalizationInformation relocInfo, Guid uuid, Mode_WorldAnalysis mode, PoseCallback callback, ref int validity, out Guid subscriptionUUID) + { + subscriptionUUID = Guid.Empty; + bool hasSupport = false; + foreach (RelocObjects relocObj in relocInfo.RelocObjects) + { + /// We try to find a supported trackable + if (relocObj.Trackable.TrackableType == ETSI.ARF.OpenAPI.WorldStorage.TrackableType.IMAGE_MARKER) + { + hasSupport |= m_imageTrackableModule.AddTrackable(relocObj.Trackable); + } + } + + if (!hasSupport) + { + /// We did not find any trackable to track this uuid + subscriptionUUID = Guid.Empty; + return InformationSubscriptionResult.NOT_SUPPORTED; + } + // We found at least one trackable + m_relocalizationInformations[uuid] = relocInfo; + + /// We add the subscription + SubscriptionInfo sub = new SubscriptionInfo(); + sub.timeValidity = Time.time + (validity / 1000.0f); + sub.pose = new ETSI.ARF.OpenAPI.WorldAnalysis.Pose(); + sub.pose.Mode = mode; + sub.uuidTarget = uuid; + sub.callback = callback; + subscriptionUUID = Guid.NewGuid(); + m_subscriptionsPoses.Add(subscriptionUUID, sub); + return InformationSubscriptionResult.OK; } #endregion diff --git a/Runtime/Scripts/WorldAnalysisARFoundationModule.cs b/Runtime/Scripts/WorldAnalysisARFoundationModule.cs new file mode 100644 index 0000000..3951fcc --- /dev/null +++ b/Runtime/Scripts/WorldAnalysisARFoundationModule.cs @@ -0,0 +1,37 @@ +using UnityEngine; + +public interface WorldAnalysisARFoundationModule +{ + /// + /// Informations about a trackable + /// + public class TrackableInfo + { + public string name; + public int timeStamp; + public ETSI.ARF.OpenAPI.WorldAnalysis.PoseEstimationState state; + public double confidence; + public Vector3 position; + public Quaternion rotation; + public ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType trackableType; + } + + /// + /// Initialize tracking module + /// + public void Initialize(); + + /// + /// Add to the list of trackable that the module need to tracked + /// + /// Trackable with its parameters + /// supported or not + public bool AddTrackable(ETSI.ARF.OpenAPI.WorldStorage.Trackable trackable); + + /// + /// + /// + /// + public TrackableInfo GetPoseTrackable(string name); + +} \ No newline at end of file diff --git a/Runtime/Scripts/WorldAnalysisARFoundationModule.cs.meta b/Runtime/Scripts/WorldAnalysisARFoundationModule.cs.meta new file mode 100644 index 0000000..ce5fc12 --- /dev/null +++ b/Runtime/Scripts/WorldAnalysisARFoundationModule.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 93834ff9465ac13478c829115e75ac90 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs b/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs new file mode 100644 index 0000000..e7da9b2 --- /dev/null +++ b/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs @@ -0,0 +1,208 @@ +using System; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.XR.ARFoundation; +using UnityEngine.XR.ARSubsystems; +using System.IO; +using Unity.XR.CoreUtils; +using static WorldAnalysisARFoundationModule; +using ETSI.ARF.OpenAPI.WorldStorage; + +public class WorldAnalysisARFoundationModuleImage : WorldAnalysisARFoundationModule +{ + /// + /// ARFoundation Image tracking management + /// + private ARTrackedImageManager m_trackedImageManager; + /// + /// Name of all images that have been added to the library + /// + private List m_trackedImageInLibrary; + /// + /// List of tracked images with tracking infos + /// + private Dictionary m_trackedImages = new Dictionary(); + /// + /// Default Constructor + /// + public WorldAnalysisARFoundationModuleImage() { } + + /// + /// Initialize image tracking module + /// + public void Initialize() + { + XROrigin origin = UnityEngine.Object.FindAnyObjectByType(); + m_trackedImageManager = origin.gameObject.AddComponent(); + m_trackedImageInLibrary = new List(); + m_trackedImageManager.trackedImagePrefab = (GameObject)Resources.Load("ARFImageTrackingPrefab"); + m_trackedImageManager.trackedImagesChanged += OnTrackedImagesChanged; + } + + /// + /// found : pourrait aussi tracké pas tracké, on pourrait aussi ajouter la cofiance si dispo dans l'API + /// + /// name of the image trackable + + public TrackableInfo GetPoseTrackable(string name) + { + if (m_trackedImages.ContainsKey(name)) + { + return m_trackedImages[name]; + } + else + { + return null; + } + } + + /// + /// Add a trackable : need to be an image + /// + /// Image trackable + /// Supported or not + public bool AddTrackable(Trackable trackable) + { + /// Here : we don't check if the trackable is allready added, AddImageToLibrary does it + if (trackable.TrackableType != ETSI.ARF.OpenAPI.WorldStorage.TrackableType.IMAGE_MARKER) + { + return false; + } + AddImageToLibrary(trackable.Name, (float)trackable.TrackableSize[0]); + return true; + } + + /// + /// Event manager for when an image is tracked + /// + /// the event + private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs) + { + foreach (var trackedImage in eventArgs.updated) + { + Vector3 position = trackedImage.transform.position; + Quaternion rotation = trackedImage.transform.rotation; + + TrackableInfo info = new TrackableInfo(); + info.name = trackedImage.referenceImage.name; + if (trackedImage.trackingState == TrackingState.Tracking) + { + info.state = ETSI.ARF.OpenAPI.WorldAnalysis.PoseEstimationState.OK; + info.confidence = 100; + } + else if (trackedImage.trackingState == TrackingState.Limited) + { + info.state = ETSI.ARF.OpenAPI.WorldAnalysis.PoseEstimationState.OK; + info.confidence = 50; + } + else + { + info.state = ETSI.ARF.OpenAPI.WorldAnalysis.PoseEstimationState.FAILURE; //ADD NOT_TRACKED ? + info.confidence = 0; + } + info.timeStamp = unchecked((int)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); + info.position = position; + info.rotation = rotation; + info.trackableType = ETSI.ARF.OpenAPI.WorldAnalysis.TrackableType.IMAGE_MARKER; + m_trackedImages[info.name] = info; + } + } + + /// + /// Add a new image to the arfoundation library + /// + /// file name + /// image width in meters + /// + protected bool AddImageToLibrary(string fileName, float imageWidthInMeters) + { + // check if image is in the library + if (m_trackedImageInLibrary.Contains(fileName)) + { + Debug.Log("Image allready added to library"); + return true; + } +#if UNITY_EDITOR + string imagePath = Application.streamingAssetsPath + "/" + fileName; +#else + string imagePath = Application.persistentDataPath + "/" + fileName ; +#endif + + bool found = CheckImageExist(ref imagePath); // try to find in jpg or png + if (!found) + { + Debug.LogWarning("Try to Track Image " + fileName + " but file not found"); + return false; + } + + //Load texture and add it to arfoundation library + var rawData = File.ReadAllBytes(imagePath); + Texture2D tex = new Texture2D(2, 2); + tex.LoadImage(rawData); + AddTextureToLibrary(tex, fileName, imageWidthInMeters); + return true; + } + + /// + /// Add a texture2D to the list of tracked image of the ARFoundation image library + /// + /// texture to track + /// name of the trackable + /// with of the image in meters + private async void AddTextureToLibrary(Texture2D imageToAdd, string imageName, float imageWidthInMeters) + { + while (ARSession.state == ARSessionState.CheckingAvailability || ARSession.state == ARSessionState.None) + { + await System.Threading.Tasks.Task.Delay(100); + } + IReferenceImageLibrary library; + if (m_trackedImageManager.referenceLibrary == null) + { + library = m_trackedImageManager.CreateRuntimeLibrary(); // Create library if it does not exist + } + else + { + library = m_trackedImageManager.referenceLibrary; + } + + if (library is MutableRuntimeReferenceImageLibrary mutableLibrary) + { + if (!m_trackedImageInLibrary.Contains(imageName)) + { + mutableLibrary.ScheduleAddImageWithValidationJob(imageToAdd, imageName, imageWidthInMeters); // check if that does not fail? + m_trackedImageInLibrary.Add(imageName); + } + } + else + { + Debug.LogError("Error adding image: Library is not mutableLibrary"); + } + m_trackedImageManager.referenceLibrary = library; + if (!m_trackedImageManager.enabled) m_trackedImageManager.enabled = true; // Necessary? + } + + /// + /// Check if image exist in jpg or png format and modify string with path + /// + /// path to check without extension + /// image exists or not + private bool CheckImageExist(ref string path) + { + string tempPath = path + ".png"; + if (File.Exists(tempPath)) + { + path = tempPath; + return true; + } + else + { + tempPath = path + ".jpg"; + if (File.Exists(tempPath)) + { + path = tempPath; + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs.meta b/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs.meta new file mode 100644 index 0000000..789e255 --- /dev/null +++ b/Runtime/Scripts/WorldAnalysisARFoundationModuleImage.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 51002affc1ae7c24abffa916faae5451 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef b/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef index 084868a..80bdd36 100644 --- a/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef +++ b/Runtime/etsi.isg.arf.worldanalysisarfoundation.asmdef @@ -5,7 +5,8 @@ "GUID:99fdaa6f193b69346bfc8863615f98f0", "GUID:065e856e9f9becb49abdf0cb6855a039", "GUID:a9420e37d7990b54abdef6688edbe313", - "GUID:92703082f92b41ba80f0d6912de66115" + "GUID:92703082f92b41ba80f0d6912de66115", + "GUID:dc960734dc080426fa6612f1c5fe95f3" ], "includePlatforms": [], "excludePlatforms": [], -- GitLab From c13cc4fc084b4b729330d007c90e3053041e0592 Mon Sep 17 00:00:00 2001 From: jlacoche Date: Tue, 21 May 2024 14:35:21 +0200 Subject: [PATCH 3/3] Update documentation --- README.md | 93 +++++++++---------------------------------------------- 1 file changed, 14 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index af3c444..dc9d00b 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,27 @@ -# Unity World Analysis ARFoundation Wrapper Package +*This repository is part of the outcomes of the Specialist Task Force 669 focusing on the World Analysis as part of the ETSI ISG Augmented Reality Framework architecture (https://www.etsi.org/deliver/etsi_gs/ARF/001_099/003/01.01.01_60/gs_ARF003v010101p.pdf).* +*The set of the World Anlysis components includes:* +*• The C++ and C# source code for servers and clients generated from OpenAPI available here (https://forge.etsi.org/rep/arf/openapi)* +*• Multiple Unity plugin (wrappers) implementing the World Analysis with various AR technologies -## Getting started +*All these components are available under the ETSI Labs group “World Analysis API Helpers”: https://labs.etsi.org/rep/arf/world-analysis-api-helpers* -To make it easy for you to get started with GitLab, here's a list of recommended next steps. +*This repository and the associated components depend on the World Storage components developed in the context of the Specialist Task Force 620 that focused on the authoring of a World Representation and which are vailable under the ETSI Labs group “World Storage API Helpers”: https://labs.etsi.org/rep/arf/world-storage-api-helpers* -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! +*If you wish to contribute to this project or any other projects in the context of the [ETSI ISG Augmented Reality Framework architecture](https://www.etsi.org/committee/1420-arf), please refer to the ["How to get involved in an ISG" section on the ETSI website](https://www.etsi.org/how-to-get-involved-in-an-isg)* -## Add your files +--- -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: -``` -cd existing_repo -git remote add origin https://labs.etsi.org/rep/arf/world-analysis-api-helpers/unity-world-analysis-arfoundation-wrapper-package.git -git branch -M main -git push -uf origin main -``` +# ARFoundation World Analysis # -## Integrate with your tools +This wrapper for the World Analysis relies on the ARFoundation (version 5.1.x) Unity package available here: https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.1/manual/index.html and is compatible with ARKit and ARCore. -- [ ] [Set up project integrations](https://labs.etsi.org/rep/arf/world-analysis-api-helpers/unity-world-analysis-arfoundation-wrapper-package/-/settings/integrations) +## Supported Trackables and Setup -## Collaborate with your team +### Image Trackables ### -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) +Image markers are supported on both iOS and Android platforms through ARKit and ARCore. -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +The name of the Trackable in the World Storage must correspond to the name of a file (jpg or png) placed in the Unity persistent data path of the application on the user device. \ No newline at end of file -- GitLab