File indexing completed on 2023-03-17 10:45:28
0001 #include "Utilities/Testing/interface/CppUnit_testdriver.icpp"
0002 #include "cppunit/extensions/HelperMacros.h"
0003
0004 class TestFKDTree : public CppUnit::TestFixture {
0005 CPPUNIT_TEST_SUITE(TestFKDTree);
0006 CPPUNIT_TEST(test2D);
0007 CPPUNIT_TEST(test3D);
0008 CPPUNIT_TEST_SUITE_END();
0009
0010 public:
0011
0012 void test2D();
0013 void test3D();
0014 };
0015
0016 CPPUNIT_TEST_SUITE_REGISTRATION(TestFKDTree);
0017
0018 #include "CommonTools/RecoAlgos/interface/FKDTree.h"
0019 #include "FWCore/Utilities/interface/Exception.h"
0020
0021 void TestFKDTree::test2D() {
0022 try {
0023 FKDTree<float, 2> tree;
0024 float minX = 0.2;
0025 float minY = 0.1;
0026 float maxX = 0.7;
0027 float maxY = 0.9;
0028 unsigned int numberOfPointsInTheBox = 1000;
0029 unsigned int numberOfPointsOutsideTheBox = 5000;
0030 std::vector<FKDPoint<float, 2> > points;
0031 std::vector<unsigned int> result;
0032
0033 FKDPoint<float, 2> minPoint(minX, minY);
0034 FKDPoint<float, 2> maxPoint(maxX, maxY);
0035 unsigned int id = 0;
0036
0037 for (unsigned int i = 0; i < numberOfPointsInTheBox; ++i) {
0038 float x = minX + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(maxX - minX));
0039
0040 float y = minY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(maxY - minY));
0041
0042 points.emplace_back(x, y, id);
0043 id++;
0044 }
0045
0046 for (unsigned int i = 0; i < numberOfPointsOutsideTheBox; ++i) {
0047 float x = static_cast<float>(rand()) / (static_cast<float>(RAND_MAX));
0048 float y;
0049 if (x <= maxX && x >= minX) {
0050 y = maxY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(1.f - maxY));
0051 if (y == maxY)
0052 y = 1.f;
0053 }
0054 points.emplace_back(x, y, id);
0055 id++;
0056 }
0057 tree.build(points);
0058 tree.search(minPoint, maxPoint, result);
0059 CPPUNIT_ASSERT_EQUAL((unsigned int)tree.size(), numberOfPointsInTheBox + numberOfPointsOutsideTheBox);
0060
0061 CPPUNIT_ASSERT_EQUAL((unsigned int)result.size(), numberOfPointsInTheBox);
0062 } catch (cms::Exception& e) {
0063 std::cerr << e.what() << std::endl;
0064 CPPUNIT_ASSERT(false);
0065 }
0066 }
0067 void TestFKDTree::test3D() {
0068 try {
0069 FKDTree<float, 3> tree;
0070 float minX = 0.2;
0071 float minY = 0.1;
0072 float minZ = 0.1;
0073 float maxX = 0.7;
0074 float maxY = 0.9;
0075 float maxZ = 0.3;
0076
0077 unsigned int numberOfPointsInTheBox = 1000;
0078 unsigned int numberOfPointsOutsideTheBox = 5000;
0079 std::vector<FKDPoint<float, 3> > points;
0080 std::vector<unsigned int> result;
0081
0082 FKDPoint<float, 3> minPoint(minX, minY, minZ);
0083 FKDPoint<float, 3> maxPoint(maxX, maxY, maxZ);
0084 unsigned int id = 0;
0085
0086 for (unsigned int i = 0; i < numberOfPointsInTheBox; ++i) {
0087 float x = minX + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(maxX - minX));
0088
0089 float y = minY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(maxY - minY));
0090 float z = minZ + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(maxZ - minZ));
0091
0092 points.emplace_back(x, y, z, id);
0093 id++;
0094 }
0095
0096 for (unsigned int i = 0; i < numberOfPointsOutsideTheBox; ++i) {
0097 float x = static_cast<float>(rand()) / (static_cast<float>(RAND_MAX));
0098 float y;
0099 if (x <= maxX && x >= minX) {
0100 y = maxY + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(1.f - maxY));
0101 if (y == maxY)
0102 y = 1.f;
0103 }
0104 float z = minZ + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX) / std::fabs(maxZ - minZ));
0105
0106 points.emplace_back(x, y, z, id);
0107 id++;
0108 }
0109 tree.build(points);
0110 tree.search(minPoint, maxPoint, result);
0111 CPPUNIT_ASSERT_EQUAL((unsigned int)tree.size(), numberOfPointsInTheBox + numberOfPointsOutsideTheBox);
0112 CPPUNIT_ASSERT_EQUAL((unsigned int)result.size(), numberOfPointsInTheBox);
0113
0114 } catch (cms::Exception& e) {
0115 std::cerr << e.what() << std::endl;
0116 CPPUNIT_ASSERT(false);
0117 }
0118 }