2015-03-03 00:38:33 +08:00
|
|
|
/****************************************************************************
|
2015-05-28 15:40:24 +08:00
|
|
|
** gmio
|
2015-05-01 00:19:45 +08:00
|
|
|
** Copyright Fougue (2 Mar. 2015)
|
2015-07-13 17:42:03 +08:00
|
|
|
** contact@fougue.pro
|
2015-03-03 00:38:33 +08:00
|
|
|
**
|
|
|
|
** 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
|
2015-03-30 15:05:25 +08:00
|
|
|
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
|
2015-03-03 00:38:33 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "stla_write.h"
|
2013-03-06 18:49:53 +08:00
|
|
|
|
2015-10-15 00:17:03 +08:00
|
|
|
#include "stl_funptr_typedefs.h"
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "stl_rw_common.h"
|
|
|
|
#include "../stl_error.h"
|
2013-03-06 18:49:53 +08:00
|
|
|
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "../../gmio_core/error.h"
|
2015-12-04 01:00:25 +08:00
|
|
|
#include "../../gmio_core/text_format.h"
|
|
|
|
#include "../../gmio_core/internal/helper_rwargs.h"
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "../../gmio_core/internal/helper_stream.h"
|
|
|
|
#include "../../gmio_core/internal/min_max.h"
|
|
|
|
#include "../../gmio_core/internal/safe_cast.h"
|
2014-03-14 00:49:39 +08:00
|
|
|
|
2013-04-27 06:11:53 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* | 14 || 17 || 17 || 15 | -> 73
|
|
|
|
* facet normal 0.986544556E+00 0.986544556E+00 0.986544556E+00
|
|
|
|
* | 11 |
|
|
|
|
* outer loop
|
|
|
|
* | 10 || 17 || 17 || 15 | -> 69
|
|
|
|
* vertex 0.167500112E+02 0.505000112E+02 0.000000000E+00
|
|
|
|
* vertex 0.164599000E+02 0.505000111E+02 0.221480112E+01
|
|
|
|
* vertex 0.166819000E+02 0.483135112E+02 0.221480112E+01
|
|
|
|
* | 8 |
|
|
|
|
* endloop
|
|
|
|
* | 8 |
|
|
|
|
* endfacet
|
|
|
|
*
|
|
|
|
* Total without EOL = 73 + 11 + 3*69 + 8 + 8 = 307
|
|
|
|
* Total with EOL(2 chars) = 307 + 7*2 = 321
|
|
|
|
*/
|
|
|
|
|
2015-03-02 23:43:42 +08:00
|
|
|
enum { GMIO_STLA_FACET_SIZE = 321 };
|
|
|
|
enum { GMIO_STLA_FACET_SIZE_P2 = 512 };
|
|
|
|
enum { GMIO_STLA_SOLID_NAME_MAX_LEN = 512 };
|
2013-04-27 06:11:53 +08:00
|
|
|
|
2015-10-28 23:38:57 +08:00
|
|
|
/* Fucntions for raw strings(ie. "const char*") */
|
2013-04-27 06:11:53 +08:00
|
|
|
|
2015-04-02 21:38:50 +08:00
|
|
|
GMIO_INLINE char* gmio_write_eol(char* buffer)
|
2013-04-27 06:11:53 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
*buffer = '\n';
|
|
|
|
return buffer + 1;
|
2013-04-27 06:11:53 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 23:38:57 +08:00
|
|
|
GMIO_INLINE char* gmio_write_rawstr(char* buffer, const char* str)
|
2015-04-02 21:38:50 +08:00
|
|
|
{
|
2015-10-28 23:38:57 +08:00
|
|
|
/* pre-condition: str must not be NULL */
|
|
|
|
#if 0
|
|
|
|
const size_t len = strlen(str);
|
|
|
|
strncpy(buffer, str, len);
|
|
|
|
return buffer + len;
|
|
|
|
#else
|
|
|
|
/* Not that good :
|
|
|
|
* the compiler cannot optimize copy for the target architecture
|
|
|
|
*/
|
|
|
|
while (*str != 0) {
|
|
|
|
*buffer = *str;
|
|
|
|
++buffer;
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
#endif
|
2015-04-02 21:38:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 23:38:57 +08:00
|
|
|
GMIO_INLINE char* gmio_write_rawstr_eol(char* buffer, const char* str)
|
2013-04-27 06:11:53 +08:00
|
|
|
{
|
2015-10-28 23:38:57 +08:00
|
|
|
buffer = gmio_write_rawstr(buffer, str);
|
|
|
|
return gmio_write_eol(buffer);
|
2013-04-27 06:11:53 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 23:11:15 +08:00
|
|
|
GMIO_INLINE char gmio_float_text_format_to_specifier(
|
2015-12-04 01:00:25 +08:00
|
|
|
enum gmio_float_text_format format)
|
2015-04-15 23:11:15 +08:00
|
|
|
{
|
|
|
|
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)
|
2013-04-27 06:11:53 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
int prec_len = 0;
|
2013-04-27 06:11:53 +08:00
|
|
|
|
2015-03-03 17:35:36 +08:00
|
|
|
buffer[0] = '%';
|
|
|
|
buffer[1] = '.';
|
2015-03-18 23:27:43 +08:00
|
|
|
prec_len = sprintf(buffer + 2, "%u", prec);
|
2015-04-15 23:11:15 +08:00
|
|
|
buffer[2 + prec_len] = format_specifier;
|
2015-03-03 17:35:36 +08:00
|
|
|
return buffer + 3 + prec_len;
|
2013-04-27 06:11:53 +08:00
|
|
|
}
|
|
|
|
|
2015-04-02 21:38:50 +08:00
|
|
|
GMIO_INLINE char* gmio_write_coords(
|
2015-03-20 00:31:08 +08:00
|
|
|
char* buffer,
|
|
|
|
const char* coords_format,
|
2015-12-04 01:00:25 +08:00
|
|
|
const struct gmio_stl_coords* coords)
|
2013-04-27 06:11:53 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
return buffer + sprintf(buffer,
|
|
|
|
coords_format, coords->x, coords->y, coords->z);
|
2013-04-27 06:11:53 +08:00
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
GMIO_INLINE gmio_bool_t gmio_rwargs_flush_buffer(
|
|
|
|
struct gmio_rwargs* args, size_t n)
|
2013-03-06 18:49:53 +08:00
|
|
|
{
|
2015-03-24 01:21:04 +08:00
|
|
|
const size_t write_count =
|
2015-12-04 01:00:25 +08:00
|
|
|
gmio_stream_write(
|
2016-01-05 18:44:10 +08:00
|
|
|
&args->stream, args->stream_memblock.ptr, sizeof(char), n);
|
2015-03-24 01:21:04 +08:00
|
|
|
return write_count == n;
|
2013-04-27 06:11:53 +08:00
|
|
|
}
|
|
|
|
|
2015-12-10 01:51:03 +08:00
|
|
|
int gmio_stla_write(struct gmio_stl_write_args* args)
|
2013-04-27 06:11:53 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
/* Constants */
|
2015-12-10 01:51:03 +08:00
|
|
|
const uint32_t total_facet_count =
|
|
|
|
args->mesh.triangle_count;
|
2015-03-03 17:35:36 +08:00
|
|
|
const uint32_t buffer_facet_count =
|
2016-01-05 18:44:10 +08:00
|
|
|
gmio_size_to_uint32(
|
|
|
|
args->core.stream_memblock.size / GMIO_STLA_FACET_SIZE_P2);
|
2015-10-28 23:38:57 +08:00
|
|
|
const char* opt_solid_name =
|
2015-12-10 01:51:03 +08:00
|
|
|
args->options.stla_solid_name;
|
2015-10-28 23:38:57 +08:00
|
|
|
const char* solid_name =
|
|
|
|
opt_solid_name != NULL ? opt_solid_name : "";
|
2015-12-04 01:00:25 +08:00
|
|
|
const enum gmio_float_text_format float32_format =
|
2015-12-10 01:51:03 +08:00
|
|
|
args->options.stla_float32_format;
|
2015-05-27 23:29:48 +08:00
|
|
|
const uint8_t float32_prec =
|
2015-12-10 01:51:03 +08:00
|
|
|
args->options.stla_float32_prec != 0 ?
|
|
|
|
args->options.stla_float32_prec :
|
|
|
|
9;
|
2015-05-27 23:29:48 +08:00
|
|
|
const gmio_bool_t write_triangles_only =
|
2015-12-10 01:51:03 +08:00
|
|
|
args->options.stl_write_triangles_only;
|
2015-03-20 00:31:08 +08:00
|
|
|
/* Variables */
|
2015-12-10 01:51:03 +08:00
|
|
|
struct gmio_rwargs* core_args = &args->core;
|
2015-03-03 17:35:36 +08:00
|
|
|
uint32_t ifacet = 0;
|
2016-01-05 18:44:10 +08:00
|
|
|
void* mblock_ptr = core_args->stream_memblock.ptr;
|
2015-09-25 19:16:41 +08:00
|
|
|
char* buffc = mblock_ptr;
|
2015-03-03 17:35:36 +08:00
|
|
|
char coords_format[64];
|
2015-04-02 16:04:24 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
2015-03-03 17:35:36 +08:00
|
|
|
|
|
|
|
/* Check validity of input parameters */
|
2015-12-10 01:51:03 +08:00
|
|
|
if (!gmio_check_rwargs(&error, core_args))
|
2015-03-18 23:27:43 +08:00
|
|
|
return error;
|
2015-12-10 01:51:03 +08:00
|
|
|
if (!gmio_stl_check_mesh(&error, &args->mesh))
|
2015-03-18 23:27:43 +08:00
|
|
|
return error;
|
2015-03-03 17:44:45 +08:00
|
|
|
if (float32_prec == 0 || float32_prec > 9)
|
2015-04-15 23:14:34 +08:00
|
|
|
return GMIO_STL_ERROR_INVALID_FLOAT32_PREC;
|
2016-01-05 18:44:10 +08:00
|
|
|
if (core_args->stream_memblock.size < GMIO_STLA_FACET_SIZE_P2)
|
2015-09-25 19:16:41 +08:00
|
|
|
return GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
|
2015-03-03 17:35:36 +08:00
|
|
|
|
|
|
|
{ /* Create XYZ coords format string (for normal and vertex coords) */
|
2015-04-15 23:11:15 +08:00
|
|
|
const char float32_specifier =
|
|
|
|
gmio_float_text_format_to_specifier(float32_format);
|
2015-03-20 00:31:08 +08:00
|
|
|
char* it = coords_format;
|
2015-04-15 23:11:15 +08:00
|
|
|
it = gmio_write_stdio_format(it, float32_specifier, float32_prec);
|
2015-10-28 23:38:57 +08:00
|
|
|
it = gmio_write_rawstr(it, " ");
|
2015-04-15 23:11:15 +08:00
|
|
|
it = gmio_write_stdio_format(it, float32_specifier, float32_prec);
|
2015-10-28 23:38:57 +08:00
|
|
|
it = gmio_write_rawstr(it, " ");
|
2015-04-15 23:11:15 +08:00
|
|
|
it = gmio_write_stdio_format(it, float32_specifier, float32_prec);
|
2015-03-20 00:31:08 +08:00
|
|
|
*it = 0; /* Write terminating null byte */
|
2015-03-03 17:35:36 +08:00
|
|
|
/* TODO: check the "format" string can contain the given precision */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write solid declaration */
|
2015-05-27 23:29:48 +08:00
|
|
|
if (!write_triangles_only) {
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "solid ");
|
|
|
|
buffc = gmio_write_rawstr_eol(buffc, solid_name);
|
2015-12-10 01:51:03 +08:00
|
|
|
if (!gmio_rwargs_flush_buffer(core_args, buffc - (char*)mblock_ptr))
|
2015-04-02 16:04:24 +08:00
|
|
|
return GMIO_ERROR_STREAM;
|
2015-03-03 17:35:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write solid's facets */
|
|
|
|
for (ifacet = 0;
|
|
|
|
ifacet < total_facet_count && gmio_no_error(error);
|
|
|
|
ifacet += buffer_facet_count)
|
|
|
|
{
|
2015-10-15 00:17:03 +08:00
|
|
|
const gmio_stl_mesh_func_get_triangle_t func_get_triangle =
|
2015-12-10 01:51:03 +08:00
|
|
|
args->mesh.func_get_triangle;
|
|
|
|
const void* mesh_cookie =
|
|
|
|
args->mesh.cookie;
|
2015-03-20 00:31:08 +08:00
|
|
|
const uint32_t clamped_facet_count =
|
|
|
|
GMIO_MIN(ifacet + buffer_facet_count, total_facet_count);
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_triangle tri;
|
2015-03-03 17:35:36 +08:00
|
|
|
uint32_t ibuffer_facet;
|
|
|
|
|
2015-12-10 01:51:03 +08:00
|
|
|
gmio_rwargs_handle_progress(core_args, ifacet, total_facet_count);
|
2015-03-13 17:32:18 +08:00
|
|
|
|
2015-03-03 17:35:36 +08:00
|
|
|
/* Writing of facets is buffered */
|
2015-09-25 19:16:41 +08:00
|
|
|
buffc = mblock_ptr;
|
2015-03-20 00:31:08 +08:00
|
|
|
for (ibuffer_facet = ifacet;
|
|
|
|
ibuffer_facet < clamped_facet_count;
|
|
|
|
++ibuffer_facet)
|
|
|
|
{
|
2015-10-15 00:17:03 +08:00
|
|
|
func_get_triangle(mesh_cookie, ibuffer_facet, &tri);
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "facet normal ");
|
2015-03-24 01:21:04 +08:00
|
|
|
buffc = gmio_write_coords(buffc, coords_format, &tri.normal);
|
|
|
|
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "\nouter loop");
|
|
|
|
buffc = gmio_write_rawstr(buffc, "\n vertex ");
|
2015-03-24 01:21:04 +08:00
|
|
|
buffc = gmio_write_coords(buffc, coords_format, &tri.v1);
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "\n vertex ");
|
2015-03-24 01:21:04 +08:00
|
|
|
buffc = gmio_write_coords(buffc, coords_format, &tri.v2);
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "\n vertex ");
|
2015-03-24 01:21:04 +08:00
|
|
|
buffc = gmio_write_coords(buffc, coords_format, &tri.v3);
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "\nendloop");
|
2015-03-24 01:21:04 +08:00
|
|
|
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr(buffc, "\nendfacet\n");
|
2015-03-03 17:35:36 +08:00
|
|
|
} /* end for (ibuffer_facet) */
|
|
|
|
|
2015-12-10 01:51:03 +08:00
|
|
|
if (!gmio_rwargs_flush_buffer(core_args, buffc - (char*)mblock_ptr))
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_ERROR_STREAM;
|
2015-03-03 17:35:36 +08:00
|
|
|
|
|
|
|
/* Task control */
|
2015-12-10 01:51:03 +08:00
|
|
|
if (gmio_no_error(error) && gmio_rwargs_is_stop_requested(core_args))
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_ERROR_TRANSFER_STOPPED;
|
2015-03-03 17:35:36 +08:00
|
|
|
} /* end for (ifacet) */
|
|
|
|
|
|
|
|
/* Write end of solid */
|
2015-05-27 23:29:48 +08:00
|
|
|
if (gmio_no_error(error) && !write_triangles_only) {
|
2015-12-10 01:51:03 +08:00
|
|
|
buffc = gmio_write_rawstr(mblock_ptr, "endsolid ");
|
2015-10-28 23:38:57 +08:00
|
|
|
buffc = gmio_write_rawstr_eol(buffc, solid_name);
|
2015-12-10 01:51:03 +08:00
|
|
|
if (!gmio_rwargs_flush_buffer(core_args, buffc - (char*)mblock_ptr))
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_ERROR_STREAM;
|
2015-03-03 17:35:36 +08:00
|
|
|
}
|
2013-04-27 06:11:53 +08:00
|
|
|
|
2015-03-03 17:35:36 +08:00
|
|
|
return error;
|
2013-03-06 18:49:53 +08:00
|
|
|
}
|