comparison trajectorymanagement/src/LCSMetric.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 LCSMETRIC_H_
2 #define LCSMETRIC_H_
3
4 #include "Metric.h"
5
6 /**
7 * LCSMetric class.
8 *
9 * The Longest Common Subsequence metric
10 * This class mesures :
11 * 1) the similarity (number of points in "common", ie relative to distance similarity threshold) ;
12 * 2) the normalized distance between two trajectories
13 */
14
15 template<typename Tr, typename To>
16 class LCSMetric: public Metric<Tr, To>
17 {
18 public:
19 /**
20 * Constructor.
21 */
22 LCSMetric() : similarityThreshold(0.0), eps(1e-6)
23 {
24 }
25
26 /**
27 * Set similarity threshold between two points.
28 *
29 * @param[in] similarityThreshold similarity threshold
30 */
31 bool setSimilarityThreshold(double similarityThreshold)
32 {
33 if (similarityThreshold >= 0.0)
34 {
35 this->similarityThreshold = similarityThreshold;
36 return true;
37 }
38 return false;
39 }
40
41 /**
42 * Set machine epsilon.
43 *
44 * @param[in] eps machine epsilon
45 */
46 bool setEps(double eps)
47 {
48 if (eps >= 0.0)
49 {
50 this->eps = eps;
51 return true;
52 }
53 return false;
54 }
55
56 /**
57 * Compute similarity between two trajectories.
58 *
59 * @param[in] a input trajectory
60 * @param[in] b input trajectory
61 * @param[out] result distance between two trajectories
62 */
63 void distance(const Trajectory<Tr> *a, const Trajectory<Tr> *b, To &result, unsigned int nbOfPoints = std::numeric_limits<unsigned int>::max())
64 {
65 result = To(0);
66 unsigned int LCSS = 0;
67 similarity(a,b, LCSS);
68 unsigned int min_size = min(a->size(),b->size());
69 result = 1 - double(LCSS/min_size);
70 }
71
72 /**
73 * Compute similarity between two trajectories.
74 *
75 * @param[in] a input trajectory
76 * @param[in] b input trajectory
77 * @param[out] result similarity between two trajectories
78 */
79 void similarity(const Trajectory<Tr> *a, const Trajectory<Tr> *b, unsigned int &result)
80 {
81 unsigned int LCS[a->size() + 1][b->size() + 1];
82
83 { //initialisation
84 for (unsigned int i = 0; i <= a->size(); ++i)
85 {
86 LCS[i][0] = 0;
87 }
88
89 for (unsigned int j = 0; j <= b->size(); ++j)
90 {
91 LCS[0][j] = 0;
92 }
93 }
94
95 { //algorithm
96 for (unsigned int i = 1; i <= a->size(); ++i)
97 {
98 for (unsigned int j = 1; j <= b->size(); ++j)
99 {
100 cv::Point3_<typeof(static_cast<Tr>(a->getPoint(i-1)).x)> p(a->getPoint(i - 1) - b->getPoint(j - 1));
101 double distance = sqrt(pow(p.x, 2) + pow(p.y, 2) + pow(p.z, 2)); // il faudrait généraliser
102 if (distance <= similarityThreshold + eps)
103 { //a[i] == b[j]
104 LCS[i][j] = LCS[i - 1][j - 1] + 1;
105 }
106 else
107 {
108 LCS[i][j] = std::max(LCS[i - 1][j], LCS[i][j - 1]);
109 }
110 }
111 }
112 }
113
114 result = LCS[a->size()][b->size()];
115 }
116
117 private:
118 /**
119 * Similarity threshold between two points.
120 */
121 double similarityThreshold;
122
123 /**
124 * Machine epsilon.
125 */
126 double eps;
127
128 };
129
130 #endif /* LCSMETRIC_H_ */