| 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 | #include "base/spherical_angle.h" |
| 18 | |
| 19 | #include <cmath> |
| 20 | |
| 21 | #include "base/constants_and_types.h" |
| 22 | |
| 23 | namespace vraudio { |
| 24 | |
| 25 | SphericalAngle::SphericalAngle(float azimuth, float elevation) |
| 26 | : azimuth_(azimuth), elevation_(elevation) {} |
| 27 | |
| 28 | SphericalAngle::SphericalAngle() : SphericalAngle(0.0f, 0.0f) {} |
| 29 | |
| 30 | SphericalAngle::SphericalAngle(const SphericalAngle& other) |
| 31 | : azimuth_(other.azimuth_), elevation_(other.elevation_) {} |
| 32 | |
| 33 | SphericalAngle& SphericalAngle::operator=(const SphericalAngle other) { |
| 34 | if (&other == this) { |
| 35 | return *this; |
| 36 | } |
| 37 | this->azimuth_ = other.azimuth_; |
| 38 | this->elevation_ = other.elevation_; |
| 39 | return *this; |
| 40 | } |
| 41 | |
| 42 | SphericalAngle SphericalAngle::FromWorldPosition( |
| 43 | const WorldPosition& world_position) { |
| 44 | return SphericalAngle( |
| 45 | std::atan2(y: -world_position[0], x: -world_position[2]), |
| 46 | std::atan2(y: world_position[1], |
| 47 | x: std::sqrt(x: world_position[0] * world_position[0] + |
| 48 | world_position[2] * world_position[2]))); |
| 49 | } |
| 50 | |
| 51 | SphericalAngle SphericalAngle::FromDegrees(float azimuth_degrees, |
| 52 | float elevation_degrees) { |
| 53 | return SphericalAngle(azimuth_degrees * kRadiansFromDegrees, |
| 54 | elevation_degrees * kRadiansFromDegrees); |
| 55 | } |
| 56 | |
| 57 | SphericalAngle SphericalAngle::FlipAzimuth() const { |
| 58 | return SphericalAngle(-azimuth_, elevation_); |
| 59 | } |
| 60 | |
| 61 | WorldPosition SphericalAngle::GetWorldPositionOnUnitSphere() const { |
| 62 | return WorldPosition(-std::cos(x: elevation_) * std::sin(x: azimuth_), |
| 63 | std::sin(x: elevation_), |
| 64 | -std::cos(x: elevation_) * std::cos(x: azimuth_)); |
| 65 | } |
| 66 | |
| 67 | SphericalAngle SphericalAngle::Rotate(const WorldRotation& rotation) const { |
| 68 | const WorldPosition original_world_position = GetWorldPositionOnUnitSphere(); |
| 69 | const WorldPosition rotated_world_position = |
| 70 | rotation * original_world_position; |
| 71 | return FromWorldPosition(world_position: rotated_world_position); |
| 72 | } |
| 73 | |
| 74 | bool SphericalAngle::operator==(const SphericalAngle& other) const { |
| 75 | return (azimuth_ == other.azimuth_) && (elevation_ == other.elevation_); |
| 76 | } |
| 77 | |
| 78 | } // namespace vraudio |
| 79 | |