gmio/src/gmio_stl/internal/stla_write.c

244 lines
8.4 KiB
C
Raw Normal View History

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
****************************************************************************/
#include "stla_write.h"
2013-03-06 18:49:53 +08:00
#include "stl_rw_common.h"
#include "../stl_error.h"
2013-03-06 18:49:53 +08:00
#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"
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
*/
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
GMIO_INLINE char* gmio_write_string(char* buffer, const char* str)
2013-04-27 06:11:53 +08:00
{
2015-03-03 17:35:36 +08:00
const char* safe_str = str != NULL ? str : "";
const size_t len = strlen(safe_str);
strncpy(buffer, safe_str, len);
return buffer + len;
2013-04-27 06:11:53 +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
}
GMIO_INLINE char* gmio_write_string_eol(char* buffer, const char* str)
{
buffer = gmio_write_string(buffer, str);
return gmio_write_eol(buffer);
}
/*static char* gmio_write_space(char* buffer)
2013-04-27 06:11:53 +08:00
{
*buffer = ' ';
return buffer + 1;
}*/
GMIO_INLINE char* gmio_write_nspaces(char* buffer, int n)
2013-04-27 06:11:53 +08:00
{
2015-03-03 17:35:36 +08:00
const int offset = n;
while (n > 0)
*(buffer + (--n)) = ' ';
return buffer + offset;
2013-04-27 06:11:53 +08:00
}
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)
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);
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
}
GMIO_INLINE char* gmio_write_coords(
2015-03-20 00:31:08 +08:00
char* buffer,
const char* coords_format,
const gmio_stl_coords_t* 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
}
static gmio_bool_t gmio_transfer_flush_buffer(gmio_transfer_t* trsf, size_t n)
2013-03-06 18:49:53 +08:00
{
const size_t write_count =
2015-09-25 19:16:41 +08:00
gmio_stream_write(&trsf->stream, trsf->memblock.ptr, sizeof(char), n);
return write_count == n;
2013-04-27 06:11:53 +08:00
}
2015-03-20 00:31:08 +08:00
int gmio_stla_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
const gmio_stl_write_options_t *options)
2013-04-27 06:11:53 +08:00
{
2015-03-20 00:31:08 +08:00
/* Constants */
2015-03-03 17:35:36 +08:00
const uint32_t total_facet_count = mesh != NULL ? mesh->triangle_count : 0;
const uint32_t buffer_facet_count =
trsf != NULL ?
2015-09-25 19:16:41 +08:00
gmio_size_to_uint32(trsf->memblock.size / GMIO_STLA_FACET_SIZE_P2)
2015-03-20 00:31:08 +08:00
: 0;
const char* solid_name =
options != NULL ? options->stla_solid_name : NULL;
const gmio_float_text_format_t float32_format =
options != NULL ?
options->stla_float32_format :
GMIO_FLOAT_TEXT_FORMAT_DECIMAL_LOWERCASE;
const uint8_t float32_prec =
options != NULL ? options->stla_float32_prec : 9;
const gmio_bool_t write_triangles_only =
options != NULL ? options->stl_write_triangles_only : GMIO_FALSE;
2015-03-20 00:31:08 +08:00
/* Variables */
2015-03-03 17:35:36 +08:00
uint32_t ifacet = 0;
2015-09-25 19:16:41 +08:00
void* mblock_ptr = trsf != NULL ? trsf->memblock.ptr : NULL;
char* buffc = mblock_ptr;
2015-03-03 17:35:36 +08:00
char coords_format[64];
int error = GMIO_ERROR_OK;
2015-03-03 17:35:36 +08:00
/* Check validity of input parameters */
2015-03-18 23:27:43 +08:00
if (!gmio_check_transfer(&error, trsf))
return error;
if (!gmio_stl_check_mesh(&error, mesh))
return error;
if (float32_prec == 0 || float32_prec > 9)
return GMIO_STL_ERROR_INVALID_FLOAT32_PREC;
2015-09-25 19:16:41 +08:00
if (trsf->memblock.size < GMIO_STLA_FACET_SIZE_P2)
return GMIO_ERROR_INVALID_MEMBLOCK_SIZE;
2015-03-03 17:35:36 +08:00
{ /* Create XYZ coords format string (for normal and vertex coords) */
const char float32_specifier =
gmio_float_text_format_to_specifier(float32_format);
2015-03-20 00:31:08 +08:00
char* it = coords_format;
it = gmio_write_stdio_format(it, float32_specifier, float32_prec);
2015-03-20 00:31:08 +08:00
it = gmio_write_nspaces(it, 2);
it = gmio_write_stdio_format(it, float32_specifier, float32_prec);
2015-03-20 00:31:08 +08:00
it = gmio_write_nspaces(it, 2);
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 */
if (!write_triangles_only) {
buffc = gmio_write_string(buffc, "solid ");
buffc = gmio_write_string_eol(buffc, solid_name);
2015-09-25 19:16:41 +08:00
if (!gmio_transfer_flush_buffer(trsf, buffc - (char*)mblock_ptr))
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-03-20 00:31:08 +08:00
const uint32_t clamped_facet_count =
GMIO_MIN(ifacet + buffer_facet_count, total_facet_count);
2015-03-03 17:35:36 +08:00
gmio_stl_triangle_t tri;
uint32_t ibuffer_facet;
gmio_transfer_handle_progress(trsf, ifacet, total_facet_count);
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)
{
mesh->func_get_triangle(mesh->cookie, ibuffer_facet, &tri);
buffc = gmio_write_string(buffc, "facet normal ");
buffc = gmio_write_coords(buffc, coords_format, &tri.normal);
buffc = gmio_write_eol(buffc);
buffc = gmio_write_string_eol(buffc, "outer loop");
buffc = gmio_write_string(buffc, " vertex ");
buffc = gmio_write_coords(buffc, coords_format, &tri.v1);
buffc = gmio_write_eol(buffc);
buffc = gmio_write_string(buffc, " vertex ");
buffc = gmio_write_coords(buffc, coords_format, &tri.v2);
buffc = gmio_write_eol(buffc);
buffc = gmio_write_string(buffc, " vertex ");
buffc = gmio_write_coords(buffc, coords_format, &tri.v3);
buffc = gmio_write_eol(buffc);
buffc = gmio_write_string_eol(buffc, "endloop");
buffc = gmio_write_string_eol(buffc, "endfacet");
2015-03-03 17:35:36 +08:00
} /* end for (ibuffer_facet) */
2015-09-25 19:16:41 +08:00
if (!gmio_transfer_flush_buffer(trsf, buffc - (char*)mblock_ptr))
error = GMIO_ERROR_STREAM;
2015-03-03 17:35:36 +08:00
/* Task control */
if (gmio_no_error(error) && gmio_transfer_is_stop_requested(trsf))
error = GMIO_ERROR_TRANSFER_STOPPED;
2015-03-03 17:35:36 +08:00
} /* end for (ifacet) */
/* Write end of solid */
if (gmio_no_error(error) && !write_triangles_only) {
2015-09-25 19:16:41 +08:00
buffc = gmio_write_string(trsf->memblock.ptr, "endsolid ");
buffc = gmio_write_string_eol(buffc, solid_name);
2015-09-25 19:16:41 +08:00
if (!gmio_transfer_flush_buffer(trsf, buffc - (char*)mblock_ptr))
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
}