main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <tuple>
  5. #include <random>
  6. #include "ItsNeighborIndex.hpp"
  7. #include "libnest2d/tools/benchmark.h"
  8. #include "libnest2d/utils/metaloop.hpp"
  9. namespace Slic3r {
  10. enum { IndexCreation, Split };
  11. struct MeasureResult
  12. {
  13. static constexpr const char * Names[] = {
  14. "Index creation [s]",
  15. "Split [s]"
  16. };
  17. double measurements[std::size(Names)] = {0.};
  18. };
  19. template<class IndexCreatorFn>
  20. static MeasureResult measure_index(const indexed_triangle_set &its, IndexCreatorFn fn)
  21. {
  22. Benchmark b;
  23. MeasureResult r;
  24. for (int i = 0; i < 10; ++i) {
  25. b.start();
  26. ItsNeighborsWrapper itsn{its, fn(its)};
  27. b.stop();
  28. r.measurements[IndexCreation] += b.getElapsedSec();
  29. b.start();
  30. auto res = its_split(itsn);
  31. b.stop();
  32. // if (res.size() != 2 || res[0].indices.size() != res[1].indices.size() )
  33. // std::cerr << "Something is wrong, split result invalid" << std::endl;
  34. r.measurements[Split] += b.getElapsedSec();
  35. }
  36. r.measurements[IndexCreation] /= 10;
  37. r.measurements[Split] /= 10;
  38. return r;
  39. }
  40. const auto Seed = 0;// std::random_device{}();
  41. static indexed_triangle_set make_sphere_rnd(double radius, double detail)
  42. {
  43. using namespace Slic3r;
  44. auto sphere = its_make_sphere(radius, detail);
  45. auto vfidx = create_vertex_faces_index(sphere);
  46. const size_t vertexnum = sphere.vertices.size();
  47. const size_t facenum = sphere.indices.size();
  48. std::mt19937 rng{Seed};
  49. std::uniform_int_distribution<size_t> distv(sphere.vertices.size() / 2, sphere.vertices.size() - 1);
  50. std::uniform_int_distribution<size_t> distf(sphere.indices.size() / 2, sphere.indices.size() - 1) ;
  51. std::vector<bool> was(vertexnum / 2, false);
  52. for (size_t i = 0; i < vertexnum / 2; ++i) {
  53. size_t image = distv(rng);
  54. if (was[image - vertexnum / 2]) continue;
  55. was[image - vertexnum / 2] = true;
  56. std::swap(sphere.vertices[i], sphere.vertices[image]);
  57. for (size_t face_id : vfidx[i]) {
  58. for (int &vi : sphere.indices[face_id])
  59. if (vi == int(i)) vi = image;
  60. }
  61. for (size_t face_id : vfidx[image]) {
  62. for (int &vi : sphere.indices[face_id])
  63. if (vi == int(image)) vi = i;
  64. }
  65. std::swap(vfidx[i], vfidx[image]);
  66. }
  67. for (size_t i = 0; i < facenum / 2; ++i) {
  68. size_t image = distf(rng);
  69. std::swap(sphere.indices[i], sphere.indices[image]);
  70. }
  71. return sphere;
  72. }
  73. static indexed_triangle_set two_spheres(double detail)
  74. {
  75. auto sphere1 = make_sphere_rnd(10., 2 * PI / detail), sphere2 = sphere1;
  76. its_transform(sphere1, identity3f().translate(Vec3f{-5.f, 0.f, 0.f}));
  77. its_transform(sphere2, identity3f().translate(Vec3f{5.f, 0.f, 0.f}));
  78. its_merge(sphere1, sphere2);
  79. return sphere1;
  80. }
  81. static indexed_triangle_set make_spheres(unsigned N, double detail)
  82. {
  83. indexed_triangle_set ret, sphere = make_sphere_rnd(10., 2. * PI / detail);
  84. for (unsigned i = 0u ; i < N; ++i)
  85. its_merge(ret, sphere);
  86. return ret;
  87. }
  88. constexpr double sq2 = std::sqrt(2.);
  89. static const std::pair<const std::string, indexed_triangle_set> ToMeasure[] = {
  90. // {"two_spheres_1x", two_spheres(60.)},
  91. // {"two_spheres_2x", two_spheres(120.)},
  92. // {"two_spheres_4x", two_spheres(240.)},
  93. // {"two_spheres_8x", two_spheres(480.)},
  94. // {"two_spheres_16x", two_spheres(2 * 480.)},
  95. // {"two_spheres_32x", two_spheres(2 * 2 * 480.)},
  96. {"two_spheres_1x", two_spheres(60.)},
  97. {"two_spheres_2x", two_spheres(sq2 * 60.)},
  98. {"two_spheres_4x", two_spheres(2 * 60.)},
  99. {"two_spheres_8x", two_spheres(sq2 * 2. * 60.)},
  100. {"two_spheres_16x", two_spheres(4. * 60.)},
  101. {"two_spheres_32x", two_spheres(sq2 * 4. * 60.)},
  102. {"two_spheres_64x", two_spheres(8. * 60.)},
  103. {"two_spheres_128x", two_spheres(sq2 * 8. * 60.)},
  104. {"two_spheres_256x", two_spheres(16. * 60.)},
  105. {"two_spheres_512x", two_spheres(sq2 * 16. * 60.)},
  106. {"2_spheres", make_spheres(2, 60.)},
  107. {"4_spheres", make_spheres(4, 60.)},
  108. {"8_spheres", make_spheres(8, 60.)},
  109. {"16_spheres", make_spheres(16, 60.)},
  110. {"32_spheres", make_spheres(32, 60.)},
  111. {"64_spheres", make_spheres(64, 60.)},
  112. {"128_spheres", make_spheres(128, 60.)},
  113. {"256_spheres", make_spheres(256, 60.)},
  114. {"512_spheres", make_spheres(512, 60.)},
  115. {"1024_spheres", make_spheres(1024, 60.)}
  116. };
  117. static const auto IndexFunctions = std::make_tuple(
  118. std::make_pair("tamas's unordered_map based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_1); }),
  119. std::make_pair("vojta std::sort based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_2); }),
  120. std::make_pair("vojta tbb::parallel_sort based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_3); }),
  121. std::make_pair("filip's vertex->face based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_5); }),
  122. std::make_pair("vojta's vertex->face", [](const auto &its) { return measure_index(its, its_create_neighbors_index_9); }),
  123. std::make_pair("vojta's vertex->face parallel", [](const auto &its) { return measure_index(its, its_create_neighbors_index_10); }),
  124. std::make_pair("tamas's std::sort based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_6); }),
  125. std::make_pair("tamas's tbb::parallel_sort based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_7); }),
  126. std::make_pair("tamas's map based", [](const auto &its) { return measure_index(its, its_create_neighbors_index_8); }),
  127. std::make_pair("TriangleMesh split", [](const auto &its) {
  128. MeasureResult r;
  129. for (int i = 0; i < 10; ++i) {
  130. TriangleMesh m{its};
  131. Benchmark b;
  132. b.start();
  133. m.repair(); // FIXME: this does more than just create neighborhood map
  134. b.stop();
  135. r.measurements[IndexCreation] += b.getElapsedSec();
  136. b.start();
  137. auto res = m.split();
  138. b.stop();
  139. r.measurements[Split] += b.getElapsedSec();
  140. // if (res.size() != 2 || res[0]->size() != res[1]->size())
  141. // std::cerr << "Something is wrong, split result invalid" << std::endl;
  142. }
  143. r.measurements[IndexCreation] /= 10;
  144. r.measurements[Split] /= 10;
  145. return r;
  146. })
  147. // std::make_pair("Vojta's vertex->face index", [](const auto &its){
  148. // Benchmark b;
  149. // b.start();
  150. // auto index = create_vertex_faces_index(its);
  151. // b.stop();
  152. // if (index.size() != its.vertices.size())
  153. // std::cerr << "Something went wrong!";
  154. // return MeasureResult{b.getElapsedSec(), 0., 0.};
  155. // }),
  156. // std::make_pair("Tamas's vertex->face index", [](const auto &its){
  157. // Benchmark b;
  158. // b.start();
  159. // VertexFaceIndex index{its};
  160. // b.stop();
  161. // if (index.size() < its.vertices.size())
  162. // std::cerr << "Something went wrong!";
  163. // return MeasureResult{b.getElapsedSec(), 0., 0.};
  164. // })
  165. );
  166. static constexpr size_t IndexFuncNum = std::tuple_size_v<decltype (IndexFunctions)>;
  167. } // namespace Slic3r
  168. int main(const int argc, const char * argv[])
  169. {
  170. using namespace Slic3r;
  171. std::array<MeasureResult, IndexFuncNum> results[std::size(ToMeasure)];
  172. std::array<std::string, IndexFuncNum> funcnames;
  173. for (size_t i = 0; i < std::size(ToMeasure); ++i) {
  174. auto &m = ToMeasure[i];
  175. auto &name = m.first;
  176. auto &mesh = m.second;
  177. // its_write_obj(mesh, (std::string(name) + ".obj").c_str());
  178. std::cout << "Mesh " << name << " has " << mesh.indices.size() << " faces and " << mesh.vertices.size() << " vertices." << std::endl;
  179. libnest2d::opt::metaloop::apply([&mesh, i, &results, &funcnames](int N, auto &e) {
  180. MeasureResult r = e.second(mesh);
  181. funcnames[N] = e.first;
  182. results[i][N] = r;
  183. }, IndexFunctions);
  184. }
  185. std::string outfilename = "out.csv";
  186. std::fstream outfile;
  187. if (argc > 1) {
  188. outfilename = argv[1];
  189. outfile.open(outfilename, std::fstream::out);
  190. std::cout << outfilename << " will be used" << std::endl;
  191. }
  192. std::ostream &out = outfile.is_open() ? outfile : std::cout;
  193. for (size_t m = 0; m < std::size(MeasureResult::Names); ++m) {
  194. out << MeasureResult::Names[m] << "\n";
  195. out << std::endl;
  196. out << "model;" ;
  197. for (const std::string &funcname : funcnames) {
  198. out << funcname << ";";
  199. }
  200. out << std::endl;
  201. for (size_t i = 0; i < std::size(ToMeasure); ++i) {
  202. const auto &result_row = results[i];
  203. const std::string &name = ToMeasure[i].first;
  204. out << name << ";";
  205. for (auto &r : result_row)
  206. out << r.measurements[m] << ";";
  207. out << std::endl;
  208. }
  209. }
  210. return 0;
  211. }