浏览代码

simplifed tag inheritance

/main
Steven Leal 4 年前
当前提交
619701a5
共有 4 个文件被更改,包括 41 次插入57 次删除
  1. 2
      com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs
  2. 1
      com.unity.perception/Runtime/Randomization/Parameters/ParameterValidationException.cs
  3. 83
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs
  4. 12
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs

2
com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs


public override void Validate()
{
base.Validate();
if (m_Categories.Count == 0)
throw new ParameterValidationException("No options added to categorical parameter");
if (!uniform)
{
if (probabilities.Count != m_Categories.Count)

1
com.unity.perception/Runtime/Randomization/Parameters/ParameterValidationException.cs


class ParameterValidationException : Exception
{
public ParameterValidationException(string msg) : base(msg) {}
public ParameterValidationException(string msg, Exception innerException) : base(msg, innerException) {}
}
}

83
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerTagManager.cs


/// </summary>
public class RandomizerTagManager
{
Dictionary<Type, List<Type>> m_TypeCache = new Dictionary<Type, List<Type>>();
Dictionary<Type, HashSet<Type>> m_TypeCache = new Dictionary<Type, HashSet<Type>>();
Dictionary<GameObject, HashSet<Type>> m_ObjectTags = new Dictionary<GameObject, HashSet<Type>>();
/// <summary>
/// Enumerates all GameObjects in the scene that have a RandomizerTag of the given type

/// <returns>GameObjects with the given RandomizerTag</returns>
public IEnumerable<GameObject> Query<T>(bool returnSubclasses = false) where T : RandomizerTag
{
var type = typeof(T);
if (!m_TagMap.ContainsKey(type))
var queriedTagType = typeof(T);
if (!m_TagMap.ContainsKey(queriedTagType))
foreach (var taggedObject in m_TagMap[type])
if (returnSubclasses)
if (returnSubclasses)
{
yield return taggedObject.gameObject;
}
else
foreach (var tagType in m_TypeCache[queriedTagType])
if (m_ObjectTags.ContainsKey(taggedObject) && m_ObjectTags[taggedObject].Contains(type))
yield return taggedObject.gameObject;
foreach (var obj in m_TagMap[tagType])
yield return obj;
else
{
foreach (var taggedObject in m_TagMap[queriedTagType])
yield return taggedObject.gameObject;
}
// Add tag to the game object to tag map
if (!m_ObjectTags.ContainsKey(obj))
m_ObjectTags[obj] = new HashSet<Type>();
m_ObjectTags[obj].Add(tagType);
{
var inheritedTags = new List<Type>();
AddTagTypeAndBaseTagTypesToCache(tagType);
m_TagMap[tagType].Add(obj);
}
while (tagType != null && tagType != typeof(RandomizerTag))
{
inheritedTags.Add(tagType);
tagType = tagType.BaseType;
}
m_TypeCache[tagType] = inheritedTags;
}
var tags = m_TypeCache[tagType];
foreach (var tag in tags)
void AddTagTypeAndBaseTagTypesToCache(Type tagType)
{
var recursiveTagType = tagType;
var inheritedTags = new HashSet<Type>();
while (recursiveTagType != null && recursiveTagType != typeof(RandomizerTag))
if (!m_TagMap.ContainsKey(tag))
m_TagMap[tag] = new HashSet<GameObject>();
m_TagMap[tag].Add(obj);
inheritedTags.Add(recursiveTagType);
if (!m_TagMap.ContainsKey(recursiveTagType))
m_TagMap[recursiveTagType] = new HashSet<GameObject>();
recursiveTagType = recursiveTagType.BaseType;
m_TypeCache[tagType] = inheritedTags;
// Grab all of the tags from the inheritance tree, and remove
// the passed in object from all of them
if (m_TypeCache.ContainsKey(tagType))
{
var tags = m_TypeCache[tagType];
foreach (var tag in tags.Where(tag => m_TagMap.ContainsKey(tag)))
{
m_TagMap[tag].Remove(obj);
}
}
// Remove entry from the object to tag map
if (m_ObjectTags.ContainsKey(obj))
{
m_ObjectTags[obj].Remove(tagType);
if (!m_ObjectTags[obj].Any())
m_ObjectTags.Remove(obj);
}
if (m_TagMap.ContainsKey(tagType) && m_TagMap[tagType].Contains(obj))
m_TagMap[tagType].Remove(obj);
}
}
}

12
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


using System.IO;
using Unity.Simulation;
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
using UnityEngine.Experimental.Perception.Randomization.Randomizers;
using UnityEngine.Experimental.Perception.Randomization.Samplers;
using UnityEngine.Perception.GroundTruth;

{
foreach (var randomizer in m_Randomizers)
foreach (var parameter in randomizer.parameters)
parameter.Validate();
{
try
{
parameter.Validate();
}
catch (ParameterValidationException exception)
{
Debug.LogException(exception, this);
}
}
}
}
}
正在加载...
取消
保存