Ocean
TestVocabularyTree.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_TEST_TESTTACKING_TEST_VOCABULARY_TREE_H
9 #define META_OCEAN_TEST_TESTTACKING_TEST_VOCABULARY_TREE_H
10 
12 
14 
16 
17 #include <array>
18 
19 namespace Ocean
20 {
21 
22 namespace Test
23 {
24 
25 namespace TestTracking
26 {
27 
28 /**
29  * This class implements a test for the VocabularyTree class.
30  * @ingroup testtracking
31  */
32 class OCEAN_TEST_TRACKING_EXPORT TestVocabularyTree
33 {
34  public:
35 
36  /**
37  * Definition of individual descriptor types.
38  */
40  {
41  /// Descriptor based on binary data.
43  /// Descriptor based on floats.
44  DT_FLOAT
45  };
46 
47  protected:
48 
49  /// The number of elements/bytes of a binary descriptor.
50  static constexpr unsigned int binaryDescriptorElements_ = 32u;
51 
52  /// The number of elements of a float descriptor.
53  static constexpr unsigned int floatDescriptorElements_ = 128u;
54 
55  /**
56  * Definition of a binary descriptor.
57  */
58  using BinaryDescriptor = std::array<uint8_t, binaryDescriptorElements_>;
59 
60  /**
61  * Definition of a float descriptor.
62  */
63  using FloatDescriptor = std::array<float, floatDescriptorElements_>;
64 
65  /**
66  * Helper class to determine data types for individual descriptor types.
67  * @tparam tDescriptorType The type of the descriptor to be sued
68  */
69  template <DescriptorType tDescriptorType>
70  class TypeHelper
71  {
72  // nothing to do here
73  };
74 
75  public:
76 
77  /**
78  * Invokes all existing tests for VocabularyTree.
79  * @param testDuration Number of seconds for each test, with range (0, infinity)
80  * @param worker The worker object to distribute the computation
81  * @return True, if succeeded
82  */
83  static bool test(const double testDuration, Worker& worker);
84 
85  /**
86  * Tests the function determining the means for clusters of binary descriptors.
87  * @param testDuration Number of seconds for each test, with range (0, infinity)
88  * @param worker The worker object to distribute the computation
89  * @return True, if succeeded
90  */
91  static bool testDetermineClustersMeanForBinaryDescriptor(const double testDuration, Worker& worker);
92 
93  /**
94  * Tests the function determining the means for clusters of float descriptors.
95  * @param testDuration Number of seconds for each test, with range (0, infinity)
96  * @param worker The worker object to distribute the computation
97  * @return True, if succeeded
98  */
99  static bool testDetermineClustersMeanForFloatDescriptor(const double testDuration, Worker& worker);
100 
101  /**
102  * Tests the constructor.
103  * @param testDuration Number of seconds for each test, with range (0, infinity)
104  * @param worker The worker object to distribute the computation
105  * @return True, if succeeded
106  * @tparam tDescriptorType The descriptor type to be used
107  */
108  template <DescriptorType tDescriptorType>
109  static bool testConstructor(const double testDuration, Worker& worker);
110 
111  /**
112  * Tests manual matching accessing the leafs.
113  * @param testDuration Number of seconds for each test, with range (0, infinity)
114  * @param worker The worker object to distribute the computation
115  * @return True, if succeeded
116  * @tparam tDescriptorType The descriptor type to be used
117  */
118  template <DescriptorType tDescriptorType>
119  static bool testMatchingViaLeafs(const double testDuration, Worker& worker);
120 
121  /**
122  * Tests descriptor matching.
123  * @param testDuration Number of seconds for each test, with range (0, infinity)
124  * @param worker The worker object to distribute the computation
125  * @return True, if succeeded
126  * @tparam tDescriptorType The descriptor type to be used
127  */
128  template <DescriptorType tDescriptorType>
129  static bool testMatchingDescriptors(const double testDuration, Worker& worker);
130 
131  /**
132  * Tests descriptor matching with forest.
133  * @param testDuration Number of seconds for each test, with range (0, infinity)
134  * @param worker The worker object to distribute the computation
135  * @return True, if succeeded
136  * @tparam tDescriptorType The descriptor type to be used
137  */
138  template <DescriptorType tDescriptorType>
139  static bool testMatchingDescriptorsWithForest(const double testDuration, Worker& worker);
140 
141  protected:
142 
143  /**
144  * Separates the individual bits of binary descriptor into individual integer values.
145  * @param descriptor The binary descriptor to be separated
146  * @return The resulting integer values, one for each bit
147  */
149 };
150 
151 template <>
152 class OCEAN_TEST_TRACKING_EXPORT TestVocabularyTree::TypeHelper<TestVocabularyTree::DT_BINARY>
153 {
154  public:
155 
156  /**
157  * The readable name of the descriptor type.
158  */
159  static constexpr const char* name_ = "Binary";
160 
161  /**
162  * Definition of a descriptor data type.
163  */
165 
166  /**
167  * Definition of a vector holding descriptors.
168  */
169  using Descriptors = std::vector<Descriptor>;
170 
171  /**
172  * Definition of the data type for the distance between two descriptors.
173  */
174  using DistanceType = unsigned int;
175 
176  /**
177  * Determines the (hamming) distance between two binary descriptors.
178  * @param descriptorA The first descriptor
179  * @param descriptorB The second descriptor
180  * @return The resulting distance
181  */
182  static DistanceType determineDistance(const BinaryDescriptor& descriptorA, const BinaryDescriptor& descriptorB);
183 
184  /**
185  * Randomizes a descriptor.
186  * @param descriptor The descriptor to be randomized
187  * @param randomGenerator The random generator object to be used
188  */
189  static void randomizeDescriptor(BinaryDescriptor& descriptor, RandomGenerator& randomGenerator);
190 
191  /**
192  * Applies a minor random modification to a given descriptor.
193  * @param descriptor The descriptor to modify
194  * @param randomGenerator The random generator object to be used
195  * @return The modified descriptor
196  */
197  static BinaryDescriptor modifyDescriptor(const BinaryDescriptor& descriptor, RandomGenerator& randomGenerator);
198 
199  /**
200  * Returns a bunch of descriptor epsilons which can be used for testing.
201  * @param numberEpsilons The number of epsilon value to return, with range [2, numberBits]
202  * @return The descriptor epsilons
203  */
204  static std::vector<DistanceType> descriptorEpsilons(const unsigned int numberEpsilons);
205 
206  /**
207  * Definition of the vocabulary tree data type.
208  */
210 
211  /**
212  * The function pointer to the cluster mean function.
213  */
214  static constexpr const VocabularyTree::ClustersMeanFunction clusterMeanFunction_ = &VocabularyTree::determineClustersMeanForBinaryDescriptor<binaryDescriptorElements_ * 8u>;
215 };
216 
217 template <>
218 class OCEAN_TEST_TRACKING_EXPORT TestVocabularyTree::TypeHelper<TestVocabularyTree::DT_FLOAT>
219 {
220  public:
221 
222  /**
223  * The readable name of the descriptor type.
224  */
225  static constexpr const char* name_ = "Float";
226 
227  /**
228  * Definition of a descriptor data type.
229  */
231 
232  /**
233  * Definition of a vector holding descriptors.
234  */
235  using Descriptors = std::vector<Descriptor>;
236 
237  /**
238  * Definition of the data type for the distance between two descriptors.
239  */
240  using DistanceType = float;
241 
242  /**
243  * Determines the square distance between two float descriptors.
244  * @param descriptorA The first descriptor
245  * @param descriptorB The second descriptor
246  * @return The resulting square distance
247  */
248  static float determineDistance(const FloatDescriptor& descriptorA, const FloatDescriptor& descriptorB);
249 
250  /**
251  * Randomizes a descriptor.
252  * @param descriptor The descriptor to be randomized
253  * @param randomGenerator The random generator object to be used
254  */
255  static void randomizeDescriptor(FloatDescriptor& descriptor, RandomGenerator& randomGenerator);
256 
257  /**
258  * Applies a minor random modification to a given descriptor.
259  * @param descriptor The descriptor to modify
260  * @param randomGenerator The random generator object to be used
261  * @return The modified descriptor
262  */
263  static FloatDescriptor modifyDescriptor(const FloatDescriptor& descriptor, RandomGenerator& randomGenerator);
264 
265  /**
266  * Returns a bunch of descriptor epsilons which can be used for testing.
267  * @param numberEpsilons The number of epsilon value to return, with range [2, numberBits]
268  * @return The descriptor epsilons
269  */
270  static std::vector<DistanceType> descriptorEpsilons(const unsigned int numberEpsilons);
271 
272  /**
273  * Definition of the vocabulary tree data type.
274  */
276 
277  /**
278  * The function pointer to the cluster mean function.
279  */
280  static constexpr const VocabularyTree::ClustersMeanFunction clusterMeanFunction_ = &VocabularyTree::determineClustersMeanForFloatDescriptor<floatDescriptorElements_>;
281 };
282 
283 }
284 
285 }
286 
287 }
288 
289 #endif // META_OCEAN_TEST_TESTTACKING_TEST_VOCABULARY_TREE_H
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
Helper class to determine data types for individual descriptor types.
Definition: TestVocabularyTree.h:71
This class implements a test for the VocabularyTree class.
Definition: TestVocabularyTree.h:33
std::vector< Descriptor > Descriptors
Definition of a vector holding descriptors.
Definition: TestVocabularyTree.h:169
static bool testDetermineClustersMeanForFloatDescriptor(const double testDuration, Worker &worker)
Tests the function determining the means for clusters of float descriptors.
static float determineDistance(const FloatDescriptor &descriptorA, const FloatDescriptor &descriptorB)
Determines the square distance between two float descriptors.
static FloatDescriptor modifyDescriptor(const FloatDescriptor &descriptor, RandomGenerator &randomGenerator)
Applies a minor random modification to a given descriptor.
static bool testMatchingDescriptors(const double testDuration, Worker &worker)
Tests descriptor matching.
static bool testConstructor(const double testDuration, Worker &worker)
Tests the constructor.
static void randomizeDescriptor(BinaryDescriptor &descriptor, RandomGenerator &randomGenerator)
Randomizes a descriptor.
std::array< float, floatDescriptorElements_ > FloatDescriptor
Definition of a float descriptor.
Definition: TestVocabularyTree.h:63
static BinaryDescriptor modifyDescriptor(const BinaryDescriptor &descriptor, RandomGenerator &randomGenerator)
Applies a minor random modification to a given descriptor.
static bool testMatchingViaLeafs(const double testDuration, Worker &worker)
Tests manual matching accessing the leafs.
static std::vector< DistanceType > descriptorEpsilons(const unsigned int numberEpsilons)
Returns a bunch of descriptor epsilons which can be used for testing.
static bool testMatchingDescriptorsWithForest(const double testDuration, Worker &worker)
Tests descriptor matching with forest.
static Indices32 separateBinaryDescriptor(const BinaryDescriptor &descriptor)
Separates the individual bits of binary descriptor into individual integer values.
static DistanceType determineDistance(const BinaryDescriptor &descriptorA, const BinaryDescriptor &descriptorB)
Determines the (hamming) distance between two binary descriptors.
std::array< uint8_t, binaryDescriptorElements_ > BinaryDescriptor
Definition of a binary descriptor.
Definition: TestVocabularyTree.h:58
static bool testDetermineClustersMeanForBinaryDescriptor(const double testDuration, Worker &worker)
Tests the function determining the means for clusters of binary descriptors.
static bool test(const double testDuration, Worker &worker)
Invokes all existing tests for VocabularyTree.
unsigned int DistanceType
Definition of the data type for the distance between two descriptors.
Definition: TestVocabularyTree.h:174
DescriptorType
Definition of individual descriptor types.
Definition: TestVocabularyTree.h:40
@ DT_BINARY
Descriptor based on binary data.
Definition: TestVocabularyTree.h:42
BinaryDescriptor Descriptor
Definition of a descriptor data type.
Definition: TestVocabularyTree.h:164
static void randomizeDescriptor(FloatDescriptor &descriptor, RandomGenerator &randomGenerator)
Randomizes a descriptor.
This class implements a Vocabulary Tree for feature descriptors.
Definition: VocabularyTree.h:223
TDescriptors(*)(const unsigned int numberClusters, const TDescriptor *treeDescriptors, const Index32 *descriptorIndices, const Index32 *clusterIndicesForDescriptors, const size_t numberDescriptorIndices, Worker *worker) ClustersMeanFunction
Definition of a function pointer allowing to determine the mean descriptors for individual clusters.
Definition: VocabularyTree.h:285
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15