| 1 | /* |
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef RESONANCE_AUDIO_BASE_SPHERICAL_ANGLE_H_ |
| 18 | #define RESONANCE_AUDIO_BASE_SPHERICAL_ANGLE_H_ |
| 19 | |
| 20 | #include "base/misc_math.h" |
| 21 | |
| 22 | namespace vraudio { |
| 23 | |
| 24 | // Represents angular position on a sphere in terms of azimuth and elevation. |
| 25 | class SphericalAngle { |
| 26 | public: |
| 27 | // Constructs a spherical angle with the given azimuth and elevation. |
| 28 | SphericalAngle(float azimuth, float elevation); |
| 29 | |
| 30 | // Constructs a default spherical angle (azimuth = 0, elevation = 0). |
| 31 | SphericalAngle(); |
| 32 | |
| 33 | // Constructs a spherical angle from a given one. |
| 34 | SphericalAngle(const SphericalAngle& other); |
| 35 | |
| 36 | SphericalAngle& operator=(const SphericalAngle other); |
| 37 | |
| 38 | // Returns a spherical angle representation of given |world_position| (World |
| 39 | // Space). |
| 40 | // |
| 41 | // @param world_position 3D position in world space. |
| 42 | // @return Spherical angle that represents the |world_position|. |
| 43 | static SphericalAngle FromWorldPosition(const WorldPosition& world_position); |
| 44 | |
| 45 | // Returns a spherical angle from azimuth and elevation in degrees. |
| 46 | static SphericalAngle FromDegrees(float azimuth_degrees, |
| 47 | float elevation_degrees); |
| 48 | |
| 49 | // Returns another spherical angle with the same elevation but the azimuth |
| 50 | // sign flipped. |
| 51 | // |
| 52 | // @return Horizontally flipped version of the spherical angle. |
| 53 | SphericalAngle FlipAzimuth() const; |
| 54 | |
| 55 | // Returns the |WorldPosition| coordinates (World Space) on the unit sphere |
| 56 | // corresponding to this spherical angle. The transformation is |
| 57 | // defined as such: |
| 58 | // x = -cos(elevation) * sin(azimuth) |
| 59 | // y = sin(elevation) |
| 60 | // z = -cos(elevation) * cos(azimuth) |
| 61 | // |
| 62 | // @return 3D position in world space. |
| 63 | WorldPosition GetWorldPositionOnUnitSphere() const; |
| 64 | |
| 65 | // Returns the rotated version of the spherical angle using given |
| 66 | // |WorldRotation|. |
| 67 | // |
| 68 | // @param rotation Rotation to be applied to the spherical angle. |
| 69 | // @return Rotated version of the spherical angle. |
| 70 | SphericalAngle Rotate(const WorldRotation& rotation) const; |
| 71 | |
| 72 | void set_azimuth(float azimuth) { azimuth_ = azimuth; } |
| 73 | void set_elevation(float elevation) { elevation_ = elevation; } |
| 74 | |
| 75 | float azimuth() const { return azimuth_; } |
| 76 | float elevation() const { return elevation_; } |
| 77 | |
| 78 | bool operator==(const SphericalAngle& other) const; |
| 79 | |
| 80 | private: |
| 81 | float azimuth_; |
| 82 | float elevation_; |
| 83 | }; |
| 84 | |
| 85 | } // namespace vraudio |
| 86 | |
| 87 | #endif // RESONANCE_AUDIO_BASE_SPHERICAL_ANGLE_H_ |
| 88 | |