gmio_stl: improve API

This commit is contained in:
Hugues Delorme 2016-01-29 12:47:01 +01:00
parent 9ae18d1b9e
commit 75cd51398e
34 changed files with 566 additions and 780 deletions

View File

@ -285,15 +285,15 @@ static void get_triangle(
static void stl_read(const void* filepath)
{
gmio_stl_read_args read = {};
read.mesh_creator.cookie = &globalSceneHelper;
read.mesh_creator.func_ascii_begin_solid = func_ascii_begin_solid;
read.mesh_creator.func_binary_begin_solid = binary_begin_solid;
read.mesh_creator.func_add_triangle = add_triangle;
read.mesh_creator.func_end_solid = end_solid;
const char* str_filepath = static_cast<const char*>(filepath);
gmio_stl_mesh_creator mesh_creator = {};
mesh_creator.cookie = &globalSceneHelper;
mesh_creator.func_ascii_begin_solid = func_ascii_begin_solid;
mesh_creator.func_binary_begin_solid = binary_begin_solid;
mesh_creator.func_add_triangle = add_triangle;
mesh_creator.func_end_solid = end_solid;
const int error =
gmio_stl_read_file(&read, static_cast<const char*>(filepath));
const int error = gmio_stl_read_file(str_filepath, mesh_creator, NULL);
if (error != GMIO_ERROR_OK)
printf("gmio error: 0x%X\n", error);
@ -306,13 +306,15 @@ static void stl_write(const char* filepath, gmio_stl_format format)
{
const aiMesh* sceneMesh = globalSceneHelper.scene->mMeshes[0];
gmio_stl_write_args write = {};
write.mesh.cookie = sceneMesh;
write.mesh.triangle_count = sceneMesh->mNumFaces;
write.mesh.func_get_triangle = get_triangle;
write.options.stla_float32_format = GMIO_FLOAT_TEXT_FORMAT_SHORTEST_UPPERCASE;
write.options.stla_float32_prec = 7;
const int error = gmio_stl_write_file(&write, format, filepath);
gmio_stl_mesh mesh = {};
mesh.cookie = sceneMesh;
mesh.triangle_count = sceneMesh->mNumFaces;
mesh.func_get_triangle = get_triangle;
gmio_stl_write_options opts = {};
opts.stla_float32_format = GMIO_FLOAT_TEXT_FORMAT_SHORTEST_UPPERCASE;
opts.stla_float32_prec = 7;
const int error = gmio_stl_write_file(format, filepath, mesh, NULL);
if (error != GMIO_ERROR_OK)
printf("gmio error: 0x%X\n", error);
}
@ -376,7 +378,7 @@ int main(int argc, char** argv)
std::vector<benchmark_cmp_result> cmp_res_vec;
cmp_res_vec.resize(GMIO_ARRAY_SIZE(cmp_args) - 1);
benchmark_cmp_batch(
5, &cmp_args[0], &cmp_res_vec[0], bmk_init, bmk_cleanup);
5, cmp_args, &cmp_res_vec[0], bmk_init, bmk_cleanup);
/* Print results */
const benchmark_cmp_result_array res_array = {

View File

@ -14,7 +14,6 @@
****************************************************************************/
#include <gmio_core/error.h>
#include <gmio_core/rwargs.h>
#include <gmio_stl/stl_io.h>
#include <gmio_stl/stl_io_options.h>
#include <gmio_stl/stl_mesh.h>
@ -48,12 +47,12 @@ static void dummy_process_triangle(
static void bmk_gmio_stl_read(const void* filepath)
{
struct my_igeom cookie = {0};
struct gmio_stl_read_args read = {0};
struct gmio_stl_mesh_creator mesh_creator = {0};
int error = GMIO_ERROR_OK;
read.mesh_creator.cookie = &cookie;
read.mesh_creator.func_add_triangle = dummy_process_triangle;
error = gmio_stl_read_file(&read, filepath);
mesh_creator.cookie = &cookie;
mesh_creator.func_add_triangle = dummy_process_triangle;
error = gmio_stl_read_file(filepath, mesh_creator, NULL);
if (error != GMIO_ERROR_OK)
printf("gmio error: 0x%X\n", error);
}
@ -75,7 +74,7 @@ static enum gmio_endianness to_byte_order(enum gmio_stl_format format)
enum { STL_TRIANGLE_ARRAY_SIZE = 512 };
struct stl_readwrite_conv
{
struct gmio_rwargs rwargs;
struct gmio_stream stream;
struct gmio_streampos out_stream_pos_begin;
enum gmio_stl_format in_format;
enum gmio_stl_format out_format;
@ -88,7 +87,7 @@ static void readwrite_ascii_begin_solid(
void* cookie, gmio_streamsize_t stream_size, const char* solid_name)
{
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
struct gmio_stream* stream = &rw_conv->rwargs.stream;
struct gmio_stream* stream = &rw_conv->stream;
GMIO_UNUSED(stream_size);
if (rw_conv->out_format == GMIO_STL_FORMAT_ASCII) {
stream->func_write(stream->cookie, "solid ", 1, 6);
@ -109,7 +108,7 @@ static void readwrite_binary_begin_solid(
void* cookie, uint32_t tri_count, const struct gmio_stlb_header* header)
{
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
struct gmio_stream* stream = &rw_conv->rwargs.stream;
struct gmio_stream* stream = &rw_conv->stream;
if (rw_conv->out_format == GMIO_STL_FORMAT_ASCII) {
stream->func_write(stream->cookie, "solid\n", 1, 6);
}
@ -129,15 +128,18 @@ static void readwrite_get_triangle(
static void stl_readwrite_flush_triangles(struct stl_readwrite_conv* rw_conv)
{
struct gmio_stl_write_args write = {0};
write.core = rw_conv->rwargs;
write.mesh.cookie = &rw_conv->triangle_array[0];
write.mesh.triangle_count = rw_conv->triangle_pos;
write.mesh.func_get_triangle = &readwrite_get_triangle;
write.options.stl_write_triangles_only = true;
write.options.stla_float32_format = GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_LOWERCASE;
write.options.stla_float32_prec = 6;
gmio_stl_write(&write, rw_conv->out_format);
struct gmio_stl_mesh mesh = {0};
struct gmio_stl_write_options options = {0};
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 = true;
options.stla_float32_format = GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_LOWERCASE;
options.stla_float32_prec = 6;
gmio_stl_write(rw_conv->out_format, rw_conv->stream, mesh, &options);
rw_conv->triangle_pos = 0;
}
@ -155,7 +157,7 @@ static void readwrite_add_triangle(
static void readwrite_end_solid(void* cookie)
{
struct stl_readwrite_conv* rw_conv = (struct stl_readwrite_conv*)cookie;
struct gmio_stream* stream = &rw_conv->rwargs.stream;
struct gmio_stream* stream = &rw_conv->stream;
if (rw_conv->triangle_pos != 0)
stl_readwrite_flush_triangles(rw_conv);
if (rw_conv->out_format == GMIO_STL_FORMAT_ASCII) {
@ -175,36 +177,32 @@ static void bmk_gmio_stl_readwrite_conv(const void* filepath)
{
FILE* infile = fopen(filepath, "rb");
FILE* outfile = fopen("_readwrite_conv.stl", "wb");
struct gmio_stl_read_args read = {0};
struct gmio_stream instream = {0};
struct stl_readwrite_conv rw_conv = {0};
struct gmio_stl_mesh_creator mesh_creator = {0};
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) {
read.core.stream_memblock = gmio_memblock_malloc(512 * 1024);
read.core.stream = gmio_stream_stdio(infile);
rw_conv.in_format = gmio_stl_get_format(&read.core.stream);
instream = gmio_stream_stdio(infile);
rw_conv.in_format = gmio_stl_get_format(&instream);
}
if (outfile != NULL) {
rw_conv.rwargs.stream_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,
rw_conv.stream = gmio_stream_stdio(outfile);
rw_conv.stream.func_get_pos(
rw_conv.stream.cookie,
&rw_conv.out_stream_pos_begin);
}
read.mesh_creator.cookie = &rw_conv;
read.mesh_creator.func_ascii_begin_solid = &readwrite_ascii_begin_solid;
read.mesh_creator.func_binary_begin_solid = &readwrite_binary_begin_solid;
read.mesh_creator.func_add_triangle = &readwrite_add_triangle;
read.mesh_creator.func_end_solid = &readwrite_end_solid;
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;
error = gmio_stl_read(&read);
gmio_memblock_deallocate(&read.core.stream_memblock);
gmio_memblock_deallocate(&rw_conv.rwargs.stream_memblock);
error = gmio_stl_read(instream, mesh_creator, NULL);
if (error != GMIO_ERROR_OK)
printf("gmio error: 0x%X\n", error);
@ -219,29 +217,25 @@ void bmk_gmio_stl_infos_get(const void* filepath)
FILE* file = fopen(filepath, "rb");
if (file != NULL) {
struct gmio_stream stream = gmio_stream_stdio(file);
struct gmio_stl_infos_get_args args = {0};
const enum gmio_stl_format format = gmio_stl_get_format(&stream);
const struct gmio_stream stream = gmio_stream_stdio(file);
struct gmio_stl_infos infos = {0};
gmio_stl_infos_get(&infos, stream, GMIO_STL_INFO_FLAG_ALL, NULL);
args.stream = stream;
gmio_stl_infos_get(&args, format, GMIO_STL_INFO_FLAG_ALL);
if (!already_exec) {
printf("stl_infos_get()\n"
" File: %s\n"
" Size: %uKo\n"
" Facets: %u\n",
(const char*)filepath,
args.infos.size / 1024,
args.infos.facet_count);
if (format == GMIO_STL_FORMAT_ASCII) {
printf(" [STLA]Solid name: %s\n",
args.infos.stla_solidname);
infos.size / 1024,
infos.facet_count);
if (infos.format == GMIO_STL_FORMAT_ASCII) {
printf(" [STLA]Solid name: %s\n", infos.stla_solidname);
}
else if (format == GMIO_STL_FORMAT_BINARY_LE
|| format == GMIO_STL_FORMAT_BINARY_BE)
else if (infos.format == GMIO_STL_FORMAT_BINARY_LE
|| infos.format == GMIO_STL_FORMAT_BINARY_BE)
{
printf(" [STLB]Header: %80.80s\n",
args.infos.stlb_header.data);
printf(" [STLB]Header: %80.80s\n", infos.stlb_header.data);
}
}
already_exec = true;

View File

@ -122,7 +122,7 @@ int main(int argc, char** argv)
/* 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[0], &cmp_res_vec[0], NULL, NULL);
benchmark_cmp_batch(5, cmp_args, &cmp_res_vec[0], NULL, NULL);
/* Print results */
const benchmark_cmp_result_array res_array = {

View File

@ -31,9 +31,6 @@ enum gmio_error
/*! No error occurred, success */
GMIO_ERROR_OK = 0,
/*! Pointer on argument gmio_rwargs is NULL */
GMIO_ERROR_NULL_RWARGS,
/*! Pointer on argument memory block is NULL */
GMIO_ERROR_NULL_MEMBLOCK,

View File

@ -22,12 +22,13 @@
struct gmio_memblock_helper
{
struct gmio_memblock* memblock;
struct gmio_memblock memblock;
bool was_allocated;
};
GMIO_INLINE
struct gmio_memblock_helper gmio_memblock_helper(struct gmio_memblock* mblock);
struct gmio_memblock_helper gmio_memblock_helper(
const struct gmio_memblock* mblock);
GMIO_INLINE
void gmio_memblock_helper_release(struct gmio_memblock_helper* helper);
@ -38,22 +39,26 @@ void gmio_memblock_helper_release(struct gmio_memblock_helper* helper);
* Implementation
*/
struct gmio_memblock_helper gmio_memblock_helper(struct gmio_memblock* mblock)
struct gmio_memblock_helper gmio_memblock_helper(
const struct gmio_memblock* mblock)
{
struct gmio_memblock_helper helper = {0};
helper.memblock = mblock;
if (mblock != NULL && (mblock->ptr == NULL || mblock->size == 0)) {
*(helper.memblock) = gmio_memblock_default();
if (gmio_memblock_isnull(mblock)) {
helper.memblock = gmio_memblock_default();
helper.was_allocated = true;
}
else {
helper.memblock = *mblock;
}
return helper;
}
void gmio_memblock_helper_release(struct gmio_memblock_helper* helper)
{
if (helper != NULL && helper->was_allocated) {
gmio_memblock_deallocate(helper->memblock);
helper->memblock = NULL;
const struct gmio_memblock mblock_null = {0};
gmio_memblock_deallocate(&helper->memblock);
helper->memblock = mblock_null;
helper->was_allocated = false;
}
}

View File

@ -1,43 +0,0 @@
/****************************************************************************
** gmio
** Copyright Fougue (2 Mar. 2015)
** contact@fougue.pro
**
** This software is a reusable library whose purpose is to provide complete
** I/O support for various CAD file formats (eg. STL)
**
** 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".
****************************************************************************/
#ifndef GMIO_INTERNAL_HELPER_RWARGS_H
#define GMIO_INTERNAL_HELPER_RWARGS_H
#include "../rwargs.h"
#include "helper_task_iface.h"
/*! Safe and convenient function for gmio_task_iface::func_is_stop_requested()
* through gmio_transfer::task_iface
*/
GMIO_INLINE bool gmio_rwargs_is_stop_requested(
const struct gmio_rwargs* args)
{
if (args != NULL)
return gmio_task_iface_is_stop_requested(&args->task_iface);
return false;
}
/*! Safe and convenient function for gmio_task_iface::func_handle_progress()
* through gmio_transfer::task_iface
*/
GMIO_INLINE void gmio_rwargs_handle_progress(
const struct gmio_rwargs* args, size_t value, size_t max_value)
{
if (args != NULL)
gmio_task_iface_handle_progress(&args->task_iface, value, max_value);
}
#endif /* GMIO_INTERNAL_HELPER_RWARGS_H */

View File

@ -17,6 +17,11 @@
#include <stdlib.h>
bool gmio_memblock_isnull(const struct gmio_memblock *mblock)
{
return mblock == NULL || mblock->ptr == NULL || mblock->size == 0;
}
struct gmio_memblock gmio_memblock(
void* ptr, size_t size, void (*func_deallocate)(void*))
{

View File

@ -43,6 +43,9 @@ struct gmio_memblock
GMIO_C_LINKAGE_BEGIN
/*! Returns true if \p mblock is NULL or points to null/void memory */
GMIO_LIB_EXPORT bool gmio_memblock_isnull(const struct gmio_memblock* mblock);
/*! Returns an initialized gmio_memblock object
*
* If \p ptr is NULL then gmio_memblock::size is forced to \c 0

View File

@ -1,49 +0,0 @@
/****************************************************************************
** gmio
** Copyright Fougue (2 Mar. 2015)
** contact@fougue.pro
**
** This software is a reusable library whose purpose is to provide complete
** I/O support for various CAD file formats (eg. STL)
**
** 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".
****************************************************************************/
/*! \file rwargs.h
* Declares structures for the common arguments of read/write operations
*
* \addtogroup gmio_core
* @{
*/
#ifndef GMIO_RWARGS_H
#define GMIO_RWARGS_H
#include "stream.h"
#include "memblock.h"
#include "task_iface.h"
/*! Common arguments for read or write functions */
struct gmio_rwargs
{
/*! The stream object to be used for I/O */
struct gmio_stream stream;
/*! Optional memory block used by the stream to bufferize I/O device
* operations
*
* If passed null to a read/write function, then a temporary memblock
* is created with the global default constructor function (see
* gmio_memblock_default())
*/
struct gmio_memblock stream_memblock;
/*! Optional interface by which the I/O operation can be controlled */
struct gmio_task_iface task_iface;
};
#endif /* GMIO_RWARGS_H */

View File

@ -16,30 +16,22 @@
#include "stl_rw_common.h"
#include "../../gmio_core/error.h"
#include "../../gmio_core/rwargs.h"
#include "../stl_error.h"
#include "../stl_io.h"
bool gmio_check_rwargs(int *error, const struct gmio_rwargs* args)
bool gmio_check_memblock(int *error, const struct gmio_memblock* mblock)
{
if (args == NULL) {
*error = GMIO_ERROR_NULL_RWARGS;
}
else {
if (args->stream_memblock.ptr == NULL)
*error = GMIO_ERROR_NULL_MEMBLOCK;
else if (args->stream_memblock.size == 0)
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
}
if (mblock == NULL || mblock->ptr == NULL)
*error = GMIO_ERROR_NULL_MEMBLOCK;
else if (mblock->size == 0)
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
return gmio_no_error(*error);
}
bool gmio_check_memblock(int *error, const struct gmio_memblock* mblock)
bool gmio_check_memblock_size(
int *error, const struct gmio_memblock *mblock, size_t minsize)
{
if (mblock->ptr == NULL)
*error = GMIO_ERROR_NULL_MEMBLOCK;
else if (mblock->size == 0)
if (gmio_check_memblock(error, mblock) && mblock->size < minsize)
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
return gmio_no_error(*error);
}
@ -51,25 +43,22 @@ bool gmio_stl_check_mesh(int *error, const struct gmio_stl_mesh* mesh)
{
*error = GMIO_STL_ERROR_NULL_FUNC_GET_TRIANGLE;
}
return gmio_no_error(*error);
}
bool gmio_stlb_check_params(
int *error,
const struct gmio_rwargs* args,
enum gmio_endianness byte_order)
bool gmio_stlb_check_byteorder(int* error, enum gmio_endianness byte_order)
{
if (!gmio_check_rwargs(error, args))
return false;
if (args->stream_memblock.size < GMIO_STLB_MIN_CONTENTS_SIZE)
*error = GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
if (byte_order != GMIO_ENDIANNESS_LITTLE
&& byte_order != GMIO_ENDIANNESS_BIG)
{
*error = GMIO_STL_ERROR_UNSUPPORTED_BYTE_ORDER;
}
return gmio_no_error(*error);
}
bool gmio_stla_check_float32_precision(int *error, uint8_t prec)
{
if (prec == 0 || prec > 9)
*error = GMIO_STL_ERROR_INVALID_FLOAT32_PREC;
return gmio_no_error(*error);
}

View File

@ -24,17 +24,17 @@
#include "../../gmio_core/endian.h"
struct gmio_memblock;
struct gmio_rwargs;
struct gmio_stl_mesh;
bool gmio_check_rwargs(int* error, const struct gmio_rwargs* args);
bool gmio_check_memblock(int* error, const struct gmio_memblock* mblock);
bool gmio_check_memblock_size(
int* error, const struct gmio_memblock* mblock, size_t minsize);
bool gmio_stl_check_mesh(int* error, const struct gmio_stl_mesh* mesh);
bool gmio_stlb_check_params(
int* error,
const struct gmio_rwargs* args,
enum gmio_endianness byte_order);
bool gmio_stla_check_float32_precision(int* error, uint8_t prec);
bool gmio_stlb_check_byteorder(int* error, enum gmio_endianness byte_order);
#endif /* GMIO_INTERNAL_STLB_RW_COMMON_H */

View File

@ -16,6 +16,7 @@
#include "stla_infos_get.h"
#include "../../gmio_core/error.h"
#include "../../gmio_core/internal/helper_memblock.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/stringstream.h"
#include "../stl_error.h"
@ -109,7 +110,10 @@ static void gmio_stringstream_stla_read_hook(
}
int gmio_stla_infos_get(
struct gmio_stl_infos_get_args* args, unsigned flags)
struct gmio_stl_infos* infos,
struct gmio_stream stream,
unsigned flags,
const struct gmio_stl_infos_get_options* opts)
{
const bool flag_facet_count =
(flags & GMIO_STL_INFO_FLAG_FACET_COUNT) != 0;
@ -118,10 +122,9 @@ int gmio_stla_infos_get(
const bool flag_stla_solidname =
(flags & GMIO_STLA_INFO_FLAG_SOLIDNAME) != 0;
struct gmio_stl_infos* infos = &args->infos;
void* const mblock_ptr = args->stream_memblock.ptr;
void* const mblock_ptr = opts->stream_memblock.ptr;
/* Leave one byte to end the string buffer with 0 */
const size_t mblock_size = args->stream_memblock.size - 1;
const size_t mblock_size = opts->stream_memblock.size - 1;
struct gmio_stla_parse_data parse_data = {0};
struct gmio_stringstream* sstream = &parse_data.strstream;
struct gmio_string* strbuff = &parse_data.strbuff;
@ -131,13 +134,11 @@ int gmio_stla_infos_get(
if (flags == 0)
return err;
if (!gmio_check_memblock(&err, &args->stream_memblock))
if (!gmio_check_memblock(&err, &opts->stream_memblock))
return err;
parse_data.strstream =
gmio_stringstream(
args->stream,
gmio_string(mblock_ptr, 0, mblock_size));
gmio_stringstream(stream, gmio_string(mblock_ptr, 0, mblock_size));
parse_data.strstream.func_stream_read_hook = gmio_stringstream_stla_read_hook;
parse_data.strbuff = gmio_string(fixed_buffer, 0, sizeof(fixed_buffer));
@ -190,139 +191,3 @@ int gmio_stla_infos_get(
return err;
}
#if 0
#include "../../gmio_core/error.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/string.h"
#include "../../gmio_core/internal/string_ascii_utils.h"
#include "stl_rw_common.h"
#include <string.h>
static uint32_t stla_facet_count(
const struct gmio_string* strbuff, const char* end_ptr)
{
const char* substr_at = NULL;
size_t strbuff_pos = 0;
uint32_t facet_count = 0;
do {
substr_at = gmio_ascii_istrstr(strbuff->ptr + strbuff_pos, "endfacet");
if (substr_at != NULL && substr_at < end_ptr) {
++facet_count;
/* Note: strlen("endfacet") == 8 */
strbuff_pos = (substr_at - strbuff->ptr) + 8;
}
else {
substr_at = NULL;
}
} while (substr_at != NULL);
return facet_count;
}
enum {
BUFF_OVERLAP_SIZE = 14,
BUFF_OVERLAP_SIZE_DIV2 = BUFF_OVERLAP_SIZE / 2
};
int gmio_stla_infos_get(
struct gmio_stl_infos_get_args* args, unsigned flags)
{
struct gmio_stream* stream = &args->stream;
struct gmio_stl_infos* infos = &args->infos;
void* mblock_ptr = args->stream_memblock.ptr;
/* Leave one byte to end the string buffer with 0 */
const size_t mblock_size = args->stream_memblock.size - 1;
struct gmio_string strbuff = gmio_string(mblock_ptr, 0, mblock_size);
const bool flag_facet_count =
(flags & GMIO_STL_INFO_FLAG_FACET_COUNT) != 0;
const bool flag_size =
(flags & GMIO_STL_INFO_FLAG_SIZE) != 0;
const bool flag_stla_solidname =
(flags & GMIO_STLA_INFO_FLAG_SOLIDNAME) != 0;
int err = GMIO_ERROR_OK;
if (!gmio_check_memblock(&err, &args->stream_memblock))
return err;
if (flags != 0) {
/* 'overlap' stores the ending/starting bytes of the previous/current
* stream buffers(memblock) */
char overlap[14] = {0}; /* 14 == 2*(strlen("endfacet") - 1) */
bool endsolid_found = false;
while (!endsolid_found && gmio_no_error(err)) {
const char* substr_at = NULL;
const size_t read_size =
gmio_stream_read(stream, mblock_ptr, 1, mblock_size);
const int stream_err = gmio_stream_error(&args->stream);
const bool overlap_has_contents = overlap[0] != 0;
bool endsolid_in_overlap = false;
err = stream_err;
strbuff.len = read_size;
strbuff.ptr[strbuff.len] = 0;
/* Copy first half of overlap buffer */
if (overlap_has_contents) {
strncpy(&overlap[BUFF_OVERLAP_SIZE_DIV2],
mblock_ptr,
GMIO_MIN(BUFF_OVERLAP_SIZE_DIV2, read_size));
}
/* Find "endsolid" in overlap */
if (overlap_has_contents) {
substr_at = strstr(overlap, "endsolid");
endsolid_found = substr_at != NULL;
endsolid_in_overlap = endsolid_found;
}
/* Find "endsolid" in memblock */
if (!endsolid_found) {
substr_at = gmio_ascii_istrstr(strbuff.ptr, "endsolid");
endsolid_found = substr_at != NULL;
}
/* Update stream size */
if (flag_size) {
/* Note: strlen("endsolid") == 8 */
if (endsolid_found) {
if (!endsolid_in_overlap)
infos->size += (substr_at - strbuff.ptr) + 8;
/* TODO : gérer le cas où "endsolid" se trouve dans overlap */
}
else {
infos->size += read_size;
}
}
/* Find "endfacet" tokens */
if (flag_facet_count && !endsolid_in_overlap) {
const char* endsolid_ptr =
endsolid_found ? substr_at : gmio_string_end(&strbuff);
/* Check in overlap */
const bool endfacet_in_overlap =
overlap_has_contents
&& strstr(overlap, "endfacet") != NULL;
infos->facet_count += endfacet_in_overlap ? 1 : 0;
/* Check in memblock */
infos->facet_count += stla_facet_count(&strbuff, endsolid_ptr);
}
/* Copy second half of overlap buffer */
if (!endsolid_found && read_size >= BUFF_OVERLAP_SIZE_DIV2) {
memset(&overlap, 0, sizeof(overlap));
strncpy(overlap,
&strbuff.ptr[read_size - BUFF_OVERLAP_SIZE_DIV2],
GMIO_MIN(BUFF_OVERLAP_SIZE_DIV2, read_size));
}
}
}
return err;
}
#endif

View File

@ -20,6 +20,9 @@
/*! Find infos from a STL ASCII stream */
int gmio_stla_infos_get(
struct gmio_stl_infos_get_args* args, unsigned flags);
struct gmio_stl_infos* infos,
struct gmio_stream stream,
unsigned flags,
const struct gmio_stl_infos_get_options* opts);
#endif /* GMIO_INTERNAL_STLA_INFOS_GET_H */

View File

@ -43,12 +43,12 @@ enum gmio_stla_token
struct gmio_stringstream_stla_cookie
{
/* Copy of gmio_stla_read() corresponding argument */
struct gmio_rwargs* rwargs;
const struct gmio_task_iface* task;
/* Cache for the input stream size */
gmio_streamsize_t stream_size;
/* Offset (in bytes) from beginning of stream : current position */
gmio_streamoffset_t stream_offset;
/* Cache for gmio_transfer::func_is_stop_requested() */
/* Cache for gmio_task_iface::func_is_stop_requested() */
bool is_stop_requested;
};

View File

@ -20,9 +20,11 @@
#include "../stl_error.h"
#include "../../gmio_core/error.h"
#include "../../gmio_core/task_iface.h"
#include "../../gmio_core/text_format.h"
#include "../../gmio_core/internal/helper_rwargs.h"
#include "../../gmio_core/internal/helper_memblock.h"
#include "../../gmio_core/internal/helper_stream.h"
#include "../../gmio_core/internal/helper_task_iface.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/safe_cast.h"
@ -47,9 +49,11 @@
* Total with EOL(2 chars) = 307 + 7*2 = 321
*/
enum { GMIO_STLA_FACET_SIZE = 321 };
enum { GMIO_STLA_FACET_SIZE_P2 = 512 };
enum { GMIO_STLA_SOLID_NAME_MAX_LEN = 512 };
enum {
GMIO_STLA_FACET_SIZE = 321,
GMIO_STLA_FACET_SIZE_P2 = 512,
GMIO_STLA_SOLID_NAME_MAX_LEN = 512
};
/* Fucntions for raw strings(ie. "const char*") */
@ -121,49 +125,52 @@ GMIO_INLINE char* gmio_write_coords(
coords_format, coords->x, coords->y, coords->z);
}
GMIO_INLINE bool gmio_rwargs_flush_buffer(
struct gmio_rwargs* args, size_t n)
GMIO_INLINE bool gmio_stream_flush_buffer(
struct gmio_stream* stream, char* buffer, const char* buffer_offset)
{
const size_t write_count =
gmio_stream_write(
&args->stream, args->stream_memblock.ptr, sizeof(char), n);
const size_t n = buffer_offset - buffer;
const size_t write_count = gmio_stream_write(stream, buffer, 1, n);
return write_count == n;
}
int gmio_stla_write(struct gmio_stl_write_args* args)
int gmio_stla_write(
struct gmio_stream stream,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* opts)
{
/* Constants */
const struct gmio_stl_write_options* options = &args->options;
const gmio_stl_mesh_func_get_triangle_t func_mesh_get_triangle =
args->mesh.func_get_triangle;
const void* mesh_cookie = args->mesh.cookie;
const uint32_t total_facet_count = args->mesh.triangle_count;
const struct gmio_task_iface* task = opts != NULL ? &opts->task_iface : NULL;
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
const size_t mblock_size = mblock_helper.memblock.size;
const uint32_t total_facet_count = mesh.triangle_count;
const uint32_t buffer_facet_count =
gmio_size_to_uint32(
args->core.stream_memblock.size / GMIO_STLA_FACET_SIZE_P2);
const char* opt_solid_name = options->stla_solid_name;
gmio_size_to_uint32(mblock_size / GMIO_STLA_FACET_SIZE_P2);
const char* opt_solid_name = opts != NULL ? opts->stla_solid_name : NULL;
const char* solid_name = opt_solid_name != NULL ? opt_solid_name : "";
const enum gmio_float_text_format f32_format = options->stla_float32_format;
const uint8_t opt_f32_prec = options->stla_float32_prec;
const enum gmio_float_text_format f32_format =
opts != NULL ?
opts->stla_float32_format :
GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE;
const uint8_t opt_f32_prec = opts != NULL ? opts->stla_float32_prec : 9;
const uint8_t f32_prec = opt_f32_prec != 0 ? opt_f32_prec : 9;
const bool write_triangles_only = options->stl_write_triangles_only;
const bool write_triangles_only =
opts != NULL ? opts->stl_write_triangles_only : false;
/* Variables */
struct gmio_rwargs* core_args = &args->core;
void* const mblock_ptr = core_args->stream_memblock.ptr;
struct gmio_memblock* const mblock = &mblock_helper.memblock;
void* const mblock_ptr = mblock->ptr;
uint32_t ifacet = 0; /* for-loop counter on facets */
char coords_format_str[64] = {0}; /* printf-like format for XYZ coords */
int error = GMIO_ERROR_OK;
/* Check validity of input parameters */
if (!gmio_check_rwargs(&error, core_args))
return error;
if (!gmio_stl_check_mesh(&error, &args->mesh))
return error;
if (f32_prec == 0 || f32_prec > 9)
return GMIO_STL_ERROR_INVALID_FLOAT32_PREC;
if (core_args->stream_memblock.size < GMIO_STLA_FACET_SIZE_P2)
return GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
if (!gmio_check_memblock_size(&error, mblock, GMIO_STLA_FACET_SIZE_P2))
goto label_end;
if (!gmio_stl_check_mesh(&error, &mesh))
goto label_end;
if (!gmio_stla_check_float32_precision(&error, f32_prec))
goto label_end;
/* Create XYZ coords format string (for normal and vertex coords) */
{
@ -182,8 +189,10 @@ int gmio_stla_write(struct gmio_stl_write_args* args)
char* buffpos = mblock_ptr;
buffpos = gmio_write_rawstr(buffpos, "solid ");
buffpos = gmio_write_rawstr_eol(buffpos, solid_name);
if (!gmio_rwargs_flush_buffer(core_args, buffpos - (char*)mblock_ptr))
return GMIO_ERROR_STREAM;
if (!gmio_stream_flush_buffer(&stream, mblock_ptr, buffpos)) {
error = GMIO_ERROR_STREAM;
goto label_end;
}
}
/* Write solid's facets */
@ -197,14 +206,14 @@ int gmio_stla_write(struct gmio_stl_write_args* args)
uint32_t ibuffer_facet;
char* buffpos = mblock_ptr;
gmio_rwargs_handle_progress(core_args, ifacet, total_facet_count);
gmio_task_iface_handle_progress(task, ifacet, total_facet_count);
/* Writing of facets is buffered */
for (ibuffer_facet = ifacet;
ibuffer_facet < clamped_facet_count;
++ibuffer_facet)
{
func_mesh_get_triangle(mesh_cookie, ibuffer_facet, &tri);
mesh.func_get_triangle(mesh.cookie, ibuffer_facet, &tri);
buffpos = gmio_write_rawstr(buffpos, "facet normal ");
buffpos = gmio_write_coords(buffpos, coords_format_str, &tri.n);
@ -221,11 +230,11 @@ int gmio_stla_write(struct gmio_stl_write_args* args)
buffpos = gmio_write_rawstr(buffpos, "\nendfacet\n");
} /* end for (ibuffer_facet) */
if (!gmio_rwargs_flush_buffer(core_args, buffpos - (char*)mblock_ptr))
if (!gmio_stream_flush_buffer(&stream, mblock_ptr, buffpos))
error = GMIO_ERROR_STREAM;
/* Task control */
if (gmio_no_error(error) && gmio_rwargs_is_stop_requested(core_args))
if (gmio_no_error(error) && gmio_task_iface_is_stop_requested(task))
error = GMIO_ERROR_TRANSFER_STOPPED;
} /* end for (ifacet) */
@ -234,9 +243,11 @@ int gmio_stla_write(struct gmio_stl_write_args* args)
char* buffpos = mblock_ptr;
buffpos = gmio_write_rawstr(buffpos, "endsolid ");
buffpos = gmio_write_rawstr_eol(buffpos, solid_name);
if (!gmio_rwargs_flush_buffer(core_args, buffpos - (char*)mblock_ptr))
if (!gmio_stream_flush_buffer(&stream, mblock_ptr, buffpos))
error = GMIO_ERROR_STREAM;
}
label_end:
gmio_memblock_helper_release(&mblock_helper);
return error;
}

View File

@ -16,14 +16,19 @@
#ifndef GMIO_INTERNAL_STLA_WRITE_H
#define GMIO_INTERNAL_STLA_WRITE_H
#include "../stl_rwargs.h"
#include "../../gmio_core/stream.h"
#include "../stl_io_options.h"
#include "../stl_mesh.h"
/*! Writes geometry in the STL ascii format
*
* \return Error code (see gmio_core/error.h and stl_error.h)
* \retval GMIO_ERROR_INVALID_MEMBLOCK_SIZE
* if <tt>args->core.memblock.size < 512</tt>
* if <tt>options->stream_memblock.size < 512</tt>
*/
int gmio_stla_write(struct gmio_stl_write_args* args);
int gmio_stla_write(
struct gmio_stream stream,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* options);
#endif /* GMIO_INTERNAL_STLA_WRITE_H */

View File

@ -20,19 +20,31 @@
#include <string.h>
static enum gmio_endianness gmio_stl_format_to_endianness(
enum gmio_stl_format format)
{
if (format == GMIO_STL_FORMAT_BINARY_BE)
return GMIO_ENDIANNESS_BIG;
else if (format == GMIO_STL_FORMAT_BINARY_LE)
return GMIO_ENDIANNESS_LITTLE;
return GMIO_ENDIANNESS_UNKNOWN;
}
int gmio_stlb_infos_get(
struct gmio_stl_infos_get_args* args,
enum gmio_endianness byte_order,
unsigned flags)
struct gmio_stl_infos* infos,
struct gmio_stream stream,
unsigned flags,
const struct gmio_stl_infos_get_options* opts)
{
if (flags != 0) {
struct gmio_stl_infos* infos = &args->infos;
const enum gmio_endianness byte_order =
gmio_stl_format_to_endianness(opts->format_hint);
uint32_t facet_count = 0;
uint8_t buff[GMIO_STLB_HEADER_SIZE + sizeof(uint32_t)];
{ /* Read header and facet count into buff */
const size_t read_size =
gmio_stream_read(&args->stream, buff, 1, sizeof(buff));
gmio_stream_read(&stream, buff, 1, sizeof(buff));
if (read_size != sizeof(buff))
return GMIO_ERROR_STREAM;
}

View File

@ -21,8 +21,9 @@
/*! Find infos from a STL binary stream */
int gmio_stlb_infos_get(
struct gmio_stl_infos_get_args* args,
enum gmio_endianness byte_order,
unsigned flags);
struct gmio_stl_infos* infos,
struct gmio_stream stream,
unsigned flags,
const struct gmio_stl_infos_get_options* opts);
#endif /* GMIO_INTERNAL_STLB_INFOS_GET_H */

View File

@ -24,8 +24,9 @@
#include "../../gmio_core/error.h"
#include "../../gmio_core/internal/byte_codec.h"
#include "../../gmio_core/internal/helper_memblock.h"
#include "../../gmio_core/internal/helper_task_iface.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/helper_rwargs.h"
#include "../../gmio_core/internal/helper_stream.h"
#include "../../gmio_core/internal/safe_cast.h"
@ -85,39 +86,45 @@ static void gmio_stlb_encode_facets_byteswap(
}
int gmio_stlb_write(
struct gmio_stl_write_args* args, enum gmio_endianness byte_order)
enum gmio_endianness byte_order,
struct gmio_stream stream,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* opts)
{
/* Constants */
const uint32_t facet_count = args->mesh.triangle_count;
const struct gmio_task_iface* task = opts != NULL ? &opts->task_iface : NULL;
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
const size_t mblock_size = mblock_helper.memblock.size;
const uint32_t facet_count = mesh.triangle_count;
const func_gmio_stlb_encode_facets_t func_encode_facets =
byte_order != GMIO_ENDIANNESS_HOST ?
gmio_stlb_encode_facets_byteswap :
gmio_stlb_encode_facets;
const bool write_triangles_only =
opts != NULL ? opts->stl_write_triangles_only : false;
const struct gmio_stlb_header* header =
opts != NULL ? &opts->stlb_header : NULL;
/* Variables */
struct gmio_rwargs* core_args = &args->core;
uint32_t i_facet = 0; /* Facet counter */
uint32_t write_facet_count =
gmio_size_to_uint32(
core_args->stream_memblock.size / GMIO_STLB_TRIANGLE_RAWSIZE);
void* mblock_ptr = core_args->stream_memblock.ptr;
gmio_size_to_uint32(mblock_size / GMIO_STLB_TRIANGLE_RAWSIZE);
void* const mblock_ptr = mblock_helper.memblock.ptr;
int error = GMIO_ERROR_OK;
/* Check validity of input parameters */
if (!gmio_stl_check_mesh(&error, &args->mesh)
|| !gmio_stlb_check_params(&error, core_args, byte_order))
{
return error;
}
if (!gmio_check_memblock(&error, &mblock_helper.memblock))
goto label_end;
if (!gmio_stl_check_mesh(&error, &mesh))
goto label_end;
if (!gmio_stlb_check_byteorder(&error, byte_order))
goto label_end;
if (!args->options.stl_write_triangles_only) {
error = gmio_stlb_write_header(
&core_args->stream,
byte_order,
args->options.stlb_header,
facet_count);
if (!write_triangles_only) {
error = gmio_stlb_write_header(&stream, byte_order, header, facet_count);
if (gmio_error(error))
return error;
goto label_end;
}
/* Write triangles */
@ -125,16 +132,15 @@ int gmio_stlb_write(
i_facet < facet_count && gmio_no_error(error);
i_facet += write_facet_count)
{
gmio_rwargs_handle_progress(core_args, i_facet, facet_count);
gmio_task_iface_handle_progress(task, i_facet, facet_count);
/* Write to memory block */
write_facet_count = GMIO_MIN(write_facet_count, facet_count - i_facet);
func_encode_facets(
&args->mesh, mblock_ptr, write_facet_count, i_facet);
func_encode_facets(&mesh, mblock_ptr, write_facet_count, i_facet);
/* Write memory block to stream */
if (gmio_stream_write(
&core_args->stream,
&stream,
mblock_ptr,
GMIO_STLB_TRIANGLE_RAWSIZE,
write_facet_count)
@ -144,9 +150,11 @@ int gmio_stlb_write(
}
/* Handle stop request */
if (gmio_no_error(error) && gmio_rwargs_is_stop_requested(core_args))
if (gmio_no_error(error) && gmio_task_iface_is_stop_requested(task))
error = GMIO_ERROR_TRANSFER_STOPPED;
} /* end for */
label_end:
gmio_memblock_helper_release(&mblock_helper);
return error;
}

View File

@ -16,16 +16,21 @@
#ifndef GMIO_INTERNAL_STLB_WRITE_H
#define GMIO_INTERNAL_STLB_WRITE_H
#include "../stl_rwargs.h"
#include "../../gmio_core/endian.h"
#include "../../gmio_core/stream.h"
#include "../stl_io_options.h"
#include "../stl_mesh.h"
/*! Writes geometry in the STL binary format
*
* \return Error code (see error.h and stl_error.h)
* \retval GMIO_INVALID_MEMBLOCK_SIZE_ERROR
* if <tt>args->core.memblock.size < GMIO_STLB_MIN_CONTENTS_SIZE</tt>
* if <tt>opts->stream_memblock.size < GMIO_STLB_MIN_CONTENTS_SIZE</tt>
*/
int gmio_stlb_write(
struct gmio_stl_write_args* args, enum gmio_endianness byte_order);
enum gmio_endianness byte_order,
struct gmio_stream stream,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* opts);
#endif /* GMIO_INTERNAL_STLB_WRITE_H */

View File

@ -45,3 +45,4 @@ enum gmio_stl_constants
};
#endif /* GMIO_STL_CONSTANTS_H */
/*! @} */

View File

@ -19,36 +19,57 @@
#include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_stream.h"
#include "stl_error.h"
#include "stl_format.h"
#include "internal/stla_infos_get.h"
#include "internal/stlb_infos_get.h"
int gmio_stl_infos_get(
struct gmio_stl_infos_get_args* args,
enum gmio_stl_format format,
unsigned flags)
struct gmio_stl_infos* infos,
struct gmio_stream stream,
unsigned flags,
const struct gmio_stl_infos_get_options* opts)
{
int error = GMIO_ERROR_OK;
const struct gmio_streampos begin_streampos = gmio_streampos(&stream, NULL);
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(&args->stream_memblock);
const struct gmio_streampos begin_streampos =
gmio_streampos(&args->stream, NULL);
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
enum gmio_stl_format format =
opts != NULL ? opts->format_hint : GMIO_STL_FORMAT_UNKNOWN;
struct gmio_stl_infos_get_options ovrdn_opts = {0};
if (opts != NULL)
ovrdn_opts = *opts;
ovrdn_opts.stream_memblock = mblock_helper.memblock;
/* Guess format when left unspecified */
if (format == GMIO_STL_FORMAT_UNKNOWN) {
format = gmio_stl_get_format(&stream);
if (format == GMIO_STL_FORMAT_UNKNOWN) {
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
goto label_end;
}
ovrdn_opts.format_hint = format;
}
if (flags & GMIO_STL_INFO_FLAG_FORMAT)
infos->format = format;
/* Dispatch to the sub-function */
switch (format) {
case GMIO_STL_FORMAT_ASCII:
error = gmio_stla_infos_get(args, flags);
error = gmio_stla_infos_get(infos, stream, flags, &ovrdn_opts);
break;
case GMIO_STL_FORMAT_BINARY_LE:
error = gmio_stlb_infos_get(args, GMIO_ENDIANNESS_LITTLE, flags);
break;
case GMIO_STL_FORMAT_BINARY_BE:
error = gmio_stlb_infos_get(args, GMIO_ENDIANNESS_BIG, flags);
error = gmio_stlb_infos_get(infos, stream, flags, &ovrdn_opts);
break;
default:
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
break;
}
gmio_memblock_helper_release(&mblock_helper);
gmio_stream_set_pos(&args->stream, &begin_streampos);
label_end:
gmio_stream_set_pos(&stream, &begin_streampos);
gmio_memblock_helper_release(&mblock_helper);
return error;
}

View File

@ -14,7 +14,7 @@
****************************************************************************/
/*! \file stl_infos.h
* TODO: description
* Retrieval of STL infos from input stream
*
* \addtogroup gmio_stl
* @{
@ -25,7 +25,6 @@
#include "stl_global.h"
#include "../gmio_core/rwargs.h"
#include "../gmio_core/internal/helper_stream.h"
#include <stddef.h>
@ -36,6 +35,9 @@
/*! Informations retrieved by gmio_stl_infos_get() */
struct gmio_stl_infos
{
/*! STL format of the input stream */
enum stl_format format;
/*! Count of facets(triangles) */
uint32_t facet_count;
@ -78,24 +80,28 @@ enum gmio_stl_info_flag
GMIO_STL_INFO_FLAG_SOLIDNAME_OR_HEADER =
GMIO_STLA_INFO_FLAG_SOLIDNAME | GMIO_STLB_INFO_FLAG_HEADER,
/*! -> gmio_stl_infos::format */
GMIO_STL_INFO_FLAG_FORMAT = 0x0010,
/*! All infos */
GMIO_STL_INFO_FLAG_ALL = 0xFFFF
};
/*! Objects to be passed to gmio_stl_infos_get() */
struct gmio_stl_infos_get_args
/*! Optional parameters of gmio_stl_infos_get() */
struct gmio_stl_infos_get_options
{
/*! Input stream */
struct gmio_stream stream;
/*! Optional memory block used by the stream to bufferize read operations
*
* If null, then a temporary memblock is created with the global default
* constructor function (see gmio_memblock_default()) */
/*! See gmio_core_readwrite_options::stream_memblock */
struct gmio_memblock stream_memblock;
/*! Output informations */
struct gmio_stl_infos infos;
/*! Assume STL input format, if GMIO_STL_FORMAT_UNKNOWN then it is
* automatically guessed */
enum gmio_stl_format format_hint;
/*! Restrict gmio_stl_infos_get() to not read further this limit
*
* Not yet supported
*/
gmio_streamsize_t size_limit;
};
GMIO_C_LINKAGE_BEGIN
@ -106,9 +112,10 @@ GMIO_C_LINKAGE_BEGIN
*/
GMIO_LIBSTL_EXPORT
int gmio_stl_infos_get(
struct gmio_stl_infos_get_args* args,
enum gmio_stl_format format,
unsigned flags);
struct gmio_stl_infos* infos,
struct gmio_stream stream,
unsigned flags, /*!< Bitor combination of gmio_stl_info_flag values */
const struct gmio_stl_infos_get_options* options);
GMIO_C_LINKAGE_END

View File

@ -23,112 +23,75 @@
#include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_stream.h"
int gmio_stl_read(struct gmio_stl_read_args* args)
int gmio_stl_read(
struct gmio_stream stream,
struct gmio_stl_mesh_creator mesh_creator,
const struct gmio_stl_read_options* options)
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
const enum gmio_stl_format format =
gmio_stl_get_format(&args->core.stream);
switch (format) {
case GMIO_STL_FORMAT_ASCII: {
error = gmio_stla_read(args);
break;
}
case GMIO_STL_FORMAT_BINARY_BE: {
error = gmio_stlb_read(args, GMIO_ENDIANNESS_BIG);
break;
}
case GMIO_STL_FORMAT_BINARY_LE: {
error = gmio_stlb_read(args, GMIO_ENDIANNESS_LITTLE);
break;
}
case GMIO_STL_FORMAT_UNKNOWN: {
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
break;
}
} /* end switch() */
const enum gmio_stl_format format = gmio_stl_get_format(&stream);
switch (format) {
case GMIO_STL_FORMAT_ASCII:
return gmio_stla_read(stream, mesh_creator, options);
case GMIO_STL_FORMAT_BINARY_BE:
return gmio_stlb_read(
stream, mesh_creator, GMIO_ENDIANNESS_BIG, options);
case GMIO_STL_FORMAT_BINARY_LE:
return gmio_stlb_read(
stream, mesh_creator, GMIO_ENDIANNESS_LITTLE, options);
case GMIO_STL_FORMAT_UNKNOWN:
return GMIO_STL_ERROR_UNKNOWN_FORMAT;
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
return GMIO_ERROR_UNKNOWN;
}
int gmio_stl_read_file(
struct gmio_stl_read_args* args, const char* filepath)
const char* filepath,
struct gmio_stl_mesh_creator mesh_creator,
const struct gmio_stl_read_options* options)
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
FILE* file = fopen(filepath, "rb");
if (file != NULL) {
args->core.stream = gmio_stream_stdio(file);
error = gmio_stl_read(args);
fclose(file);
}
else {
error = GMIO_ERROR_STDIO;
}
FILE* file = fopen(filepath, "rb");
if (file != NULL) {
const int error =
gmio_stl_read(gmio_stream_stdio(file), mesh_creator, options);
fclose(file);
return error;
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
return GMIO_ERROR_STDIO;
}
int gmio_stl_write(
struct gmio_stl_write_args* args, enum gmio_stl_format format)
enum gmio_stl_format format,
struct gmio_stream stream,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* options)
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(&args->core.stream_memblock);
switch (format) {
case GMIO_STL_FORMAT_ASCII: {
error = gmio_stla_write(args);
break;
}
case GMIO_STL_FORMAT_BINARY_BE: {
error = gmio_stlb_write(args, GMIO_ENDIANNESS_BIG);
break;
}
case GMIO_STL_FORMAT_BINARY_LE: {
error = gmio_stlb_write(args, GMIO_ENDIANNESS_LITTLE);
break;
}
case GMIO_STL_FORMAT_UNKNOWN: {
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
}
} /* end switch() */
gmio_memblock_helper_release(&mblock_helper);
switch (format) {
case GMIO_STL_FORMAT_ASCII:
return gmio_stla_write(stream, mesh, options);
case GMIO_STL_FORMAT_BINARY_BE:
return gmio_stlb_write(GMIO_ENDIANNESS_BIG, stream, mesh, options);
case GMIO_STL_FORMAT_BINARY_LE:
return gmio_stlb_write(GMIO_ENDIANNESS_LITTLE, stream, mesh, options);
case GMIO_STL_FORMAT_UNKNOWN:
return GMIO_STL_ERROR_UNKNOWN_FORMAT;
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
return GMIO_ERROR_UNKNOWN;
}
int gmio_stl_write_file(
struct gmio_stl_write_args* args,
enum gmio_stl_format format,
const char* filepath)
const char* filepath,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* options)
{
int error = GMIO_ERROR_OK;
if (args != NULL) {
FILE* file = fopen(filepath, "wb");
if (file != NULL) {
args->core.stream = gmio_stream_stdio(file);
error = gmio_stl_write(args, format);
fclose(file);
}
else {
error = GMIO_ERROR_STDIO;
}
FILE* file = fopen(filepath, "wb");
if (file != NULL) {
const int error =
gmio_stl_write(format, gmio_stream_stdio(file), mesh, options);
fclose(file);
return error;
}
else {
error = GMIO_ERROR_NULL_RWARGS;
}
return error;
return GMIO_ERROR_STDIO;
}
static const struct gmio_stlb_header internal_stlb_zero_header = {0};

View File

@ -24,7 +24,12 @@
#define GMIO_STL_IO_H
#include "stl_global.h"
#include "stl_rwargs.h"
#include "stl_format.h"
#include "stl_io_options.h"
#include "stl_mesh.h"
#include "stl_mesh_creator.h"
#include "../gmio_core/stream.h"
#include "../gmio_core/endian.h"
GMIO_C_LINKAGE_BEGIN
@ -33,37 +38,46 @@ GMIO_C_LINKAGE_BEGIN
*
* \return Error code (see gmio_core/error.h and stl_error.h)
*/
GMIO_LIBSTL_EXPORT
int gmio_stl_read(struct gmio_stl_read_args* args);
GMIO_LIBSTL_EXPORT int gmio_stl_read(
struct gmio_stream stream,
struct gmio_stl_mesh_creator mesh_creator,
const struct gmio_stl_read_options* options);
/*! Reads STL mesh from a file, format is automatically guessed
*
* This is just a facility function over gmio_stl_read(). The stream object
* pointed to by \c args->core.stream is automatically initialized to read file
* at \p filepath (see gmio_stream_stdio(FILE*))
* This is just a facility function over gmio_stl_read(). The internal stream
* object is created to read file at \p filepath (see gmio_stream_stdio(FILE*))
*
* The file is opened with fopen() so \p filepath shall follow the file name
* specifications of the running environment
*/
GMIO_LIBSTL_EXPORT
int gmio_stl_read_file(struct gmio_stl_read_args* args, const char* filepath);
GMIO_LIBSTL_EXPORT int gmio_stl_read_file(
const char* filepath,
struct gmio_stl_mesh_creator mesh_creator,
const struct gmio_stl_read_options* options);
/*! Reads geometry from STL ascii stream
*
* \return Error code (see gmio_core/error.h and stl_error.h)
*/
GMIO_LIBSTL_EXPORT
int gmio_stla_read(struct gmio_stl_read_args* args);
int gmio_stla_read(
struct gmio_stream stream,
struct gmio_stl_mesh_creator mesh_creator,
const struct gmio_stl_read_options* options);
/*! Reads geometry from STL binary stream
*
* \return Error code (see gmio_core/error.h and stl_error.h)
* \retval GMIO_ERROR_INVALID_MEMBLOCK_SIZE
* if <tt>args->core.stream_memblock.size < GMIO_STLB_MIN_CONTENTS_SIZE</tt>
* if <tt>options->stream_memblock.size < GMIO_STLB_MIN_CONTENTS_SIZE</tt>
*/
GMIO_LIBSTL_EXPORT
int gmio_stlb_read(
struct gmio_stl_read_args* args, enum gmio_endianness byte_order);
struct gmio_stream stream,
struct gmio_stl_mesh_creator mesh_creator,
enum gmio_endianness byte_order,
const struct gmio_stl_read_options* options);
/*! Writes STL mesh to stream
*
@ -71,13 +85,15 @@ int gmio_stlb_read(
*/
GMIO_LIBSTL_EXPORT
int gmio_stl_write(
struct gmio_stl_write_args* args, enum gmio_stl_format format);
enum gmio_stl_format format,
struct gmio_stream stream,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* options);
/*! Writes STL mesh to stream
*
* This is just a facility function over gmio_stl_write(). The stream object
* pointed to by \c args->core.stream is automatically initialized to write
* file to \p filepath (see gmio_stream_stdio(FILE*))
* This is just a facility function over gmio_stl_write(). The internal stream
* object is created to read file at \p filepath (see gmio_stream_stdio(FILE*))
*
* The file is opened with fopen() so \p filepath shall follow the file name
* specifications of the running environment
@ -86,9 +102,10 @@ int gmio_stl_write(
*/
GMIO_LIBSTL_EXPORT
int gmio_stl_write_file(
struct gmio_stl_write_args* args,
enum gmio_stl_format format,
const char* filepath);
const char* filepath,
struct gmio_stl_mesh mesh,
const struct gmio_stl_write_options* options);
/*! Writes STL binary header data to stream
*

View File

@ -26,11 +26,45 @@
#include "stl_global.h"
#include "stlb_header.h"
#include "../gmio_core/endian.h"
#include "../gmio_core/stream.h"
#include "../gmio_core/task_iface.h"
#include "../gmio_core/text_format.h"
struct gmio_stl_read_options
{
/*! Used by the stream to bufferize I/O operations
*
* If null(see gmio_memblock_isnull()), then a temporary memblock is
* created with the global default constructor function
* (see gmio_memblock_default()) */
struct gmio_memblock stream_memblock;
/*! Optional interface by which the I/O operation can be controlled */
struct gmio_task_iface task_iface;
/*! Optional pointer to a function that returns the size(in bytes) of the
* STL ascii data to read
*
* Useful only with STL ascii format. If set to NULL then by default
* gmio_stream::func_size() is called.
*
* The resulting stream size is passed to
* gmio_task_iface::func_handle_progress() as the \p max_value argument.
*/
gmio_streamsize_t (*func_stla_get_streamsize)(
struct gmio_stream* stream,
struct gmio_memblock* stream_memblock);
};
/*! Options for gmio_stl_write() */
struct gmio_stl_write_options
{
/*! See gmio_stl_read_options::stream_memblock */
struct gmio_memblock stream_memblock;
/*! See gmio_stl_read_options::task_iface */
struct gmio_task_iface task_iface;
/*! Flag allowing to skip writting of any header/footer data, but just
* triangles
*
@ -78,7 +112,7 @@ struct gmio_stl_write_options
* \li calling gmio_stl_write() with <tt>options == NULL</tt>
* \li OR <tt>stlb_header_data == NULL</tt>
*/
const struct gmio_stlb_header* stlb_header;
struct gmio_stlb_header stlb_header;
};
#endif /* GMIO_STL_IO_OPTIONS_H */

View File

@ -1,70 +0,0 @@
/****************************************************************************
** gmio
** Copyright Fougue (2 Mar. 2015)
** contact@fougue.pro
**
** This software is a reusable library whose purpose is to provide complete
** I/O support for various CAD file formats (eg. STL)
**
** 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".
****************************************************************************/
/*! \file stl_rwargs.h
* Read/write structures for STL
*
* \addtogroup gmio_stl
* @{
*/
#ifndef GMIO_STL_RWARGS_H
#define GMIO_STL_RWARGS_H
#include "stl_format.h"
#include "stl_global.h"
#include "stl_io_options.h"
#include "stl_mesh.h"
#include "stl_mesh_creator.h"
#include "../gmio_core/rwargs.h"
/*! Arguments for STL read functions */
struct gmio_stl_read_args
{
/*! Common(core) objects needed for the read operation */
struct gmio_rwargs core;
/*! Defines the callbacks for the mesh creation */
struct gmio_stl_mesh_creator mesh_creator;
/*! Optional pointer to a function that returns the size(in bytes) of the
* STL ascii data to read
*
* Useful only with STL ascii format. If set to NULL then by default
* gmio_stream::func_size() is called.
*
* The resulting stream size is passed to
* gmio_task_iface::func_handle_progress() as the \p max_value argument.
*/
gmio_streamsize_t (*func_stla_get_streamsize)(
struct gmio_stream* stream,
struct gmio_memblock* stream_memblock);
};
/*! Arguments for STL write functions */
struct gmio_stl_write_args
{
/*! Common(core) objects needed for the write operation */
struct gmio_rwargs core;
/*! Defines the mesh to output */
struct gmio_stl_mesh mesh;
/*! Options for the write operation, can be safely set to \c {0} to use
* default values */
struct gmio_stl_write_options options;
};
#endif /* GMIO_STL_RWARGS_H */

View File

@ -22,10 +22,9 @@
#include "internal/stla_parsing.h"
#include "../gmio_core/error.h"
#include "../gmio_core/rwargs.h"
#include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_rwargs.h"
#include "../gmio_core/internal/helper_stream.h"
#include "../gmio_core/internal/helper_task_iface.h"
#include "../gmio_core/internal/min_max.h"
#include "../gmio_core/internal/stringstream.h"
#include "../gmio_core/internal/string_ascii_utils.h"
@ -89,51 +88,51 @@ static void gmio_stringstream_stla_read_hook(
{
struct gmio_stringstream_stla_cookie* tcookie =
(struct gmio_stringstream_stla_cookie*)(cookie);
const struct gmio_rwargs* rwargs =
tcookie != NULL ? tcookie->rwargs : NULL;
if (tcookie != NULL) {
const struct gmio_task_iface* task = tcookie->task;
tcookie->stream_offset += strbuff->len;
tcookie->is_stop_requested = gmio_rwargs_is_stop_requested(rwargs);
gmio_rwargs_handle_progress(
rwargs, tcookie->stream_offset, tcookie->stream_size);
tcookie->is_stop_requested = gmio_task_iface_is_stop_requested(task);
gmio_task_iface_handle_progress(
task, tcookie->stream_offset, tcookie->stream_size);
}
}
/* Root function, parses a whole solid */
static void parse_solid(struct gmio_stla_parse_data* data);
int gmio_stla_read(struct gmio_stl_read_args* args)
int gmio_stla_read(
struct gmio_stream stream,
struct gmio_stl_mesh_creator mesh_creator,
const struct gmio_stl_read_options* opts)
{
struct gmio_rwargs* core_args = &args->core;
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(&core_args->stream_memblock);
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
struct gmio_memblock* const mblock = &mblock_helper.memblock;
char fixed_buffer[GMIO_STLA_READ_STRING_MAX_LEN];
struct gmio_stla_parse_data parse_data;
parse_data.token = unknown_token;
parse_data.error = false;
parse_data.strstream_cookie.rwargs = core_args;
parse_data.strstream_cookie.task = opts != NULL ? &opts->task_iface : NULL;
parse_data.strstream_cookie.stream_offset = 0;
parse_data.strstream_cookie.stream_size =
args->func_stla_get_streamsize != NULL ?
args->func_stla_get_streamsize(
&core_args->stream, &core_args->stream_memblock) :
gmio_stream_size(&core_args->stream);
opts != NULL && opts->func_stla_get_streamsize != NULL ?
opts->func_stla_get_streamsize(&stream, mblock) :
gmio_stream_size(&stream);
parse_data.strstream_cookie.is_stop_requested = false;
parse_data.strstream.stream = core_args->stream;
parse_data.strstream.strbuff.ptr = core_args->stream_memblock.ptr;
parse_data.strstream.strbuff.max_len = core_args->stream_memblock.size;
parse_data.strstream.stream = stream;
parse_data.strstream.strbuff.ptr = mblock->ptr;
parse_data.strstream.strbuff.max_len = mblock->size;
parse_data.strstream.cookie = &parse_data.strstream_cookie;
parse_data.strstream.func_stream_read_hook = gmio_stringstream_stla_read_hook;
gmio_stringstream_init_pos(&parse_data.strstream);
parse_data.strbuff = gmio_string(fixed_buffer, 0, sizeof(fixed_buffer));
parse_data.creator = &args->mesh_creator;
parse_data.creator = &mesh_creator;
parse_solid(&parse_data);

View File

@ -26,8 +26,8 @@
#include "../gmio_core/internal/byte_swap.h"
#include "../gmio_core/internal/convert.h"
#include "../gmio_core/internal/helper_memblock.h"
#include "../gmio_core/internal/helper_rwargs.h"
#include "../gmio_core/internal/helper_stream.h"
#include "../gmio_core/internal/helper_task_iface.h"
#include "../gmio_core/internal/safe_cast.h"
#include <string.h>
@ -89,14 +89,17 @@ static void gmio_stlb_decode_facets_byteswap(
}
int gmio_stlb_read(
struct gmio_stl_read_args* args, enum gmio_endianness byte_order)
struct gmio_stream stream,
struct gmio_stl_mesh_creator mesh_creator,
enum gmio_endianness byte_order,
const struct gmio_stl_read_options* opts)
{
/* Variables */
struct gmio_memblock_helper mblock_helper =
gmio_memblock_helper(&args->core.stream_memblock);
struct gmio_rwargs* core_args = &args->core;
struct gmio_stl_mesh_creator* mesh_creator = &args->mesh_creator;
void* mblock_ptr = core_args->stream_memblock.ptr;
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
struct gmio_memblock* mblock = &mblock_helper.memblock;
void* mblock_ptr = mblock->ptr;
const struct gmio_task_iface* task = opts != NULL ? &opts->task_iface : NULL;
struct gmio_stlb_header header;
uint32_t i_facet = 0; /* Facet counter */
uint32_t total_facet_count = 0; /* Facet count, as declared in the stream */
@ -107,15 +110,16 @@ int gmio_stlb_read(
gmio_stlb_decode_facets_byteswap :
gmio_stlb_decode_facets;
const uint32_t max_facet_count_per_read =
gmio_size_to_uint32(
args->core.stream_memblock.size / GMIO_STLB_TRIANGLE_RAWSIZE);
gmio_size_to_uint32(mblock->size / GMIO_STLB_TRIANGLE_RAWSIZE);
/* Check validity of input parameters */
if (!gmio_stlb_check_params(&error, core_args, byte_order))
if (!gmio_check_memblock_size(&error, mblock, GMIO_STLB_MIN_CONTENTS_SIZE))
goto label_end;
if (!gmio_stlb_check_byteorder(&error, byte_order))
goto label_end;
/* Read header */
if (gmio_stream_read(&core_args->stream, &header, GMIO_STLB_HEADER_SIZE, 1)
if (gmio_stream_read(&stream, &header, GMIO_STLB_HEADER_SIZE, 1)
!= 1)
{
error = GMIO_STL_ERROR_HEADER_WRONG_SIZE;
@ -123,7 +127,7 @@ int gmio_stlb_read(
}
/* Read facet count */
if (gmio_stream_read(&core_args->stream, mblock_ptr, sizeof(uint32_t), 1)
if (gmio_stream_read(&stream, mblock_ptr, sizeof(uint32_t), 1)
!= 1)
{
error = GMIO_STL_ERROR_FACET_COUNT;
@ -136,20 +140,20 @@ int gmio_stlb_read(
/* Callback to notify triangle count and header data */
gmio_stl_mesh_creator_binary_begin_solid(
mesh_creator, total_facet_count, &header);
&mesh_creator, total_facet_count, &header);
/* Read triangles */
gmio_rwargs_handle_progress(core_args, 0, total_facet_count);
gmio_task_iface_handle_progress(task, 0, total_facet_count);
while (gmio_no_error(error) && i_facet < total_facet_count) {
const uint32_t read_facet_count =
gmio_size_to_uint32(
gmio_stream_read(
&core_args->stream,
&stream,
mblock_ptr,
GMIO_STLB_TRIANGLE_RAWSIZE,
max_facet_count_per_read));
if (gmio_stream_error(&core_args->stream) != 0)
if (gmio_stream_error(&stream) != 0)
error = GMIO_ERROR_STREAM;
else if (read_facet_count > 0)
error = GMIO_ERROR_OK;
@ -158,16 +162,16 @@ int gmio_stlb_read(
if (gmio_no_error(error)) {
func_decode_facets(
mesh_creator, mblock_ptr, read_facet_count, i_facet);
&mesh_creator, mblock_ptr, read_facet_count, i_facet);
i_facet += read_facet_count;
if (gmio_rwargs_is_stop_requested(core_args))
if (gmio_task_iface_is_stop_requested(task))
error = GMIO_ERROR_TRANSFER_STOPPED;
}
gmio_rwargs_handle_progress(core_args, i_facet, total_facet_count);
gmio_task_iface_handle_progress(task, i_facet, total_facet_count);
} /* end while */
if (gmio_no_error(error))
gmio_stl_mesh_creator_end_solid(mesh_creator);
gmio_stl_mesh_creator_end_solid(&mesh_creator);
if (gmio_no_error(error) && i_facet != total_facet_count)
error = GMIO_STL_ERROR_FACET_COUNT;

View File

@ -49,11 +49,11 @@ static void gmio_stl_data__ascii_begin_solid(
{
struct gmio_stl_data* data = (struct gmio_stl_data*)cookie;
memset(&data->solid_name[0], 0, sizeof(data->solid_name));
memset(data->solid_name, 0, sizeof(data->solid_name));
if (solid_name != NULL) {
const size_t len =
GMIO_MIN(sizeof(data->solid_name), strlen(solid_name));
strncpy(&data->solid_name[0], solid_name, len);
strncpy(data->solid_name, solid_name, len);
}
/* Try to guess how many vertices we could have assume we'll need 200 bytes
@ -86,7 +86,7 @@ static void gmio_stl_data__add_triangle(
data->tri_array.capacity = cap;
}
memcpy(&data->tri_array.ptr[tri_id], triangle, GMIO_STLB_TRIANGLE_RAWSIZE);
data->tri_array.count = GMIO_MAX(data->tri_array.count, tri_id + 1);
data->tri_array.count = tri_id + 1;
}
static void gmio_stl_data__get_triangle(

View File

@ -16,7 +16,7 @@
#include "utest_assert.h"
#include "../src/gmio_core/global.h"
#include "../src/gmio_core/rwargs.h"
#include "../src/gmio_core/stream.h"
#include "../src/gmio_stl/stl_constants.h"
#include "../src/gmio_stl/stl_triangle.h"
@ -76,21 +76,16 @@ const char* test_platform__compiler()
* See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
*/
{
const struct gmio_rwargs args_null_bracket0 = {0};
struct gmio_rwargs args_null_memset0;
const struct gmio_stream null_bracket0 = {0};
struct gmio_stream null_memset0;
memset(&args_null_memset0, 0, sizeof(struct gmio_rwargs));
memset(&null_memset0, 0, sizeof(struct gmio_stream));
UTEST_ASSERT(memcmp(
&args_null_bracket0,
&args_null_memset0,
sizeof(struct gmio_rwargs))
&null_bracket0,
&null_memset0,
sizeof(struct gmio_stream))
== 0);
UTEST_ASSERT(sizeof(struct gmio_rwargs)
>= (sizeof(struct gmio_stream)
+ sizeof(struct gmio_memblock)
+ sizeof(struct gmio_task_iface)));
}
/* Check sizeof() operator with fixed-size arrays */

View File

@ -51,27 +51,27 @@ const char* generic_test_stl_infos(const struct gmio_test_stl_infos* test)
{
FILE* file = fopen(test->filepath, "rb");
gmio_streamsize_t expected_size = test->expected_size;
struct gmio_stl_infos_get_args args = {0};
struct gmio_stl_infos infos = {0};
struct gmio_stream stream = gmio_stream_stdio(file);
int error = GMIO_ERROR_OK;
args.stream = gmio_stream_stdio(file);
printf("\n%s\n", test->filepath);
error = gmio_stl_infos_get(
&args, test->format, GMIO_STL_INFO_FLAG_ALL);
error = gmio_stl_infos_get(&infos, stream, GMIO_STL_INFO_FLAG_ALL, NULL);
if (test->format != GMIO_STL_FORMAT_UNKNOWN) {
UTEST_ASSERT(error == GMIO_ERROR_OK);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
}
else {
UTEST_ASSERT(error == GMIO_STL_ERROR_UNKNOWN_FORMAT);
UTEST_COMPARE_INT(GMIO_STL_ERROR_UNKNOWN_FORMAT, error);
}
if (test->expected_size == -1)
expected_size = gmio_stream_size(&args.stream);
expected_size = gmio_stream_size(&stream);
fclose(file);
if (test->expected_size != -2)
UTEST_COMPARE_UINT(expected_size, args.infos.size);
UTEST_COMPARE_UINT(expected_size, infos.size);
return NULL;
}

View File

@ -18,7 +18,6 @@
#include "stl_utils.h"
#include "../src/gmio_core/error.h"
#include "../src/gmio_core/rwargs.h"
#include "../src/gmio_stl/internal/stl_rw_common.h"
#include "../src/gmio_stl/stl_error.h"
#include "../src/gmio_stl/stl_io.h"
@ -27,33 +26,45 @@
const char* test_stl_internal__rw_common()
{
/* gmio_check_transfer() */
/* gmio_check_memblock() */
{
int error = GMIO_ERROR_OK;
uint8_t buff[128] = {0};
struct gmio_rwargs rwargs = {0};
struct gmio_memblock mblock = {0};
UTEST_ASSERT(!gmio_check_rwargs(&error, NULL));
UTEST_ASSERT(error == GMIO_ERROR_NULL_RWARGS);
UTEST_ASSERT(!gmio_check_rwargs(&error, &rwargs));
UTEST_ASSERT(!gmio_check_memblock(&error, NULL));
UTEST_ASSERT(error == GMIO_ERROR_NULL_MEMBLOCK);
rwargs.stream_memblock = gmio_memblock(&buff[0], 0, NULL);
UTEST_ASSERT(!gmio_check_rwargs(&error, &rwargs));
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
UTEST_ASSERT(error == GMIO_ERROR_NULL_MEMBLOCK);
mblock = gmio_memblock(buff, 0, NULL);
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
/* Verify that gmio_check_transfer() doesn't touch error when in case of
/* Verify that gmio_check_memblock() doesn't touch error when in case of
* success */
rwargs.stream_memblock = gmio_memblock(&buff[0], sizeof(buff), NULL);
UTEST_ASSERT(!gmio_check_rwargs(&error, &rwargs));
mblock = gmio_memblock(buff, sizeof(buff), NULL);
UTEST_ASSERT(!gmio_check_memblock(&error, &mblock));
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
error = GMIO_ERROR_OK;
UTEST_ASSERT(gmio_check_rwargs(&error, &rwargs));
UTEST_ASSERT(gmio_check_memblock(&error, &mblock));
UTEST_ASSERT(error == GMIO_ERROR_OK);
}
/* gmio_check_memblock_size() */
{
uint8_t buff[1024] = {0};
struct gmio_memblock mblock =
gmio_memblock(buff, GMIO_STLB_MIN_CONTENTS_SIZE / 2, NULL);
int error = GMIO_ERROR_OK;
UTEST_ASSERT(!gmio_check_memblock_size(
&error, &mblock, GMIO_STLB_MIN_CONTENTS_SIZE));
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
}
/* gmio_stl_check_mesh() */
{
int error = GMIO_ERROR_OK;
@ -78,28 +89,14 @@ const char* test_stl_internal__rw_common()
UTEST_ASSERT(error == GMIO_ERROR_OK);
}
/* gmio_stlb_check_params() */
/* gmio_stlb_check_byteorder() */
{
int error = GMIO_ERROR_OK;
struct gmio_rwargs rwargs = {0};
uint8_t buff[1024] = {0};
UTEST_ASSERT(!gmio_stlb_check_params(&error, NULL, GMIO_ENDIANNESS_HOST));
UTEST_ASSERT(error == GMIO_ERROR_NULL_RWARGS);
error = GMIO_ERROR_OK;
rwargs.stream_memblock =
gmio_memblock(&buff[0], GMIO_STLB_MIN_CONTENTS_SIZE / 2, NULL);
UTEST_ASSERT(!gmio_stlb_check_params(&error, &rwargs, GMIO_ENDIANNESS_HOST));
UTEST_ASSERT(error == GMIO_ERROR_INVALID_MEMBLOCK_SIZE);
error = GMIO_ERROR_OK;
rwargs.stream_memblock =
gmio_memblock(&buff[0], sizeof(buff), NULL);
UTEST_ASSERT(gmio_stlb_check_params(&error, &rwargs, GMIO_ENDIANNESS_HOST));
UTEST_ASSERT(gmio_stlb_check_byteorder(&error, GMIO_ENDIANNESS_HOST));
UTEST_ASSERT(error == GMIO_ERROR_OK);
UTEST_ASSERT(!gmio_stlb_check_params(&error, &rwargs, GMIO_ENDIANNESS_UNKNOWN));
UTEST_ASSERT(!gmio_stlb_check_byteorder(&error, GMIO_ENDIANNESS_UNKNOWN));
UTEST_ASSERT(error == GMIO_STL_ERROR_UNSUPPORTED_BYTE_ORDER);
}

View File

@ -43,7 +43,7 @@ void stl_testcase_result__ascii_begin_solid(
if (res != NULL) {
res->solid_name[0] = 0;
if (solid_name != NULL)
strcpy(&res->solid_name[0], solid_name);
strcpy(res->solid_name, solid_name);
}
}
@ -127,18 +127,18 @@ const char* test_stl_read()
const size_t expected_count =
sizeof(expected) / sizeof(struct stl_testcase);
size_t i; /* for loop counter */
struct gmio_stl_read_args read = {0};
struct gmio_stl_mesh_creator mesh_creator = {0};
struct stl_testcase_result result = {0};
read.mesh_creator.cookie = &result;
read.mesh_creator.func_ascii_begin_solid = &stl_testcase_result__ascii_begin_solid;
read.mesh_creator.func_add_triangle = &gmio_stl_nop_add_triangle;
mesh_creator.cookie = &result;
mesh_creator.func_ascii_begin_solid = &stl_testcase_result__ascii_begin_solid;
mesh_creator.func_add_triangle = &gmio_stl_nop_add_triangle;
for (i = 0; i < expected_count; ++i) {
const enum gmio_stl_format format =
gmio_stl_get_format_file(expected[i].filepath);
const int err =
gmio_stl_read_file(&read, expected[i].filepath);
gmio_stl_read_file(expected[i].filepath, mesh_creator, NULL);
/* Check format */
if (format != expected[i].format) {
@ -149,7 +149,7 @@ const char* test_stl_read()
expected[i].format,
format);
}
UTEST_ASSERT(format == expected[i].format);
UTEST_COMPARE_UINT(expected[i].format, format);
/* Check error code */
if (err != expected[i].errorcode) {
@ -160,7 +160,7 @@ const char* test_stl_read()
expected[i].errorcode,
err);
}
UTEST_ASSERT(err == expected[i].errorcode);
UTEST_COMPARE_UINT(expected[i].errorcode, err);
/* Check solid name */
if (expected[i].format == GMIO_STL_FORMAT_ASCII) {
@ -174,7 +174,7 @@ const char* test_stl_read()
expected_name,
result.solid_name);
}
UTEST_ASSERT(strcmp(result.solid_name, expected_name) == 0);
UTEST_COMPARE_CSTR(expected_name, result.solid_name);
}
}
@ -197,17 +197,16 @@ const char* test_stlb_write_header()
error = gmio_stlb_write_header(
&stream, GMIO_ENDIANNESS_LITTLE, &header, 0);
fclose(outfile);
UTEST_ASSERT(error == GMIO_ERROR_OK);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
}
{
struct gmio_stl_read_args read = {0};
struct gmio_stl_data data = {0};
read.mesh_creator = gmio_stl_data_mesh_creator(&data);
error = gmio_stl_read_file(&read, filepath);
UTEST_ASSERT(error == GMIO_ERROR_OK);
error = gmio_stl_read_file(
filepath, gmio_stl_data_mesh_creator(&data), NULL);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
UTEST_ASSERT(gmio_stlb_header_equal(&header, &data.header));
UTEST_ASSERT(data.tri_array.count == 0);
UTEST_COMPARE_UINT(0, data.tri_array.count);
}
return NULL;
@ -232,26 +231,30 @@ const char* test_stlb_write()
/* Read input model file */
{
struct gmio_stl_read_args read = {0};
read.mesh_creator = gmio_stl_data_mesh_creator(&data);
error = gmio_stl_read_file(&read, model_filepath);
UTEST_ASSERT(error == GMIO_ERROR_OK);
error = gmio_stl_read_file(
model_filepath, gmio_stl_data_mesh_creator(&data), NULL);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
}
/* Write back input model file
* Write also the model file in big-endian STL format
*/
{
struct gmio_stl_write_args write = {0};
write.mesh = gmio_stl_data_mesh(&data);
write.options.stlb_header = &data.header;
struct gmio_stl_write_options opts = {0};
opts.stlb_header = data.header;
error = gmio_stl_write_file(
&write, GMIO_STL_FORMAT_BINARY_LE, model_filepath_out);
UTEST_ASSERT(error == GMIO_ERROR_OK);
GMIO_STL_FORMAT_BINARY_LE,
model_filepath_out,
gmio_stl_data_mesh(&data),
&opts);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
/* Big-endian version */
error = gmio_stl_write_file(
&write, GMIO_STL_FORMAT_BINARY_BE, model_filepath_out_be);
GMIO_STL_FORMAT_BINARY_BE,
model_filepath_out_be,
gmio_stl_data_mesh(&data),
&opts);
}
/* Check input and output models are equal */
@ -269,13 +272,13 @@ const char* test_stlb_write()
UTEST_FAIL("fopen() error for in/out model files");
}
do {
bytes_read_in = fread(&buffer_in[0], 1, buff_size, in);
bytes_read_out = fread(&buffer_out[0], 1, buff_size, out);
bytes_read_in = fread(buffer_in, 1, buff_size, in);
bytes_read_out = fread(buffer_out, 1, buff_size, out);
if (bytes_read_in != bytes_read_out) {
fclose_2(in, out);
UTEST_FAIL("Different byte count between in/out");
}
if (memcmp(&buffer_in[0], &buffer_out[0], buff_size) != 0) {
if (memcmp(buffer_in, buffer_out, buff_size) != 0) {
fclose_2(in, out);
UTEST_FAIL("Different buffer contents between in/out");
}
@ -287,12 +290,13 @@ const char* test_stlb_write()
/* Check output LE/BE models are equal */
{
struct gmio_stl_data data_be = {0};
struct gmio_stl_read_args read = {0};
read.mesh_creator = gmio_stl_data_mesh_creator(&data_be);
error = gmio_stl_read_file(&read, model_filepath_out_be);
UTEST_ASSERT(error == GMIO_ERROR_OK);
error = gmio_stl_read_file(
model_filepath_out_be,
gmio_stl_data_mesh_creator(&data_be),
NULL);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
UTEST_ASSERT(gmio_stlb_header_equal(&data.header, &data_be.header));
UTEST_ASSERT(data.tri_array.count == data_be.tri_array.count);
UTEST_COMPARE_UINT(data.tri_array.count, data_be.tri_array.count);
UTEST_ASSERT(memcmp(data.tri_array.ptr,
data_be.tri_array.ptr,
data.tri_array.count * sizeof(struct gmio_stl_triangle))
@ -315,39 +319,40 @@ const char* test_stla_write()
/* Read input model file */
{
struct gmio_stl_read_args read = {0};
read.mesh_creator = gmio_stl_data_mesh_creator(&data);
error = gmio_stl_read_file(&read, model_filepath);
UTEST_ASSERT(error == GMIO_ERROR_OK);
error = gmio_stl_read_file(
model_filepath, gmio_stl_data_mesh_creator(&data), NULL);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
}
/* Write the model to STL ascii format */
{
struct gmio_stl_write_args write = {0};
write.mesh = gmio_stl_data_mesh(&data);
gmio_stlb_header_to_printable_str(&data.header, &header_str[0], '_');
write.options.stla_solid_name = &header_str[0];
write.options.stla_float32_prec = 7;
write.options.stla_float32_format =
GMIO_FLOAT_TEXT_FORMAT_SHORTEST_LOWERCASE;
struct gmio_stl_write_options opts = {0};
gmio_stlb_header_to_printable_str(&data.header, header_str, '_');
opts.stla_solid_name = header_str;
opts.stla_float32_prec = 7;
opts.stla_float32_format = GMIO_FLOAT_TEXT_FORMAT_SHORTEST_LOWERCASE;
error = gmio_stl_write_file(
&write, GMIO_STL_FORMAT_ASCII, model_filepath_out);
UTEST_ASSERT(error == GMIO_ERROR_OK);
GMIO_STL_FORMAT_ASCII,
model_filepath_out,
gmio_stl_data_mesh(&data),
&opts);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
}
/* Read the output STL ascii model */
{
char trim_header_str[sizeof(header_str)] = {0};
struct gmio_stl_data data_stla = {0};
struct gmio_stl_read_args read = {0};
size_t i = 0;
read.mesh_creator = gmio_stl_data_mesh_creator(&data_stla);
strncpy(&trim_header_str[0], &header_str[0], sizeof(header_str));
strncpy(trim_header_str, header_str, sizeof(header_str));
gmio_string_trim_from_end(trim_header_str, sizeof(header_str));
error = gmio_stl_read_file(&read, model_filepath_out);
UTEST_ASSERT(error == GMIO_ERROR_OK);
UTEST_ASSERT(data.tri_array.count == data_stla.tri_array.count);
UTEST_ASSERT(strcmp(&trim_header_str[0], &data_stla.solid_name[0]) == 0);
error = gmio_stl_read_file(
model_filepath_out,
gmio_stl_data_mesh_creator(&data_stla),
NULL);
UTEST_COMPARE_INT(GMIO_ERROR_OK, error);
UTEST_COMPARE_UINT(data.tri_array.count, data_stla.tri_array.count);
UTEST_COMPARE_CSTR(trim_header_str, data_stla.solid_name);
for (i = 0; i < data.tri_array.count; ++i) {
const struct gmio_stl_triangle* lhs = &data.tri_array.ptr[i];
const struct gmio_stl_triangle* rhs = &data_stla.tri_array.ptr[i];
@ -377,19 +382,19 @@ void generate_stlb_tests_models()
0 /* attr */
};
struct gmio_stl_data data = {0};
struct gmio_stl_write_args write = {0};
data.tri_array.ptr = &tri;
data.tri_array.count = 1;
write.mesh = gmio_stl_data_mesh(&data);
gmio_stl_write_file(
&write,
GMIO_STL_FORMAT_BINARY_LE,
"models/solid_one_facet.le_stlb");
"models/solid_one_facet.le_stlb",
gmio_stl_data_mesh(&data),
NULL);
gmio_stl_write_file(
&write,
GMIO_STL_FORMAT_BINARY_BE,
"models/solid_one_facet.be_stlb");
"models/solid_one_facet.be_stlb",
gmio_stl_data_mesh(&data),
NULL);
}
}