Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../../commons/benchmark_tools.h"
- #include <gmio_core/error.h>
- #include <gmio_stl/stl_format.h>
- #include <gmio_stl/stl_io.h>
- #include <gmio_stl/stl_infos.h>
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <stdlib.h>
- #include <stdio.h>
- namespace BmkGmio { static std::vector<float> global_vec_coords; }
- namespace BmkAntimony {
- /* antimony/lib/fab/src/formats/stl.c */
- static void save_stl(float* verts, unsigned count, const char* filename)
- {
- FILE* stl = fopen(filename, "wb");
- // 80-character header
- fprintf(stl,
- "This is a binary STL file made in kokopelli \n"
- "(github.com/mkeeter/kokopelli)\n\n");
- int tris = count / 9;
- for (unsigned i=0; i < sizeof(float); ++i) {
- fputc(((char*)&tris)[i], stl);
- }
- for (int t=0; t < tris; ++t) {
- // Write the face normal (which we'll keep empty)
- for (unsigned j=0; j < 3*sizeof(float); ++j) fputc(0, stl);
- // Write out all of the vertices.
- for (unsigned v=0; v < 9 * sizeof(float); ++v) {
- fputc(((char*)&verts[t*9])[v], stl);
- }
- fputc(0, stl);
- fputc(0, stl);
- }
- fclose(stl);
- }
- static void stlb_write_le(const void* filepath)
- {
- save_stl(&BmkGmio::global_vec_coords[0],
- BmkGmio::global_vec_coords.size(),
- static_cast<const char*>(filepath));
- }
- } // namespace BmkAntimony
- namespace BmkGmio {
- static void begin_solid(
- void* cookie, const struct gmio_stl_mesh_creator_infos* infos)
- {
- auto vec_coords = static_cast<std::vector<float>*>(cookie);
- if (infos->format == GMIO_STL_FORMAT_ASCII) {
- const unsigned ustream_size =
- static_cast<unsigned>(infos->stla_stream_size);
- const unsigned estimated_facet_count = ustream_size / 200u;
- vec_coords->resize(std::max(1u, 9 * estimated_facet_count));
- }
- else {
- vec_coords->resize(9 * infos->stlb_triangle_count);
- }
- }
- static void get_triangle(
- const void* cookie, uint32_t tri_id, gmio_stl_triangle* triangle)
- {
- auto vec_coords = static_cast<const std::vector<float>*>(cookie);
- std::memset(&triangle->n, 0, 3*sizeof(float));
- std::memcpy(&triangle->v1, &vec_coords->at(9*tri_id), 9*sizeof(float));
- }
- static void add_triangle(
- void* cookie, uint32_t tri_id, const gmio_stl_triangle* triangle)
- {
- auto vec_coords = static_cast<std::vector<float>*>(cookie);
- std::memcpy(&(*vec_coords)[9*tri_id], &triangle->v1, 9*sizeof(float));
- }
- static void stl_read(const void* filepath)
- {
- const char* str_filepath = static_cast<const char*>(filepath);
- gmio_stl_mesh_creator mesh_creator = {};
- mesh_creator.cookie = &global_vec_coords;
- mesh_creator.func_begin_solid = begin_solid;
- mesh_creator.func_add_triangle = add_triangle;
- const int error = gmio_stl_read_file(str_filepath, &mesh_creator, NULL);
- if (error != GMIO_ERROR_OK)
- std::cout << "gmio error: 0x" << std::hex << error << std::endl;
- }
- static void stl_write_generic(const char* filepath, gmio_stl_format format)
- {
- gmio_stl_mesh mesh = {};
- mesh.cookie = &global_vec_coords;
- mesh.triangle_count = static_cast<uint32_t>(global_vec_coords.size() / 9);
- mesh.func_get_triangle = get_triangle;
- gmio_stl_write_options opts = {};
- opts.stla_solid_name = filepath;
- opts.stla_float32_prec = 7;
- opts.stlb_header = gmio_stlb_header_str(filepath);
- const int error = gmio_stl_write_file(format, filepath, &mesh, &opts);
- if (error != GMIO_ERROR_OK)
- std::cout << "gmio error: 0x" << std::hex << error << std::endl;
- }
- static void stla_write(const void* filepath)
- {
- stl_write_generic(
- static_cast<const char*>(filepath), GMIO_STL_FORMAT_ASCII);
- }
- static void stlb_write_le(const void* filepath)
- {
- stl_write_generic(
- static_cast<const char*>(filepath), GMIO_STL_FORMAT_BINARY_LE);
- }
- static void stlb_write_be(const void* filepath)
- {
- stl_write_generic(
- static_cast<const char*>(filepath), GMIO_STL_FORMAT_BINARY_BE);
- }
- } // namespace BmkGmio
- static void bmk_init()
- {
- BmkGmio::global_vec_coords.clear();
- }
- int main(int argc, char** argv)
- {
- if (argc > 1) {
- const char* filepath = argv[1];
- std::cout << std::endl << "Input file: " << filepath << std::endl;
- /* Declare benchmarks */
- const benchmark_cmp_arg cmp_args[] = {
- { "read",
- BmkGmio::stl_read, filepath,
- NULL, NULL },
- { "write(ascii)",
- BmkGmio::stla_write, "__bmk_antimony_gmio.stla",
- NULL, NULL },
- { "write(binary/le)",
- BmkGmio::stlb_write_le, "__bmk_antimony_gmio.stlb_le",
- BmkAntimony::stlb_write_le, "__bmk_antimony.stlb_le" },
- { "write(binary/be)",
- BmkGmio::stlb_write_be, "__bmk_antimony_gmio.stlb_be",
- NULL, NULL },
- {}
- };
- /* Execute benchmarks */
- std::vector<benchmark_cmp_result> cmp_res_vec;
- cmp_res_vec.resize(GMIO_ARRAY_SIZE(cmp_args) - 1);
- benchmark_cmp_batch(
- 5, cmp_args, &cmp_res_vec[0], bmk_init, nullptr);
- /* Print results */
- const benchmark_cmp_result_array res_array = {
- &cmp_res_vec.at(0), cmp_res_vec.size() };
- const benchmark_cmp_result_header header = { "gmio", "Antimony" };
- benchmark_print_results(
- BENCHMARK_PRINT_FORMAT_MARKDOWN, header, res_array);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement