|
Author |
hbghlyj
Posted at 2025-1-18 23:57:35
Python- import numpy as np
- def uniform_random_rotation(x):
- """Apply a random rotation in 3D, with a distribution uniform over the
- sphere.
- Arguments:
- x: vector or set of vectors with dimension (n, 3), where n is the
- number of vectors
- Returns:
- Array of shape (n, 3) containing the randomly rotated vectors of x,
- about the mean coordinate of x.
- Algorithm taken from "Fast Random Rotation Matrices" (James Avro, 1992):
- https://doi.org/10.1016/B978-0-08-050755-2.50034-8
- """
- def generate_random_z_axis_rotation():
- """Generate random rotation matrix about the z axis."""
- R = np.eye(3)
- x1 = np.random.rand()
- R[0, 0] = R[1, 1] = np.cos(2 * np.pi * x1)
- R[0, 1] = -np.sin(2 * np.pi * x1)
- R[1, 0] = np.sin(2 * np.pi * x1)
- return R
- # There are two random variables in [0, 1) here (naming is same as paper)
- x2 = 2 * np.pi * np.random.rand()
- x3 = np.random.rand()
- # Rotation of all points around x axis using matrix
- R = generate_random_z_axis_rotation()
- v = np.array([
- np.cos(x2) * np.sqrt(x3),
- np.sin(x2) * np.sqrt(x3),
- np.sqrt(1 - x3)
- ])
- H = np.eye(3) - (2 * np.outer(v, v))
- M = -(H @ R)
- x = x.reshape((-1, 3))
- mean_coord = np.mean(x, axis=0)
- return ((x - mean_coord) @ M) + mean_coord @ M
Copy the Code Mathematica- uniformRandomRotation[x_] := Module[
- {generateRandomZAxisRotation, x2, x3, R, v, H, M, meanCoord},
-
- generateRandomZAxisRotation[] := Module[{m, r},
- r = RandomReal[];
- m = IdentityMatrix[3];
- m[[1,1]] = m[[2,2]] = Cos[2 Pi r];
- m[[1,2]] = -Sin[2 Pi r];
- m[[2,1]] = Sin[2 Pi r];
- m
- ];
-
- x2 = 2 Pi RandomReal[];
- x3 = RandomReal[];
- R = generateRandomZAxisRotation[];
- v = {Cos[x2] Sqrt[x3], Sin[x2] Sqrt[x3], Sqrt[1 - x3]};
- H = IdentityMatrix[3] - 2 Outer[Times, v, v];
- M = - (H . R);
- meanCoord = Mean[x];
-
- ((x - meanCoord) M) + meanCoord M
- ]
Copy the Code |
|