Log in or register to post comments

getting lost of tracker data

September 24, 2011 - 4:43am #1

I am using a large amount of markers. The current way of getting the position and rotation in QCAR is not very suitable for this. I have to stick all the info in a normal array because that makes it much easier to examine all the data in a for loop.

Let me give you an example:

//Check each marker if it's tracked or not
if(m_Trackable0.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable0.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[0] = true;
	markerPosition[0] = m_Trackable0.transform.position;
	markerRotation[0] = m_Trackable0.transform.rotation;
}
else{
	markerTracking[0] = false;
}
			
if(m_Trackable1.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable1.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[1] = true;
	markerPosition[1] = m_Trackable1.transform.position;
	markerRotation[1] = m_Trackable1.transform.rotation;
}
else{
	markerTracking[1] = false;
}
			
if(m_Trackable2.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable2.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[2] = true;
	markerPosition[2] = m_Trackable2.transform.position;
	markerRotation[2] = m_Trackable2.transform.rotation;
}
else{
	markerTracking[2] = false;
}
			
if(m_Trackable3.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable3.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[3] = true;
	markerPosition[3] = m_Trackable3.transform.position;
	markerRotation[3] = m_Trackable3.transform.rotation;
}
else{
	markerTracking[3] = false;
}
			
if(m_Trackable4.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable4.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[4] = true;
	markerPosition[4] = m_Trackable4.transform.position;
	markerRotation[4] = m_Trackable4.transform.rotation;
}
else{
	markerTracking[4] = false;
}
			
if(m_Trackable5.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable5.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[5] = true;
	markerPosition[5] = m_Trackable5.transform.position;
	markerRotation[5] = m_Trackable5.transform.rotation;
}
else{
	markerTracking[5] = false;
}
			
if(m_Trackable6.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable6.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[6] = true;
	markerPosition[6] = m_Trackable6.transform.position;
	markerRotation[6] = m_Trackable6.transform.rotation;
}
else{
	markerTracking[6] = false;
}
			
if(m_Trackable7.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable7.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[7] = true;
	markerPosition[7] = m_Trackable7.transform.position;
	markerRotation[7] = m_Trackable7.transform.rotation;
}
else{
	markerTracking[7] = false;
}
			
if(m_Trackable8.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable8.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[8] = true;
	markerPosition[8] = m_Trackable8.transform.position;
	markerRotation[8] = m_Trackable8.transform.rotation;
}
else{
	markerTracking[8] = false;
}
			
if(m_Trackable9.CurrentStatus == TrackableBehaviour.Status.DETECTED ||  m_Trackable9.CurrentStatus == TrackableBehaviour.Status.TRACKED){
	markerTracking[9] = true;
	markerPosition[9] = m_Trackable9.transform.position;
	markerRotation[9] = m_Trackable9.transform.rotation;
}
else{
	markerTracking[9] = false;
}

And that's only 10 markers. Imagine what it looks like when using 512 markers.

Can't you guys make a function which returns an array with all the info of all the markers currently in view? That would be much cleaner and easier to use. Of course I could make this function my self, but that is not the point. I don't want to sit here for a few hours while I am copying the same bit of code and modifying the names of the variables for 512 amount of times.

Re: getting lost of tracker data

September 25, 2011 - 11:41pm #3

Thanks, That is much cleaner indeed.

Re: getting lost of tracker data

September 25, 2011 - 6:08am #2

Oh no, don't copy and paste 512 times, you'll go crazy :) Here's a bit of code that will loop through all the MarkerBehaviours in your scene and organize the data you want by MarkerID. Unfortunately you still have to drag out 512 prefabs, but that is something we're improving for a later release.

using UnityEngine;

// A custom handler that implements the ITrackerEventHandler interface.
public class TrackerEventHandler : MonoBehaviour,
                                   ITrackerEventHandler
{

    private MarkerBehaviour[] markerBehaviours;

    private bool[] markerTracking = new bool[512];
    private Vector3[] markerPosition = new Vector3[512];
    private Quaternion[] markerRotation = new Quaternion[512];

    void Start()
    {
        TrackerBehaviour trackerBehaviour = GetComponent<TrackerBehaviour>();
        if (trackerBehaviour)
        {
            trackerBehaviour.RegisterTrackerEventHandler(this);
        }
        
        markerBehaviours = (MarkerBehaviour[]) FindObjectsOfType(typeof(MarkerBehaviour));
    }


    // Implementation of the ITrackerEventHandler function called after all
    // trackables have changed.
    public void OnTrackablesUpdated()
    {
        foreach (MarkerBehaviour mb in markerBehaviours)
        {
            if (mb.CurrentStatus == TrackableBehaviour.Status.DETECTED ||
                mb.CurrentStatus == TrackableBehaviour.Status.TRACKED)
            {
                markerTracking[mb.MarkerID] = true;
                markerPosition[mb.MarkerID] = mb.transform.position;
                markerRotation[mb.MarkerID] = mb.transform.rotation;
            }
            else
            {
                markerTracking[mb.MarkerID] = false;
            }
        }

        // Log data on tap
        if (Input.touches.Length > 0 && Input.touches[0].phase == TouchPhase.Began)
        {
            for (int i = 0; i < markerBehaviours.Length; i++)
            {
                if (markerTracking[i])
                {
                    Debug.Log("Marker " + i + " is tracked at " + markerPosition[i] + 
                              " with rotation " + markerRotation[i]);
                }
            }
        }
    }
}

Or, even simpler, if you want to handle the visible markers on the spot:

using UnityEngine;

// A custom handler that implements the ITrackerEventHandler interface.
public class TrackerEventHandler2 : MonoBehaviour,
                                    ITrackerEventHandler
{

    private MarkerBehaviour[] markerBehaviours;

    void Start()
    {
        TrackerBehaviour trackerBehaviour = GetComponent<TrackerBehaviour>();
        if (trackerBehaviour)
        {
            trackerBehaviour.RegisterTrackerEventHandler(this);
        }
        
        markerBehaviours = (MarkerBehaviour[]) FindObjectsOfType(typeof(MarkerBehaviour));
    }


    // Implementation of the ITrackerEventHandler function called after all
    // trackables have changed.
    public void OnTrackablesUpdated()
    {
        foreach (MarkerBehaviour mb in markerBehaviours)
        {
            if (mb.CurrentStatus == TrackableBehaviour.Status.DETECTED ||
                mb.CurrentStatus == TrackableBehaviour.Status.TRACKED)
            {
                Debug.Log("Marker " + mb.MarkerID + " is tracked at " + mb.transform.position + 
                          " with rotation " + mb.transform.rotation);
            }
        }
    }
}

Attach either script to the ARCamera. Let me know if this suits your needs!

- Kim

Log in or register to post comments