using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Collections.LowLevel.Unsafe;
namespace UnityEngine.Perception.GroundTruth
{
///
/// Record that maps a pose to a timestamp
///
[Serializable]
public class PoseTimestampRecord
{
///
/// The percentage within the clip that the pose starts, a value from 0 (beginning) to 1 (end)
///
[Tooltip("The percentage within the clip that the pose starts, a value from 0 (beginning) to 1 (end)")]
public float startOffsetPercent;
///
/// The label to use for any captures inside of this time period
///
public string poseLabel;
}
///
/// The animation pose config is a configuration file that maps a time range in an animation clip to a ground truth
/// pose. The timestamp record is defined by a pose label and a start time. The timestamp records are order dependent.
///
[CreateAssetMenu(fileName = "AnimationPoseConfig", menuName = "Perception/Animation Pose Config")]
public class AnimationPoseConfig : ScriptableObject
{
///
/// The animation clip used for all of the timestamps
///
public AnimationClip animationClip;
///
/// The list of timestamps, order dependent
///
public List timestamps;
SortedList sortedTimestamps;
void OnEnable()
{
sortedTimestamps = new SortedList(timestamps.Count);
foreach (var ts in timestamps)
{
sortedTimestamps.Add(ts.startOffsetPercent, ts.poseLabel);
}
}
const string k_Unset = "unset";
///
/// Retrieves the pose for the clip at the current time.
///
/// The time in question
/// The pose for the passed in time
public string GetPoseAtTime(float time)
{
if (time < 0 || time > 1) return k_Unset;
if (timestamps == null || !timestamps.Any()) return k_Unset;
// Special case code if there is only 1 timestamp in the config
if (sortedTimestamps.Keys.Count == 1)
{
return time > sortedTimestamps.Keys[0] ? sortedTimestamps.Values[0] : k_Unset;
}
for (var i = 0; i < sortedTimestamps.Keys.Count - 1; i++)
{
if (time >= sortedTimestamps.Keys[i] && time <= sortedTimestamps.Keys[i + 1]) return sortedTimestamps.Values[i];
}
return time < sortedTimestamps.Keys.Last() ? k_Unset : sortedTimestamps.Values.Last();
}
}
}