Autoware.Auto
mahalanobis_distance.hpp
Go to the documentation of this file.
1 // Copyright 2021 Apex.AI, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HELPER_FUNCTIONS__MAHALANOBIS_DISTANCE_HPP_
16 #define HELPER_FUNCTIONS__MAHALANOBIS_DISTANCE_HPP_
17 
18 #include <Eigen/Cholesky>
19 
20 namespace autoware
21 {
22 namespace common
23 {
24 namespace helper_functions
25 {
33 template<typename T, std::int32_t kNumOfStates>
35  const Eigen::Matrix<T, kNumOfStates, 1> & sample,
36  const Eigen::Matrix<T, kNumOfStates, 1> & mean,
37  const Eigen::Matrix<T, kNumOfStates, kNumOfStates> & covariance_factor
38 )
39 {
40  // This is equivalent to the squared Mahalanobis distance of the form: diff.T * C.inv() * diff
41  // Instead of the covariance matrix C we have its lower-triangular factor L, such that C = L * L.T
42  // squared_mahalanobis_distance = diff.T * C.inv() * diff
43  // = diff.T * (L * L.T).inv() * diff
44  // = diff.T * L.T.inv() * L.inv() * diff
45  // = (L.inv() * diff).T * (L.inv() * diff)
46  // this allows us to efficiently find the squared Mahalanobis distance using (L.inv() * diff),
47  // which can be found as a solution to: L * x = diff.
48  const auto diff = sample - mean;
49  const auto x = covariance_factor.ldlt().solve(diff);
50  return x.transpose() * x;
51 }
52 
60 template<typename T, std::int32_t kNumOfStates>
62  const Eigen::Matrix<T, kNumOfStates, 1> & sample,
63  const Eigen::Matrix<T, kNumOfStates, 1> & mean,
64  const Eigen::Matrix<T, kNumOfStates, kNumOfStates> & covariance_factor
65 )
66 {
67  return sqrtf(calculate_squared_mahalanobis_distance(sample, mean, covariance_factor));
68 }
69 } // namespace helper_functions
70 } // namespace common
71 } // namespace autoware
72 
73 #endif // HELPER_FUNCTIONS__MAHALANOBIS_DISTANCE_HPP_
float float32_t
Definition: types.hpp:36
types::float32_t calculate_mahalanobis_distance(const Eigen::Matrix< T, kNumOfStates, 1 > &sample, const Eigen::Matrix< T, kNumOfStates, 1 > &mean, const Eigen::Matrix< T, kNumOfStates, kNumOfStates > &covariance_factor)
Calculate mahalanobis distance.
Definition: mahalanobis_distance.hpp:61
DifferentialState x
Definition: kinematic_bicycle.snippet.hpp:28
void sample(const Trajectory &in, Trajectory &out, std::chrono::nanoseconds period, SlerpF slerp_fn)
Sample a trajectory using interpolation; does not extrapolate.
Definition: motion_common.hpp:194
types::float32_t calculate_squared_mahalanobis_distance(const Eigen::Matrix< T, kNumOfStates, 1 > &sample, const Eigen::Matrix< T, kNumOfStates, 1 > &mean, const Eigen::Matrix< T, kNumOfStates, kNumOfStates > &covariance_factor)
Calculate square of mahalanobis distance.
Definition: mahalanobis_distance.hpp:34
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24