This code does not use cone inclination, fix it to take it into account. def…
- User
This code does not use cone inclination, fix it to take it into account.
def cone_base_vertices(cone_top, cone_base_center, cone_opening_angle, cone_inclination, num_points=100): """ Generate vertices for the cone's base, projected onto the sky. """ # Convert apex and base center to Cartesian coordinates apex_vector = radec_to_cartesian(cone_top.ra.deg, cone_top.dec.deg) base_center_vector = radec_to_cartesian(cone_base_center.ra.deg, cone_base_center.dec.deg)
# Compute cone axis vector axis_vector = base_center_vector - apex_vector distance = np.linalg.norm(axis_vector) axis_vector = axis_vector / distance # Compute base radius cone_opening_angle_rad = cone_opening_angle.to(u.radian).value base_radius = np.tan(cone_opening_angle_rad) * distance # At base center, compute orthonormal vectors perpendicular to axis_vector # Find a vector not parallel to axis_vector if np.allclose(axis_vector, [0, 0, 1]) or np.allclose(axis_vector, [0, 0, -1]): v1 = np.array([1, 0, 0]) else: v1 = np.array([0, 0, 1]) # Compute perpendicular vectors v_east = np.cross(axis_vector, v1) v_east /= np.linalg.norm(v_east) v_north = np.cross(axis_vector, v_east) v_north /= np.linalg.norm(v_north) # Generate circle points around the base theta = np.linspace(0, 2 * np.pi, num_points) circle_points = base_center_vector[:, np.newaxis] + base_radius * ( v_east[:, np.newaxis] * np.cos(theta) + v_north[:, np.newaxis] * np.sin(theta) ) circle_points /= np.linalg.norm(circle_points, axis=0) # Normalize to unit sphere # Convert circle points to RA and Dec ra_base_vertices, dec_base_vertices = cartesian_to_radec( circle_points[0], circle_points[1], circle_points[2] ) # Return base vertices as SkyCoord base_vertices = SkyCoord(ra=ra_base_vertices * u.deg, dec=dec_base_vertices * u.deg, frame='icrs') return base_vertices - Assistant
To fix the code so that it takes the
cone_inclinationinto account, you need to modify the way theaxis_vector(the cone's axis) is computed. The current code computes theaxis_vectorsimply as the vector from the apex to the base center, without considering the inclination. To incorporate thecone_inclination, you need to rotate theaxis_vectorby thecone_inclinationangle around the appropriate axis.Here's how you can modify the code:
-
Remove
cone_base_centeras an input parameter: Since the base center depends on the apex, the axis, and the inclination, you should compute it within the function rather than passing it as an argument. -
Compute local East and North vectors at the apex: These vectors are perpendicular to the apex vector and are used to define the plane for rotation.
-
Compute the new
axis_vectorconsidering thecone_inclination: Rotate the axis vector bycone_inclinationradians towards the local North direction using the local East and North vectors. -
Compute the
base_center_vectorbased on the newaxis_vector: The base center is now along the inclined axis at a certain distance from the apex. -
Compute
base_radiusand generate circle points around the base: Use the newaxis_vectorto compute the base radius and generate the base vertices accordingly.
Here's the corrected code:
def cone_base_vertices(cone_top, cone_opening_angle, cone_inclination, distance=1.0, num_points=100): """ Generate vertices for the cone's base, projected onto the sky, taking into account the cone_inclination. """ # Convert apex to Cartesian coordinates apex_vector = radec_to_cartesian(cone_top.ra.deg, cone_top.dec.deg) # Convert cone_inclination to radians cone_inclination_rad = cone_inclination.to(u.radian).value # Compute local east and north vectors at apex_vector # Choose a vector not parallel to apex_vector if np.allclose(apex_vector, [0, 0, 1]) or np.allclose(apex_vector, [0, 0, -1]): v1 = np.array([1, 0, 0]) else: v1 = np.array([0, 0, 1]) # Compute east_vector east_vector = np.cross(v1, apex_vector) east_vector /= np.linalg.norm(east_vector) # Compute north_vector north_vector = np.cross(apex_vector, east_vector) north_vector /= np.linalg.norm(north_vector) # Compute axis_vector with inclination axis_vector = apex_vector * np.cos(cone_inclination_rad) - north_vector * np.sin(cone_inclination_rad) axis_vector /= np.linalg.norm(axis_vector) # Compute base_center_vector base_center_vector = apex_vector + axis_vector * distance base_center_vector /= np.linalg.norm(base_center_vector) # Compute base radius cone_opening_angle_rad = cone_opening_angle.to(u.radian).value base_radius = np.tan(cone_opening_angle_rad) * distance # At base center, compute orthonormal vectors perpendicular to axis_vector # Find a vector not parallel to axis_vector if np.allclose(axis_vector, [0, 0, 1]) or np.allclose(axis_vector, [0, 0, -1]): v1 = np.array([1, 0, 0]) else: v1 = np.array([0, 0, 1]) # Compute perpendicular vectors v_east = np.cross(axis_vector, v1) v_east /= np.linalg.norm(v_east) v_north = np.cross(axis_vector, v_east) v_north /= np.linalg.norm(v_north) # Generate circle points around the base theta = np.linspace(0, 2 * np.pi, num_points) circle_points = base_center_vector[:, np.newaxis] + base_radius * ( v_east[:, np.newaxis] * np.cos(theta) + v_north[:, np.newaxis] * np.sin(theta) ) circle_points /= np.linalg.norm(circle_points, axis=0) # Normalize to unit sphere # Convert circle points to RA and Dec ra_base_vertices, dec_base_vertices = cartesian_to_radec( circle_points[0], circle_points[1], circle_points[2] ) # Return base vertices as SkyCoord base_vertices = SkyCoord(ra=ra_base_vertices * u.deg, dec=dec_base_vertices * u.deg, frame='icrs') return base_verticesExplanation of the Changes:
-
Computing the Local East and North Vectors: The
east_vectorandnorth_vectorare computed at the apex point. They are perpendicular to the apex vector and serve as the basis for rotating the axis vector. -
Rotating the Axis Vector: The
axis_vectoris rotated by thecone_inclinationangle towards the local North direction. This is accomplished using the rotation formula:axis_vector = apex_vector * np.cos(cone_inclination_rad) - north_vector * np.sin(cone_inclination_rad)This adjusts the axis vector to account for the cone's inclination.
-
Computing the Base Center Vector: The
base_center_vectoris computed by moving along the inclined axis vector from the apex by a certain distance. This distance could be an input parameter or set to a default value. -
Generating the Circle Points: The circle points defining the base of the cone are generated in the plane perpendicular to the new
axis_vector. Thev_eastandv_northvectors are computed to be perpendicular to theaxis_vectorand are used to parameterize the circle.
By making these changes, the code now correctly accounts for the
cone_inclinationwhen generating the vertices of the cone's base. -