namespace UnityEngine.Animations.Rigging
{
///
/// Rigid transform with only translation and rotation components.
///
[System.Serializable]
public struct AffineTransform
{
/// Translation component of the AffineTransform.
public Vector3 translation;
/// Rotation component of the AffineTransform.
public Quaternion rotation;
///
/// Constructor.
///
/// Translation component of the AffineTransform.
/// Rotation component of the AffineTransform.
public AffineTransform(Vector3 t, Quaternion r)
{
translation = t;
rotation = r;
}
///
/// Sets the translation and rotation in the AffineTransform.
///
/// Translation component of the AffineTransform.
/// Rotation component of the AffineTransform.
public void Set(Vector3 t, Quaternion r)
{
translation = t;
rotation = r;
}
///
/// Transforms a Vector3 point by the AffineTransform.
///
/// Vector3 point.
/// Transformed Vector3 point.
public Vector3 Transform(Vector3 p) =>
rotation * p + translation;
///
/// Transforms a Vector3 point by the inverse of the AffineTransform.
///
/// Vector3 point.
/// Transformed Vector3 point.
public Vector3 InverseTransform(Vector3 p) =>
Quaternion.Inverse(rotation) * (p - translation);
///
/// Calculates the inverse of the AffineTransform.
///
/// The inverse of the AffineTransform.
public AffineTransform Inverse()
{
var invR = Quaternion.Inverse(rotation);
return new AffineTransform(invR * -translation, invR);
}
///
/// Multiply a transform by the inverse of the AffineTransform.
///
/// AffineTransform value.
/// Multiplied AffineTransform result.
public AffineTransform InverseMul(AffineTransform transform)
{
var invR = Quaternion.Inverse(rotation);
return new AffineTransform(invR * (transform.translation - translation), invR * transform.rotation);
}
///
/// Transforms a Vector3 point by the AffineTransform.
///
/// AffineTransform value.
/// Vector3 point.
/// Transformed Vector3 point.
public static Vector3 operator *(AffineTransform lhs, Vector3 rhs) =>
lhs.rotation * rhs + lhs.translation;
///
/// Multiplies two AffineTransform.
///
/// First AffineTransform value.
/// Second AffineTransform value.
/// Multiplied AffineTransform result.
public static AffineTransform operator *(AffineTransform lhs, AffineTransform rhs) =>
new AffineTransform(lhs.Transform(rhs.translation), lhs.rotation * rhs.rotation);
///
/// Rotates an AffineTransform.
///
/// Quaternion rotation.
/// AffineTransform value.
/// Rotated AffineTransform result.
public static AffineTransform operator *(Quaternion lhs, AffineTransform rhs) =>
new AffineTransform(lhs * rhs.translation, lhs * rhs.rotation);
///
/// Transforms a Quaternion value by the AffineTransform.
///
/// AffineTransform value.
/// Quaternion rotation.
/// Transformed AffineTransform result.
public static AffineTransform operator *(AffineTransform lhs, Quaternion rhs) =>
new AffineTransform(lhs.translation, lhs.rotation * rhs);
///
/// AffineTransform identity value.
///
public static AffineTransform identity { get; } = new AffineTransform(Vector3.zero, Quaternion.identity);
}
}