浏览代码

removed range as required ISampler interface property

/main
Steven Leal 4 年前
当前提交
e168b600
共有 15 个文件被更改,包括 136 次插入102 次删除
  1. 23
      com.unity.perception/Editor/Randomization/VisualElements/Sampler/SamplerElement.cs
  2. 2
      com.unity.perception/Runtime/GroundTruth/Labelers/Visualization/Materials/OutlineMaterial.mat
  3. 2
      com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs
  4. 10
      com.unity.perception/Runtime/Randomization/Samplers/ISampler.cs
  5. 77
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/AnimationCurveSampler.cs
  6. 14
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs
  7. 11
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs
  8. 11
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs
  9. 5
      com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs
  10. 5
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/AnimationCurveSamplerTests.cs
  11. 21
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/NormalSamplerTests.cs
  12. 23
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs
  13. 21
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/UniformSamplerTests.cs
  14. 10
      com.unity.perception/Runtime/Randomization/Samplers/SamplerValidationException.cs
  15. 3
      com.unity.perception/Runtime/Randomization/Samplers/SamplerValidationException.cs.meta

23
com.unity.perception/Editor/Randomization/VisualElements/Sampler/SamplerElement.cs


void CreateSampler(Type samplerType)
{
var newSampler = (ISampler)Activator.CreateInstance(samplerType);
CopyFloatRangeToNewSampler(newSampler);
m_Sampler = newSampler;
m_Property.managedReferenceValue = newSampler;
m_Property.serializedObject.ApplyModifiedProperties();
}
if (m_RangeProperty != null)
newSampler.range = new FloatRange(
m_RangeProperty.FindPropertyRelative("minimum").floatValue,
m_RangeProperty.FindPropertyRelative("maximum").floatValue);
void CopyFloatRangeToNewSampler(ISampler newSampler)
{
if (m_RangeProperty == null)
return;
var rangeField = newSampler.GetType().GetField(m_RangeProperty.name);
if (rangeField == null)
return;
m_Sampler = newSampler;
m_Property.managedReferenceValue = newSampler;
m_Property.serializedObject.ApplyModifiedProperties();
var range = new FloatRange(
m_RangeProperty.FindPropertyRelative("minimum").floatValue,
m_RangeProperty.FindPropertyRelative("maximum").floatValue);
rangeField.SetValue(newSampler, range);
}
void CreatePropertyFields()

2
com.unity.perception/Runtime/GroundTruth/Labelers/Visualization/Materials/OutlineMaterial.mat


m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 2
version: 4
--- !u!21 &2100000
Material:
serializedVersion: 6

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


{
base.Validate();
foreach (var sampler in samplers)
sampler.range.Validate();
sampler.Validate();
}
}
}

10
com.unity.perception/Runtime/Randomization/Samplers/ISampler.cs


public interface ISampler
{
/// <summary>
/// A range bounding the values generated by this sampler
/// </summary>
FloatRange range { get; set; }
/// <summary>
/// <summary>
/// Validates that the sampler is configured properly
/// </summary>
void Validate();
}
}

77
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/AnimationCurveSampler.cs


