浏览代码

added categorical parameter documentation

/usim-randomization
Steven Leal 4 年前
当前提交
816a04b6
共有 18 个文件被更改,包括 97 次插入281 次删除
  1. 4
      com.unity.perception/Editor/Randomization/CategoricalOptionElement.cs
  2. 2
      com.unity.perception/Editor/Randomization/FloatRangeElement.cs
  3. 2
      com.unity.perception/Editor/Randomization/ParameterConfigurationEditor.cs
  4. 2
      com.unity.perception/Editor/Randomization/ParameterDragBar.cs
  5. 2
      com.unity.perception/Editor/Randomization/ParameterDragManipulator.cs
  6. 4
      com.unity.perception/Editor/Randomization/ParameterElement.cs
  7. 2
      com.unity.perception/Editor/Randomization/RandomSeedField.cs
  8. 2
      com.unity.perception/Editor/Randomization/SamplerElement.cs
  9. 2
      com.unity.perception/Editor/Randomization/ScenarioBaseEditor.cs
  10. 10
      com.unity.perception/Editor/Randomization/StaticData.cs
  11. 53
      com.unity.perception/Runtime/Randomization/Configuration/ParameterConfiguration.cs
  12. 71
      com.unity.perception/Runtime/Randomization/Parameters/CategoricalParameter.cs
  13. 6
      com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs
  14. 6
      com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs
  15. 88
      com.unity.perception/Editor/Randomization/ParameterDrawer.cs
  16. 3
      com.unity.perception/Editor/Randomization/ParameterDrawer.cs.meta
  17. 116
      com.unity.perception/Editor/Randomization/ParameterDrawerElement.cs
  18. 3
      com.unity.perception/Editor/Randomization/ParameterDrawerElement.cs.meta

