Advertisement
HuguesDelorme

Benchmark code gmio vs Antimony

Jul 4th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include "../../commons/benchmark_tools.h"
  2.  
  3. #include <gmio_core/error.h>
  4. #include <gmio_stl/stl_format.h>
  5. #include <gmio_stl/stl_io.h>
  6. #include <gmio_stl/stl_infos.h>
  7.  
  8. #include <algorithm>
  9. #include <iostream>
  10. #include <vector>
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14.  
  15. namespace BmkGmio { static std::vector<float> global_vec_coords; }
  16.  
  17. namespace BmkAntimony {
  18.  
  19. /*  antimony/lib/fab/src/formats/stl.c */
  20. static void save_stl(float* verts, unsigned count, const char* filename)
  21. {
  22.     FILE* stl = fopen(filename, "wb");
  23.  
  24.     // 80-character header
  25.     fprintf(stl,
  26.             "This is a binary STL file made in kokopelli    \n"
  27.             "(github.com/mkeeter/kokopelli)\n\n");
  28.  
  29.     int tris = count / 9;
  30.     for (unsigned i=0; i < sizeof(float); ++i) {
  31.         fputc(((char*)&tris)[i], stl);
  32.     }
  33.  
  34.     for (int t=0; t < tris; ++t) {
  35.  
  36.         // Write the face normal (which we'll keep empty)
  37.         for (unsigned j=0; j < 3*sizeof(float); ++j) fputc(0, stl);
  38.  
  39.         // Write out all of the vertices.
  40.         for (unsigned v=0; v < 9 * sizeof(float); ++v) {
  41.             fputc(((char*)&verts[t*9])[v], stl);
  42.         }
  43.  
  44.         fputc(0, stl);
  45.         fputc(0, stl);
  46.     }
  47.  
  48.     fclose(stl);
  49. }
  50.  
  51. static void stlb_write_le(const void* filepath)
  52. {
  53.     save_stl(&BmkGmio::global_vec_coords[0],
  54.              BmkGmio::global_vec_coords.size(),
  55.              static_cast<const char*>(filepath));
  56. }
  57.  
  58. } // namespace BmkAntimony
  59.  
  60. namespace BmkGmio {
  61.  
  62. static void begin_solid(
  63.         void* cookie, const struct gmio_stl_mesh_creator_infos* infos)
  64. {
  65.     auto vec_coords = static_cast<std::vector<float>*>(cookie);
  66.     if (infos->format == GMIO_STL_FORMAT_ASCII) {
  67.         const unsigned ustream_size =
  68.                 static_cast<unsigned>(infos->stla_stream_size);
  69.         const unsigned estimated_facet_count = ustream_size / 200u;
  70.         vec_coords->resize(std::max(1u, 9 * estimated_facet_count));
  71.     }
  72.     else {
  73.         vec_coords->resize(9 * infos->stlb_triangle_count);
  74.     }
  75. }
  76.  
  77. static void get_triangle(
  78.         const void* cookie, uint32_t tri_id, gmio_stl_triangle* triangle)
  79. {
  80.     auto vec_coords = static_cast<const std::vector<float>*>(cookie);
  81.     std::memset(&triangle->n, 0, 3*sizeof(float));
  82.     std::memcpy(&triangle->v1, &vec_coords->at(9*tri_id), 9*sizeof(float));
  83. }
  84.  
  85. static void add_triangle(
  86.         void* cookie, uint32_t tri_id, const gmio_stl_triangle* triangle)
  87. {
  88.     auto vec_coords = static_cast<std::vector<float>*>(cookie);
  89.     std::memcpy(&(*vec_coords)[9*tri_id], &triangle->v1, 9*sizeof(float));
  90. }
  91.  
  92. static void stl_read(const void* filepath)
  93. {
  94.     const char* str_filepath = static_cast<const char*>(filepath);
  95.     gmio_stl_mesh_creator mesh_creator = {};
  96.     mesh_creator.cookie = &global_vec_coords;
  97.     mesh_creator.func_begin_solid = begin_solid;
  98.     mesh_creator.func_add_triangle = add_triangle;
  99.     const int error = gmio_stl_read_file(str_filepath, &mesh_creator, NULL);
  100.     if (error != GMIO_ERROR_OK)
  101.         std::cout << "gmio error: 0x" << std::hex << error << std::endl;
  102. }
  103.  
  104. static void stl_write_generic(const char* filepath, gmio_stl_format format)
  105. {
  106.     gmio_stl_mesh mesh = {};
  107.     mesh.cookie = &global_vec_coords;
  108.     mesh.triangle_count = static_cast<uint32_t>(global_vec_coords.size() / 9);
  109.     mesh.func_get_triangle = get_triangle;
  110.  
  111.     gmio_stl_write_options opts = {};
  112.     opts.stla_solid_name = filepath;
  113.     opts.stla_float32_prec = 7;
  114.     opts.stlb_header = gmio_stlb_header_str(filepath);
  115.     const int error = gmio_stl_write_file(format, filepath, &mesh, &opts);
  116.     if (error != GMIO_ERROR_OK)
  117.         std::cout << "gmio error: 0x" << std::hex << error << std::endl;
  118. }
  119.  
  120. static void stla_write(const void* filepath)
  121. {
  122.     stl_write_generic(
  123.                 static_cast<const char*>(filepath), GMIO_STL_FORMAT_ASCII);
  124. }
  125.  
  126. static void stlb_write_le(const void* filepath)
  127. {
  128.     stl_write_generic(
  129.                 static_cast<const char*>(filepath), GMIO_STL_FORMAT_BINARY_LE);
  130. }
  131.  
  132. static void stlb_write_be(const void* filepath)
  133. {
  134.     stl_write_generic(
  135.                 static_cast<const char*>(filepath), GMIO_STL_FORMAT_BINARY_BE);
  136. }
  137.  
  138. } // namespace BmkGmio
  139.  
  140. static void bmk_init()
  141. {
  142.     BmkGmio::global_vec_coords.clear();
  143. }
  144.  
  145. int main(int argc, char** argv)
  146. {
  147.     if (argc > 1) {
  148.         const char* filepath = argv[1];
  149.         std::cout << std::endl << "Input file: " << filepath << std::endl;
  150.  
  151.         /* Declare benchmarks */
  152.         const benchmark_cmp_arg cmp_args[] = {
  153.             { "read",
  154.               BmkGmio::stl_read, filepath,
  155.               NULL, NULL },
  156.             { "write(ascii)",
  157.               BmkGmio::stla_write, "__bmk_antimony_gmio.stla",
  158.               NULL, NULL },
  159.             { "write(binary/le)",
  160.               BmkGmio::stlb_write_le, "__bmk_antimony_gmio.stlb_le",
  161.               BmkAntimony::stlb_write_le, "__bmk_antimony.stlb_le" },
  162.             { "write(binary/be)",
  163.               BmkGmio::stlb_write_be, "__bmk_antimony_gmio.stlb_be",
  164.               NULL, NULL },
  165.             {}
  166.         };
  167.  
  168.         /* Execute benchmarks */
  169.         std::vector<benchmark_cmp_result> cmp_res_vec;
  170.         cmp_res_vec.resize(GMIO_ARRAY_SIZE(cmp_args) - 1);
  171.         benchmark_cmp_batch(
  172.                     5, cmp_args, &cmp_res_vec[0], bmk_init, nullptr);
  173.  
  174.         /* Print results */
  175.         const benchmark_cmp_result_array res_array = {
  176.             &cmp_res_vec.at(0), cmp_res_vec.size() };
  177.         const benchmark_cmp_result_header header = { "gmio", "Antimony" };
  178.         benchmark_print_results(
  179.                     BENCHMARK_PRINT_FORMAT_MARKDOWN, header, res_array);
  180.     }
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement