From 66a54f9036acad927641f82d8cbde652d1d82193 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Thu, 16 May 2024 14:55:58 +0200 Subject: [PATCH] :art: Improve rectangle_to_polygon_region by using numpy array and loop --- src/ipyaladin/region_converter.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/ipyaladin/region_converter.py b/src/ipyaladin/region_converter.py index 3d707969..d2833c83 100644 --- a/src/ipyaladin/region_converter.py +++ b/src/ipyaladin/region_converter.py @@ -85,7 +85,7 @@ def to_global_xyz(self, x: float, y: float, z: float) -> tuple[float, float, flo """ rotated_matrix = np.sum( - np.multiply(self.rotation_matrix, np.array([[x], [y], [z]])), axis=0 + self.rotation_matrix * np.array([[x], [y], [z]]), axis=0 ) return rotated_matrix[0], rotated_matrix[1], rotated_matrix[2] @@ -156,21 +156,19 @@ def rectangle_to_polygon_region(region: RectangleSkyRegion) -> PolygonSkyRegion: x1, y1, z1 = cos_lon * cos_lat, sin_lon * cos_lat, sin_lat - y2 = y1 * sin_pa - z1 * cos_pa - z2 = y1 * cos_pa + z1 * sin_pa - vertices = [frame_rotation.to_global_coo(x1, y2, z2)] + pa_matrix = np.array([[y1 * sin_pa, z1 * sin_pa], [y1 * cos_pa, z1 * sin_pa]]) - y2 = y1 * sin_pa + z1 * cos_pa - z2 = y1 * cos_pa - z1 * sin_pa - vertices.append(frame_rotation.to_global_coo(x1, y2, z2)) + multipliers = [ + [[1, -1], [1, 1]], + [[1, 1], [1, -1]], + [[-1, 1], [-1, -1]], + [[-1, -1], [-1, 1]], + ] + vertices = [] - y2 = -y1 * sin_pa + z1 * cos_pa - z2 = -y1 * cos_pa - z1 * sin_pa - vertices.append(frame_rotation.to_global_coo(x1, y2, z2)) - - y2 = -y1 * sin_pa - z1 * cos_pa - z2 = -y1 * cos_pa + z1 * sin_pa - vertices.append(frame_rotation.to_global_coo(x1, y2, z2)) + for multiplier in multipliers: + y2, z2 = np.sum(np.array(multiplier) * pa_matrix, axis=1) + vertices.append(frame_rotation.to_global_coo(x1, y2, z2)) return PolygonSkyRegion( vertices=SkyCoord(vertices, unit="rad", frame="icrs"),