gmio/src/gmio_stl/stla_write.c

222 lines
7.3 KiB
C
Raw Normal View History

2015-03-03 00:38:33 +08:00
/****************************************************************************
**
** 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
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 "stl_io.h"
2013-03-06 18:49:53 +08:00
#include "internal/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"
2015-03-03 01:00:10 +08:00
#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
static 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 : "";
strcpy(buffer, safe_str);
return buffer + strlen(safe_str);
2013-04-27 06:11:53 +08:00
}
static char* gmio_write_string_eol(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);
buffer[len] = '\n';
return buffer + len + 1;
2013-04-27 06:11:53 +08:00
}
static 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
}
/*static char* gmio_write_space(char* buffer)
2013-04-27 06:11:53 +08:00
{
*buffer = ' ';
return buffer + 1;
}*/
static 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
}
static char* gmio_write_stdio_format(char* buffer, 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-03-03 17:35:36 +08:00
buffer[2 + prec_len] = 'E';
return buffer + 3 + prec_len;
2013-04-27 06:11:53 +08:00
}
2015-03-20 00:31:08 +08:00
static char* gmio_write_coords(
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 =
gmio_stream_write(&trsf->stream, trsf->buffer.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,
2015-03-20 00:31:08 +08:00
const gmio_stla_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 char* solid_name = options != NULL ? options->solid_name : NULL;
const uint8_t float32_prec = options != NULL ? options->float32_prec : 9;
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 ?
gmio_size_to_uint32(trsf->buffer.size / GMIO_STLA_FACET_SIZE_P2)
2015-03-20 00:31:08 +08:00
: 0;
/* Variables */
2015-03-03 17:35:36 +08:00
uint32_t ifacet = 0;
void* buffer_ptr = trsf != NULL ? trsf->buffer.ptr : NULL;
char* buffc = buffer_ptr;
2015-03-03 17:35:36 +08:00
char coords_format[64];
int error = GMIO_NO_ERROR;
/* 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)
2015-03-18 23:27:43 +08:00
return GMIO_STLA_WRITE_INVALID_REAL32_PREC_ERROR;
if (trsf->buffer.size < GMIO_STLA_FACET_SIZE_P2)
2015-03-18 23:27:43 +08:00
return GMIO_INVALID_BUFFER_SIZE_ERROR;
2015-03-03 17:35:36 +08:00
{ /* Create XYZ coords format string (for normal and vertex coords) */
2015-03-20 00:31:08 +08:00
char* it = coords_format;
it = gmio_write_stdio_format(it, float32_prec);
it = gmio_write_nspaces(it, 2);
it = gmio_write_stdio_format(it, float32_prec);
it = gmio_write_nspaces(it, 2);
it = gmio_write_stdio_format(it, float32_prec);
*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 */
{
buffc = gmio_write_string(buffc, "solid ");
buffc = gmio_write_string_eol(buffc, solid_name);
if (!gmio_transfer_flush_buffer(trsf, buffc - (char*)buffer_ptr))
2015-03-03 17:35:36 +08:00
return GMIO_STREAM_ERROR;
}
/* 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 */
buffc = buffer_ptr;
2015-03-20 00:31:08 +08:00
for (ibuffer_facet = ifacet;
ibuffer_facet < clamped_facet_count;
++ibuffer_facet)
{
2015-03-03 17:35:36 +08:00
mesh->get_triangle_func(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) */
if (!gmio_transfer_flush_buffer(trsf, buffc - (char*)buffer_ptr))
2015-03-03 17:35:36 +08:00
error = GMIO_STREAM_ERROR;
/* Task control */
if (gmio_no_error(error) && gmio_transfer_is_stop_requested(trsf))
error = GMIO_TRANSFER_STOPPED_ERROR;
2015-03-03 17:35:36 +08:00
} /* end for (ifacet) */
/* Write end of solid */
if (gmio_no_error(error)) {
buffc = gmio_write_string(trsf->buffer.ptr, "endsolid ");
buffc = gmio_write_string_eol(buffc, solid_name);
if (!gmio_transfer_flush_buffer(trsf, buffc - (char*)buffer_ptr))
2015-03-03 17:35:36 +08:00
error = GMIO_STREAM_ERROR;
}
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
}