OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/beamformer/array_util.h" | 11 #include "webrtc/modules/audio_processing/beamformer/array_util.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
| 19 namespace { |
| 20 |
| 21 const float kMaxDotProduct = 1e-6f; |
| 22 |
| 23 } // namespace |
19 | 24 |
20 float GetMinimumSpacing(const std::vector<Point>& array_geometry) { | 25 float GetMinimumSpacing(const std::vector<Point>& array_geometry) { |
21 RTC_CHECK_GT(array_geometry.size(), 1u); | 26 RTC_CHECK_GT(array_geometry.size(), 1u); |
22 float mic_spacing = std::numeric_limits<float>::max(); | 27 float mic_spacing = std::numeric_limits<float>::max(); |
23 for (size_t i = 0; i < (array_geometry.size() - 1); ++i) { | 28 for (size_t i = 0; i < (array_geometry.size() - 1); ++i) { |
24 for (size_t j = i + 1; j < array_geometry.size(); ++j) { | 29 for (size_t j = i + 1; j < array_geometry.size(); ++j) { |
25 mic_spacing = | 30 mic_spacing = |
26 std::min(mic_spacing, Distance(array_geometry[i], array_geometry[j])); | 31 std::min(mic_spacing, Distance(array_geometry[i], array_geometry[j])); |
27 } | 32 } |
28 } | 33 } |
29 return mic_spacing; | 34 return mic_spacing; |
30 } | 35 } |
31 | 36 |
| 37 Point PairDirection(const Point& a, const Point& b) { |
| 38 return {b.x() - a.x(), b.y() - a.y(), b.z() - a.z()}; |
| 39 } |
| 40 |
| 41 float DotProduct(const Point& a, const Point& b) { |
| 42 return a.x() * b.x() + a.y() * b.y() + a.z() * b.z(); |
| 43 } |
| 44 |
| 45 Point CrossProduct(const Point& a, const Point& b) { |
| 46 return {a.y() * b.z() - a.z() * b.y(), a.z() * b.x() - a.x() * b.z(), |
| 47 a.x() * b.y() - a.y() * b.x()}; |
| 48 } |
| 49 |
| 50 bool AreParallel(const Point& a, const Point& b) { |
| 51 Point cross_product = CrossProduct(a, b); |
| 52 return DotProduct(cross_product, cross_product) < kMaxDotProduct; |
| 53 } |
| 54 |
| 55 bool ArePerpendicular(const Point& a, const Point& b) { |
| 56 return std::abs(DotProduct(a, b)) < kMaxDotProduct; |
| 57 } |
| 58 |
| 59 Point IsGeometryLinear(const std::vector<Point>& array_geometry) { |
| 60 RTC_DCHECK_GT(array_geometry.size(), 1u); |
| 61 const Point first_pair_direction = |
| 62 PairDirection(array_geometry[0], array_geometry[1]); |
| 63 for (size_t i = 2u; i < array_geometry.size(); ++i) { |
| 64 const Point pair_direction = |
| 65 PairDirection(array_geometry[i - 1], array_geometry[i]); |
| 66 if (!AreParallel(first_pair_direction, pair_direction)) { |
| 67 return Point(0.f, 0.f, 0.f); |
| 68 } |
| 69 } |
| 70 return first_pair_direction; |
| 71 } |
| 72 |
| 73 Point IsGeometryPlanar(const std::vector<Point>& array_geometry) { |
| 74 RTC_DCHECK_GT(array_geometry.size(), 1u); |
| 75 const Point first_pair_direction = |
| 76 PairDirection(array_geometry[0], array_geometry[1]); |
| 77 Point pair_direction(0.f, 0.f, 0.f); |
| 78 size_t i = 2u; |
| 79 bool is_linear = true; |
| 80 for (; i < array_geometry.size() && is_linear; ++i) { |
| 81 pair_direction = PairDirection(array_geometry[i - 1], array_geometry[i]); |
| 82 if (!AreParallel(first_pair_direction, pair_direction)) { |
| 83 is_linear = false; |
| 84 } |
| 85 } |
| 86 if (is_linear) { |
| 87 return Point(0.f, 0.f, 0.f); |
| 88 } |
| 89 const Point normal_direction = |
| 90 CrossProduct(first_pair_direction, pair_direction); |
| 91 for (; i < array_geometry.size(); ++i) { |
| 92 pair_direction = PairDirection(array_geometry[i - 1], array_geometry[i]); |
| 93 if (!ArePerpendicular(normal_direction, pair_direction)) { |
| 94 return Point(0.f, 0.f, 0.f); |
| 95 } |
| 96 } |
| 97 return normal_direction; |
| 98 } |
| 99 |
| 100 Point GetArrayNormal(const std::vector<Point>& array_geometry) { |
| 101 const Point direction = IsGeometryLinear(array_geometry); |
| 102 if (DotProduct(direction, direction) > kMaxDotProduct) { |
| 103 return Point(direction.y(), -direction.x(), 0.f); |
| 104 } |
| 105 const Point normal = IsGeometryPlanar(array_geometry); |
| 106 if (DotProduct(normal, normal) > kMaxDotProduct && |
| 107 normal.z() < kMaxDotProduct) { |
| 108 return normal; |
| 109 } |
| 110 return Point(0.f, 0.f, 0.f); |
| 111 } |
| 112 |
| 113 Point AzimuthToPoint(float azimuth) { |
| 114 return Point(std::cos(azimuth), std::sin(azimuth), 0.f); |
| 115 } |
| 116 |
32 } // namespace webrtc | 117 } // namespace webrtc |
OLD | NEW |