gmio_stl: simplify API for write operation

This commit is contained in:
Hugues Delorme 2015-04-02 17:59:02 +02:00
parent 8d8cff6f0e
commit 4fe921c11a
7 changed files with 147 additions and 111 deletions

View File

@ -13,16 +13,16 @@
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
****************************************************************************/
#include "stl_io.h"
#include "stla_write.h"
#include "internal/stl_rw_common.h"
#include "stl_error.h"
#include "stl_rw_common.h"
#include "../stl_error.h"
#include "../gmio_core/error.h"
#include "../gmio_core/internal/helper_stream.h"
#include "../gmio_core/internal/helper_transfer.h"
#include "../gmio_core/internal/min_max.h"
#include "../gmio_core/internal/safe_cast.h"
#include "../../gmio_core/error.h"
#include "../../gmio_core/internal/helper_stream.h"
#include "../../gmio_core/internal/helper_transfer.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/safe_cast.h"
#include <stdio.h>
#include <string.h>
@ -113,11 +113,11 @@ static gmio_bool_t gmio_transfer_flush_buffer(gmio_transfer_t* trsf, size_t n)
int gmio_stla_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
const gmio_stla_write_options_t* options)
/* Options */
const char* solid_name,
uint8_t float32_prec)
{
/* Constants */
const char* solid_name = options != NULL ? options->solid_name : NULL;
const uint8_t float32_prec = options != NULL ? options->float32_prec : 9;
const uint32_t total_facet_count = mesh != NULL ? mesh->triangle_count : 0;
const uint32_t buffer_facet_count =
trsf != NULL ?

View File

@ -0,0 +1,35 @@
/****************************************************************************
** GeomIO Library
** Copyright FougSys (2 Mar. 2015)
** contact@fougsys.fr
**
** 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_STLA_WRITE_H
#define GMIO_INTERNAL_STLA_WRITE_H
#include "../stl_mesh.h"
#include "../../gmio_core/transfer.h"
/*! Writes geometry in the STL ascii format
*
* \return Error code (see error.h and stl_error.h)
* \retval GMIO_ERROR_INVALID_BUFFER_SIZE
* if <tt>trsf->buffer.size < 512</tt>
*/
int gmio_stla_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
/* Options */
const char* solid_name,
uint8_t float32_prec);
#endif /* GMIO_INTERNAL_STLA_WRITE_H */

View File

@ -13,19 +13,18 @@
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
****************************************************************************/
#include "stl_io.h"
#include "stlb_write.h"
#include "stl_error.h"
#include "internal/stl_rw_common.h"
#include "internal/stlb_byte_swap.h"
#include "stl_rw_common.h"
#include "stlb_byte_swap.h"
#include "../stl_error.h"
#include "../gmio_core/endian.h"
#include "../gmio_core/error.h"
#include "../gmio_core/internal/byte_codec.h"
#include "../gmio_core/internal/min_max.h"
#include "../gmio_core/internal/helper_stream.h"
#include "../gmio_core/internal/helper_transfer.h"
#include "../gmio_core/internal/safe_cast.h"
#include "../../gmio_core/error.h"
#include "../../gmio_core/internal/byte_codec.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/helper_stream.h"
#include "../../gmio_core/internal/helper_transfer.h"
#include "../../gmio_core/internal/safe_cast.h"
#include <string.h>
@ -68,13 +67,11 @@ static void gmio_stlb_write_facets(
int gmio_stlb_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
const gmio_stlb_write_options_t* options)
/* Options */
const uint8_t* header_data,
gmio_endianness_t byte_order)
{
/* Constants */
const gmio_endianness_t byte_order =
options != NULL ? options->byte_order : GMIO_ENDIANNESS_HOST;
const uint8_t* header_data =
options != NULL ? options->header_data : NULL;
const uint32_t facet_count =
mesh != NULL ? mesh->triangle_count : 0;
/* Variables */

View File

@ -0,0 +1,37 @@
/****************************************************************************
** GeomIO Library
** Copyright FougSys (2 Mar. 2015)
** contact@fougsys.fr
**
** 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_STLB_WRITE_H
#define GMIO_INTERNAL_STLB_WRITE_H
#include "../stl_mesh.h"
#include "../../gmio_core/endian.h"
#include "../../gmio_core/transfer.h"
/*! Writes geometry in the STL binary format
*
* \return Error code (see error.h and stl_error.h)
* \retval GMIO_INVALID_BUFFER_SIZE_ERROR
* if <tt>trsf->buffer.size < GMIO_STLB_MIN_CONTENTS_SIZE</tt>
*/
GMIO_LIBSTL_EXPORT
int gmio_stlb_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
/* Options */
const uint8_t* header_data,
gmio_endianness_t byte_order);
#endif /* GMIO_INTERNAL_STLB_WRITE_H */

