|
| 1 | +"""Wake geometry construction and SO(3) coordinate transforms.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from scipy.spatial import ConvexHull |
| 5 | + |
| 6 | + |
| 7 | +def rotation_x(theta: float) -> np.ndarray: |
| 8 | + """Rotation matrix about the x-axis by angle theta.""" |
| 9 | + return np.array([ |
| 10 | + [1, 0, 0], |
| 11 | + [0, np.cos(theta), -np.sin(theta)], |
| 12 | + [0, np.sin(theta), np.cos(theta)], |
| 13 | + ]) |
| 14 | + |
| 15 | + |
| 16 | +def rotation_y(theta: float) -> np.ndarray: |
| 17 | + """Rotation matrix about the y-axis by angle theta.""" |
| 18 | + return np.array([ |
| 19 | + [np.cos(theta), 0, np.sin(theta)], |
| 20 | + [0, 1, 0], |
| 21 | + [-np.sin(theta), 0, np.cos(theta)], |
| 22 | + ]) |
| 23 | + |
| 24 | + |
| 25 | +def rotation_z(theta: float) -> np.ndarray: |
| 26 | + """Rotation matrix about the z-axis by angle theta.""" |
| 27 | + return np.array([ |
| 28 | + [np.cos(theta), -np.sin(theta), 0], |
| 29 | + [np.sin(theta), np.cos(theta), 0], |
| 30 | + [0, 0, 1], |
| 31 | + ]) |
| 32 | + |
| 33 | + |
| 34 | +def apply_rotation(wedge: np.ndarray, angles: tuple[float, float, float]) -> np.ndarray: |
| 35 | + """Apply SO(3) rotation (Euler angles) to a set of 3D points. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + wedge : np.ndarray |
| 40 | + Array of shape (N, 3) representing points in R^3. |
| 41 | + angles : tuple of float |
| 42 | + Euler angles (alpha, beta, gamma) for rotations about x, y, z axes. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + np.ndarray |
| 47 | + Rotated points with the same shape as input. |
| 48 | + """ |
| 49 | + center = np.mean(wedge, axis=0) |
| 50 | + shifted = wedge - center |
| 51 | + rotated = shifted @ rotation_x(angles[0]).T |
| 52 | + rotated = rotated @ rotation_y(angles[1]).T |
| 53 | + rotated = rotated @ rotation_z(angles[2]).T |
| 54 | + return rotated + center |
| 55 | + |
| 56 | + |
| 57 | +def build_wake_wedge( |
| 58 | + tip: np.ndarray, |
| 59 | + deficit_angle: float, |
| 60 | + wake_depth: float, |
| 61 | + wake_length: float, |
| 62 | +) -> np.ndarray: |
| 63 | + """Construct a 6-vertex wake wedge geometry in physical space. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + tip : np.ndarray |
| 68 | + 3D coordinates of the wake tip point. |
| 69 | + deficit_angle : float |
| 70 | + Opening angle of the wake wedge. |
| 71 | + wake_depth : float |
| 72 | + Radial depth of the wake in Mpc. |
| 73 | + wake_length : float |
| 74 | + Length of the wake along the string direction in Mpc. |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + np.ndarray |
| 79 | + Array of shape (6, 3) defining the wake wedge vertices. |
| 80 | + """ |
| 81 | + end_points = np.array([ |
| 82 | + [tip[0] + wake_depth * np.cos(deficit_angle / 2), |
| 83 | + tip[1] + wake_depth * np.sin(deficit_angle / 2), |
| 84 | + tip[2]], |
| 85 | + [tip[0] + wake_depth * np.cos(deficit_angle / 2), |
| 86 | + tip[1] - wake_depth * np.sin(deficit_angle / 2), |
| 87 | + tip[2]], |
| 88 | + ]) |
| 89 | + projected = np.array([ |
| 90 | + tip + [0, 0, wake_length], |
| 91 | + end_points[0] + [0, 0, wake_length], |
| 92 | + end_points[1] + [0, 0, wake_length], |
| 93 | + ]) |
| 94 | + return np.array([tip, end_points[0], end_points[1], |
| 95 | + projected[0], projected[1], projected[2]]) |
| 96 | + |
| 97 | + |
| 98 | +def point_in_hull(hull_points: np.ndarray, test_points: np.ndarray) -> bool: |
| 99 | + """Check whether test points lie inside the convex hull of hull_points. |
| 100 | +
|
| 101 | + Uses the vertex-stability method: if adding the test points does not |
| 102 | + change the hull vertices, all test points are interior. |
| 103 | +
|
| 104 | + Parameters |
| 105 | + ---------- |
| 106 | + hull_points : np.ndarray |
| 107 | + Array of shape (M, 3) defining the convex hull. |
| 108 | + test_points : np.ndarray |
| 109 | + Array of shape (K, 3) of points to test. |
| 110 | +
|
| 111 | + Returns |
| 112 | + ------- |
| 113 | + bool |
| 114 | + True if all test points are inside the hull. |
| 115 | + """ |
| 116 | + hull = ConvexHull(hull_points) |
| 117 | + combined = np.append(hull_points, test_points, axis=0) |
| 118 | + new_hull = ConvexHull(combined) |
| 119 | + return list(hull.vertices) == list(new_hull.vertices) |
0 commit comments