using System; using System.Collections.Generic; using UnityEngine.Serialization; namespace UnityEngine.Perception.GroundTruth { /// /// Required interface for entries in a . Exposes the string label which is the "key" /// for the entry. /// public interface ILabelEntry { /// /// The label to use as the key for the entry. This label will be matched with the labels in the GameObject's /// component. /// string label { get; } } /// /// A definition for how a should be resolved to a single label and id for ground truth generation. /// /// The entry type. Must derive from public class LabelConfig : ScriptableObject where T : ILabelEntry { /// /// The name of the serialized field for label entries. /// public const string labelEntriesFieldName = nameof(m_LabelEntries); [FormerlySerializedAs("LabelEntries")] [FormerlySerializedAs("LabelingConfigurations")] [SerializeField] List m_LabelEntries = new List(); /// /// A sequence of which defines the labels relevant for this configuration and their values. /// public IReadOnlyList labelEntries => m_LabelEntries; /// /// Attempts to find the matching index in for the given . /// /// /// The matching index is the first class name in the given Labeling which matches an entry in . /// /// The to match /// When this method returns, contains the matching , or default if no match was found. /// Returns true if a match was found. False if not. public bool TryGetMatchingConfigurationEntry(Labeling labeling, out T labelEntry) { return TryGetMatchingConfigurationEntry(labeling, out labelEntry, out int _); } /// /// Initialize the list of LabelEntries on this LabelingConfiguration. Should only be called immediately after instantiation. /// /// The LabelEntry values to associate with this LabelingConfiguration /// Thrown once the LabelConfig has been used at runtime. /// The specific timing of this depends on the LabelConfig implementation. public void Init(IEnumerable newLabelEntries) { m_LabelEntries = new List(newLabelEntries); OnInit(); } /// /// Called when the labelEntries list is assigned using /// protected virtual void OnInit() { } /// /// Attempts to find the matching index in for the given . /// /// /// The matching index is the first class name in the given Labeling which matches an entry in . /// /// The to match /// When this method returns, contains the matching , or default if no match was found. /// When this method returns, contains the index of the matching , or -1 if no match was found. /// Returns true if a match was found. False if not. public bool TryGetMatchingConfigurationEntry(Labeling labeling, out T labelEntry, out int labelEntryIndex) { foreach (var labelingClass in labeling.labels) { for (var i = 0; i < m_LabelEntries.Count; i++) { var entry = m_LabelEntries[i]; if (string.Equals(entry.label, labelingClass)) { labelEntry = m_LabelEntries[i]; labelEntryIndex = i; return true; } } } labelEntryIndex = -1; labelEntry = default; return false; } } }