comparison trajectorymanagement/src/CanberraMetric.h @ 1159:e1e7acef8eab

moved trajectory management library into Traffic Intelligence
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 22 Feb 2021 22:09:35 -0500
parents
children
comparison
equal deleted inserted replaced
1158:7eb972942f22 1159:e1e7acef8eab
1 #ifndef CANBERRAMETRIC_H_
2 #define CANBERRAMETRIC_H_
3
4 #include "Metric.h"
5
6 #include "TrajectoryExceptions.h"
7
8 /**
9 * CanberraMetric class.
10 *
11 * The Canberra metric measures the similarity between two trajectories.
12 */
13 template<typename Tr>
14 class CanberraMetric: public Metric<Tr, double>
15 {
16 public:
17 /**
18 * Compute distance between two trajectories.
19 *
20 * @param[in] a input trajectory
21 * @param[in] b input trajectory
22 * @param[out] result distance between two trajectories
23 */
24 void distance(const Trajectory<Tr> *a, const Trajectory<Tr> *b, double &result, unsigned int nbOfPoints = std::numeric_limits<unsigned int>::max())
25 {
26 result = double(0);
27
28 if (nbOfPoints == std::numeric_limits<unsigned int>::max())
29 checkTrajectoryLength(a->size(), b->size());
30
31 double diff = double(0);
32 for (unsigned int i = 0; i < a->size() and i < nbOfPoints; ++i)
33 {
34 cv::Point3_<typeof(static_cast<Tr>(a->getPoint(i)).x)> A(a->getPoint(i));
35 cv::Point3_<typeof(static_cast<Tr>(a->getPoint(i)).x)> B(b->getPoint(i));
36 diff += calculate(A.x, B.x);
37 diff += calculate(A.y, B.y);
38 diff += calculate(A.z, B.z);
39 }
40
41 result = diff;
42 }
43
44 private:
45 /**
46 * Check the length of the trajectories.
47 *
48 * @param[in] a the size of the trajectory
49 * @param[in] b the size of the trajectory
50 */
51 void checkTrajectoryLength(size_t a, size_t b) const
52 {
53 if (a != b || a == 0)
54 {
55 throw TrajectoryLengthErrorException();
56 }
57 }
58
59 /**
60 * Compute similarity between two values.
61 *
62 * @param a first value
63 * @param b second value
64 * @return result
65 */
66 double calculate(double a, double b)
67 {
68 double result = std::abs(a - b);
69
70 a = std::fabs(a);
71 b = std::fabs(b);
72
73 a += b;
74
75 if (a == 0)
76 {
77 return 0;
78 }
79
80 result /= a;
81
82 return result;
83 }
84 };
85
86 #endif /* CANBERRAMETRIC_H_ */