diff --git a/src/gmio_core/text_format.h b/src/gmio_core/text_format.h new file mode 100644 index 0000000..6ed90c6 --- /dev/null +++ b/src/gmio_core/text_format.h @@ -0,0 +1,43 @@ +/**************************************************************************** +** 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". +****************************************************************************/ + +/*! \file text_format.h + * Formats for textual representation + */ + +#ifndef GMIO_TEXT_FORMAT_H +#define GMIO_TEXT_FORMAT_H + +#include "global.h" + +/*! This enum defines the various formats to textually represent a float */ +enum gmio_float_text_format +{ + /*! Decimal floating point, lowercase (ex: 392.65) */ + GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE = 0, + /*! Decimal floating point, uppercase (ex: 392.65) */ + GMIO_FLOAT_TEXT_FORMAT_DECIMAL_UPPERCASE, + /*! Scientific notation, lowercase (ex: 3.9265e+2) */ + GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_LOWERCASE, + /*! Scientific notation, uppercase (ex: 3.9265E+2) */ + GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_UPPERCASE, + /*! Use the shortest representation: decimal or scientific lowercase */ + GMIO_FLOAT_TEXT_FORMAT_SHORTEST_LOWERCASE, + /*! Use the shortest representation: decimal or scientific uppercase */ + GMIO_FLOAT_TEXT_FORMAT_SHORTEST_UPPERCASE +}; +typedef enum gmio_float_text_format gmio_float_text_format_t; + +#endif /* GMIO_TEXT_FORMAT_H */ diff --git a/src/gmio_stl/internal/stla_write.c b/src/gmio_stl/internal/stla_write.c index 5d91071..59060d1 100644 --- a/src/gmio_stl/internal/stla_write.c +++ b/src/gmio_stl/internal/stla_write.c @@ -83,14 +83,30 @@ GMIO_INLINE char* gmio_write_nspaces(char* buffer, int n) return buffer + offset; } -GMIO_INLINE char* gmio_write_stdio_format(char* buffer, uint8_t prec) +GMIO_INLINE char gmio_float_text_format_to_specifier( + gmio_float_text_format_t format) +{ + switch (format) { + case GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE: return 'f'; + case GMIO_FLOAT_TEXT_FORMAT_DECIMAL_UPPERCASE: return 'F'; + case GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_LOWERCASE: return 'e'; + case GMIO_FLOAT_TEXT_FORMAT_SCIENTIFIC_UPPERCASE: return 'E'; + case GMIO_FLOAT_TEXT_FORMAT_SHORTEST_LOWERCASE: return 'g'; + case GMIO_FLOAT_TEXT_FORMAT_SHORTEST_UPPERCASE: return 'G'; + } + /* Default, should not be here */ + return GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE; +} + +GMIO_INLINE char* gmio_write_stdio_format( + char* buffer, char format_specifier, uint8_t prec) { int prec_len = 0; buffer[0] = '%'; buffer[1] = '.'; prec_len = sprintf(buffer + 2, "%u", prec); - buffer[2 + prec_len] = 'E'; + buffer[2 + prec_len] = format_specifier; return buffer + 3 + prec_len; } @@ -115,6 +131,7 @@ int gmio_stla_write( const gmio_stl_mesh_t* mesh, /* Options */ const char* solid_name, + gmio_float_text_format_t float32_format, uint8_t float32_prec) { /* Constants */ @@ -141,12 +158,14 @@ int gmio_stla_write( return GMIO_ERROR_INVALID_BUFFER_SIZE; { /* Create XYZ coords format string (for normal and vertex coords) */ + const char float32_specifier = + gmio_float_text_format_to_specifier(float32_format); char* it = coords_format; - it = gmio_write_stdio_format(it, float32_prec); + it = gmio_write_stdio_format(it, float32_specifier, float32_prec); it = gmio_write_nspaces(it, 2); - it = gmio_write_stdio_format(it, float32_prec); + it = gmio_write_stdio_format(it, float32_specifier, float32_prec); it = gmio_write_nspaces(it, 2); - it = gmio_write_stdio_format(it, float32_prec); + it = gmio_write_stdio_format(it, float32_specifier, float32_prec); *it = 0; /* Write terminating null byte */ /* TODO: check the "format" string can contain the given precision */ } diff --git a/src/gmio_stl/internal/stla_write.h b/src/gmio_stl/internal/stla_write.h index 3eb0a37..b81c354 100644 --- a/src/gmio_stl/internal/stla_write.h +++ b/src/gmio_stl/internal/stla_write.h @@ -17,6 +17,7 @@ #define GMIO_INTERNAL_STLA_WRITE_H #include "../stl_mesh.h" +#include "../../gmio_core/text_format.h" #include "../../gmio_core/transfer.h" /*! Writes geometry in the STL ascii format @@ -30,6 +31,7 @@ int gmio_stla_write( const gmio_stl_mesh_t* mesh, /* Options */ const char* solid_name, + gmio_float_text_format_t float32_format, uint8_t float32_prec); #endif /* GMIO_INTERNAL_STLA_WRITE_H */ diff --git a/src/gmio_stl/stl_io.c b/src/gmio_stl/stl_io.c index 077143f..c6809ef 100644 --- a/src/gmio_stl/stl_io.c +++ b/src/gmio_stl/stl_io.c @@ -127,9 +127,13 @@ int gmio_stl_write( case GMIO_STL_FORMAT_ASCII: { const char* solid_name = options != NULL ? options->stla_solid_name : NULL; - const uint8_t float32_prec = + const gmio_float_text_format_t float_fmt = + options != NULL ? options->stla_float32_format : + GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE; + const uint8_t float_prec = options != NULL ? options->stla_float32_prec : 9; - error = gmio_stla_write(trsf, mesh, solid_name, float32_prec); + error = gmio_stla_write( + trsf, mesh, solid_name, float_fmt, float_prec); break; } case GMIO_STL_FORMAT_BINARY_BE: { diff --git a/src/gmio_stl/stl_io_options.h b/src/gmio_stl/stl_io_options.h index 7042b0a..e1b1316 100644 --- a/src/gmio_stl/stl_io_options.h +++ b/src/gmio_stl/stl_io_options.h @@ -22,6 +22,7 @@ #include "stl_global.h" #include "../gmio_core/endian.h" +#include "../gmio_core/text_format.h" /*! Options for gmio_stl_write() */ struct gmio_stl_write_options @@ -36,6 +37,15 @@ struct gmio_stl_write_options */ const char* stla_solid_name; + /*! The format used when writting float values as strings + * + * Option useful only with STL ascii format (GMIO_STL_FORMAT_ASCII). + * + * Defaulted to \c GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE when calling + * gmio_stl_write() with \c options==NULL + */ + gmio_float_text_format_t stla_float32_format; + /*! The maximum number of significant digits when writting float values * * Option useful only with STL ascii format (GMIO_STL_FORMAT_ASCII).