PoseExtensions.cs
2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// Several method extensions to <c>Pose</c> for inverse-transforming additional Unity types.
/// </summary>
public static class PoseExtensions
{
/// <summary>
/// Inversely transform the <paramref name="position"/> by <c>Pose</c>.
/// </summary>
/// <param name="pose">The <c>Pose</c> to use.</param>
/// <param name="position">A position to inversely transform</param>
/// <returns>An position inversely transformed by the <paramref name="pose"/>.</returns>
public static Vector3 InverseTransformPosition(this Pose pose, Vector3 position)
{
return Quaternion.Inverse(pose.rotation) * (position - pose.position);
}
/// <summary>
/// Inversely transform the <paramref name="direction"/> by <c>Pose</c>.
/// </summary>
/// <param name="pose">The <c>Pose</c> to use.</param>
/// <param name="direction">A direction to inversely transform</param>
/// <returns>An direction inversely transformed by the <paramref name="pose"/>.</returns>
public static Vector3 InverseTransformDirection(this Pose pose, Vector3 direction)
{
return Quaternion.Inverse(pose.rotation) * direction;
}
/// <summary>
/// Inversely transform the <paramref name="positions"/> by <c>Pose</c>. The transform is made in-place.
/// </summary>
/// <param name="pose">The <c>Pose</c> to use.</param>
/// <param name="positions">A <c>List</c> of positions to inversely transform</param>
public static void InverseTransformPositions(this Pose pose, List<Vector3> positions)
{
if (positions == null)
throw new ArgumentNullException("positions");
for (int i = 0; i < positions.Count; ++i)
{
positions[i] = pose.InverseTransformPosition(positions[i]);
}
}
}
}