namespace UnityEngine.Experimental.Perception.Randomization.Samplers
{
/// <summary>
/// Returns random values according to a range and probability distribution denoted by a user provided AnimationCurve. The X axis of the AnimationCurve corresponds to the values this sampler will pick from, and the Y axis corresponds to the relative probability of the values. The relative probabilities (Y axis) do not need to max out at 1, as only the shape of the curve matters. The Y values cannot however be negative.
/// Returns random values according to a range and probability distribution denoted by a user provided AnimationCurve.
/// The X axis of the AnimationCurve corresponds to the values this sampler will pick from,
/// and the Y axis corresponds to the relative probability of the values.
/// The relative probabilities (Y axis) do not need to max out at 1, as only the shape of the curve matters.
/// The Y values cannot however be negative.
/// The range field is not used by the AnimationCurve sampler.
/// </summary>
[field: NonSerialized]
public FloatRange range { get; set; }
/// <summary>
public AnimationCurve distributionCurve;
public AnimationCurve distributionCurve = new AnimationCurve(
new Keyframe(0, 0), new Keyframe(0.5f, 1), new Keyframe(1, 0));
/// Number of samples used for integrating over the provided AnimationCurve. The larger the number of samples, the more accurate the resulting probability distribution will be.
/// Number of samples used for integrating over the provided AnimationCurve.
/// The larger the number of samples, the more accurate the resulting probability distribution will be.
public int numOfSamplesForIntegration = 1000;
public int numOfSamplesForIntegration = 500;
bool m_CurveValid;
bool m_Initialized;
/// Constructs an Animation Curve Sampler
/// Generates one sample
public AnimationCurveSampler()
/// <returns>The generated sample</returns>
public float Sample()
distributionCurve = new AnimationCurve();
distributionCurve.AddKey(0, 0);
distributionCurve.AddKey(0.5f, 1);
distributionCurve.AddKey(1, 0);
Initialize();
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
return SamplerUtility.AnimationCurveSample(
m_IntegratedCurve, rng.NextFloat(), m_Interval, m_StartTime, m_EndTime);
/// Generates one sample
/// Validates that the sampler is configured properly
/// <returns>The generated sample</returns>
public float Sample()
/// <exception cref="SamplerValidationException"></exception>
public void Validate()
Initialize();
if (!m_CurveValid)
{
return 0;
}
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
return SamplerUtility.AnimationCurveSample(m_IntegratedCurve, rng.NextFloat(), m_Interval, m_StartTime, m_EndTime);
if (distributionCurve.length == 0)
throw new SamplerValidationException("The distribution curve provided is empty");
m_CurveValid = false;
if (distributionCurve.length == 0)
{
Debug.LogError("The distribution curve provided for an Animation Curve sampler is empty.");
if (m_Initialized)
}
m_CurveValid = true;
if (m_IntegratedCurve == null)
{
m_IntegratedCurve = new float[numOfSamplesForIntegration];
SamplerUtility.IntegrateCurve(m_IntegratedCurve, distributionCurve);
m_StartTime = distributionCurve.keys[0].time;
m_EndTime = distributionCurve.keys[distributionCurve.length - 1].time;
m_Interval = (m_EndTime - m_StartTime) / (numOfSamplesForIntegration - 1);
}
Validate();
m_IntegratedCurve = new float[numOfSamplesForIntegration];
SamplerUtility.IntegrateCurve(m_IntegratedCurve, distributionCurve);
m_StartTime = distributionCurve.keys[0].time;
m_EndTime = distributionCurve.keys[distributionCurve.length - 1].time;
m_Interval = (m_EndTime - m_StartTime) / (numOfSamplesForIntegration - 1);
m_Initialized = true;
}
}
}

14
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs


public float value;
/// <summary>
/// A range bounding the values generated by this sampler
/// </summary>
public FloatRange range
{
get => new FloatRange(value, value);
set { }
}
/// <summary>
/// Constructs a ConstantSampler
/// </summary>
public ConstantSampler()

{
return value;
}
/// <summary>
/// Validates that the sampler is configured properly
/// </summary>
public void Validate() {}
}
}

11
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs


/// <summary>
/// A range bounding the values generated by this sampler
/// </summary>
[field: SerializeField]
public FloatRange range { get; set; }
public FloatRange range;
/// <summary>
/// Constructs a normal distribution sampler

var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
return SamplerUtility.TruncatedNormalSample(
rng.NextFloat(), range.minimum, range.maximum, mean, standardDeviation);
}
/// <summary>
/// Validates that the sampler is configured properly
/// </summary>
public void Validate()
{
range.Validate();
}
}
}

11
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs


/// <summary>
/// A range bounding the values generated by this sampler
/// </summary>
[field: SerializeField]
public FloatRange range { get; set; }
public FloatRange range;
/// <summary>
/// Constructs a UniformSampler

{
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
return rng.NextFloat(range.minimum, range.maximum);
}
/// <summary>
/// Validates that the sampler is configured properly
/// </summary>
public void Validate()
{
range.Validate();
}
}
}

5
com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs


