DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

// MSVC++ requires this to be set before any other includes to get M_PI.
#define _USE_MATH_DEFINES

#include "modules/audio_processing/beamformer/array_util.h"

#include <math.h>
#include <vector>

#include "test/gtest.h"

namespace webrtc {

bool operator==(const Point& lhs, const Point& rhs) {
  return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
}

TEST(ArrayUtilTest, PairDirection) {
  EXPECT_EQ(Point(1.f, 2.f, 3.f),
            PairDirection(Point(0.f, 0.f, 0.f), Point(1.f, 2.f, 3.f)));
  EXPECT_EQ(Point(-1.f, -2.f, -3.f),
            PairDirection(Point(1.f, 2.f, 3.f), Point(0.f, 0.f, 0.f)));
  EXPECT_EQ(Point(0.f, 0.f, 0.f),
            PairDirection(Point(1.f, 0.f, 0.f), Point(1.f, 0.f, 0.f)));
  EXPECT_EQ(Point(-1.f, 2.f, 0.f),
            PairDirection(Point(1.f, 0.f, 0.f), Point(0.f, 2.f, 0.f)));
  EXPECT_EQ(Point(-4.f, 4.f, -4.f),
            PairDirection(Point(1.f, -2.f, 3.f), Point(-3.f, 2.f, -1.f)));
}

TEST(ArrayUtilTest, DotProduct) {
  EXPECT_FLOAT_EQ(0.f, DotProduct(Point(0.f, 0.f, 0.f), Point(1.f, 2.f, 3.f)));
  EXPECT_FLOAT_EQ(0.f, DotProduct(Point(1.f, 0.f, 2.f), Point(0.f, 3.f, 0.f)));
  EXPECT_FLOAT_EQ(0.f, DotProduct(Point(1.f, 1.f, 0.f), Point(1.f, -1.f, 0.f)));
  EXPECT_FLOAT_EQ(2.f, DotProduct(Point(1.f, 0.f, 0.f), Point(2.f, 0.f, 0.f)));
  EXPECT_FLOAT_EQ(-6.f,
                  DotProduct(Point(-2.f, 0.f, 0.f), Point(3.f, 0.f, 0.f)));
  EXPECT_FLOAT_EQ(-10.f,
                  DotProduct(Point(1.f, -2.f, 3.f), Point(-3.f, 2.f, -1.f)));
}

TEST(ArrayUtilTest, CrossProduct) {
  EXPECT_EQ(Point(0.f, 0.f, 0.f),
            CrossProduct(Point(0.f, 0.f, 0.f), Point(1.f, 2.f, 3.f)));
  EXPECT_EQ(Point(0.f, 0.f, 1.f),
            CrossProduct(Point(1.f, 0.f, 0.f), Point(0.f, 1.f, 0.f)));
  EXPECT_EQ(Point(1.f, 0.f, 0.f),
            CrossProduct(Point(0.f, 1.f, 0.f), Point(0.f, 0.f, 1.f)));
  EXPECT_EQ(Point(0.f, -1.f, 0.f),
            CrossProduct(Point(1.f, 0.f, 0.f), Point(0.f, 0.f, 1.f)));
  EXPECT_EQ(Point(-4.f, -8.f, -4.f),
            CrossProduct(Point(1.f, -2.f, 3.f), Point(-3.f, 2.f, -1.f)));
}

TEST(ArrayUtilTest, AreParallel) {
  EXPECT_TRUE(AreParallel(Point(0.f, 0.f, 0.f), Point(1.f, 2.f, 3.f)));
  EXPECT_FALSE(AreParallel(Point(1.f, 0.f, 2.f), Point(0.f, 3.f, 0.f)));
  EXPECT_FALSE(AreParallel(Point(1.f, 2.f, 0.f), Point(1.f, -0.5f, 0.f)));
  EXPECT_FALSE(AreParallel(Point(1.f, -2.f, 3.f), Point(-3.f, 2.f, -1.f)));
  EXPECT_TRUE(AreParallel(Point(1.f, 0.f, 0.f), Point(2.f, 0.f, 0.f)));
  EXPECT_TRUE(AreParallel(Point(1.f, 2.f, 3.f), Point(-2.f, -4.f, -6.f)));
}

TEST(ArrayUtilTest, ArePerpendicular) {
  EXPECT_TRUE(ArePerpendicular(Point(0.f, 0.f, 0.f), Point(1.f, 2.f, 3.f)));
  EXPECT_TRUE(ArePerpendicular(Point(1.f, 0.f, 2.f), Point(0.f, 3.f, 0.f)));
  EXPECT_TRUE(ArePerpendicular(Point(1.f, 2.f, 0.f), Point(1.f, -0.5f, 0.f)));
  EXPECT_FALSE(ArePerpendicular(Point(1.f, -2.f, 3.f), Point(-3.f, 2.f, -1.f)));
  EXPECT_FALSE(ArePerpendicular(Point(1.f, 0.f, 0.f), Point(2.f, 0.f, 0.f)));
  EXPECT_FALSE(ArePerpendicular(Point(1.f, 2.f, 3.f), Point(-2.f, -4.f, -6.f)));
}

TEST(ArrayUtilTest, GetMinimumSpacing) {
  std::vector<Point> geometry;
  geometry.push_back(Point(0.f, 0.f, 0.f));
  geometry.push_back(Point(0.1f, 0.f, 0.f));
  EXPECT_FLOAT_EQ(0.1f, GetMinimumSpacing(geometry));
  geometry.push_back(Point(0.f, 0.05f, 0.f));
  EXPECT_FLOAT_EQ(0.05f, GetMinimumSpacing(geometry));
  geometry.push_back(Point(0.f, 0.f, 0.02f));
  EXPECT_FLOAT_EQ(0.02f, GetMinimumSpacing(geometry));
  geometry.push_back(Point(-0.003f, -0.004f, 0.02f));
  EXPECT_FLOAT_EQ(0.005f, GetMinimumSpacing(geometry));
}

TEST(ArrayUtilTest, GetDirectionIfLinear) {
  std::vector<Point> geometry;
  geometry.push_back(Point(0.f, 0.f, 0.f));
  geometry.push_back(Point(0.1f, 0.f, 0.f));
  EXPECT_TRUE(
      AreParallel(Point(1.f, 0.f, 0.f), *GetDirectionIfLinear(geometry)));
  geometry.push_back(Point(0.15f, 0.f, 0.f));
  EXPECT_TRUE(
      AreParallel(Point(1.f, 0.f, 0.f), *GetDirectionIfLinear(geometry)));
  geometry.push_back(Point(-0.2f, 0.f, 0.f));
  EXPECT_TRUE(
      AreParallel(Point(1.f, 0.f, 0.f), *GetDirectionIfLinear(geometry)));
  geometry.push_back(Point(0.05f, 0.f, 0.f));
  EXPECT_TRUE(
      AreParallel(Point(1.f, 0.f, 0.f), *GetDirectionIfLinear(geometry)));
  geometry.push_back(Point(0.1f, 0.1f, 0.f));
  EXPECT_FALSE(GetDirectionIfLinear(geometry));
  geometry.push_back(Point(0.f, 0.f, -0.2f));
  EXPECT_FALSE(GetDirectionIfLinear(geometry));
}

TEST(ArrayUtilTest, GetNormalIfPlanar) {
  std::vector<Point> geometry;
  geometry.push_back(Point(0.f, 0.f, 0.f));
  geometry.push_back(Point(0.1f, 0.f, 0.f));
  EXPECT_FALSE(GetNormalIfPlanar(geometry));
  geometry.push_back(Point(0.15f, 0.f, 0.f));
  EXPECT_FALSE(GetNormalIfPlanar(geometry));
  geometry.push_back(Point(0.1f, 0.2f, 0.f));
  EXPECT_TRUE(AreParallel(Point(0.f, 0.f, 1.f), *GetNormalIfPlanar(geometry)));
  geometry.push_back(Point(0.f, -0.15f, 0.f));
  EXPECT_TRUE(AreParallel(Point(0.f, 0.f, 1.f), *GetNormalIfPlanar(geometry)));
  geometry.push_back(Point(0.f, 0.1f, 0.2f));
  EXPECT_FALSE(GetNormalIfPlanar(geometry));
  geometry.push_back(Point(0.f, 0.f, -0.15f));
  EXPECT_FALSE(GetNormalIfPlanar(geometry));
  geometry.push_back(Point(0.1f, 0.2f, 0.f));
  EXPECT_FALSE(GetNormalIfPlanar(geometry));
}

TEST(ArrayUtilTest, GetArrayNormalIfExists) {
  std::vector<Point> geometry;
  geometry.push_back(Point(0.f, 0.f, 0.f));
  geometry.push_back(Point(0.1f, 0.f, 0.f));
  EXPECT_TRUE(
      AreParallel(Point(0.f, 1.f, 0.f), *GetArrayNormalIfExists(geometry)));
  geometry.push_back(Point(0.15f, 0.f, 0.f));
  EXPECT_TRUE(
      AreParallel(Point(0.f, 1.f, 0.f), *GetArrayNormalIfExists(geometry)));
  geometry.push_back(Point(0.1f, 0.f, 0.2f));
  EXPECT_TRUE(
      AreParallel(Point(0.f, 1.f, 0.f), *GetArrayNormalIfExists(geometry)));
  geometry.push_back(Point(0.f, 0.f, -0.1f));
  EXPECT_TRUE(
      AreParallel(Point(0.f, 1.f, 0.f), *GetArrayNormalIfExists(geometry)));
  geometry.push_back(Point(0.1f, 0.2f, 0.3f));
  EXPECT_FALSE(GetArrayNormalIfExists(geometry));
  geometry.push_back(Point(0.f, -0.1f, 0.f));
  EXPECT_FALSE(GetArrayNormalIfExists(geometry));
  geometry.push_back(Point(1.f, 0.f, -0.2f));
  EXPECT_FALSE(GetArrayNormalIfExists(geometry));
}

TEST(ArrayUtilTest, DegreesToRadians) {
  EXPECT_FLOAT_EQ(0.f, DegreesToRadians(0.f));
  EXPECT_FLOAT_EQ(static_cast<float>(M_PI) / 6.f, DegreesToRadians(30.f));
  EXPECT_FLOAT_EQ(-static_cast<float>(M_PI) / 4.f, DegreesToRadians(-45.f));
  EXPECT_FLOAT_EQ(static_cast<float>(M_PI) / 3.f, DegreesToRadians(60.f));
  EXPECT_FLOAT_EQ(-static_cast<float>(M_PI) / 2.f, DegreesToRadians(-90.f));
  EXPECT_FLOAT_EQ(2.f * static_cast<float>(M_PI) / 3.f,
                  DegreesToRadians(120.f));
  EXPECT_FLOAT_EQ(-3.f * static_cast<float>(M_PI) / 4.f,
                  DegreesToRadians(-135.f));
  EXPECT_FLOAT_EQ(5.f * static_cast<float>(M_PI) / 6.f,
                  DegreesToRadians(150.f));
  EXPECT_FLOAT_EQ(-static_cast<float>(M_PI), DegreesToRadians(-180.f));
}

TEST(ArrayUtilTest, RadiansToDegrees) {
  EXPECT_FLOAT_EQ(0.f, RadiansToDegrees(0.f));
  EXPECT_FLOAT_EQ(30.f, RadiansToDegrees(M_PI / 6.f));
  EXPECT_FLOAT_EQ(-45.f, RadiansToDegrees(-M_PI / 4.f));
  EXPECT_FLOAT_EQ(60.f, RadiansToDegrees(M_PI / 3.f));
  EXPECT_FLOAT_EQ(-90.f, RadiansToDegrees(-M_PI / 2.f));
  EXPECT_FLOAT_EQ(120.f, RadiansToDegrees(2.f * M_PI / 3.f));
  EXPECT_FLOAT_EQ(-135.f, RadiansToDegrees(-3.f * M_PI / 4.f));
  EXPECT_FLOAT_EQ(150.f, RadiansToDegrees(5.f * M_PI / 6.f));
  EXPECT_FLOAT_EQ(-180.f, RadiansToDegrees(-M_PI));
}

}  // namespace webrtc