|
10 | 10 | import torch
|
11 | 11 |
|
12 | 12 |
|
| 13 | +def quaternion_angular_distance(q1: torch.Tensor, q2: torch.Tensor) -> torch.Tensor: |
| 14 | + """ |
| 15 | + Computes the angular distance between two quaternions. |
| 16 | + Args: |
| 17 | + q1: First quaternion (assume normalized). |
| 18 | + q2: Second quaternion (assume normalized). |
| 19 | + Returns: |
| 20 | + Angular distance between the two quaternions. |
| 21 | + """ |
| 22 | + |
| 23 | + # Compute the cosine of the angle between the two quaternions |
| 24 | + cos_theta = torch.sum(q1 * q2, dim=-1) |
| 25 | + # we use atan2 instead of acos for better numerical stability |
| 26 | + cos_theta = torch.clamp(cos_theta, -1.0, 1.0) |
| 27 | + abs_dot = torch.abs(cos_theta) |
| 28 | + # identity sin^2(theta) = 1 - cos^2(theta) |
| 29 | + sin_half_theta = torch.sqrt(1.0 - torch.square(abs_dot)) |
| 30 | + theta = 2.0 * torch.atan2(sin_half_theta, abs_dot) |
| 31 | + |
| 32 | + # theta for the ones that are close gets 0 and we don't care about them |
| 33 | + close = quaternion_close(q1, q2) |
| 34 | + theta[close] = 0 |
| 35 | + return theta |
| 36 | + |
| 37 | + |
13 | 38 | def quaternion_close(q1: torch.Tensor, q2: torch.Tensor, eps: float = 1e-4):
|
14 | 39 | """
|
15 | 40 | Returns true if two quaternions are close to each other. Assumes the quaternions are normalized.
|
16 | 41 | Based on: https://math.stackexchange.com/a/90098/516340
|
17 | 42 |
|
18 | 43 | """
|
19 |
| - dist = 1 - torch.square(torch.sum(q1*q2, dim=-1)) |
| 44 | + dist = 1 - torch.square(torch.sum(q1 * q2, dim=-1)) |
20 | 45 | return torch.all(dist < eps)
|
21 | 46 |
|
22 | 47 |
|
| 48 | +def quaternion_slerp(q1: torch.Tensor, q2: torch.Tensor, t: Union[float, torch.tensor]) -> torch.Tensor: |
| 49 | + """ |
| 50 | + Spherical linear interpolation between two quaternions. |
| 51 | + Args: |
| 52 | + q1: First quaternion (assume normalized). |
| 53 | + q2: Second quaternion (assume normalized). |
| 54 | + t: Interpolation parameter. |
| 55 | + Returns: |
| 56 | + Interpolated quaternion. |
| 57 | + """ |
| 58 | + # Compute the cosine of the angle between the two quaternions |
| 59 | + cos_theta = torch.sum(q1 * q2, dim=-1) |
| 60 | + |
| 61 | + # reverse the direction of q2 if q1 and q2 are not in the same hemisphere |
| 62 | + to_invert = cos_theta < 0 |
| 63 | + q2[to_invert] = -q2[to_invert] |
| 64 | + cos_theta[to_invert] = -cos_theta[to_invert] |
| 65 | + |
| 66 | + # If the quaternions are close, perform a linear interpolation |
| 67 | + if torch.all(cos_theta > 1.0 - 1e-6): |
| 68 | + return q1 + t * (q2 - q1) |
| 69 | + |
| 70 | + # Ensure the angle is between 0 and pi |
| 71 | + theta = torch.acos(cos_theta) |
| 72 | + sin_theta = torch.sin(theta) |
| 73 | + |
| 74 | + # Perform the interpolation |
| 75 | + w1 = torch.sin((1.0 - t) * theta) / sin_theta |
| 76 | + w2 = torch.sin(t * theta) / sin_theta |
| 77 | + return w1[:, None] * q1 + w2[:, None] * q2 |
| 78 | + |
| 79 | + |
23 | 80 | def acos_linear_extrapolation(
|
24 | 81 | x: torch.Tensor,
|
25 | 82 | bound: Union[float, Tuple[float, float]] = 1.0 - 1e-4,
|
|
0 commit comments