Nullable <TrajectoryHeuristicDebug> FindTrajectoryHeuristic(DebugMemory debugMemory)
        {
            if (!trajectory.IsValid)
            {
                return(null);
            }

            int heuristicTypeCode = BurstRuntime.GetHashCode32 <TrajectoryHeuristicDebug>();

            for (DebugReference reference = debugMemory.FirstOrDefault; reference.IsValid; reference = debugMemory.Next(reference))
            {
                if (reference.identifier.typeHashCode != heuristicTypeCode)
                {
                    continue;
                }

                TrajectoryHeuristicDebug heuristicDebug = debugMemory.ReadObject <TrajectoryHeuristicDebug>(reference);
                if (heuristicDebug.closestMatch.Equals(closestMatch))
                {
                    return(heuristicDebug);
                }
            }

            return(null);
        }
        public bool MatchPoseAndTrajectory(PoseSet candidates,
                                           SamplingTime samplingTime,
                                           Trajectory trajectory,
                                           MatchOptions options         = MatchOptions.None,
                                           float trajectoryWeight       = 0.6f,
                                           float minTrajectoryDeviation = 0.03f,
                                           float maxTotalDeviation      = -1.0f)
        {
            if ((options & MatchOptions.DontMatchIfCandidateIsPlaying) > 0 && IsTimeInSequences(samplingTime, candidates.sequences))
            {
                if ((options & MatchOptions.LoopSegment) > 0)
                {
                    LoopSegmentIfEndReached(samplingTime);
                }

                return(true);
            }


            if (!trajectory.debugIdentifier.IsValid)
            {
                DebugPushGroup();
                DebugWriteUnblittableObject(ref trajectory);
            }

            DebugWriteUnblittableObject(ref candidates);
            DebugWriteBlittableObject(ref samplingTime);


            SamplingTime closestMatch = FindClosestPoseAndTrajectoryMatch(candidates, samplingTime, trajectory, trajectoryWeight, maxTotalDeviation);

            if (closestMatch.IsValid)
            {
                bool validMatch = true;
                if (minTrajectoryDeviation >= 0.0f)
                {
                    float trajectoryDeviation = RelativeDeviation(closestMatch.timeIndex, trajectory);
                    validMatch = trajectoryDeviation >= minTrajectoryDeviation;

                    SamplingTime outputTime = closestMatch;
                    outputTime.debugIdentifier = DebugIdentifier.Invalid;
                    DebugWriteBlittableObject(ref outputTime, true);

                    TrajectoryHeuristicDebug heuristicDebug = new TrajectoryHeuristicDebug()
                    {
                        desiredTrajectory      = trajectory.debugIdentifier,
                        candidate              = samplingTime.debugIdentifier,
                        closestMatch           = closestMatch.debugIdentifier,
                        outputTime             = outputTime.debugIdentifier,
                        trajectoryDeviation    = trajectoryDeviation,
                        minTrajectoryDeviation = minTrajectoryDeviation
                    };
                    DebugWriteBlittableObject(ref heuristicDebug);

                    closestMatch = outputTime;
                }

                if (validMatch)
                {
                    PlayAtTime(closestMatch);
                    return(true);
                }
            }

            if ((options & MatchOptions.LoopSegment) > 0)
            {
                LoopSegmentIfEndReached(samplingTime);
            }

            return(false);
        }