2013-01-15 17:40:35 +08:00
|
|
|
#include "stlb_read.h"
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2013-01-15 02:30:42 +08:00
|
|
|
#include "../endian.h"
|
2014-01-27 22:30:46 +08:00
|
|
|
#include "../error.h"
|
|
|
|
#include "../internal/convert.h"
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2014-01-23 01:29:57 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-01-27 22:03:50 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
foug_endianness_t host_endianness;
|
|
|
|
foug_endianness_t stream_endianness;
|
|
|
|
} _internal_foug_endianness_info_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint32_t facet_count;
|
|
|
|
uint32_t i_facet_offset;
|
|
|
|
_internal_foug_endianness_info_t endian_info;
|
|
|
|
} _internal_foug_read_params_helper;
|
|
|
|
|
2014-01-23 01:29:57 +08:00
|
|
|
static void read_triangle_memcpy(const uint8_t* buffer, foug_stlb_triangle_t* triangle)
|
|
|
|
{
|
|
|
|
/* *triangle = *((foug_stlb_triangle_t*)(buffer)); */
|
|
|
|
memcpy(triangle, buffer, FOUG_STLB_TRIANGLE_RAWSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_coords_alignsafe(const uint8_t* buffer, foug_stl_coords_t* coords)
|
|
|
|
{
|
|
|
|
memcpy(coords, buffer, FOUG_STL_COORDS_RAWSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_triangle_alignsafe(const uint8_t* buffer, foug_stlb_triangle_t* triangle)
|
|
|
|
{
|
|
|
|
read_coords_alignsafe(buffer, &triangle->data.normal);
|
|
|
|
read_coords_alignsafe(buffer + 1*FOUG_STL_COORDS_RAWSIZE, &triangle->data.v1);
|
|
|
|
read_coords_alignsafe(buffer + 2*FOUG_STL_COORDS_RAWSIZE, &triangle->data.v2);
|
|
|
|
read_coords_alignsafe(buffer + 3*FOUG_STL_COORDS_RAWSIZE, &triangle->data.v3);
|
|
|
|
memcpy(&triangle->attribute_byte_count, buffer + 4*FOUG_STL_COORDS_RAWSIZE, sizeof(uint16_t));
|
|
|
|
}
|
|
|
|
|
2013-03-06 18:49:53 +08:00
|
|
|
static void foug_stlb_read_facets(foug_stlb_geom_input_t* geom,
|
2014-01-27 22:03:50 +08:00
|
|
|
const uint8_t* buffer,
|
|
|
|
const _internal_foug_read_params_helper* params)
|
2013-01-25 01:38:43 +08:00
|
|
|
{
|
2014-01-27 22:03:50 +08:00
|
|
|
const uint32_t facet_count = params->facet_count;
|
|
|
|
const uint32_t i_facet_offset = params->i_facet_offset;
|
2013-02-21 21:53:41 +08:00
|
|
|
foug_stlb_triangle_t triangle;
|
2013-01-25 01:38:43 +08:00
|
|
|
uint32_t buffer_offset;
|
|
|
|
uint32_t i_facet;
|
|
|
|
|
2014-01-23 02:17:19 +08:00
|
|
|
if (geom == NULL || geom->process_triangle_func == NULL)
|
2013-01-25 01:38:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
buffer_offset = 0;
|
|
|
|
for (i_facet = 0; i_facet < facet_count; ++i_facet) {
|
2013-02-21 21:53:41 +08:00
|
|
|
/* Decode data */
|
2014-01-23 01:29:57 +08:00
|
|
|
#ifdef FOUG_STLB_READWRITE_ALIGNSAFE
|
|
|
|
read_triangle_alignsafe(buffer + buffer_offset, &triangle);
|
|
|
|
#else
|
|
|
|
read_triangle_memcpy(buffer + buffer_offset, &triangle);
|
|
|
|
#endif
|
|
|
|
buffer_offset += FOUG_STLB_TRIANGLE_RAWSIZE;
|
2013-02-21 21:53:41 +08:00
|
|
|
|
|
|
|
/* Declare triangle */
|
2014-01-27 22:03:50 +08:00
|
|
|
geom->process_triangle_func(geom->cookie,
|
|
|
|
i_facet_offset + i_facet,
|
|
|
|
&triangle.data,
|
|
|
|
triangle.attribute_byte_count);
|
2013-01-25 01:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-27 06:16:38 +08:00
|
|
|
int foug_stlb_read(foug_stlb_geom_input_t* geom,
|
|
|
|
foug_transfer_t* trsf,
|
|
|
|
foug_endianness_t byte_order)
|
2013-01-11 01:48:46 +08:00
|
|
|
{
|
2014-01-27 22:03:50 +08:00
|
|
|
_internal_foug_read_params_helper read_params;
|
|
|
|
uint32_t total_facet_count = 0; /* Count of facets as declared in the stream */
|
|
|
|
int error = FOUG_DATAX_NO_ERROR; /* Helper variable to store function result error code */
|
2013-01-25 01:38:43 +08:00
|
|
|
|
2014-01-27 22:03:50 +08:00
|
|
|
/* Check validity of input parameters */
|
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-04-27 06:16:38 +08:00
|
|
|
if (byte_order != FOUG_LITTLE_ENDIAN)
|
|
|
|
return FOUG_STLB_READ_UNSUPPORTED_BYTE_ORDER;
|
2013-01-15 02:30:42 +08:00
|
|
|
|
2014-01-27 22:03:50 +08:00
|
|
|
/* Initialize read_params */
|
|
|
|
memset(&read_params, 0, sizeof(_internal_foug_read_params_helper));
|
|
|
|
read_params.endian_info.host_endianness = foug_host_endianness();
|
|
|
|
read_params.endian_info.stream_endianness = byte_order;
|
|
|
|
|
2013-01-15 02:30:42 +08:00
|
|
|
/* Read header */
|
2013-03-05 23:22:26 +08:00
|
|
|
{
|
|
|
|
uint8_t header_data[FOUG_STLB_HEADER_SIZE];
|
2013-03-06 18:49:53 +08:00
|
|
|
if (foug_stream_read(&trsf->stream, header_data, 1, FOUG_STLB_HEADER_SIZE)
|
2013-03-05 23:22:26 +08:00
|
|
|
!= FOUG_STLB_HEADER_SIZE)
|
|
|
|
{
|
|
|
|
return FOUG_STLB_READ_HEADER_WRONG_SIZE_ERROR;
|
|
|
|
}
|
2013-03-06 18:49:53 +08:00
|
|
|
if (geom != NULL && geom->process_header_func != NULL)
|
2014-01-27 22:03:50 +08:00
|
|
|
geom->process_header_func(geom->cookie, header_data);
|
2013-03-05 23:22:26 +08:00
|
|
|
}
|
2013-01-15 02:30:42 +08:00
|
|
|
|
|
|
|
/* Read facet count */
|
2013-03-06 18:49:53 +08:00
|
|
|
if (foug_stream_read(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1)
|
2013-01-15 02:30:42 +08:00
|
|
|
return FOUG_STLB_READ_FACET_COUNT_ERROR;
|
|
|
|
|
2013-03-06 18:49:53 +08:00
|
|
|
total_facet_count = foug_decode_uint32_le(trsf->buffer);
|
|
|
|
if (geom != NULL && geom->begin_triangles_func != NULL)
|
2014-01-27 22:03:50 +08:00
|
|
|
geom->begin_triangles_func(geom->cookie, total_facet_count);
|
2013-01-15 02:30:42 +08:00
|
|
|
|
|
|
|
/* Read triangles */
|
2014-01-27 22:03:50 +08:00
|
|
|
while (foug_datax_no_error(error)
|
|
|
|
&& read_params.i_facet_offset < total_facet_count)
|
|
|
|
{
|
|
|
|
read_params.facet_count = foug_stream_read(&trsf->stream,
|
|
|
|
trsf->buffer,
|
|
|
|
FOUG_STLB_TRIANGLE_RAWSIZE,
|
|
|
|
trsf->buffer_size / FOUG_STLB_TRIANGLE_RAWSIZE);
|
2013-03-06 18:49:53 +08:00
|
|
|
if (foug_stream_error(&trsf->stream) != 0)
|
2013-03-05 08:04:29 +08:00
|
|
|
error = FOUG_DATAX_STREAM_ERROR;
|
2014-01-27 22:03:50 +08:00
|
|
|
else if (read_params.facet_count > 0)
|
2013-03-05 08:04:29 +08:00
|
|
|
error = FOUG_DATAX_NO_ERROR;
|
2013-01-24 18:29:51 +08:00
|
|
|
else
|
|
|
|
break; /* Exit if no facet to read */
|
|
|
|
|
2013-03-05 08:04:29 +08:00
|
|
|
if (foug_datax_no_error(error)) {
|
2013-03-05 23:22:26 +08:00
|
|
|
uint8_t progress_pc;
|
|
|
|
|
2014-01-27 22:03:50 +08:00
|
|
|
foug_stlb_read_facets(geom, trsf->buffer, &read_params);
|
|
|
|
read_params.i_facet_offset += read_params.facet_count;
|
|
|
|
progress_pc = foug_percentage(0, total_facet_count, read_params.i_facet_offset);
|
2013-03-06 18:49:53 +08:00
|
|
|
if (!foug_task_control_handle_progress(&trsf->task_control, progress_pc))
|
2013-03-05 08:04:29 +08:00
|
|
|
error = FOUG_DATAX_TASK_STOPPED_ERROR;
|
2013-01-15 02:30:42 +08:00
|
|
|
}
|
|
|
|
} /* end while */
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2013-03-06 18:49:53 +08:00
|
|
|
if (foug_datax_no_error(error)
|
|
|
|
&& geom != NULL && geom->end_triangles_func != NULL)
|
|
|
|
{
|
2014-01-27 22:03:50 +08:00
|
|
|
geom->end_triangles_func(geom->cookie);
|
2013-03-06 18:49:53 +08:00
|
|
|
}
|
2013-01-11 01:48:46 +08:00
|
|
|
|
2014-01-27 22:03:50 +08:00
|
|
|
if (foug_datax_no_error(error) && read_params.i_facet_offset != total_facet_count)
|
2013-01-15 17:40:35 +08:00
|
|
|
error = FOUG_STLB_READ_FACET_COUNT_ERROR;
|
|
|
|
return error;
|
2013-01-15 02:30:42 +08:00
|
|
|
}
|