benchmark_geodesic_grid.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <AP_gbenchmark.h>
  18. #include <AP_Math/AP_GeodesicGrid.h>
  19. static const Vector3f triangles[20][3] = {
  20. {{-M_GOLDEN, 1, 0}, {-1, 0,-M_GOLDEN}, {-M_GOLDEN,-1, 0}},
  21. {{-1, 0,-M_GOLDEN}, {-M_GOLDEN,-1, 0}, { 0,-M_GOLDEN,-1}},
  22. {{-M_GOLDEN,-1, 0}, { 0,-M_GOLDEN,-1}, { 0,-M_GOLDEN, 1}},
  23. {{-1, 0,-M_GOLDEN}, { 0,-M_GOLDEN,-1}, { 1, 0,-M_GOLDEN}},
  24. {{ 0,-M_GOLDEN,-1}, { 0,-M_GOLDEN, 1}, { M_GOLDEN,-1, 0}},
  25. {{ 0,-M_GOLDEN,-1}, { 1, 0,-M_GOLDEN}, { M_GOLDEN,-1, 0}},
  26. {{ M_GOLDEN,-1, 0}, { 1, 0,-M_GOLDEN}, { M_GOLDEN, 1, 0}},
  27. {{ 1, 0,-M_GOLDEN}, { M_GOLDEN, 1, 0}, { 0, M_GOLDEN,-1}},
  28. {{ 1, 0,-M_GOLDEN}, { 0, M_GOLDEN,-1}, {-1, 0,-M_GOLDEN}},
  29. {{ 0, M_GOLDEN,-1}, {-M_GOLDEN, 1, 0}, {-1, 0,-M_GOLDEN}},
  30. {{ M_GOLDEN,-1, 0}, { 1, 0, M_GOLDEN}, { M_GOLDEN, 1, 0}},
  31. {{ 1, 0, M_GOLDEN}, { M_GOLDEN, 1, 0}, { 0, M_GOLDEN, 1}},
  32. {{ M_GOLDEN, 1, 0}, { 0, M_GOLDEN, 1}, { 0, M_GOLDEN,-1}},
  33. {{ 1, 0, M_GOLDEN}, { 0, M_GOLDEN, 1}, {-1, 0, M_GOLDEN}},
  34. {{ 0, M_GOLDEN, 1}, { 0, M_GOLDEN,-1}, {-M_GOLDEN, 1, 0}},
  35. {{ 0, M_GOLDEN, 1}, {-1, 0, M_GOLDEN}, {-M_GOLDEN, 1, 0}},
  36. {{-M_GOLDEN, 1, 0}, {-1, 0, M_GOLDEN}, {-M_GOLDEN,-1, 0}},
  37. {{-1, 0, M_GOLDEN}, {-M_GOLDEN,-1, 0}, { 0,-M_GOLDEN, 1}},
  38. {{-1, 0, M_GOLDEN}, { 0,-M_GOLDEN, 1}, { 1, 0, M_GOLDEN}},
  39. {{ 0,-M_GOLDEN, 1}, { M_GOLDEN,-1, 0}, { 1, 0, M_GOLDEN}},
  40. };
  41. static bool section_triangle(unsigned int section_index,
  42. Vector3f& a,
  43. Vector3f& b,
  44. Vector3f& c) {
  45. if (section_index >= 80) {
  46. return false;
  47. }
  48. unsigned int i = section_index / 4;
  49. unsigned int j = section_index % 4;
  50. auto& t = triangles[i];
  51. Vector3f mt[3]{(t[0] + t[1]) / 2, (t[1] + t[2]) / 2, (t[2] + t[0]) / 2};
  52. switch (j) {
  53. case 0:
  54. a = mt[0];
  55. b = mt[1];
  56. c = mt[2];
  57. break;
  58. case 1:
  59. a = t[0];
  60. b = mt[0];
  61. c = mt[2];
  62. break;
  63. case 2:
  64. a = mt[0];
  65. b = t[1];
  66. c = mt[1];
  67. break;
  68. case 3:
  69. a = mt[2];
  70. b = mt[1];
  71. c = t[2];
  72. break;
  73. }
  74. return true;
  75. }
  76. static void BM_GeodesicGridSections(benchmark::State& state)
  77. {
  78. Vector3f v, a, b, c;
  79. int section = state.range_x();
  80. section_triangle(section, a, b, c);
  81. v = (a + b + c) / 3.0f;
  82. while (state.KeepRunning()) {
  83. int s = AP_GeodesicGrid::section(v);
  84. gbenchmark_escape(&s);
  85. }
  86. }
  87. /* Benchmark each section */
  88. BENCHMARK(BM_GeodesicGridSections)->DenseRange(0, 79);
  89. BENCHMARK_MAIN()