View File

@ -17,6 +17,8 @@
#include "stl_error.h"
#include "stl_format.h"
#include "internal/stla_write.h"
#include "internal/stlb_write.h"
#include "../gmio_core/error.h"
#include "../gmio_core/stream.h"
#include "../gmio_core/transfer.h"
@ -85,7 +87,8 @@ int gmio_stl_write_file(
gmio_stl_format_t format,
const char *filepath,
const gmio_stl_mesh_t *mesh,
gmio_task_iface_t *task_iface)
gmio_task_iface_t *task_iface,
const gmio_stl_write_options_t *options)
{
int error = GMIO_ERROR_OK;
FILE* file = NULL;
@ -98,7 +101,7 @@ int gmio_stl_write_file(
if (task_iface != NULL)
trsf.task_iface = *task_iface;
error = gmio_stl_write(format, &trsf, mesh);
error = gmio_stl_write(format, &trsf, mesh, options);
fclose(file);
gmio_buffer_deallocate(&trsf.buffer);
}
@ -112,26 +115,31 @@ int gmio_stl_write_file(
int gmio_stl_write(
gmio_stl_format_t format,
gmio_transfer_t *trsf,
const gmio_stl_mesh_t *mesh)
const gmio_stl_mesh_t *mesh,
const gmio_stl_write_options_t *options)
{
const uint8_t* header_data =
options != NULL ? options->stlb_header_data : NULL;
int error = GMIO_ERROR_OK;
if (trsf != NULL) {
switch (format) {
case GMIO_STL_FORMAT_ASCII: {
error = gmio_stla_write(trsf, mesh, NULL);
const char* solid_name =
options != NULL ? options->stla_solid_name : NULL;
const uint8_t float32_prec =
options != NULL ? options->stla_float32_prec : 9;
error = gmio_stla_write(trsf, mesh, solid_name, float32_prec);
break;
}
case GMIO_STL_FORMAT_BINARY_BE: {
const gmio_stlb_write_options_t opts = { NULL,
GMIO_ENDIANNESS_BIG };
error = gmio_stlb_write(trsf, mesh, &opts);
error = gmio_stlb_write(
trsf, mesh, header_data, GMIO_ENDIANNESS_BIG);
break;
}
case GMIO_STL_FORMAT_BINARY_LE: {
const gmio_stlb_write_options_t opts = { NULL,
GMIO_ENDIANNESS_LITTLE };
error = gmio_stlb_write(trsf, mesh, &opts);
error = gmio_stlb_write(
trsf, mesh, header_data, GMIO_ENDIANNESS_LITTLE);
break;
}
case GMIO_STL_FORMAT_UNKNOWN: {

View File

@ -37,7 +37,7 @@ GMIO_C_LINKAGE_BEGIN
* the string has to be encoded using the system's charset (locale-8bit)
* \param creator Defines the callbacks for the mesh creation
* \param task_iface The interface object by which the read operation can be
* controlled. Optional, can be safely set to NULL
* controlled. Optional, can be safely set to NULL
*
* Internally, it uses:
* \li the builtin stream wrapper around FILE* (see gmio_stream_stdio())
@ -71,7 +71,9 @@ int gmio_stl_read(
* the string has to be encoded using the system's charset (locale-8bit)
* \param mesh Defines the mesh to output
* \param task_iface The interface object by which the write operation can be
* controlled. Optional, can be safely set to NULL
* controlled. Optional, can be safely set to NULL
* \param options Options for the write operation, can be safely set to NULL
* to use default values
*
* Internally, it uses:
* \li the builtin stream wrapper around FILE* (see gmio_stream_stdio())
@ -85,13 +87,16 @@ int gmio_stl_write_file(
gmio_stl_format_t format,
const char* filepath,
const gmio_stl_mesh_t* mesh,
gmio_task_iface_t* task_iface);
gmio_task_iface_t* task_iface,
const gmio_stl_write_options_t* options);
/*! Writes STL mesh to stream
*
* \param format STL format of the output
* \param trsf Defines needed objects for the write operation
* \param mesh Defines the mesh to output
* \param options Options for the write operation, can be safely set to NULL
* to use default values
*
* \return Error code (see error.h and stl_error.h)
*/
@ -99,18 +104,15 @@ GMIO_LIBSTL_EXPORT
int gmio_stl_write(
gmio_stl_format_t format,
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh);
/* ========================================================================
* STL ascii
* ======================================================================== */
const gmio_stl_mesh_t* mesh,
const gmio_stl_write_options_t* options);
/*! Reads geometry from STL ascii stream
*
* \param trsf Defines needed objects for the read operation
* \param creator Defines the callbacks for the mesh creation
*
* Stream size is passed to gmio_transfer::handle_progress_func() as the
* Stream size is passed to gmio_task_iface::handle_progress_func() as the
* \p max_value argument.
*
* Possible options in a future version could be:
@ -124,27 +126,6 @@ int gmio_stla_read(
gmio_transfer_t* trsf,
gmio_stl_mesh_creator_t* creator);
/*! Writes geometry in the STL ascii format
*
* \param mesh Defines the mesh to write
* \param trsf Defines needed objects for the write operation
* \param options Options for the operation, can be \c NULL to use default
* values
*
* \return Error code (see error.h and stl_error.h)
* \retval GMIO_ERROR_INVALID_BUFFER_SIZE
* if <tt>trsf->buffer.size < 512</tt>
*/
GMIO_LIBSTL_EXPORT
int gmio_stla_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
const gmio_stla_write_options_t* options);
/* ========================================================================
* STL binary
* ======================================================================== */
/*! Reads geometry from STL binary stream
*
* \param trsf Defines needed objects for the read operation
@ -161,23 +142,6 @@ int gmio_stlb_read(
gmio_stl_mesh_creator_t* creator,
gmio_endianness_t byte_order);
/*! Writes geometry in the STL binary format
*
* \param mesh Defines the mesh to write
* \param trsf Defines needed objects for the write operation
* \param options Options for the operation, can be \c NULL to use default
* values
*
* \return Error code (see error.h and stl_error.h)
* \retval GMIO_INVALID_BUFFER_SIZE_ERROR
* if <tt>trsf->buffer.size < GMIO_STLB_MIN_CONTENTS_SIZE</tt>
*/
GMIO_LIBSTL_EXPORT
int gmio_stlb_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
const gmio_stlb_write_options_t* options);
GMIO_C_LINKAGE_END
#endif /* GMIO_STL_IO_H */

View File

@ -23,43 +23,38 @@
#include "stl_global.h"
#include "../gmio_core/endian.h"
/*! Options for gmio_stla_write() */
struct gmio_stla_write_options
/*! Options for gmio_stl_write() */
struct gmio_stl_write_options
{
/*! Name of the solid to appear in "solid <name> \n facet normal ..."
*
* Defaulted to an empty string "" when :
* \li calling gmio_stla_write() with <tt>options == NULL</tt>
* \li OR <tt>solid_name == NULL</tt>
*/
const char* solid_name;
/*! The maximum number of significant digits to write float values
* Option useful only with STL ascii format (GMIO_STL_FORMAT_ASCII).
*
* Defaulted to \c 9 when calling gmio_stla_write() with \c options==NULL
* Defaulted to an empty string \c "" when :
* \li calling gmio_stl_write() with <tt>options == NULL</tt>
* \li OR <tt>stla_solid_name == NULL</tt>
*/
uint8_t float32_prec;
};
typedef struct gmio_stla_write_options gmio_stla_write_options_t;
const char* stla_solid_name;
/*! The maximum number of significant digits when writting float values
*
* Option useful only with STL ascii format (GMIO_STL_FORMAT_ASCII).
*
* Defaulted to \c 9 when calling gmio_stl_write() with \c options==NULL
*/
uint8_t stla_float32_prec;
/*! Options for gmio_stlb_write() */
struct gmio_stlb_write_options
{
/*! Header data consisting of 80 bytes
*
* Defaulted to an array containing 0 when :
* \li calling gmio_stlb_write() with <tt>options == NULL</tt>
* \li OR <tt>header_data == NULL</tt>
*/
const uint8_t* header_data;
/*! Byte order of the output STL binary data
* Option useful only with STL binary formats (GMIO_STL_FORMAT_BINARY_LE
* or GMIO_STL_FORMAT_BINARY_BE).
*
* Defaulted to host's endianness when calling gmio_stlb_write()
* with \c options==NULL
* Defaulted to an array containing 0 when :
* \li calling gmio_stl_write() with <tt>options == NULL</tt>
* \li OR <tt>stlb_header_data == NULL</tt>
*/
gmio_endianness_t byte_order;
const uint8_t* stlb_header_data;
};
typedef struct gmio_stlb_write_options gmio_stlb_write_options_t;
typedef struct gmio_stl_write_options gmio_stl_write_options_t;
#endif /* GMIO_STL_IO_OPTIONS_H */