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/misc_math.h" |
18 | |
19 | namespace vraudio { |
20 | |
21 | WorldPosition::WorldPosition() { setZero(); } |
22 | |
23 | WorldRotation::WorldRotation() { setIdentity(); } |
24 | |
25 | bool LinearLeastSquareFitting(const std::vector<float>& x_array, |
26 | const std::vector<float>& y_array, float* slope, |
27 | float* intercept, float* r_squared) { |
28 | // The array sizes must agree. |
29 | if (x_array.size() != y_array.size()) { |
30 | return false; |
31 | } |
32 | |
33 | // At least two points are needed to fit a line. |
34 | if (x_array.size() < 2) { |
35 | return false; |
36 | } |
37 | |
38 | float x_sum = 0.0f; |
39 | float y_sum = 0.0f; |
40 | float x_square_sum = 0.0f; |
41 | float xy_sum = 0.0f; |
42 | |
43 | for (size_t i = 0; i < x_array.size(); ++i) { |
44 | const float x = x_array[i]; |
45 | const float y = y_array[i]; |
46 | x_sum += x; |
47 | y_sum += y; |
48 | x_square_sum += x * x; |
49 | xy_sum += x * y; |
50 | } |
51 | |
52 | const float n_inverse = 1.0f / static_cast<float>(x_array.size()); |
53 | const float x_mean = x_sum * n_inverse; |
54 | const float y_mean = y_sum * n_inverse; |
55 | const float x_square_mean = x_square_sum * n_inverse; |
56 | const float xy_mean = xy_sum * n_inverse; |
57 | const float x_mean_square = x_mean * x_mean; |
58 | |
59 | // Prevent division by zero, which means a vertical line and the slope is |
60 | // infinite. |
61 | if (x_square_mean == x_mean_square) { |
62 | return false; |
63 | } |
64 | |
65 | *slope = (xy_mean - x_mean * y_mean) / (x_square_mean - x_mean_square); |
66 | *intercept = y_mean - *slope * x_mean; |
67 | |
68 | // Compute the coefficient of determination. |
69 | float total_sum_of_squares = 0.0f; |
70 | float residual_sum_of_squares = 0.0f; |
71 | for (size_t i = 0; i < x_array.size(); ++i) { |
72 | const float y_i = y_array[i]; |
73 | total_sum_of_squares += (y_i - y_mean) * (y_i - y_mean); |
74 | const float y_fit = *slope * x_array[i] + *intercept; |
75 | residual_sum_of_squares += (y_fit - y_i) * (y_fit - y_i); |
76 | } |
77 | |
78 | if (total_sum_of_squares == 0.0f) { |
79 | if (residual_sum_of_squares == 0.0f) { |
80 | // A special case where all y's are equal, where the |r_squared| should |
81 | // be 1.0, and the line is a perfectly horizontal line. |
82 | *r_squared = 1.0f; |
83 | return true; |
84 | } else { |
85 | // Division by zero. |
86 | return false; |
87 | } |
88 | } |
89 | |
90 | *r_squared = 1.0f - residual_sum_of_squares / total_sum_of_squares; |
91 | return true; |
92 | } |
93 | |
94 | } // namespace vraudio |
95 | |