Ejemplo n.º 1
0
        public int ChoosePrimaryUser(long timestamp, int oldPrimaryUser, QueryPrimaryUserTrackingIdCallback callback)
        {
            // Stale hand pointers could confuse clients if they see them as part of callback parameters
            this.RemoveStaleHandPointers();

            this.kinectPrimaryUserTracker.Update(this.handPointers.Values, timestamp, callback);
            int newPrimaryUser = this.kinectPrimaryUserTracker.PrimaryUserTrackingId;

            if (oldPrimaryUser != newPrimaryUser)
            {
                // if primary user id has changed, update tracked pointers
                foreach (var handPointer in this.handPointers.Values)
                {
                    // If tracking id is valid and matches the new primary user,
                    // this hand pointer corresponds to the new primary user.
                    bool isPrimaryUser = (handPointer.TrackingId == newPrimaryUser) && (newPrimaryUser != KinectPrimaryUserTracker.InvalidUserTrackingId);

                    if (handPointer.IsPrimaryUser != isPrimaryUser)
                    {
                        bool oldIsPrimaryHandOfPrimaryUser = handPointer.IsPrimaryHandOfPrimaryUser;
                        handPointer.IsPrimaryUser = isPrimaryUser;

                        this.HandleHandPointerChanges(handPointer, false, false, oldIsPrimaryHandOfPrimaryUser != handPointer.IsPrimaryHandOfPrimaryUser, false);
                    }
                }
            }

            return(newPrimaryUser);
        }
        /// <summary>
        /// Iterate through all candidate users to determine which user is primary.
        /// The first user with a primary hand pointer (which means in the active region) is
        /// promoted to be primary user. 
        /// The primary user remains so until they no longer have a primary hand pointer.
        /// </summary>
        /// <param name="candidateHandPointers">
        /// Current list of available hand pointers derived from the interaction stream data.
        /// If this is null the function acts as if there are no tracked users and will end up
        /// with no primary user.
        /// </param>
        /// <param name="timestamp">
        /// Time of update. Corresponds to InteractionStream and KinectSensor event timestamps.
        /// </param>
        /// <param name="queryDelegate">
        /// Delegate used to ask client whether the proposed primary user tracking Id
        /// should be chosen or if an alternative tracking Id should be chosen instead.
        /// </param>
        internal void Update(IEnumerable<HandPointer> candidateHandPointers, long timestamp, QueryPrimaryUserTrackingIdCallback queryDelegate)
        {
            int firstPrimaryUserCandidate = InvalidUserTrackingId;
            int primaryUserTrackingId = InvalidUserTrackingId;
            bool currentPrimaryUserStillPrimary = false;

            if (candidateHandPointers != null)
            {
                var trackingIdsAvailable = new HashSet<int>();

                foreach (var handPointer in candidateHandPointers)
                {
                    if (handPointer.TrackingId == InvalidUserTrackingId)
                    {
                        continue;
                    }

                    trackingIdsAvailable.Add(handPointer.TrackingId);
                    
                    if (handPointer.IsPrimaryHandOfUser)
                    {
                        if (this.PrimaryUserTrackingId == handPointer.TrackingId)
                        {
                            // If the current primary user still has an active hand, we should continue to consider them the primary user.
                            currentPrimaryUserStillPrimary = true;
                        }
                        else if (InvalidUserTrackingId == firstPrimaryUserCandidate)
                        {
                            // Else if this is the first user with an active hand, they are the alternative candidate for primary user.
                            firstPrimaryUserCandidate = handPointer.TrackingId;
                        }
                    }
                }

                // Give client a chance to pick the primary user
                primaryUserTrackingId = currentPrimaryUserStillPrimary ? this.PrimaryUserTrackingId : firstPrimaryUserCandidate;
                if (queryDelegate != null)
                {
                    primaryUserTrackingId = queryDelegate(primaryUserTrackingId, candidateHandPointers, timestamp);
                }

                // If client specified an invalid tracking Id, silently fall back to the invalid tracking Id.
                if (!trackingIdsAvailable.Contains(primaryUserTrackingId))
                {
                    primaryUserTrackingId = InvalidUserTrackingId;
                }
            }

            this.PrimaryUserTrackingId = primaryUserTrackingId;
        }
        /// <summary>
        /// Iterate through all candidate users to determine which user is primary.
        /// The first user with a primary hand pointer (which means in the active region) is
        /// promoted to be primary user.
        /// The primary user remains so until they no longer have a primary hand pointer.
        /// </summary>
        /// <param name="candidateHandPointers">
        /// Current list of available hand pointers derived from the interaction stream data.
        /// If this is null the function acts as if there are no tracked users and will end up
        /// with no primary user.
        /// </param>
        /// <param name="timestamp">
        /// Time of update. Corresponds to InteractionStream and KinectSensor event timestamps.
        /// </param>
        /// <param name="queryDelegate">
        /// Delegate used to ask client whether the proposed primary user tracking Id
        /// should be chosen or if an alternative tracking Id should be chosen instead.
        /// </param>
        internal void Update(IEnumerable <HandPointer> candidateHandPointers, long timestamp, QueryPrimaryUserTrackingIdCallback queryDelegate)
        {
            int  firstPrimaryUserCandidate      = InvalidUserTrackingId;
            int  primaryUserTrackingId          = InvalidUserTrackingId;
            bool currentPrimaryUserStillPrimary = false;

            if (candidateHandPointers != null)
            {
                var trackingIdsAvailable = new HashSet <int>();

                foreach (var handPointer in candidateHandPointers)
                {
                    if (handPointer.TrackingId == InvalidUserTrackingId)
                    {
                        continue;
                    }

                    trackingIdsAvailable.Add(handPointer.TrackingId);

                    if (handPointer.IsPrimaryHandOfUser)
                    {
                        if (this.PrimaryUserTrackingId == handPointer.TrackingId)
                        {
                            // If the current primary user still has an active hand, we should continue to consider them the primary user.
                            currentPrimaryUserStillPrimary = true;
                        }
                        else if (InvalidUserTrackingId == firstPrimaryUserCandidate)
                        {
                            // Else if this is the first user with an active hand, they are the alternative candidate for primary user.
                            firstPrimaryUserCandidate = handPointer.TrackingId;
                        }
                    }
                }

                // Give client a chance to pick the primary user
                primaryUserTrackingId = currentPrimaryUserStillPrimary ? this.PrimaryUserTrackingId : firstPrimaryUserCandidate;
                if (queryDelegate != null)
                {
                    primaryUserTrackingId = queryDelegate(primaryUserTrackingId, candidateHandPointers, timestamp);
                }

                // If client specified an invalid tracking Id, silently fall back to the invalid tracking Id.
                if (!trackingIdsAvailable.Contains(primaryUserTrackingId))
                {
                    primaryUserTrackingId = InvalidUserTrackingId;
                }
            }

            this.PrimaryUserTrackingId = primaryUserTrackingId;
        }
