2015-03-03 00:38:33 +08:00
|
|
|
/****************************************************************************
|
2015-05-28 15:40:24 +08:00
|
|
|
** gmio
|
2016-06-24 18:03:07 +08:00
|
|
|
** Copyright Fougue (24 Jun. 2016)
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "stlb_write.h"
|
2013-01-15 20:10:44 +08:00
|
|
|
|
2015-10-15 00:17:03 +08:00
|
|
|
#include "stl_funptr_typedefs.h"
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "stl_rw_common.h"
|
|
|
|
#include "stlb_byte_swap.h"
|
|
|
|
#include "../stl_error.h"
|
2015-05-27 23:29:48 +08:00
|
|
|
#include "../stl_io.h"
|
2015-12-04 01:00:25 +08:00
|
|
|
#include "../stl_io_options.h"
|
2014-01-29 02:06:24 +08:00
|
|
|
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "../../gmio_core/error.h"
|
|
|
|
#include "../../gmio_core/internal/byte_codec.h"
|
2016-01-29 19:47:01 +08:00
|
|
|
#include "../../gmio_core/internal/helper_memblock.h"
|
|
|
|
#include "../../gmio_core/internal/helper_task_iface.h"
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "../../gmio_core/internal/min_max.h"
|
|
|
|
#include "../../gmio_core/internal/helper_stream.h"
|
|
|
|
#include "../../gmio_core/internal/safe_cast.h"
|
2014-01-29 02:06:24 +08:00
|
|
|
|
2013-01-15 20:10:44 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-01-26 22:54:06 +08:00
|
|
|
GMIO_INLINE void encode_facet(
|
|
|
|
const struct gmio_stl_triangle* triangle, uint8_t* buffer)
|
2014-01-23 01:29:57 +08:00
|
|
|
{
|
2016-01-26 22:54:06 +08:00
|
|
|
memcpy(buffer, triangle, GMIO_STLB_TRIANGLE_RAWSIZE);
|
2014-01-23 01:29:57 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:54:06 +08:00
|
|
|
typedef void (*func_gmio_stlb_encode_facets_t)(
|
|
|
|
const struct gmio_stl_mesh*,
|
|
|
|
uint8_t*, /* buffer */
|
|
|
|
const uint32_t, /* facet_count */
|
|
|
|
const uint32_t); /* i_facet_offset */
|
|
|
|
|
|
|
|
static void gmio_stlb_encode_facets(
|
2015-12-04 01:00:25 +08:00
|
|
|
const struct gmio_stl_mesh* mesh,
|
2016-01-26 22:54:06 +08:00
|
|
|
uint8_t* buffer,
|
|
|
|
const uint32_t facet_count,
|
|
|
|
const uint32_t i_facet_offset)
|
2013-02-05 19:24:13 +08:00
|
|
|
{
|
2015-10-15 00:17:03 +08:00
|
|
|
const gmio_stl_mesh_func_get_triangle_t func_get_triangle =
|
2015-11-06 22:33:01 +08:00
|
|
|
mesh->func_get_triangle;
|
2015-10-15 00:17:03 +08:00
|
|
|
const void* cookie = mesh->cookie;
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_triangle triangle;
|
2016-01-26 22:54:06 +08:00
|
|
|
uint32_t i_facet;
|
2013-02-05 19:24:13 +08:00
|
|
|
|
2015-03-03 17:35:36 +08:00
|
|
|
triangle.attribute_byte_count = 0;
|
2015-10-15 00:17:03 +08:00
|
|
|
for (i_facet = 0; i_facet < facet_count; ++i_facet) {
|
|
|
|
func_get_triangle(cookie, i_facet_offset + i_facet, &triangle);
|
2016-01-26 22:54:06 +08:00
|
|
|
encode_facet(&triangle, buffer);
|
|
|
|
buffer += GMIO_STLB_TRIANGLE_RAWSIZE;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 02:06:24 +08:00
|
|
|
|
2016-01-26 22:54:06 +08:00
|
|
|
static void gmio_stlb_encode_facets_byteswap(
|
|
|
|
const struct gmio_stl_mesh* mesh,
|
|
|
|
uint8_t* buffer,
|
|
|
|
const uint32_t facet_count,
|
|
|
|
const uint32_t i_facet_offset)
|
|
|
|
{
|
|
|
|
const gmio_stl_mesh_func_get_triangle_t func_get_triangle =
|
|
|
|
mesh->func_get_triangle;
|
|
|
|
const void* cookie = mesh->cookie;
|
|
|
|
struct gmio_stl_triangle triangle;
|
|
|
|
uint32_t i_facet;
|
2013-02-05 19:24:13 +08:00
|
|
|
|
2016-01-26 22:54:06 +08:00
|
|
|
triangle.attribute_byte_count = 0;
|
|
|
|
for (i_facet = 0; i_facet < facet_count; ++i_facet) {
|
|
|
|
func_get_triangle(cookie, i_facet_offset + i_facet, &triangle);
|
|
|
|
gmio_stl_triangle_bswap(&triangle);
|
|
|
|
encode_facet(&triangle, buffer);
|
|
|
|
buffer += GMIO_STLB_TRIANGLE_RAWSIZE;
|
|
|
|
}
|
2013-02-05 19:24:13 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 00:31:08 +08:00
|
|
|
int gmio_stlb_write(
|
2016-01-29 19:47:01 +08:00
|
|
|
enum gmio_endianness byte_order,
|
2016-03-01 21:50:31 +08:00
|
|
|
struct gmio_stream* stream,
|
|
|
|
const struct gmio_stl_mesh* mesh,
|
2016-01-29 19:47:01 +08:00
|
|
|
const struct gmio_stl_write_options* opts)
|
2013-01-15 20:10:44 +08:00
|
|
|
{
|
2015-03-20 00:31:08 +08:00
|
|
|
/* Constants */
|
2016-01-29 19:47:01 +08:00
|
|
|
const struct gmio_task_iface* task = opts != NULL ? &opts->task_iface : NULL;
|
|
|
|
struct gmio_memblock_helper mblock_helper =
|
|
|
|
gmio_memblock_helper(opts != NULL ? &opts->stream_memblock : NULL);
|
|
|
|
const size_t mblock_size = mblock_helper.memblock.size;
|
2016-03-01 21:50:31 +08:00
|
|
|
const uint32_t facet_count = mesh != NULL ? mesh->triangle_count : 0;
|
2016-01-26 22:54:06 +08:00
|
|
|
const func_gmio_stlb_encode_facets_t func_encode_facets =
|
|
|
|
byte_order != GMIO_ENDIANNESS_HOST ?
|
|
|
|
gmio_stlb_encode_facets_byteswap :
|
|
|
|
gmio_stlb_encode_facets;
|
2016-01-29 19:47:01 +08:00
|
|
|
const bool write_triangles_only =
|
|
|
|
opts != NULL ? opts->stl_write_triangles_only : false;
|
|
|
|
const struct gmio_stlb_header* header =
|
|
|
|
opts != NULL ? &opts->stlb_header : NULL;
|
2016-01-26 22:54:06 +08:00
|
|
|
|
2015-03-20 00:31:08 +08:00
|
|
|
/* Variables */
|
2016-01-26 22:54:06 +08:00
|
|
|
uint32_t i_facet = 0; /* Facet counter */
|
|
|
|
uint32_t write_facet_count =
|
2016-01-29 19:47:01 +08:00
|
|
|
gmio_size_to_uint32(mblock_size / GMIO_STLB_TRIANGLE_RAWSIZE);
|
|
|
|
void* const mblock_ptr = mblock_helper.memblock.ptr;
|
2015-04-02 16:04:24 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
2015-03-03 17:35:36 +08:00
|
|
|
|
|
|
|
/* Check validity of input parameters */
|
2016-01-29 19:47:01 +08:00
|
|
|
if (!gmio_check_memblock(&error, &mblock_helper.memblock))
|
|
|
|
goto label_end;
|
2016-03-01 21:50:31 +08:00
|
|
|
if (!gmio_stl_check_mesh(&error, mesh))
|
2016-01-29 19:47:01 +08:00
|
|
|
goto label_end;
|
|
|
|
if (!gmio_stlb_check_byteorder(&error, byte_order))
|
|
|
|
goto label_end;
|
|
|
|
|
|
|
|
if (!write_triangles_only) {
|
2016-03-11 19:43:30 +08:00
|
|
|
error = gmio_stlb_header_write(stream, byte_order, header, facet_count);
|
2015-05-27 23:29:48 +08:00
|
|
|
if (gmio_error(error))
|
2016-01-29 19:47:01 +08:00
|
|
|
goto label_end;
|
2013-02-05 19:24:13 +08:00
|
|
|
}
|
2015-03-03 17:35:36 +08:00
|
|
|
|
|
|
|
/* Write triangles */
|
|
|
|
for (i_facet = 0;
|
|
|
|
i_facet < facet_count && gmio_no_error(error);
|
2016-01-26 22:54:06 +08:00
|
|
|
i_facet += write_facet_count)
|
2015-03-03 17:35:36 +08:00
|
|
|
{
|
2016-01-29 19:47:01 +08:00
|
|
|
gmio_task_iface_handle_progress(task, i_facet, facet_count);
|
2015-03-13 17:32:18 +08:00
|
|
|
|
2015-09-25 19:16:41 +08:00
|
|
|
/* Write to memory block */
|
2016-01-26 22:54:06 +08:00
|
|
|
write_facet_count = GMIO_MIN(write_facet_count, facet_count - i_facet);
|
2016-03-01 21:50:31 +08:00
|
|
|
func_encode_facets(mesh, mblock_ptr, write_facet_count, i_facet);
|
2013-01-15 20:10:44 +08:00
|
|
|
|
2015-09-25 19:16:41 +08:00
|
|
|
/* Write memory block to stream */
|
2015-03-13 00:46:40 +08:00
|
|
|
if (gmio_stream_write(
|
2016-03-01 21:50:31 +08:00
|
|
|
stream,
|
2015-09-25 19:16:41 +08:00
|
|
|
mblock_ptr,
|
2015-03-13 00:46:40 +08:00
|
|
|
GMIO_STLB_TRIANGLE_RAWSIZE,
|
2016-01-26 22:54:06 +08:00
|
|
|
write_facet_count)
|
|
|
|
!= write_facet_count)
|
2015-03-03 17:35:36 +08:00
|
|
|
{
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_ERROR_STREAM;
|
2015-03-03 17:35:36 +08:00
|
|
|
}
|
|
|
|
|
2015-03-13 00:46:40 +08:00
|
|
|
/* Handle stop request */
|
2016-01-29 19:47:01 +08:00
|
|
|
if (gmio_no_error(error) && gmio_task_iface_is_stop_requested(task))
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_ERROR_TRANSFER_STOPPED;
|
2015-03-03 17:35:36 +08:00
|
|
|
} /* end for */
|
|
|
|
|
2016-01-29 19:47:01 +08:00
|
|
|
label_end:
|
|
|
|
gmio_memblock_helper_release(&mblock_helper);
|
2015-03-03 17:35:36 +08:00
|
|
|
return error;
|
2013-01-15 20:10:44 +08:00
|
|
|
}
|