4
com.unity.perception/Editor/Randomization/CategoricalOptionElement.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class CategoricalOptionElement : VisualElement
class CategoricalOptionElement : VisualElement
public CategoricalOptionElement(
internal CategoricalOptionElement(
SerializedProperty optionsProperty,
SerializedProperty probabilitiesProperty)
{

2
com.unity.perception/Editor/Randomization/FloatRangeElement.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class FloatRangeElement : VisualElement
class FloatRangeElement : VisualElement
{
public FloatRangeElement(SerializedProperty property)
{

2
com.unity.perception/Editor/Randomization/ParameterConfigurationEditor.cs


namespace UnityEngine.Perception.Randomization.Editor
{
[CustomEditor(typeof(ParameterConfiguration))]
public class ParameterConfigurationEditor : UnityEditor.Editor
class ParameterConfigurationEditor : UnityEditor.Editor
{
VisualElement m_Root;
VisualElement m_ParameterContainer;

2
com.unity.perception/Editor/Randomization/ParameterDragBar.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class ParameterDragBar : VisualElement
class ParameterDragBar : VisualElement
{
public ParameterDragBar()
{

2
com.unity.perception/Editor/Randomization/ParameterDragManipulator.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class ParameterDragManipulator : MouseManipulator
class ParameterDragManipulator : MouseManipulator
{
bool m_Active;
float m_Offset;

4
com.unity.perception/Editor/Randomization/ParameterElement.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class ParameterElement : VisualElement
class ParameterElement : VisualElement
{
int m_ParameterIndex;
bool m_Filtered;

? "Select a property"
: TargetPropertyDisplayText(parameter.target);
var options = GatherPropertyOptions(parameter.target.gameObject, parameter.OutputType);
var options = GatherPropertyOptions(parameter.target.gameObject, parameter.sampleType);
foreach (var option in options)
{
m_TargetPropertyMenu.menu.AppendAction(

2
com.unity.perception/Editor/Randomization/RandomSeedField.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class RandomSeedField : IntegerField
class RandomSeedField : IntegerField
{
public RandomSeedField(SerializedProperty property)
{

2
com.unity.perception/Editor/Randomization/SamplerElement.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public class SamplerElement : VisualElement
class SamplerElement : VisualElement
{
Parameter m_Parameter;
ISampler m_Sampler;

2
com.unity.perception/Editor/Randomization/ScenarioBaseEditor.cs


namespace UnityEngine.Perception.Randomization.Editor
{
[CustomEditor(typeof(ScenarioBase), true)]
public class ScenarioBaseEditor : UnityEditor.Editor
class ScenarioBaseEditor : UnityEditor.Editor
{
ScenarioBase m_Scenario;
SerializedObject m_SerializedObject;

10
com.unity.perception/Editor/Randomization/StaticData.cs


namespace UnityEngine.Perception.Randomization.Editor
{
public static class StaticData
static class StaticData
public const string uxmlDir = k_RandomizationDir + "/Uxml";
internal const string uxmlDir = k_RandomizationDir + "/Uxml";
public static readonly string samplerSerializedFieldType;
internal static readonly string samplerSerializedFieldType;
public static Type[] parameterTypes;
public static Type[] samplerTypes;
internal static Type[] parameterTypes;
internal static Type[] samplerTypes;
static StaticData()
{

53
com.unity.perception/Runtime/Randomization/Configuration/ParameterConfiguration.cs


[AddComponentMenu("Perception/Randomization/ParameterConfiguration")]
public class ParameterConfiguration : MonoBehaviour
{
public static HashSet<ParameterConfiguration> configurations = new HashSet<ParameterConfiguration>();
internal static HashSet<ParameterConfiguration> configurations = new HashSet<ParameterConfiguration>();
public Parameter GetParameter(string parameterName)
/// <param name="parameterName">The name of the parameter to lookup</param>
/// <param name="parameterType">The type of parameter to lookup</param>
/// <returns>The parameter if found, null otherwise</returns>
/// <exception cref="ParameterConfigurationException"></exception>
public Parameter GetParameter(string parameterName, Type parameterType)
if (parameter.name == parameterName)
if (parameter.name == parameterName && parameter.GetType() == parameterType)
throw new ParameterConfigurationException(
$"Parameter with name {parameterName} not found");
return null;
/// Find a parameter in this configuration by name
/// Find a parameter in this configuration by name and type
/// <param name="parameterName"></param>
/// <typeparam name="T">The type of parameter to look for</typeparam>
/// <returns>The parameter if found, null otherwise</returns>
public T GetParameter<T>(string parameterName) where T : Parameter
{
foreach (var parameter in parameters)

}
throw new ParameterConfigurationException(
$"Parameter with name {parameterName} and type {typeof(T).Name} not found");
return null;
void NameAndAddParameterToList(Parameter parameter)
{
parameter.name = $"Parameter{parameters.Count}";
parameters.Add(parameter);
}
string PlaceholderParameterName() => $"Parameter{parameters.Count}";
/// <typeparam name="T">The type of parameter to add</typeparam>
/// <returns>The newly added parameter</returns>
NameAndAddParameterToList(parameter);
parameter.name = PlaceholderParameterName();
parameters.Add(parameter);
return parameter;
}

/// <param name="parameterType">The type of parameter to add</param>
/// <returns>The newly added parameter</returns>
/// <exception cref="ParameterConfigurationException"></exception>
NameAndAddParameterToList(parameter);
parameter.name = PlaceholderParameterName();
parameters.Add(parameter);
/// <summary>
/// Calls apply on all parameters with GameObject targets in this configuration
/// </summary>
public void ApplyParameters(int seedOffset, ParameterApplicationFrequency frequency)
internal void ApplyParameters(int seedOffset, ParameterApplicationFrequency frequency)
{
foreach (var parameter in parameters)
if (parameter.target.applicationFrequency == frequency)

/// <summary>
/// Resets sampler states to their baseSeed then offsets using the current scenario iteration
/// </summary>
public void ResetParameterStates(int scenarioIteration)
internal void ResetParameterStates(int scenarioIteration)
/// <summary>
/// Calls Validate() on all parameters within this configuration
/// </summary>
public void ValidateParameters()
internal void ValidateParameters()
{
var parameterNames = new HashSet<string>();
foreach (var parameter in parameters)

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


namespace UnityEngine.Perception.Randomization.Parameters
{
/// <summary>
/// Generates samples by choosing one option from a weighted list of choices.
/// Generates samples by choosing one option from a list of choices
/// <typeparam name="T">The sample type of the categorical parameter</typeparam>
[Serializable]
public abstract class CategoricalParameter<T> : CategoricalParameterBase
{

[SerializeField] List<T> m_Options = new List<T>();
[SerializeField] List<T> m_Categories = new List<T>();
/// <summary>
/// Returns a list containing the samplers attached to this parameter
/// </summary>
public sealed override Type OutputType => typeof(T);
/// <summary>
/// The sample type generated by this parameter
/// </summary>
public sealed override Type sampleType => typeof(T);
public T GetOption(int index) => m_Options[index];
/// <summary>
/// Returns the category stored at the specified index
/// </summary>
/// <param name="index">The index of the category to lookup</param>
/// <returns>The category stored at the specified index</returns>
public T GetCategory(int index) => m_Categories[index];
/// <summary>
/// Returns the probability value stored at the specified index
/// </summary>
/// <param name="index">The index of the probability value to lookup</param>
/// <returns>The probability value stored at the specified index</returns>
public float GetProbability(int index) => probabilities[index];
internal CategoricalParameter() { }

/// </summary>
/// <param name="categoricalOptions">List of categorical options</param>
/// <param name="categoricalOptions">List of categories</param>
/// <exception cref="ArgumentException"></exception>
if (options.Count == 0)
if (categories.Count == 0)
throw new ArgumentException("List of options is empty");
uniform = true;
foreach (var option in categoricalOptions)

/// <summary>
/// Create a new categorical parameter from a list of categories and their associated probabilities
/// Creates a new categorical parameter from a list of categories and their associated probabilities
/// <param name="categoricalOptions">List of options and their associated probabilities</param>
/// <param name="categoricalOptions">List of categories and their associated probabilities</param>
/// <exception cref="ArgumentException"></exception>
if (options.Count == 0)
if (categories.Count == 0)
throw new ArgumentException("List of options is empty");
foreach (var (category, probability) in categoricalOptions)
AddOption(category, probability);

internal override void AddOption()
{
m_Options.Add(default);
m_Categories.Add(default);
m_Options.Add(option);
m_Categories.Add(option);
m_Options.RemoveAt(index);
m_Categories.RemoveAt(index);
m_Options.Clear();
m_Categories.Clear();
public IReadOnlyList<(T, float)> options
/// <summary>
/// Returns a list of the potential categories this parameter can generate
/// </summary>
public IReadOnlyList<(T, float)> categories
var catOptions = new List<(T, float)>(m_Options.Count);
var catOptions = new List<(T, float)>(m_Categories.Count);
catOptions.Add((m_Options[i], probabilities[i]));
catOptions.Add((m_Categories[i], probabilities[i]));
public override void Validate()
/// <summary>
/// Validates the categorical probabilities assigned to this parameter
/// </summary>
/// <exception cref="ParameterValidationException"></exception>
internal override void Validate()
if (probabilities.Count != m_Options.Count)
if (probabilities.Count != m_Categories.Count)
throw new ParameterValidationException(
"Number of options must be equal to the number of probabilities");
NormalizeProbabilities();

}
/// <summary>
/// Generates one parameter sample
/// Generates a sample
/// <returns>The generated sample</returns>
? m_Options[(int)(randomValue * m_Options.Count)]
: m_Options[BinarySearch(randomValue)];
? m_Categories[(int)(randomValue * m_Categories.Count)]
: m_Categories[BinarySearch(randomValue)];
public sealed override void ApplyToTarget(int seedOffset)
internal sealed override void ApplyToTarget(int seedOffset)
{
if (!hasTarget)
return;

6
com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs


[Serializable]
public abstract class NumericParameter<T> : Parameter where T : struct
{
public sealed override Type OutputType => typeof(T);
public sealed override Type sampleType => typeof(T);
/// <summary>
/// Generates one parameter sample

/// <param name="jobHandle">The JobHandle returned from scheduling the sampling job</param>
public abstract NativeArray<T> Samples(int sampleCount, out JobHandle jobHandle);
public sealed override void ApplyToTarget(int seedOffset)
internal sealed override void ApplyToTarget(int seedOffset)
{
if (!hasTarget)
return;

public override void Validate()
internal override void Validate()
{
base.Validate();
foreach (var sampler in samplers)

6
com.unity.perception/Runtime/Randomization/Parameters/Parameter.cs


/// <summary>
/// The sample type generated by this parameter
/// </summary>
public abstract Type OutputType { get; }
public abstract Type sampleType { get; }
public abstract void ApplyToTarget(int seedOffset);
internal abstract void ApplyToTarget(int seedOffset);
public virtual void Validate()
internal virtual void Validate()
{
if (hasTarget)
{

88
com.unity.perception/Editor/Randomization/ParameterDrawer.cs


/*
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Parameters.Attributes;
using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
[CustomPropertyDrawer(typeof(Parameter), true)]
public class ParameterDrawer : PropertyDrawer
{
List<Parameter> m_Parameters;
string[] m_Options;
int m_SelectedOptionIndex;
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
return new ParameterDrawerElement(property, fieldInfo);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GatherParameterOptions();
GetParameterOptionIndex(property);
EditorGUI.BeginProperty(position, label, property);
var originalOption = m_SelectedOptionIndex;
m_SelectedOptionIndex = EditorGUI.Popup(position, fieldInfo.Name, m_SelectedOptionIndex, m_Options);
if (originalOption != m_SelectedOptionIndex)
{
property.objectReferenceValue = m_SelectedOptionIndex == 0
? null
: m_Parameters[m_SelectedOptionIndex - 1];
property.serializedObject.ApplyModifiedProperties();
}
EditorGUI.EndProperty();
}
void GatherParameterOptions()
{
var parameterType = fieldInfo.FieldType;
m_Parameters = new List<Parameter>();
if (parameterType == typeof(Parameter))
m_Parameters = Resources.FindObjectsOfTypeAll<Parameter>().ToList();
else
{
var genericParameters = Resources.FindObjectsOfTypeAll<Parameter>();
foreach (var parameter in genericParameters)
{
if (parameter.GetType() == parameterType)
m_Parameters.Add(parameter);
}
}
m_Parameters.Sort((p1, p2) => p1.parameterName.CompareTo(p2.parameterName));
m_Options = new string[m_Parameters.Count + 1];
m_Options[0] = "None";
for (var i = 1; i <= m_Parameters.Count; i++)
{
var parameter = m_Parameters[i - 1];
var metadata = ParameterMetaData.GetMetaData(parameter.GetType());
m_Options[i] = $"{parameter.parameterName} ({metadata.typeDisplayName})";
}
}
void GetParameterOptionIndex(SerializedProperty property)
{
var selectedParameter = property.objectReferenceValue;
if (selectedParameter != null)
{
for (var i = 0; i < m_Parameters.Count; i++)
if (m_Parameters[i].GetInstanceID() == selectedParameter.GetInstanceID())
{
m_SelectedOptionIndex = i + 1;
break;
}
}
else
m_SelectedOptionIndex = 0;
}
}
}
*/

3
com.unity.perception/Editor/Randomization/ParameterDrawer.cs.meta


fileFormatVersion: 2
guid: 4723714765b74b33850e58baa73bf9c2
timeCreated: 1595428457

116
com.unity.perception/Editor/Randomization/ParameterDrawerElement.cs


/*
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Parameters.Attributes;
using UnityEngine.UIElements;
namespace UnityEngine.Perception.Randomization.Editor
{
public class ParameterDrawerElement : BindableElement
{
SerializedProperty m_Property;
FieldInfo m_FieldInfo;
ToolbarMenu m_ParameterMenu;
public ParameterDrawerElement(SerializedProperty property, FieldInfo fieldInfo)
{
m_Property = property;
m_FieldInfo = fieldInfo;
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
$"{StaticData.uxmlDir}/ParameterDrawerElement.uxml");
template.CloneTree(this);
var propertyLabel = this.Q<Label>();
propertyLabel.text = property.displayName;
m_ParameterMenu = this.Q<ToolbarMenu>();
binding = new ParameterDrawerBinding(this);
}
string GetSelectedOptionText()
{
var parameter = m_Property.objectReferenceValue as Parameter;
if (parameter == null)
return "None";
return DisplayName(parameter);
}
static string DisplayName(Parameter parameter)
{
return $"{parameter.parameterName} ({parameter.MetaData.typeDisplayName})";
}
void UpdateMenuOptions()
{
m_ParameterMenu.menu.MenuItems().Clear();
var parameters = GatherParameterOptions();
var options = GetStringOptions(parameters);
for (var i = 0; i < options.Length; i++)
{
var index = i;
var option = options[i];
m_ParameterMenu.menu.AppendAction(option, action =>
{
m_Property.objectReferenceValue = option == "None" ? null : parameters[index - 1];
m_Property.serializedObject.ApplyModifiedProperties();
m_ParameterMenu.text = GetSelectedOptionText();
});
}
}
Parameter[] GatherParameterOptions()
{
var parameterType = m_FieldInfo.FieldType;
var parameters = new List<Parameter>();
if (parameterType == typeof(Parameter))
parameters = Resources.FindObjectsOfTypeAll<Parameter>().ToList();
else
{
var genericParameters = Resources.FindObjectsOfTypeAll<Parameter>();
foreach (var parameter in genericParameters)
{
if (parameter.GetType() == parameterType)
parameters.Add(parameter);
}
}
parameters.Sort((p1, p2) => p1.parameterName.CompareTo(p2.parameterName));
return parameters.ToArray();
}
string[] GetStringOptions(Parameter[] parameters)
{
var options = new string[parameters.Length + 1];
options[0] = "None";
for (var i = 1; i <= parameters.Length; i++)
{
var parameter = parameters[i - 1];
var metadata = ParameterMetaData.GetMetaData(parameter.GetType());
options[i] = $"{parameter.parameterName} ({metadata.typeDisplayName})";
}
return options;
}
class ParameterDrawerBinding : IBinding
{
ParameterDrawerElement m_Element;
public ParameterDrawerBinding(ParameterDrawerElement element) => m_Element = element;
public void PreUpdate() { }
public void Update()
{
m_Element.UpdateMenuOptions();
m_Element.m_ParameterMenu.text = m_Element.GetSelectedOptionText();
}
public void Release() { }
}
}
}
*/

3
com.unity.perception/Editor/Randomization/ParameterDrawerElement.cs.meta


fileFormatVersion: 2
guid: 3fd3617d27b14531a75a7a0ef1e45452
timeCreated: 1596417699
正在加载...
取消
保存