using System;
using Unity.MLAgents.Sensors;
namespace Unity.MLAgents.Extensions.Sensors
{
///
/// Settings that define the observations generated for physics-based sensors.
///
[Serializable]
public struct PhysicsSensorSettings
{
///
/// Whether to use model space (relative to the root body) translations as observations.
///
public bool UseModelSpaceTranslations;
///
/// Whether to use model space (relative to the root body) rotations as observations.
///
public bool UseModelSpaceRotations;
///
/// Whether to use local space (relative to the parent body) translations as observations.
///
public bool UseLocalSpaceTranslations;
///
/// Whether to use local space (relative to the parent body) translations as observations.
///
public bool UseLocalSpaceRotations;
///
/// Whether to use model space (relative to the root body) linear velocities as observations.
///
public bool UseModelSpaceLinearVelocity;
///
/// Whether to use local space (relative to the parent body) linear velocities as observations.
///
public bool UseLocalSpaceLinearVelocity;
///
/// Creates a PhysicsSensorSettings with reasonable default values.
///
///
public static PhysicsSensorSettings Default()
{
return new PhysicsSensorSettings
{
UseModelSpaceTranslations = true,
UseModelSpaceRotations = true,
};
}
///
/// Whether any model space observations are being used.
///
public bool UseModelSpace
{
get { return UseModelSpaceTranslations || UseModelSpaceRotations || UseModelSpaceLinearVelocity; }
}
///
/// Whether any local space observations are being used.
///
public bool UseLocalSpace
{
get { return UseLocalSpaceTranslations || UseLocalSpaceRotations || UseLocalSpaceLinearVelocity; }
}
///
/// The number of floats needed to represent a given number of transforms.
///
///
///
public int TransformSize(int numTransforms)
{
int obsPerTransform = 0;
obsPerTransform += UseModelSpaceTranslations ? 3 : 0;
obsPerTransform += UseModelSpaceRotations ? 4 : 0;
obsPerTransform += UseLocalSpaceTranslations ? 3 : 0;
obsPerTransform += UseLocalSpaceRotations ? 4 : 0;
obsPerTransform += UseModelSpaceLinearVelocity ? 3 : 0;
obsPerTransform += UseLocalSpaceLinearVelocity ? 3 : 0;
return numTransforms * obsPerTransform;
}
}
internal static class ObservationWriterPhysicsExtensions
{
///
/// Utility method for writing a PoseExtractor to an ObservationWriter.
///
///
///
///
/// The offset into the ObservationWriter to start writing at.
/// The number of observations written.
public static int WritePoses(this ObservationWriter writer, PhysicsSensorSettings settings, PoseExtractor poseExtractor, int baseOffset = 0)
{
var offset = baseOffset;
if (settings.UseModelSpace)
{
var poses = poseExtractor.ModelSpacePoses;
var vels = poseExtractor.ModelSpaceVelocities;
for(var i=0; i