/// <summary> /// Enable this search result for tracking /// /// Creates an ImageTarget for local detection and tracking of this target. /// If the given game object has no ImageTargetBehaviour, a new one will be created. /// Note that this call may result in an earlier ImageTarget that was enabled for /// tracking to be destroyed, including its ImageTargetBehaviour. /// Thus it is not advised to hold a pointer to an /// ealier created ImageTarget after calling enableTracking again. Returns /// NULL if the target failed to be enabled for tracking. /// </summary> public override ImageTargetBehaviour EnableTracking(TargetSearchResult result, GameObject gameObject) { // create a new trackable in native IntPtr imageTargetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ImageTargetData))); int newTrackableCount = QCARWrapper.Instance.TargetFinderEnableTracking(result.TargetSearchResultPtr, imageTargetPtr); ImageTargetData imageTargetData = (ImageTargetData) Marshal.PtrToStructure(imageTargetPtr, typeof(ImageTargetData)); Marshal.FreeHGlobal(imageTargetPtr); StateManagerImpl stateManager = (StateManagerImpl)TrackerManager.Instance.GetStateManager(); // if successful, create a new trackable here and add it to internal collection ImageTargetBehaviour imageTargetBehaviour = null; if (imageTargetData.id == -1) { Debug.LogError("TargetSearchResult " + result.TargetName + " could not be enabled for tracking."); } else { ImageTarget newImageTarget = new CloudRecoImageTargetImpl(result.TargetName, imageTargetData.id, imageTargetData.size); // Add newly created Image Target to dictionary. mImageTargets[imageTargetData.id] = newImageTarget; // Find or create ImageTargetBehaviour for this ImageTarget: imageTargetBehaviour = stateManager.FindOrCreateImageTargetBehaviourForTrackable(newImageTarget, gameObject); } // get a list of currently existing trackables IntPtr imageTargetIdArrayPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * newTrackableCount); QCARWrapper.Instance.TargetFinderGetImageTargets(imageTargetIdArrayPtr, newTrackableCount); List <int> targetIds = new List <int>(); for (int i = 0; i < newTrackableCount; i++) { IntPtr targetIdPtr = new IntPtr(imageTargetIdArrayPtr.ToInt32() + i * Marshal.SizeOf(typeof(int))); int targetID = Marshal.ReadInt32(targetIdPtr); targetIds.Add(targetID); } Marshal.FreeHGlobal(imageTargetIdArrayPtr); // delete those targets that no longer exist foreach (ImageTarget imageTarget in mImageTargets.Values.ToArray()) { if (!targetIds.Contains(imageTarget.ID)) { stateManager.DestroyTrackableBehavioursForTrackable(imageTarget); mImageTargets.Remove(imageTarget.ID); } } return(imageTargetBehaviour); }
public override ImageTargetAbstractBehaviour EnableTracking(TargetFinder.TargetSearchResult result, GameObject gameObject) { IntPtr trackableData = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ImageTargetData))); int trackableIdArrayLength = QCARWrapper.Instance.TargetFinderEnableTracking(result.TargetSearchResultPtr, trackableData); ImageTargetData data = (ImageTargetData)Marshal.PtrToStructure(trackableData, typeof(ImageTargetData)); Marshal.FreeHGlobal(trackableData); StateManagerImpl stateManager = (StateManagerImpl)TrackerManager.Instance.GetStateManager(); ImageTargetAbstractBehaviour behaviour = null; if (data.id == -1) { Debug.LogError("TargetSearchResult " + result.TargetName + " could not be enabled for tracking."); } else { ImageTarget trackable = new CloudRecoImageTargetImpl(result.TargetName, data.id, data.size); this.mImageTargets[data.id] = trackable; behaviour = stateManager.FindOrCreateImageTargetBehaviourForTrackable(trackable, gameObject); } IntPtr trackableIdArray = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(int)) * trackableIdArrayLength)); QCARWrapper.Instance.TargetFinderGetImageTargets(trackableIdArray, trackableIdArrayLength); List <int> list = new List <int>(); for (int i = 0; i < trackableIdArrayLength; i++) { IntPtr ptr = new IntPtr(trackableIdArray.ToInt32() + (i * Marshal.SizeOf(typeof(int)))); int item = Marshal.ReadInt32(ptr); list.Add(item); } Marshal.FreeHGlobal(trackableIdArray); foreach (ImageTarget target2 in this.mImageTargets.Values.ToArray <ImageTarget>()) { if (!list.Contains(target2.ID)) { stateManager.DestroyTrackableBehavioursForTrackable(target2, true); this.mImageTargets.Remove(target2.ID); } } return(behaviour); }
/// <summary> /// Enable this search result for tracking /// /// Creates an ImageTarget for local detection and tracking of this target. /// If the given game object has no ImageTargetBehaviour, a new one will be created. /// Note that this call may result in an earlier ImageTarget that was enabled for /// tracking to be destroyed, including its ImageTargetBehaviour. /// Thus it is not advised to hold a pointer to an /// ealier created ImageTarget after calling enableTracking again. Returns /// NULL if the target failed to be enabled for tracking. /// </summary> public override ImageTargetBehaviour EnableTracking(TargetSearchResult result, GameObject gameObject) { // create a new trackable in native IntPtr imageTargetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ImageTargetData))); int newTrackableCount = QCARWrapper.Instance.TargetFinderEnableTracking(result.TargetSearchResultPtr, imageTargetPtr); ImageTargetData imageTargetData = (ImageTargetData) Marshal.PtrToStructure(imageTargetPtr, typeof(ImageTargetData)); Marshal.FreeHGlobal(imageTargetPtr); StateManagerImpl stateManager = (StateManagerImpl)TrackerManager.Instance.GetStateManager(); // if successful, create a new trackable here and add it to internal collection ImageTargetBehaviour imageTargetBehaviour = null; if (imageTargetData.id == -1) { Debug.LogError("TargetSearchResult " + result.TargetName + " could not be enabled for tracking."); } else { ImageTarget newImageTarget = new CloudRecoImageTargetImpl(result.TargetName, imageTargetData.id, imageTargetData.size); // Add newly created Image Target to dictionary. mImageTargets[imageTargetData.id] = newImageTarget; // Find or create ImageTargetBehaviour for this ImageTarget: imageTargetBehaviour = stateManager.FindOrCreateImageTargetBehaviourForTrackable(newImageTarget, gameObject); } // get a list of currently existing trackables IntPtr imageTargetIdArrayPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * newTrackableCount); QCARWrapper.Instance.TargetFinderGetImageTargets(imageTargetIdArrayPtr, newTrackableCount); List<int> targetIds = new List<int>(); for (int i = 0; i < newTrackableCount; i++) { IntPtr targetIdPtr = new IntPtr(imageTargetIdArrayPtr.ToInt32() + i * Marshal.SizeOf(typeof(int))); int targetID = Marshal.ReadInt32(targetIdPtr); targetIds.Add(targetID); } Marshal.FreeHGlobal(imageTargetIdArrayPtr); // delete those targets that no longer exist foreach (ImageTarget imageTarget in mImageTargets.Values.ToArray()) { if (!targetIds.Contains(imageTarget.ID)) { stateManager.DestroyTrackableBehavioursForTrackable(imageTarget); mImageTargets.Remove(imageTarget.ID); } } return imageTargetBehaviour; }