private void CreateImageTargets() { int trackableDataArrayLength = QCARWrapper.Instance.DataSetGetNumTrackableType(TypeMapping.GetTypeID(typeof(ImageTarget)), this.mDataSetPtr); IntPtr trackableDataArray = Marshal.AllocHGlobal((int)(Marshal.SizeOf(typeof(ImageTargetData)) * trackableDataArrayLength)); if (QCARWrapper.Instance.DataSetGetTrackablesOfType(TypeMapping.GetTypeID(typeof(ImageTarget)), trackableDataArray, trackableDataArrayLength, this.mDataSetPtr) == 0) { Debug.LogError("Could not create Image Targets"); } else { for (int i = 0; i < trackableDataArrayLength; i++) { IntPtr ptr = new IntPtr(trackableDataArray.ToInt32() + (i * Marshal.SizeOf(typeof(ImageTargetData)))); ImageTargetData data = (ImageTargetData)Marshal.PtrToStructure(ptr, typeof(ImageTargetData)); if (!this.mTrackablesDict.ContainsKey(data.id)) { int capacity = 0x80; StringBuilder trackableName = new StringBuilder(capacity); QCARWrapper.Instance.DataSetGetTrackableName(this.mDataSetPtr, data.id, trackableName, capacity); ImageTarget target = new ImageTargetImpl(trackableName.ToString(), data.id, ImageTargetType.PREDEFINED, this); this.mTrackablesDict[data.id] = target; } } Marshal.FreeHGlobal(trackableDataArray); } }
/// <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); }
// Creates a new Image Target with the data of the Image Target with the // given name. // Returns false if Image Target does not exist. public void GetImageTarget(string name, out ImageTargetData it) { try { it = imageTargets[name]; } catch { throw; } }
// Add Virtual Button to the Image Target with the given imageTargetName. public void AddVirtualButton(VirtualButtonData item, string imageTargetName) { try { ImageTargetData it = imageTargets[imageTargetName]; it.virtualButtons.Add(item); } catch { throw; } }
private void CreateImageTargets() { // Allocate array for all Image Targets. int numImageTargets = dataSetGetNumTrackableType( (int)TrackableBehaviour.TrackableType.IMAGE_TARGET, mDataSetPtr); IntPtr imageTargetDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ImageTargetData)) * numImageTargets); // Copy Image Target properties from native. if (dataSetGetTrackablesOfType((int)TrackableBehaviour.TrackableType.IMAGE_TARGET, imageTargetDataPtr, numImageTargets, mDataSetPtr) == 0) { Debug.LogError("Could not create Image Target Behaviours"); return; } // Create Image Target Behaviours. for (int i = 0; i < numImageTargets; ++i) { IntPtr trackablePtr = new IntPtr(imageTargetDataPtr.ToInt32() + i * Marshal.SizeOf(typeof(ImageTargetData))); ImageTargetData trackableData = (ImageTargetData) Marshal.PtrToStructure(trackablePtr, typeof(ImageTargetData)); // Do not overwrite existing Trackables. if (mTrackableBehaviourDict.ContainsKey(trackableData.id)) { continue; } // QCAR support names up to 64 characters in length, but here we allocate // a slightly larger buffer: int nameLength = 128; System.Text.StringBuilder trackableName = new System.Text.StringBuilder(nameLength); dataSetGetTrackableName(mDataSetPtr, trackableData.id, trackableName, nameLength); ImageTargetBehaviour itb = CreateImageTarget(trackableData.id, trackableName.ToString(), trackableData.size); // Create Virtual Buttons for this Image Target. CreateVirtualButtons(itb); // Add newly created Image Target to dictionary. mTrackableBehaviourDict[trackableData.id] = itb; } Marshal.FreeHGlobal(imageTargetDataPtr); }
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); }
// Set attributes of the Image Target with the given name. // If the Image Target does not yet exist it is created automatically. public void SetImageTarget(ImageTargetData item, string name) { imageTargets[name] = item; }
// Creates a new Image Target with the data of the Image Target with the // given name. // Returns false if Image Target does not exist. public void GetImageTarget(string name, out ImageTargetData it) { try { it = imageTargets[name]; } catch { throw; } }
// Set attributes of the Image Target with the given name. // If the Image Target does not yet exist it is created automatically. public void SetImageTarget(ImageTargetData item, string name) { imageTargets[name] = item; }