1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "base/spherical_angle.h"
18
19#include <cmath>
20
21#include "base/constants_and_types.h"
22
23namespace vraudio {
24
25SphericalAngle::SphericalAngle(float azimuth, float elevation)
26 : azimuth_(azimuth), elevation_(elevation) {}
27
28SphericalAngle::SphericalAngle() : SphericalAngle(0.0f, 0.0f) {}
29
30SphericalAngle::SphericalAngle(const SphericalAngle& other)
31 : azimuth_(other.azimuth_), elevation_(other.elevation_) {}
32
33SphericalAngle& 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
42SphericalAngle 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
51SphericalAngle SphericalAngle::FromDegrees(float azimuth_degrees,
52 float elevation_degrees) {
53 return SphericalAngle(azimuth_degrees * kRadiansFromDegrees,
54 elevation_degrees * kRadiansFromDegrees);
55}
56
57SphericalAngle SphericalAngle::FlipAzimuth() const {
58 return SphericalAngle(-azimuth_, elevation_);
59}
60
61WorldPosition 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
67SphericalAngle 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
74bool SphericalAngle::operator==(const SphericalAngle& other) const {
75 return (azimuth_ == other.azimuth_) && (elevation_ == other.elevation_);
76}
77
78} // namespace vraudio
79

source code of qtmultimedia/src/3rdparty/resonance-audio/resonance_audio/base/spherical_angle.cc