if (samplerFieldPair.Key == "range")
{
var rangeObj = (JObject)samplerFieldPair.Value;
sampler.range = new FloatRange(
rangeObj["minimum"].ToObject<float>(), rangeObj["maximum"].ToObject<float>());
var field = sampler.GetType().GetField(samplerFieldPair.Key);
var range = new FloatRange(rangeObj["minimum"].ToObject<float>(), rangeObj["maximum"].ToObject<float>());
field.SetValue(sampler, range);
}
else
{

5
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/AnimationCurveSamplerTests.cs


namespace RandomizationTests.SamplerTests
{
[TestFixture]
public class AnimationCurveSamplerTests : RangedSamplerTests<AnimationCurveSampler>
public class AnimationCurveSamplerTestsBase : SamplerTestsBase<AnimationCurveSampler>
public AnimationCurveSamplerTests()
public AnimationCurveSamplerTestsBase()
m_BaseSampler.range = new FloatRange(m_BaseSampler.distributionCurve.keys[0].time, m_BaseSampler.distributionCurve.keys[m_BaseSampler.distributionCurve.length - 1].time);
}
}
}

21
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/NormalSamplerTests.cs


namespace RandomizationTests.SamplerTests
{
[TestFixture]
public class NormalSamplerTests : RangedSamplerTests<NormalSampler>
public class NormalSamplerTestsBase : SamplerTestsBase<NormalSampler>
public NormalSamplerTests()
public NormalSamplerTestsBase()
}
[Test]
public void SamplesInRange()
{
var samples = new float[k_TestSampleCount];
for (var i = 0; i < samples.Length; i++)
{
samples[i] = m_Sampler.Sample();
}
Assert.AreEqual(samples.Length, k_TestSampleCount);
for (var i = 0; i < samples.Length; i++)
{
Assert.GreaterOrEqual(samples[i], m_Sampler.range.minimum);
Assert.LessOrEqual(samples[i], m_Sampler.range.maximum);
}
}
}
}

23
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs


namespace RandomizationTests.SamplerTests
{
public abstract class RangedSamplerTests<T> where T : ISampler
public abstract class SamplerTestsBase<T> where T : ISampler
const int k_TestSampleCount = 30;
protected const int k_TestSampleCount = 30;
T m_Sampler;
protected T m_Sampler;
GameObject m_ScenarioObj;
static ScenarioBase activeScenario => ScenarioBase.activeScenario;

public void TearDown()
{
Object.DestroyImmediate(m_ScenarioObj);
}
[Test]
public void SamplesInRange()
{
var samples = new float[k_TestSampleCount];
for (var i = 0; i < samples.Length; i++)
{
samples[i] = m_Sampler.Sample();
}
Assert.AreEqual(samples.Length, k_TestSampleCount);
for (var i = 0; i < samples.Length; i++)
{
Assert.GreaterOrEqual(samples[i], m_Sampler.range.minimum);
Assert.LessOrEqual(samples[i], m_Sampler.range.maximum);
}
}
[Test]

21
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/UniformSamplerTests.cs


namespace RandomizationTests.SamplerTests
{
[TestFixture]
public class UniformSamplerTests : RangedSamplerTests<UniformSampler>
public class UniformSamplerTestsBase : SamplerTestsBase<UniformSampler>
public UniformSamplerTests()
public UniformSamplerTestsBase()
}
[Test]
public void SamplesInRange()
{
var samples = new float[k_TestSampleCount];
for (var i = 0; i < samples.Length; i++)
{
samples[i] = m_Sampler.Sample();
}
Assert.AreEqual(samples.Length, k_TestSampleCount);
for (var i = 0; i < samples.Length; i++)
{
Assert.GreaterOrEqual(samples[i], m_Sampler.range.minimum);
Assert.LessOrEqual(samples[i], m_Sampler.range.maximum);
}
}
}
}

10
com.unity.perception/Runtime/Randomization/Samplers/SamplerValidationException.cs


using System;
namespace UnityEngine.Experimental.Perception.Randomization.Samplers
{
public class SamplerValidationException : Exception
{
public SamplerValidationException(string msg) : base(msg) {}
public SamplerValidationException(string msg, Exception innerException) : base(msg, innerException) {}
}
}

3
com.unity.perception/Runtime/Randomization/Samplers/SamplerValidationException.cs.meta


fileFormatVersion: 2
guid: 232e97ede3ab4d769096833ee3e8d0e4
timeCreated: 1610570590
正在加载...
取消
保存