2014-03-28 23:33:35 +08:00
|
|
|
#include "stl_io.h"
|
|
|
|
|
|
|
|
#include "stl_error.h"
|
|
|
|
#include "internal/stl_rw_common.h"
|
|
|
|
#include "internal/stlb_byte_swap.h"
|
|
|
|
|
|
|
|
#include "../gmio_core/endian.h"
|
|
|
|
#include "../gmio_core/error.h"
|
|
|
|
#include "../gmio_core/internal/convert.h"
|
|
|
|
#include "../gmio_core/internal/byte_swap.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
GMIO_INLINE static void read_triangle_memcpy(const uint8_t* buffer,
|
|
|
|
gmio_stl_triangle_t* triangle)
|
|
|
|
{
|
|
|
|
/* *triangle = *((gmio_stl_triangle_t*)(buffer)); */
|
|
|
|
memcpy(triangle, buffer, GMIO_STLB_TRIANGLE_RAWSIZE);
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:52:04 +08:00
|
|
|
static void gmio_stlb_read_facets(gmio_stl_mesh_creator_t* creator,
|
2014-03-28 23:33:35 +08:00
|
|
|
const uint8_t* buffer,
|
2014-03-31 21:39:21 +08:00
|
|
|
const gmio_stlb_readwrite_helper_t* rparams)
|
2014-03-28 23:33:35 +08:00
|
|
|
{
|
|
|
|
const uint32_t facet_count = rparams->facet_count;
|
|
|
|
const uint32_t i_facet_offset = rparams->i_facet_offset;
|
|
|
|
gmio_stl_triangle_t triangle;
|
|
|
|
uint32_t buffer_offset = 0;
|
|
|
|
uint32_t i_facet = 0;
|
|
|
|
|
|
|
|
if (creator == NULL || creator->add_triangle_func == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i_facet = 0; i_facet < facet_count; ++i_facet) {
|
|
|
|
/* Decode data */
|
|
|
|
read_triangle_memcpy(buffer + buffer_offset, &triangle);
|
|
|
|
buffer_offset += GMIO_STLB_TRIANGLE_RAWSIZE;
|
|
|
|
|
|
|
|
if (rparams->fix_endian_func != NULL)
|
|
|
|
rparams->fix_endian_func(&triangle);
|
|
|
|
|
|
|
|
/* Declare triangle */
|
|
|
|
creator->add_triangle_func(creator->cookie, i_facet_offset + i_facet, &triangle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:52:04 +08:00
|
|
|
int gmio_stlb_read(gmio_stl_mesh_creator_t *creator,
|
2014-03-28 23:33:35 +08:00
|
|
|
gmio_transfer_t* trsf,
|
|
|
|
gmio_endianness_t byte_order)
|
|
|
|
{
|
|
|
|
const gmio_endianness_t host_byte_order = gmio_host_endianness();
|
2014-11-19 16:41:10 +08:00
|
|
|
gmio_stlb_readwrite_helper_t rparams = {0};
|
2014-03-28 23:33:35 +08:00
|
|
|
uint8_t header_data[GMIO_STLB_HEADER_SIZE];
|
|
|
|
uint32_t total_facet_count = 0; /* Count of facets as declared in the stream */
|
|
|
|
int error = GMIO_NO_ERROR; /* Helper variable to store function result error code */
|
|
|
|
|
|
|
|
/* Check validity of input parameters */
|
|
|
|
if (!gmio_stlb_check_params(&error, trsf, byte_order))
|
|
|
|
return error;
|
|
|
|
|
|
|
|
/* Initialize rparams */
|
|
|
|
if (host_byte_order != byte_order)
|
|
|
|
rparams.fix_endian_func = gmio_stl_triangle_bswap;
|
|
|
|
|
|
|
|
/* Read header */
|
|
|
|
if (gmio_stream_read(&trsf->stream, header_data, 1, GMIO_STLB_HEADER_SIZE)
|
|
|
|
!= GMIO_STLB_HEADER_SIZE)
|
|
|
|
{
|
|
|
|
return GMIO_STLB_READ_HEADER_WRONG_SIZE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read facet count */
|
|
|
|
if (gmio_stream_read(&trsf->stream, trsf->buffer, sizeof(uint32_t), 1) != 1)
|
|
|
|
return GMIO_STLB_READ_FACET_COUNT_ERROR;
|
|
|
|
|
|
|
|
memcpy(&total_facet_count, trsf->buffer, sizeof(uint32_t));
|
|
|
|
if (host_byte_order != byte_order)
|
|
|
|
total_facet_count = gmio_uint32_bswap(total_facet_count);
|
|
|
|
|
|
|
|
/* Callback to notify triangle count and header data */
|
|
|
|
if (creator != NULL && creator->binary_begin_solid_func != NULL)
|
|
|
|
creator->binary_begin_solid_func(creator->cookie, total_facet_count, header_data);
|
|
|
|
|
|
|
|
/* Read triangles */
|
|
|
|
while (gmio_no_error(error)
|
|
|
|
&& rparams.i_facet_offset < total_facet_count)
|
|
|
|
{
|
|
|
|
rparams.facet_count = gmio_stream_read(&trsf->stream,
|
|
|
|
trsf->buffer,
|
|
|
|
GMIO_STLB_TRIANGLE_RAWSIZE,
|
|
|
|
trsf->buffer_size / GMIO_STLB_TRIANGLE_RAWSIZE);
|
|
|
|
if (gmio_stream_error(&trsf->stream) != 0)
|
|
|
|
error = GMIO_STREAM_ERROR;
|
|
|
|
else if (rparams.facet_count > 0)
|
|
|
|
error = GMIO_NO_ERROR;
|
|
|
|
else
|
|
|
|
break; /* Exit if no facet to read */
|
|
|
|
|
|
|
|
if (gmio_no_error(error)) {
|
|
|
|
uint8_t progress_pc;
|
|
|
|
|
|
|
|
gmio_stlb_read_facets(creator, trsf->buffer, &rparams);
|
|
|
|
rparams.i_facet_offset += rparams.facet_count;
|
|
|
|
progress_pc = gmio_percentage(0, total_facet_count, rparams.i_facet_offset);
|
|
|
|
if (!gmio_task_control_handle_progress(&trsf->task_control, progress_pc))
|
|
|
|
error = GMIO_TASK_STOPPED_ERROR;
|
|
|
|
}
|
|
|
|
} /* end while */
|
|
|
|
|
|
|
|
if (gmio_no_error(error)
|
|
|
|
&& creator != NULL
|
|
|
|
&& creator->end_solid_func != NULL)
|
|
|
|
{
|
|
|
|
creator->end_solid_func(creator->cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gmio_no_error(error) && rparams.i_facet_offset != total_facet_count)
|
|
|
|
error = GMIO_STLB_READ_FACET_COUNT_ERROR;
|
|
|
|
return error;
|
|
|
|
}
|