| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | // Utility functions for 3D coordinates. |
| | | 4 | | // In Cartesian coordinates, the x-axis points right, the y-axis points up, and the z-axis points |
| | | 5 | | // forward. The coordinates are given by (x, y, z). |
| | | 6 | | // In spherical coordinates, the azimuth is measured in degrees from the z-axis clockwise to the |
| | | 7 | | // x-axis, and elevation is measured in degrees from the x-z plane up to the y-axis. The coordinates |
| | | 8 | | // are given by (r, azimuth, elevation). |
| | | 9 | | // In cylindrical coordinates, the azimuth is measured in degrees from the z-axis clockwise to the |
| | | 10 | | // x-axis. The coordinates are given by (r, azimuth, height). |
| | | 11 | | public static class Coordinates3 { |
| | 0 | 12 | | public static Vector3 ConvertCartesianToSpherical(in Vector3 cartesian) { |
| | 0 | 13 | | float r = cartesian.magnitude; |
| | 0 | 14 | | float azimuth = Mathf.Atan2(cartesian.x, cartesian.z) * Mathf.Rad2Deg; |
| | 0 | 15 | | float elevation = Mathf.Atan2(cartesian.y, Mathf.Sqrt(cartesian.x * cartesian.x + |
| | | 16 | | cartesian.z * cartesian.z)) * |
| | | 17 | | Mathf.Rad2Deg; |
| | 0 | 18 | | return new Vector3(r, azimuth, elevation); |
| | 0 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | public static Vector3 ConvertCartesianToSpherical(float x, float y, float z) { |
| | 0 | 22 | | return ConvertCartesianToSpherical(new Vector3(x, y, z)); |
| | 0 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | public static Vector3 ConvertSphericalToCartesian(in Vector3 spherical) { |
| | 0 | 26 | | float y = spherical.x * Mathf.Sin(spherical.z * Mathf.Deg2Rad); |
| | 0 | 27 | | float x = spherical.x * Mathf.Cos(spherical.z * Mathf.Deg2Rad) * |
| | | 28 | | Mathf.Sin(spherical.y * Mathf.Deg2Rad); |
| | 0 | 29 | | float z = spherical.x * Mathf.Cos(spherical.z * Mathf.Deg2Rad) * |
| | | 30 | | Mathf.Cos(spherical.y * Mathf.Deg2Rad); |
| | 0 | 31 | | return new Vector3(x, y, z); |
| | 0 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | public static Vector3 ConvertSphericalToCartesian(float r, float azimuth, float elevation) { |
| | 0 | 35 | | return ConvertSphericalToCartesian(new Vector3(r, azimuth, elevation)); |
| | 0 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | public static Vector3 ConvertCartesianToCylindrical(in Vector3 cartesian) { |
| | 0 | 39 | | float r = Mathf.Sqrt(cartesian.x * cartesian.x + cartesian.z * cartesian.z); |
| | 0 | 40 | | float azimuth = Mathf.Atan2(cartesian.x, cartesian.z) * Mathf.Rad2Deg; |
| | 0 | 41 | | float height = cartesian.y; |
| | 0 | 42 | | return new Vector3(r, azimuth, height); |
| | 0 | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | public static Vector3 ConvertCartesianToCylindrical(float x, float y, float z) { |
| | 0 | 46 | | return ConvertCartesianToCylindrical(new Vector3(x, y, z)); |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | public static Vector3 ConvertCylindricalToCartesian(in Vector3 cylindrical) { |
| | 0 | 50 | | float y = cylindrical.z; |
| | 0 | 51 | | float x = cylindrical.x * Mathf.Sin(cylindrical.y * Mathf.Deg2Rad); |
| | 0 | 52 | | float z = cylindrical.x * Mathf.Cos(cylindrical.y * Mathf.Deg2Rad); |
| | 0 | 53 | | return new Vector3(x, y, z); |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | public static Vector3 ConvertCylindricalToCartesian(float r, float azimuth, float height) { |
| | 0 | 57 | | return ConvertCylindricalToCartesian(new Vector3(r, azimuth, height)); |
| | 0 | 58 | | } |
| | | 59 | | |
| | 5758 | 60 | | public static Vector3 FromProto(Simulation.CartesianCoordinates coordinates) { |
| | 5758 | 61 | | return new Vector3(coordinates?.X ?? 0, coordinates?.Y ?? 0, coordinates?.Z ?? 0); |
| | 5758 | 62 | | } |
| | | 63 | | |
| | 1910 | 64 | | public static Simulation.CartesianCoordinates ToProto(in Vector3 cartesian) { |
| | 1910 | 65 | | return new Simulation.CartesianCoordinates() { |
| | | 66 | | X = cartesian.x, |
| | | 67 | | Y = cartesian.y, |
| | | 68 | | Z = cartesian.z, |
| | | 69 | | }; |
| | 1910 | 70 | | } |
| | | 71 | | } |