Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include "Benchmark.h"
  2. #include <chrono>
  3.  
  4. uint32_t Benchmark::n = 1000;
  5.  
  6. void Benchmark::Run(uint32_t n)
  7. {
  8. Benchmark::n = n;
  9. std::vector<SimpleVector> vectors = GenerateRandomVectors();
  10. auto PCLOctree = GeneratePCLOctree(vectors);
  11. auto COGSOctree = GenerateCOGSOctree(vectors);
  12.  
  13. SimpleVector search_location;
  14. search_location.x = static_cast<float>(rand() % 1000) / 100.0f;
  15. search_location.y = static_cast<float>(rand() % 1000) / 100.0f;
  16. search_location.z = static_cast<float>(rand() % 1000) / 100.0f;
  17.  
  18. // PCLOctree Radius Search
  19. auto start = std::chrono::high_resolution_clock::now();
  20. uint32_t pcl_points = PCLOctreeRadius(PCLOctree, search_location, 1);
  21. auto stop = std::chrono::high_resolution_clock::now();
  22. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
  23. std::cout << "PCLOctree Radius Search took: " << duration.count() << " microseconds" << std::endl;
  24. std::cout << "PCLOctree Radius Search found: " << pcl_points << " points" << std::endl;
  25.  
  26. // COGSOctree Radius Search
  27. start = std::chrono::high_resolution_clock::now();
  28. uint32_t cogs_points = COGSOctreeRadius(COGSOctree, search_location, 1);
  29. stop = std::chrono::high_resolution_clock::now();
  30. duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
  31. std::cout << "COGSOctree Radius Search took: " << duration.count() << " microseconds" << std::endl;
  32. std::cout << "COGSOctree Radius Search found: " << cogs_points << " points" << std::endl;
  33.  
  34. // Naive Radius Search
  35. start = std::chrono::high_resolution_clock::now();
  36. uint32_t naive_points = NaiveRadius(vectors, search_location, 1);
  37. stop = std::chrono::high_resolution_clock::now();
  38. duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
  39. std::cout << "Naive Radius Search took: " << duration.count() << " microseconds" << std::endl;
  40. std::cout << "Naive Radius Search found: " << naive_points << " points" << std::endl;
  41. }
  42.  
  43. std::vector<SimpleVector> Benchmark::GenerateRandomVectors()
  44. {
  45. std::vector<SimpleVector> vectors;
  46. for (uint32_t i = 0; i < n; ++i)
  47. {
  48. SimpleVector vector;
  49. vector.x = static_cast<float>(rand() % 1000) / 100.0f;
  50. vector.y = static_cast<float>(rand() % 1000) / 100.0f;
  51. vector.z = static_cast<float>(rand() % 1000) / 100.0f;
  52. vectors.push_back(vector);
  53. }
  54. return vectors;
  55. }
  56.  
  57. pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> Benchmark::GeneratePCLOctree(std::vector<SimpleVector> &points)
  58. {
  59. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
  60. cloud->width = n;
  61. cloud->height = 1;
  62. cloud->points.resize(cloud->width * cloud->height);
  63. for (uint32_t i = 0; i < n; ++i)
  64. {
  65. cloud->points[i].x = points[i].x;
  66. cloud->points[i].y = points[i].y;
  67. cloud->points[i].z = points[i].z;
  68. }
  69. pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree(64);
  70. octree.setInputCloud(cloud);
  71. octree.addPointsFromInputCloud();
  72. return octree;
  73. }
  74.  
  75. cogs::Octree Benchmark::GenerateCOGSOctree(std::vector<SimpleVector> &points)
  76. {
  77. cogs::PointCloud pc;
  78. pc.Resize(n);
  79. glm::vec3 *v = pc.GetPositions();
  80. for (int i = 0; i < n; i++, v++)
  81. {
  82. v->x = points[i].x;
  83. v->y = points[i].y;
  84. v->z = points[i].z;
  85. }
  86. cogs::Octree octree(pc);
  87. return octree;
  88. }
  89.  
  90. uint32_t Benchmark::PCLOctreeRadius(pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> &octree, SimpleVector search_location, float_t radius)
  91. {
  92. pcl::PointXYZ location;
  93. location.x = search_location.x;
  94. location.y = search_location.y;
  95. location.z = search_location.z;
  96. std::vector<int> points_indices;
  97. std::vector<float> points_distances;
  98. octree.radiusSearch(location, radius, points_indices, points_distances);
  99. return points_indices.size();
  100. }
  101.  
  102. uint32_t Benchmark::COGSOctreeRadius(cogs::Octree &octree, SimpleVector search_location, float_t radius)
  103. {
  104. glm::vec3 location(search_location.x, search_location.y, search_location.z);
  105. std::vector <uint32_t> points_indices = octree.GetPointsInRadius(location, radius);
  106. return points_indices.size();
  107. }
  108.  
  109. uint32_t Benchmark::NaiveRadius(std::vector<SimpleVector> &points, SimpleVector search_location, float_t radius)
  110. {
  111. std::vector <uint32_t> points_indices;
  112. for (uint32_t i = 0; i < points.size(); i++)
  113. {
  114. float distance = std::sqrt(
  115. std::pow(points[i].x - search_location.x, 2) +
  116. std::pow(points[i].y - search_location.y, 2) +
  117. std::pow(points[i].z - search_location.z, 2));
  118. if (distance < radius)
  119. {
  120. points_indices.push_back(i);
  121. }
  122. }
  123. return points_indices.size();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement