2013-01-15 20:10:44 +08:00
|
|
|
#include "stlb_write.h"
|
|
|
|
|
|
|
|
#include "../endian.h"
|
2013-03-05 08:04:29 +08:00
|
|
|
#include "../error.h"
|
2013-01-15 20:10:44 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-01-23 01:29:57 +08:00
|
|
|
static void write_triangle_memcpy(const foug_stlb_triangle_t* triangle, uint8_t* buffer)
|
|
|
|
{
|
|
|
|
memcpy(buffer, triangle, FOUG_STLB_TRIANGLE_RAWSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_coords_alignsafe(const foug_stl_coords_t* coords, uint8_t* buffer)
|
|
|
|
{
|
|
|
|
memcpy(buffer, coords, FOUG_STL_COORDS_RAWSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void write_triangle_alignsafe(const foug_stlb_triangle_t* triangle, uint8_t* buffer)
|
|
|
|
{
|
|
|
|
write_coords_alignsafe(&triangle->data.normal, buffer);
|
|
|
|
write_coords_alignsafe(&triangle->data.v1, buffer + 1*FOUG_STL_COORDS_RAWSIZE);
|
|
|
|
write_coords_alignsafe(&triangle->data.v2, buffer + 2*FOUG_STL_COORDS_RAWSIZE);
|
|
|
|
write_coords_alignsafe(&triangle->data.v3, buffer + 3*FOUG_STL_COORDS_RAWSIZE);
|
|
|
|
memcpy(buffer + 4*FOUG_STL_COORDS_RAWSIZE, &triangle->attribute_byte_count, sizeof(uint16_t));
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:11:00 +08:00
|
|
|
static void foug_stlb_write_facets(const foug_stlb_geom_output_t* geom,
|
2013-02-05 19:24:13 +08:00
|
|
|
uint8_t* buffer,
|
|
|
|
uint32_t ifacet_start,
|
|
|
|
uint32_t facet_count)
|
|
|
|
{
|
2013-02-21 21:53:41 +08:00
|
|
|
foug_stlb_triangle_t triangle;
|
2013-02-05 19:24:13 +08:00
|
|
|
uint32_t buffer_offset;
|
|
|
|
uint32_t i_facet;
|
|
|
|
|
2013-03-27 20:11:00 +08:00
|
|
|
if (geom == NULL || geom->get_triangle_func == NULL)
|
2013-02-05 19:24:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
buffer_offset = 0;
|
|
|
|
for (i_facet = ifacet_start; i_facet < (ifacet_start + facet_count); ++i_facet) {
|
2013-03-27 20:11:00 +08:00
|
|
|
geom->get_triangle_func(geom, i_facet, &triangle);
|
2013-02-05 19:24:13 +08:00
|
|
|
|
2014-01-23 01:29:57 +08:00
|
|
|
#ifdef FOUG_STLB_READWRITE_ALIGNSAFE
|
|
|
|
write_triangle_alignsafe(&triangle, buffer + buffer_offset);
|
|
|
|
#else
|
|
|
|
write_triangle_memcpy(&triangle, buffer + buffer_offset);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
buffer_offset += FOUG_STLB_TRIANGLE_RAWSIZE;
|
2013-02-05 19:24:13 +08:00
|
|
|
} /* end for */
|
|
|
|
}
|
|
|
|
|
2013-04-27 06:16:38 +08:00
|
|
|
int foug_stlb_write(const foug_stlb_geom_output_t* geom,
|
2014-01-10 05:22:57 +08:00
|
|
|
foug_transfer_t* trsf,
|
|
|
|
foug_endianness_t byte_order)
|
2013-01-15 20:10:44 +08:00
|
|
|
{
|
2014-01-23 02:17:19 +08:00
|
|
|
const uint32_t facet_count = geom != NULL ? geom->triangle_count : 0;
|
2013-01-25 01:38:43 +08:00
|
|
|
uint32_t i_facet;
|
2013-02-05 19:24:13 +08:00
|
|
|
uint32_t buffer_facet_count;
|
|
|
|
uint32_t ifacet_start;
|
2013-01-25 01:38:43 +08:00
|
|
|
int error;
|
|
|
|
|
2013-03-06 18:49:53 +08:00
|
|
|
if (trsf->buffer == NULL)
|
2013-03-05 08:04:29 +08:00
|
|
|
return FOUG_DATAX_NULL_BUFFER_ERROR;
|
2013-03-06 18:49:53 +08:00
|
|
|
if (trsf->buffer_size < FOUG_STLB_MIN_CONTENTS_SIZE)
|
2013-03-05 08:04:29 +08:00
|
|
|
return FOUG_DATAX_INVALID_BUFFER_SIZE_ERROR;
|
2013-03-06 18:49:53 +08:00
|
|
|
if (geom == NULL || geom->get_triangle_func == NULL)
|
2013-01-15 20:10:44 +08:00
|
|
|
return FOUG_STLB_WRITE_NULL_GET_TRIANGLE_FUNC;
|
2013-04-27 06:16:38 +08:00
|
|
|
if (byte_order != FOUG_LITTLE_ENDIAN/* && byte_order != FOUG_BIG_ENDIAN*/)
|
|
|
|
return FOUG_STLB_WRITE_UNSUPPORTED_BYTE_ORDER;
|
2013-01-15 20:10:44 +08:00
|
|
|
|
|
|
|
/* Write header */
|
2013-03-05 23:22:26 +08:00
|
|
|
{
|
2014-01-23 02:17:19 +08:00
|
|
|
const uint8_t* header_data = NULL;
|
|
|
|
if (geom->header != NULL) {
|
|
|
|
header_data = geom->header;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Use buffer to store an empty header (filled with zeroes) */
|
|
|
|
memset(trsf->buffer, 0, FOUG_STLB_HEADER_SIZE);
|
|
|
|
header_data = (const uint8_t*)trsf->buffer;
|
|
|
|
}
|
2013-03-06 18:49:53 +08:00
|
|
|
if (foug_stream_write(&trsf->stream, header_data, FOUG_STLB_HEADER_SIZE, 1) != 1)
|
2013-03-05 23:22:26 +08:00
|
|
|
return FOUG_DATAX_STREAM_ERROR;
|
|
|
|
}
|
2013-01-15 20:10:44 +08:00
|
|
|
|
|
|
|
/* Write facet count */
|
2013-04-27 06:16:38 +08:00
|
|
|
if (byte_order == FOUG_LITTLE_ENDIAN)
|
|
|
|
foug_encode_uint32_le(facet_count, trsf->buffer);
|
|
|
|
else
|
|
|
|
foug_encode_uint32_be(facet_count, trsf->buffer);
|
2013-03-06 18:49:53 +08:00
|
|
|
if (foug_stream_write(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1)
|
2013-03-05 08:04:29 +08:00
|
|
|
return FOUG_DATAX_STREAM_ERROR;
|
2013-01-15 20:10:44 +08:00
|
|
|
|
|
|
|
/* Write triangles */
|
2013-03-05 08:04:29 +08:00
|
|
|
error = FOUG_DATAX_NO_ERROR;
|
2013-01-15 20:10:44 +08:00
|
|
|
|
2014-01-23 01:29:57 +08:00
|
|
|
buffer_facet_count = trsf->buffer_size / FOUG_STLB_TRIANGLE_RAWSIZE;
|
2013-02-05 19:24:13 +08:00
|
|
|
ifacet_start = 0;
|
|
|
|
for (i_facet = 0;
|
2013-03-05 08:04:29 +08:00
|
|
|
i_facet < facet_count && foug_datax_no_error(error);
|
2013-02-05 19:24:13 +08:00
|
|
|
i_facet += buffer_facet_count)
|
|
|
|
{
|
|
|
|
/* Write to buffer */
|
|
|
|
if (buffer_facet_count > (facet_count - ifacet_start))
|
|
|
|
buffer_facet_count = facet_count - ifacet_start;
|
2013-03-06 18:49:53 +08:00
|
|
|
foug_stlb_write_facets(geom, trsf->buffer, ifacet_start, buffer_facet_count);
|
2013-02-05 19:24:13 +08:00
|
|
|
ifacet_start += buffer_facet_count;
|
|
|
|
|
|
|
|
/* Write buffer to stream */
|
2014-01-23 01:29:57 +08:00
|
|
|
if (foug_stream_write(&trsf->stream, trsf->buffer, FOUG_STLB_TRIANGLE_RAWSIZE, buffer_facet_count)
|
2013-02-05 19:24:13 +08:00
|
|
|
!= buffer_facet_count)
|
|
|
|
{
|
2013-03-05 08:04:29 +08:00
|
|
|
error = FOUG_DATAX_STREAM_ERROR;
|
2013-02-05 19:24:13 +08:00
|
|
|
}
|
2013-01-15 20:10:44 +08:00
|
|
|
|
|
|
|
/* Task control */
|
2013-03-05 23:22:26 +08:00
|
|
|
if (foug_datax_no_error(error)
|
2013-03-06 18:49:53 +08:00
|
|
|
&& !foug_task_control_handle_progress(&trsf->task_control,
|
2013-03-05 23:22:26 +08:00
|
|
|
foug_percentage(0, facet_count, i_facet + 1)))
|
|
|
|
{
|
|
|
|
error = FOUG_DATAX_TASK_STOPPED_ERROR;
|
2013-01-15 20:10:44 +08:00
|
|
|
}
|
|
|
|
} /* end for */
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|