Steven Leal
4 年前
当前提交
37eea993
共有 6 个文件被更改,包括 221 次插入 和 110 次删除
-
2com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs
-
123com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs
-
94com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs
-
3com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs.meta
-
109com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs
-
0/com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs.meta
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using Newtonsoft.Json; |
|||
using UnityEditor; |
|||
using UnityEngine.Perception.Randomization.Parameters; |
|||
using UnityEngine.Perception.Randomization.Randomizers; |
|||
using UnityEngine.Perception.Randomization.Samplers; |
|||
|
|||
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization |
|||
{ |
|||
static class ScenarioTemplateSerializer |
|||
{ |
|||
[MenuItem("Tests/Deserialize Test")] |
|||
public static void DeserializeTest() |
|||
{ |
|||
var jsonString = File.ReadAllText($"{Application.streamingAssetsPath}/data.json"); |
|||
var schema = JsonConvert.DeserializeObject<TemplateConfigurationOptions>(jsonString); |
|||
var backToJson = JsonConvert.SerializeObject(schema, Formatting.Indented); |
|||
Debug.Log(backToJson); |
|||
} |
|||
|
|||
[MenuItem("Tests/Serialize Scenario To Json Test")] |
|||
public static void SerializeScenarioToJsonTest() |
|||
{ |
|||
var template = SerializeScenarioIntoTemplate(Object.FindObjectOfType<ScenarioBase>()); |
|||
Debug.Log(JsonConvert.SerializeObject(template, Formatting.Indented)); |
|||
} |
|||
|
|||
public static TemplateConfigurationOptions SerializeScenarioIntoTemplate(ScenarioBase scenario) |
|||
{ |
|||
return new TemplateConfigurationOptions |
|||
{ |
|||
groups = SerializeRandomizers(scenario.randomizers) |
|||
}; |
|||
} |
|||
|
|||
static Dictionary<string, Group> SerializeRandomizers(IEnumerable<Randomizer> randomizers) |
|||
{ |
|||
var serializedRandomizers = new Dictionary<string, Group>(); |
|||
foreach (var randomizer in randomizers) |
|||
{ |
|||
var randomizerData = SerializeRandomizer(randomizer); |
|||
if (randomizerData.items.Count == 0) |
|||
continue; |
|||
serializedRandomizers.Add(randomizer.GetType().Name, randomizerData); |
|||
} |
|||
return serializedRandomizers; |
|||
} |
|||
|
|||
static Group SerializeRandomizer(Randomizer randomizer) |
|||
{ |
|||
var randomizerObj = new Group(); |
|||
var parameterFields = randomizer.GetType().GetFields(); |
|||
foreach (var parameterField in parameterFields) |
|||
{ |
|||
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), parameterField.FieldType)) |
|||
continue; |
|||
var parameter = (Randomization.Parameters.Parameter)parameterField.GetValue(randomizer); |
|||
var parameterData = SerializeParameter(parameter); |
|||
if (parameterData.items.Count == 0) |
|||
continue; |
|||
randomizerObj.items.Add(parameterField.Name, parameterData); |
|||
} |
|||
return randomizerObj; |
|||
} |
|||
|
|||
static Parameter SerializeParameter(Randomization.Parameters.Parameter parameter) |
|||
{ |
|||
var parameterData = new Parameter(); |
|||
var samplerFields = parameter.GetType().GetFields(); |
|||
foreach (var samplerField in samplerFields) |
|||
{ |
|||
if (!samplerField.FieldType.IsAssignableFrom(typeof(ISampler))) |
|||
continue; |
|||
var sampler = (ISampler)samplerField.GetValue(parameter); |
|||
var samplerData = SerializeSampler(sampler); |
|||
if (samplerData.defaultSampler == null) |
|||
continue; |
|||
parameterData.items.Add(samplerField.Name, samplerData); |
|||
} |
|||
return parameterData; |
|||
} |
|||
|
|||
static SamplerOptions SerializeSampler(ISampler sampler) |
|||
{ |
|||
var samplerData = new SamplerOptions(); |
|||
if (sampler is Samplers.ConstantSampler constantSampler) |
|||
samplerData.defaultSampler = new ConstantSampler |
|||
{ |
|||
value = constantSampler.value |
|||
}; |
|||
else if (sampler is Samplers.UniformSampler uniformSampler) |
|||
samplerData.defaultSampler = new UniformSampler |
|||
{ |
|||
min = uniformSampler.range.minimum, |
|||
max = uniformSampler.range.maximum |
|||
}; |
|||
else if (sampler is Samplers.NormalSampler normalSampler) |
|||
samplerData.defaultSampler = new NormalSampler |
|||
{ |
|||
min = normalSampler.range.minimum, |
|||
max = normalSampler.range.maximum, |
|||
mean = normalSampler.mean, |
|||
standardDeviation = normalSampler.standardDeviation |
|||
}; |
|||
else |
|||
throw new ArgumentException($"Invalid sampler type ({sampler.GetType()})"); |
|||
return samplerData; |
|||
} |
|||
|
|||
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) { |
|||
while (toCheck != null && toCheck != typeof(object)) { |
|||
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck; |
|||
if (generic == cur) { |
|||
return true; |
|||
} |
|||
toCheck = toCheck.BaseType; |
|||
} |
|||
return false; |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization |
|||
{ |
|||
#region Interfaces
|
|||
public interface IGroupItem { } |
|||
|
|||
public interface IParameterItem { } |
|||
|
|||
public interface ISamplerOption { } |
|||
|
|||
public interface IScalarValue { } |
|||
#endregion
|
|||
|
|||
#region GroupedObjects
|
|||
public class TemplateConfigurationOptions |
|||
{ |
|||
public Dictionary<string, Group> groups = new Dictionary<string, Group>(); |
|||
} |
|||
|
|||
public class StandardMetadata |
|||
{ |
|||
public string name = string.Empty; |
|||
public string description = string.Empty; |
|||
} |
|||
|
|||
public class Group |
|||
{ |
|||
public StandardMetadata metadata = new StandardMetadata(); |
|||
[JsonConverter(typeof(GroupItemsConverter))] |
|||
public Dictionary<string, IGroupItem> items = new Dictionary<string, IGroupItem>(); |
|||
} |
|||
|
|||
public class Parameter : IGroupItem |
|||
{ |
|||
public StandardMetadata metadata = new StandardMetadata(); |
|||
[JsonConverter(typeof(ParameterItemsConverter))] |
|||
public Dictionary<string, IParameterItem> items = new Dictionary<string, IParameterItem>(); |
|||
} |
|||
#endregion
|
|||
|
|||
#region SamplerOptions
|
|||
[JsonConverter(typeof(SamplerOptionsConverter))] |
|||
public class SamplerOptions : IParameterItem |
|||
{ |
|||
public StandardMetadata metadata = new StandardMetadata(); |
|||
public ISamplerOption defaultSampler; |
|||
} |
|||
|
|||
public class UniformSampler : ISamplerOption |
|||
{ |
|||
public double min; |
|||
public double max; |
|||
} |
|||
|
|||
public class NormalSampler : ISamplerOption |
|||
{ |
|||
public double min; |
|||
public double max; |
|||
public double mean; |
|||
public double standardDeviation; |
|||
} |
|||
|
|||
public class ConstantSampler : ISamplerOption |
|||
{ |
|||
public double value; |
|||
} |
|||
#endregion
|
|||
|
|||
#region ScalarValues
|
|||
[JsonConverter(typeof(ScalarConverter))] |
|||
public class Scalar : IGroupItem, IParameterItem |
|||
{ |
|||
public StandardMetadata metadata = new StandardMetadata(); |
|||
public IScalarValue value; |
|||
} |
|||
|
|||
public class StringScalarValue : IScalarValue |
|||
{ |
|||
public string str; |
|||
} |
|||
|
|||
public class DoubleScalarValue : IScalarValue |
|||
{ |
|||
public double num; |
|||
} |
|||
|
|||
public class BooleanScalarValue : IScalarValue |
|||
{ |
|||
public bool boolean; |
|||
} |
|||
#endregion
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f1253a3d94bb468b93f80c2178652540 |
|||
timeCreated: 1612927921 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using Newtonsoft.Json; |
|||
using UnityEditor; |
|||
|
|||
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization |
|||
{ |
|||
public static class ScenarioSerialization |
|||
{ |
|||
[MenuItem("Tests/Deserialize Test")] |
|||
public static void DeserializeTest() |
|||
{ |
|||
var jsonString = File.ReadAllText($"{Application.streamingAssetsPath}/data.json"); |
|||
var schema = JsonConvert.DeserializeObject<TemplateConfigurationOptions>(jsonString); |
|||
var backToJson = JsonConvert.SerializeObject(schema, Formatting.Indented); |
|||
Debug.Log(backToJson); |
|||
} |
|||
} |
|||
|
|||
#region Interfaces
|
|||
public interface IGroupItem { } |
|||
|
|||
public interface IParameterItem { } |
|||
|
|||
public interface ISamplerOption { } |
|||
|
|||
public interface IScalarValue { } |
|||
#endregion
|
|||
|
|||
#region GroupedObjects
|
|||
public class TemplateConfigurationOptions |
|||
{ |
|||
public Dictionary<string, Group> groups; |
|||
} |
|||
|
|||
public class StandardMetadata |
|||
{ |
|||
public string name = string.Empty; |
|||
public string description = string.Empty; |
|||
} |
|||
|
|||
public class Group |
|||
{ |
|||
public StandardMetadata metadata; |
|||
[JsonConverter(typeof(GroupItemsConverter))] |
|||
public Dictionary<string, IGroupItem> items; |
|||
} |
|||
|
|||
public class Parameter : IGroupItem |
|||
{ |
|||
public StandardMetadata metadata; |
|||
[JsonConverter(typeof(ParameterItemsConverter))] |
|||
public Dictionary<string, IParameterItem> items; |
|||
} |
|||
#endregion
|
|||
|
|||
#region SamplerOptions
|
|||
[JsonConverter(typeof(SamplerOptionsConverter))] |
|||
public class SamplerOptions : IParameterItem |
|||
{ |
|||
public StandardMetadata metadata; |
|||
public ISamplerOption defaultSampler; |
|||
} |
|||
|
|||
public class UniformSampler : ISamplerOption |
|||
{ |
|||
public double min; |
|||
public double max; |
|||
} |
|||
|
|||
public class NormalSampler : ISamplerOption |
|||
{ |
|||
public double min; |
|||
public double max; |
|||
public double mean; |
|||
public double standardDeviation; |
|||
} |
|||
|
|||
public class ConstantSampler : ISamplerOption |
|||
{ |
|||
public double value; |
|||
} |
|||
#endregion
|
|||
|
|||
#region ScalarValues
|
|||
[JsonConverter(typeof(ScalarConverter))] |
|||
public class Scalar : IGroupItem, IParameterItem |
|||
{ |
|||
public StandardMetadata metadata; |
|||
public IScalarValue value; |
|||
} |
|||
|
|||
public class StringScalarValue : IScalarValue |
|||
{ |
|||
public string str; |
|||
} |
|||
|
|||
public class DoubleScalarValue : IScalarValue |
|||
{ |
|||
public double num; |
|||
} |
|||
|
|||
public class BooleanScalarValue : IScalarValue |
|||
{ |
|||
public bool boolean; |
|||
} |
|||
#endregion
|
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue