2015-09-01 23:01:38 +08:00
|
|
|
/****************************************************************************
|
|
|
|
** gmio benchmarks
|
|
|
|
** Copyright Fougue (2 Mar. 2015)
|
|
|
|
** contact@fougue.pro
|
|
|
|
**
|
|
|
|
** This software provides performance benchmarks for the gmio library
|
|
|
|
** (https://github.com/fougue/gmio)
|
|
|
|
**
|
|
|
|
** This software is governed by the CeCILL-B license under French law and
|
|
|
|
** abiding by the rules of distribution of free software. You can use,
|
|
|
|
** modify and/ or redistribute the software under the terms of the CeCILL-B
|
|
|
|
** license as circulated by CEA, CNRS and INRIA at the following URL
|
|
|
|
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <gmio_core/error.h>
|
2015-12-04 01:00:25 +08:00
|
|
|
#include <gmio_core/rwargs.h>
|
2015-09-01 23:01:38 +08:00
|
|
|
#include <gmio_stl/stl_io.h>
|
2015-12-04 01:00:25 +08:00
|
|
|
#include <gmio_stl/stl_io_options.h>
|
|
|
|
#include <gmio_stl/stl_mesh.h>
|
|
|
|
#include <gmio_stl/stl_mesh_creator.h>
|
2015-09-01 23:01:38 +08:00
|
|
|
|
2015-09-01 23:30:49 +08:00
|
|
|
#include "../commons/benchmark_tools.h"
|
2015-09-01 23:01:38 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_triangle;
|
|
|
|
|
|
|
|
struct my_igeom
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
|
|
|
uint32_t facet_count;
|
2015-12-04 01:00:25 +08:00
|
|
|
};
|
2015-09-01 23:01:38 +08:00
|
|
|
|
|
|
|
static void dummy_process_triangle(
|
|
|
|
void* cookie,
|
|
|
|
uint32_t triangle_id,
|
2015-12-04 01:00:25 +08:00
|
|
|
const struct gmio_stl_triangle* triangle)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct my_igeom* my_igeom = (struct my_igeom*)(cookie);
|
2015-09-23 21:30:17 +08:00
|
|
|
GMIO_UNUSED(triangle_id);
|
|
|
|
GMIO_UNUSED(triangle);
|
2015-09-01 23:01:38 +08:00
|
|
|
if (my_igeom != NULL)
|
|
|
|
++(my_igeom->facet_count);
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:30:17 +08:00
|
|
|
static void bmk_gmio_stl_read(const char* filepath)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct my_igeom cookie = {0};
|
|
|
|
struct gmio_stl_mesh_creator mesh_creator = {0};
|
2015-09-01 23:01:38 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
|
|
|
|
|
|
|
mesh_creator.cookie = &cookie;
|
|
|
|
mesh_creator.func_add_triangle = dummy_process_triangle;
|
2015-12-04 01:00:25 +08:00
|
|
|
error = gmio_stl_read_file(filepath, NULL, &mesh_creator);
|
2015-09-01 23:01:38 +08:00
|
|
|
if (error != GMIO_ERROR_OK)
|
|
|
|
printf("gmio error: 0x%X\n", error);
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
static enum gmio_endianness to_byte_order(enum gmio_stl_format format)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case GMIO_STL_FORMAT_BINARY_LE:
|
|
|
|
return GMIO_ENDIANNESS_LITTLE;
|
|
|
|
case GMIO_STL_FORMAT_BINARY_BE:
|
|
|
|
return GMIO_ENDIANNESS_BIG;
|
|
|
|
case GMIO_STL_FORMAT_ASCII:
|
|
|
|
case GMIO_STL_FORMAT_UNKNOWN:
|
|
|
|
return GMIO_ENDIANNESS_HOST;
|
|
|
|
}
|
|
|
|
return GMIO_ENDIANNESS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { STL_TRIANGLE_ARRAY_SIZE = 512 };
|
2015-12-04 01:00:25 +08:00
|
|
|
struct stl_readwrite_conv
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_rwargs rwargs;
|
2015-12-04 01:03:46 +08:00
|
|
|
struct gmio_streampos out_stream_pos_begin;
|
2015-12-04 01:00:25 +08:00
|
|
|
enum gmio_stl_format in_format;
|
|
|
|
enum gmio_stl_format out_format;
|
|
|
|
struct gmio_stl_triangle triangle_array[STL_TRIANGLE_ARRAY_SIZE];
|
2015-09-01 23:01:38 +08:00
|
|
|
unsigned triangle_pos;
|
|
|
|
uint32_t total_triangle_count;
|
2015-12-04 01:00:25 +08:00
|
|
|
};
|
2015-09-01 23:01:38 +08:00
|
|
|
|
|
|
|
static void readwrite_ascii_begin_solid(
|
2015-11-06 20:43:03 +08:00
|
|
|
void* cookie, gmio_streamsize_t stream_size, const char* solid_name)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
|
|
|
|
struct gmio_stream* stream = &rw_conv->rwargs.stream;
|
2015-09-23 21:30:17 +08:00
|
|
|
GMIO_UNUSED(stream_size);
|
2015-09-01 23:01:38 +08:00
|
|
|
if (rw_conv->out_format == GMIO_STL_FORMAT_ASCII) {
|
|
|
|
stream->func_write(stream->cookie, "solid ", 1, 6);
|
|
|
|
stream->func_write(stream->cookie, solid_name, 1, strlen(solid_name));
|
|
|
|
stream->func_write(stream->cookie, "\n", 1, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* For binary STL, facet count <- 0 because it cannot be known at this
|
|
|
|
* point. Header will be correctly rewritten at the end of the read
|
|
|
|
* procedure (in gmio_stl_mesh_creator::func_end_solid() callback)
|
|
|
|
*/
|
2015-12-04 01:00:25 +08:00
|
|
|
const enum gmio_endianness byte_order = to_byte_order(rw_conv->out_format);
|
2015-09-01 23:01:38 +08:00
|
|
|
gmio_stlb_write_header(stream, byte_order, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readwrite_binary_begin_solid(
|
2015-12-04 01:00:25 +08:00
|
|
|
void* cookie, uint32_t tri_count, const struct gmio_stlb_header* header)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
|
|
|
|
struct gmio_stream* stream = &rw_conv->rwargs.stream;
|
2015-09-01 23:01:38 +08:00
|
|
|
if (rw_conv->out_format == GMIO_STL_FORMAT_ASCII) {
|
|
|
|
stream->func_write(stream->cookie, "solid\n", 1, 6);
|
|
|
|
}
|
|
|
|
else {
|
2015-12-04 01:00:25 +08:00
|
|
|
const enum gmio_endianness byte_order = to_byte_order(rw_conv->out_format);
|
2015-09-01 23:01:38 +08:00
|
|
|
gmio_stlb_write_header(stream, byte_order, header, tri_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readwrite_get_triangle(
|
2015-12-04 01:00:25 +08:00
|
|
|
const void* cookie, uint32_t tri_id, struct gmio_stl_triangle* triangle)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
const struct gmio_stl_triangle* tri_array = (const struct gmio_stl_triangle*)cookie;
|
2015-09-01 23:01:38 +08:00
|
|
|
*triangle = tri_array[tri_id];
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
static void stl_readwrite_flush_triangles(struct stl_readwrite_conv* rw_conv)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_mesh mesh = {0};
|
|
|
|
struct gmio_stl_write_options options = {0};
|
2015-09-01 23:01:38 +08:00
|
|
|
mesh.cookie = &rw_conv->triangle_array[0];
|
|
|
|
mesh.triangle_count = rw_conv->triangle_pos;
|
|
|
|
mesh.func_get_triangle = &readwrite_get_triangle;
|
|
|
|
options.stl_write_triangles_only = GMIO_TRUE;
|
|
|
|
options.stla_float32_format = GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_LOWERCASE;
|
|
|
|
options.stla_float32_prec = 6;
|
2015-12-04 01:00:25 +08:00
|
|
|
gmio_stl_write(&rw_conv->rwargs, &mesh, rw_conv->out_format, &options);
|
2015-09-01 23:01:38 +08:00
|
|
|
rw_conv->triangle_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readwrite_add_triangle(
|
2015-12-04 01:00:25 +08:00
|
|
|
void* cookie, uint32_t tri_id, const struct gmio_stl_triangle* triangle)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
|
2015-09-01 23:01:38 +08:00
|
|
|
if (rw_conv->triangle_pos >= STL_TRIANGLE_ARRAY_SIZE)
|
|
|
|
stl_readwrite_flush_triangles(rw_conv);
|
|
|
|
rw_conv->triangle_array[rw_conv->triangle_pos] = *triangle;
|
|
|
|
++(rw_conv->triangle_pos);
|
|
|
|
rw_conv->total_triangle_count = tri_id + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readwrite_end_solid(void* cookie)
|
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
|
|
|
|
struct gmio_stream* stream = &rw_conv->rwargs.stream;
|
2015-09-01 23:01:38 +08:00
|
|
|
if (rw_conv->triangle_pos != 0)
|
|
|
|
stl_readwrite_flush_triangles(rw_conv);
|
|
|
|
if (rw_conv->out_format == GMIO_STL_FORMAT_ASCII) {
|
|
|
|
stream->func_write(stream->cookie, "endsolid", 1, 8);
|
|
|
|
}
|
|
|
|
else if (rw_conv->in_format == GMIO_STL_FORMAT_ASCII) {
|
2015-12-04 01:00:25 +08:00
|
|
|
const enum gmio_endianness byte_order = to_byte_order(rw_conv->out_format);
|
2015-09-01 23:01:38 +08:00
|
|
|
/* The total facet count has to be written because it wasn't known at
|
|
|
|
* the beginning of the read procedure */
|
2015-09-23 00:16:39 +08:00
|
|
|
stream->func_set_pos(stream->cookie, &rw_conv->out_stream_pos_begin);
|
2015-09-01 23:01:38 +08:00
|
|
|
gmio_stlb_write_header(
|
|
|
|
stream, byte_order, NULL, rw_conv->total_triangle_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:30:17 +08:00
|
|
|
static void bmk_gmio_stl_readwrite_conv(const char* filepath)
|
2015-09-01 23:01:38 +08:00
|
|
|
{
|
|
|
|
FILE* infile = fopen(filepath, "rb");
|
|
|
|
FILE* outfile = fopen("_readwrite_conv.stl", "wb");
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_rwargs in_rwargs = {0};
|
|
|
|
struct stl_readwrite_conv rw_conv = {0};
|
|
|
|
struct gmio_stl_mesh_creator mesh_creator = {0};
|
2015-09-01 23:01:38 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
|
|
|
|
|
|
|
/* rw_conv.out_format = GMIO_STL_FORMAT_BINARY_LE; */
|
|
|
|
rw_conv.out_format = GMIO_STL_FORMAT_ASCII;
|
|
|
|
|
|
|
|
if (infile != NULL) {
|
2015-12-04 01:00:25 +08:00
|
|
|
in_rwargs.memblock = gmio_memblock_malloc(512 * 1024);
|
|
|
|
in_rwargs.stream = gmio_stream_stdio(infile);
|
|
|
|
rw_conv.in_format = gmio_stl_get_format(&in_rwargs.stream);
|
2015-09-01 23:01:38 +08:00
|
|
|
}
|
|
|
|
if (outfile != NULL) {
|
2015-12-04 01:00:25 +08:00
|
|
|
rw_conv.rwargs.memblock = gmio_memblock_malloc(512 * 1024);
|
|
|
|
rw_conv.rwargs.stream = gmio_stream_stdio(outfile);
|
|
|
|
rw_conv.rwargs.stream.func_get_pos(
|
|
|
|
rw_conv.rwargs.stream.cookie,
|
2015-09-23 00:16:39 +08:00
|
|
|
&rw_conv.out_stream_pos_begin);
|
2015-09-01 23:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mesh_creator.cookie = &rw_conv;
|
|
|
|
mesh_creator.func_ascii_begin_solid = &readwrite_ascii_begin_solid;
|
|
|
|
mesh_creator.func_binary_begin_solid = &readwrite_binary_begin_solid;
|
|
|
|
mesh_creator.func_add_triangle = &readwrite_add_triangle;
|
|
|
|
mesh_creator.func_end_solid = &readwrite_end_solid;
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
error = gmio_stl_read(&in_rwargs, &mesh_creator);
|
2015-09-01 23:01:38 +08:00
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
gmio_memblock_deallocate(&in_rwargs.memblock);
|
|
|
|
gmio_memblock_deallocate(&rw_conv.rwargs.memblock);
|
2015-09-01 23:01:38 +08:00
|
|
|
|
|
|
|
if (error != GMIO_ERROR_OK)
|
|
|
|
printf("gmio error: 0x%X\n", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc > 1) {
|
2015-09-23 21:30:17 +08:00
|
|
|
const char* filepath = argv[1];
|
|
|
|
|
|
|
|
/* Declare benchmarks */
|
2015-12-04 01:00:25 +08:00
|
|
|
struct benchmark_cmp_arg cmp_args[] = {
|
2015-09-23 21:30:17 +08:00
|
|
|
{ "read_file()",
|
|
|
|
bmk_gmio_stl_read, NULL,
|
|
|
|
NULL, NULL },
|
|
|
|
{ "readwrite_conv()",
|
|
|
|
bmk_gmio_stl_readwrite_conv, NULL,
|
|
|
|
NULL, NULL },
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
const size_t cmp_count =
|
2015-12-04 01:00:25 +08:00
|
|
|
sizeof(cmp_args) / sizeof(struct benchmark_cmp_arg) - 1;
|
|
|
|
struct benchmark_cmp_result cmp_res[2] = {0};
|
|
|
|
struct benchmark_cmp_result_array res_array = {0};
|
|
|
|
const struct benchmark_cmp_result_header header = { "gmio", NULL };
|
2015-09-23 21:30:17 +08:00
|
|
|
|
|
|
|
cmp_args[0].func1_filepath = filepath;
|
|
|
|
cmp_args[1].func1_filepath = filepath;
|
|
|
|
|
|
|
|
res_array.ptr = &cmp_res[0];
|
|
|
|
res_array.count = cmp_count;
|
|
|
|
|
|
|
|
/* Execute benchmarks */
|
|
|
|
benchmark_cmp_batch(5, &cmp_args[0], &cmp_res[0], NULL, NULL);
|
|
|
|
|
|
|
|
/* Print results */
|
|
|
|
benchmark_print_results(
|
|
|
|
BENCHMARK_PRINT_FORMAT_MARKDOWN, header, res_array);
|
2015-09-01 23:01:38 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|