Ejemplo n.º 4
0
        public int ChoosePrimaryUser(long timestamp, int oldPrimaryUser, QueryPrimaryUserTrackingIdCallback callback)
        {
            // Stale hand pointers could confuse clients if they see them as part of callback parameters
            this.RemoveStaleHandPointers();

            this.kinectPrimaryUserTracker.Update(this.handPointers.Values, timestamp, callback);
            int newPrimaryUser = this.kinectPrimaryUserTracker.PrimaryUserTrackingId;

            if (oldPrimaryUser != newPrimaryUser)
            {
                // if primary user id has changed, update tracked pointers
                foreach (var handPointer in this.handPointers.Values)
                {
                    // If tracking id is valid and matches the new primary user,
                    // this hand pointer corresponds to the new primary user.
                    bool isPrimaryUser = (handPointer.TrackingId == newPrimaryUser) && (newPrimaryUser != KinectPrimaryUserTracker.InvalidUserTrackingId);

                    if (handPointer.IsPrimaryUser != isPrimaryUser)
                    {
                        bool oldIsPrimaryHandOfPrimaryUser = handPointer.IsPrimaryHandOfPrimaryUser;
                        handPointer.IsPrimaryUser = isPrimaryUser;

                        this.HandleHandPointerChanges(handPointer, false, false, oldIsPrimaryHandOfPrimaryUser != handPointer.IsPrimaryHandOfPrimaryUser, false);
                    }
                }
            }

            return newPrimaryUser;
        }