gmio/src/gmio_stl/internal/stlb_write.c

141 lines
4.6 KiB
C
Raw Normal View History

2015-03-03 00:38:33 +08:00
/****************************************************************************
2015-05-28 15:40:24 +08:00
** gmio
2015-05-01 00:19:45 +08:00
** Copyright Fougue (2 Mar. 2015)
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
****************************************************************************/
#include "stlb_write.h"
#include "stl_rw_common.h"
#include "stlb_byte_swap.h"
#include "../stl_error.h"
#include "../stl_io.h"
#include "../../gmio_core/error.h"
#include "../../gmio_core/internal/byte_codec.h"
#include "../../gmio_core/internal/min_max.h"
#include "../../gmio_core/internal/helper_stream.h"
#include "../../gmio_core/internal/helper_transfer.h"
#include "../../gmio_core/internal/safe_cast.h"
#include <string.h>
2015-03-31 16:10:26 +08:00
GMIO_INLINE void write_triangle_memcpy(
2015-09-25 19:16:41 +08:00
const gmio_stl_triangle_t* triangle, uint8_t* mblock)
{
2015-09-25 19:16:41 +08:00
memcpy(mblock, triangle, GMIO_STLB_TRIANGLE_RAWSIZE);
}
2015-03-20 00:31:08 +08:00
static void gmio_stlb_write_facets(
const gmio_stl_mesh_t* mesh,
2015-09-25 19:16:41 +08:00
uint8_t* mblock,
2015-03-20 00:31:08 +08:00
const gmio_stlb_readwrite_helper_t* wparams)
{
2015-03-03 17:35:36 +08:00
const uint32_t facet_count = wparams->facet_count;
const uint32_t i_facet_offset = wparams->i_facet_offset;
gmio_stl_triangle_t triangle;
2015-09-25 19:16:41 +08:00
uint32_t mblock_offset = 0;
2015-03-03 17:35:36 +08:00
uint32_t i_facet = 0;
if (mesh == NULL || mesh->func_get_triangle == NULL)
2015-03-03 17:35:36 +08:00
return;
2015-03-03 17:35:36 +08:00
triangle.attribute_byte_count = 0;
2015-03-20 00:31:08 +08:00
for (i_facet = i_facet_offset;
i_facet < (i_facet_offset + facet_count);
++i_facet)
{
mesh->func_get_triangle(mesh->cookie, i_facet, &triangle);
if (wparams->func_fix_endian != NULL)
wparams->func_fix_endian(&triangle);
2015-09-25 19:16:41 +08:00
write_triangle_memcpy(&triangle, mblock + mblock_offset);
2015-09-25 19:16:41 +08:00
mblock_offset += GMIO_STLB_TRIANGLE_RAWSIZE;
2015-03-03 17:35:36 +08:00
} /* end for */
}
2015-03-20 00:31:08 +08:00
int gmio_stlb_write(
gmio_transfer_t* trsf,
const gmio_stl_mesh_t* mesh,
const gmio_stl_write_options_t* options,
gmio_endianness_t byte_order)
{
2015-03-20 00:31:08 +08:00
/* Constants */
const uint32_t facet_count =
mesh != NULL ? mesh->triangle_count : 0;
const gmio_bool_t write_triangles_only =
options != NULL ? options->stl_write_triangles_only : GMIO_FALSE;
2015-03-20 00:31:08 +08:00
/* Variables */
2015-09-25 19:16:41 +08:00
void* mblock_ptr = trsf != NULL ? trsf->memblock.ptr : NULL;
2015-03-03 17:35:36 +08:00
gmio_stlb_readwrite_helper_t wparams = {0};
uint32_t i_facet = 0;
int error = GMIO_ERROR_OK;
2015-03-03 17:35:36 +08:00
/* Check validity of input parameters */
gmio_stl_check_mesh(&error, mesh);
gmio_stlb_check_params(&error, trsf, byte_order);
if (gmio_error(error))
return error;
/* Initialize wparams */
if (byte_order != GMIO_ENDIANNESS_HOST)
wparams.func_fix_endian = gmio_stl_triangle_bswap;
/* Note: trsf != NULL certified by gmio_stlb_check_params() */
/* coverity[var_deref_op : FALSE] */
2015-03-20 00:31:08 +08:00
wparams.facet_count = gmio_size_to_uint32(
2015-09-25 19:16:41 +08:00
trsf->memblock.size / GMIO_STLB_TRIANGLE_RAWSIZE);
2015-03-03 17:35:36 +08:00
if (!write_triangles_only) {
error = gmio_stlb_write_header(
&trsf->stream,
byte_order,
options != NULL ? options->stlb_header_data : NULL,
facet_count);
if (gmio_error(error))
return error;
}
2015-03-03 17:35:36 +08:00
/* Write triangles */
for (i_facet = 0;
i_facet < facet_count && gmio_no_error(error);
i_facet += wparams.facet_count)
{
gmio_transfer_handle_progress(trsf, i_facet, facet_count);
2015-09-25 19:16:41 +08:00
/* Write to memory block */
wparams.facet_count = GMIO_MIN(wparams.facet_count,
facet_count - wparams.i_facet_offset);
2015-09-25 19:16:41 +08:00
gmio_stlb_write_facets(mesh, mblock_ptr, &wparams);
2015-03-03 17:35:36 +08:00
wparams.i_facet_offset += wparams.facet_count;
2015-09-25 19:16:41 +08:00
/* Write memory block to stream */
if (gmio_stream_write(
&trsf->stream,
2015-09-25 19:16:41 +08:00
mblock_ptr,
GMIO_STLB_TRIANGLE_RAWSIZE,
wparams.facet_count)
2015-03-03 17:35:36 +08:00
!= wparams.facet_count)
{
error = GMIO_ERROR_STREAM;
2015-03-03 17:35:36 +08:00
}
/* Handle stop request */
if (gmio_no_error(error) && gmio_transfer_is_stop_requested(trsf))
error = GMIO_ERROR_TRANSFER_STOPPED;
2015-03-03 17:35:36 +08:00
} /* end for */
return error;
}