Mercurial Hosting > traffic-intelligence
comparison trajectorymanagement/src/HausdorffMetric.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 HAUSDORFFMETRIC_H_ | |
2 #define HAUSDORFFMETRIC_H_ | |
3 | |
4 #include "Metric.h" | |
5 | |
6 #include "TrajectoryExceptions.h" | |
7 | |
8 /** | |
9 * HausdorffMetric class. | |
10 * | |
11 * The Hausdorff metric measures the similarity between two trajectories. | |
12 */ | |
13 template<typename Tr, typename To> | |
14 class HausdorffMetric: public Metric<Tr, To> | |
15 { | |
16 public: | |
17 /** | |
18 * Compute similarity 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, To &result, unsigned int nbOfPoints = std::numeric_limits<unsigned int>::max()) | |
25 { | |
26 result = To(0); | |
27 | |
28 checkTrajectoryLength(a->size(), b->size()); | |
29 | |
30 To distAB = computeDistance(a, b); | |
31 To distBA = computeDistance(b, a); | |
32 | |
33 result = std::max(distAB, distBA); | |
34 } | |
35 | |
36 private: | |
37 /** | |
38 * Check the length of the trajectories. | |
39 * | |
40 * @param[in] a the size of the trajectory | |
41 * @param[in] b the size of the trajectory | |
42 */ | |
43 void checkTrajectoryLength(size_t a, size_t b) const | |
44 { | |
45 if (a == 0 || b == 0) | |
46 { | |
47 throw TrajectoryLengthErrorException(); | |
48 } | |
49 } | |
50 | |
51 /** | |
52 * Compute the maximum distance from the trajectory given as a first parameter to the other. | |
53 * | |
54 * @param[in] a input trajectory | |
55 * @param[in] b input trajectory | |
56 * @return distance between two trajectories | |
57 */ | |
58 To computeDistance(const Trajectory<Tr> *a, const Trajectory<Tr> *b) | |
59 { | |
60 To maxDist = -1; | |
61 for (unsigned ia = 0; ia < a->size(); ++ia) | |
62 { | |
63 To minDist = (To)norm(a->getPoint(ia) - b->getPoint(0)); | |
64 for (unsigned ib = 1; ib < b->size(); ++ib) | |
65 { | |
66 To dist = (To)norm(a->getPoint(ia) - b->getPoint(ib)); | |
67 if (dist < minDist) | |
68 { | |
69 minDist = dist; | |
70 } | |
71 } | |
72 | |
73 if (minDist > maxDist) | |
74 { | |
75 maxDist = minDist; | |
76 } | |
77 } | |
78 | |
79 return maxDist; | |
80 } | |
81 }; | |
82 | |
83 #endif /* HAUSDORFFMETRIC_H_ */ |