2015-03-24 01:21:04 +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-24 01:21:04 +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-24 01:21:04 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "stl_io.h"
|
|
|
|
|
|
|
|
#include "stl_error.h"
|
|
|
|
#include "stl_format.h"
|
2015-04-02 23:59:02 +08:00
|
|
|
#include "internal/stla_write.h"
|
|
|
|
#include "internal/stlb_write.h"
|
2015-03-24 01:21:04 +08:00
|
|
|
#include "../gmio_core/error.h"
|
|
|
|
#include "../gmio_core/stream.h"
|
|
|
|
#include "../gmio_core/transfer.h"
|
2015-05-27 23:29:48 +08:00
|
|
|
#include "../gmio_core/internal/byte_codec.h"
|
2015-03-24 01:21:04 +08:00
|
|
|
#include "../gmio_core/internal/helper_stream.h"
|
|
|
|
|
|
|
|
int gmio_stl_read_file(
|
|
|
|
const char* filepath,
|
2015-04-02 22:09:08 +08:00
|
|
|
gmio_stl_mesh_creator_t* creator,
|
|
|
|
gmio_task_iface_t* task_iface)
|
2015-03-24 01:21:04 +08:00
|
|
|
{
|
2015-04-02 16:04:24 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
2015-03-24 01:21:04 +08:00
|
|
|
FILE* file = NULL;
|
|
|
|
|
|
|
|
file = fopen(filepath, "rb");
|
|
|
|
if (file != NULL) {
|
2015-09-20 05:14:42 +08:00
|
|
|
gmio_transfer_t trsf = {0};
|
2015-03-24 01:21:04 +08:00
|
|
|
trsf.stream = gmio_stream_stdio(file);
|
2015-04-02 21:36:57 +08:00
|
|
|
trsf.buffer = gmio_buffer_default();
|
2015-04-02 22:09:08 +08:00
|
|
|
if (task_iface != NULL)
|
|
|
|
trsf.task_iface = *task_iface;
|
2015-03-24 01:21:04 +08:00
|
|
|
|
|
|
|
error = gmio_stl_read(&trsf, creator);
|
|
|
|
fclose(file);
|
2015-04-02 21:36:57 +08:00
|
|
|
gmio_buffer_deallocate(&trsf.buffer);
|
2015-03-24 01:21:04 +08:00
|
|
|
}
|
|
|
|
else {
|
2015-05-28 23:31:36 +08:00
|
|
|
error = GMIO_ERROR_STDIO;
|
2015-03-24 01:21:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gmio_stl_read(gmio_transfer_t *trsf, gmio_stl_mesh_creator_t *creator)
|
|
|
|
{
|
2015-04-02 16:04:24 +08:00
|
|
|
int error = GMIO_ERROR_OK;
|
2015-03-24 01:21:04 +08:00
|
|
|
|
|
|
|
if (trsf != NULL) {
|
|
|
|
const gmio_stl_format_t stl_format = gmio_stl_get_format(&trsf->stream);
|
|
|
|
|
|
|
|
switch (stl_format) {
|
2015-04-02 16:58:10 +08:00
|
|
|
case GMIO_STL_FORMAT_ASCII: {
|
2015-03-30 15:30:55 +08:00
|
|
|
error = gmio_stla_read(trsf, creator);
|
2015-03-24 01:21:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-04-02 16:58:10 +08:00
|
|
|
case GMIO_STL_FORMAT_BINARY_BE: {
|
2015-04-02 15:45:02 +08:00
|
|
|
error = gmio_stlb_read(trsf, creator, GMIO_ENDIANNESS_BIG);
|
2015-03-24 01:21:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-04-02 16:58:10 +08:00
|
|
|
case GMIO_STL_FORMAT_BINARY_LE: {
|
2015-04-02 15:45:02 +08:00
|
|
|
error = gmio_stlb_read(trsf, creator, GMIO_ENDIANNESS_LITTLE);
|
2015-03-24 01:21:04 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-04-02 16:58:10 +08:00
|
|
|
case GMIO_STL_FORMAT_UNKNOWN: {
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
|
2015-03-24 01:21:04 +08:00
|
|
|
}
|
|
|
|
} /* end switch() */
|
|
|
|
}
|
|
|
|
else {
|
2015-04-02 16:04:24 +08:00
|
|
|
error = GMIO_ERROR_NULL_TRANSFER;
|
2015-03-24 01:21:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2015-04-02 22:38:14 +08:00
|
|
|
|
|
|
|
int gmio_stl_write_file(
|
|
|
|
gmio_stl_format_t format,
|
|
|
|
const char *filepath,
|
2015-04-02 23:09:02 +08:00
|
|
|
const gmio_stl_mesh_t *mesh,
|
2015-04-02 23:59:02 +08:00
|
|
|
gmio_task_iface_t *task_iface,
|
|
|
|
const gmio_stl_write_options_t *options)
|
2015-04-02 22:38:14 +08:00
|
|
|
{
|
|
|
|
int error = GMIO_ERROR_OK;
|
|
|
|
FILE* file = NULL;
|
|
|
|
|
|
|
|
file = fopen(filepath, "wb");
|
|
|
|
if (file != NULL) {
|
2015-09-20 05:14:42 +08:00
|
|
|
gmio_transfer_t trsf = {0};
|
2015-04-02 22:38:14 +08:00
|
|
|
trsf.stream = gmio_stream_stdio(file);
|
|
|
|
trsf.buffer = gmio_buffer_default();
|
|
|
|
if (task_iface != NULL)
|
|
|
|
trsf.task_iface = *task_iface;
|
|
|
|
|
2015-04-02 23:59:02 +08:00
|
|
|
error = gmio_stl_write(format, &trsf, mesh, options);
|
2015-04-02 22:38:14 +08:00
|
|
|
fclose(file);
|
|
|
|
gmio_buffer_deallocate(&trsf.buffer);
|
|
|
|
}
|
|
|
|
else {
|
2015-05-28 23:31:36 +08:00
|
|
|
error = GMIO_ERROR_STDIO;
|
2015-04-02 22:38:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gmio_stl_write(
|
|
|
|
gmio_stl_format_t format,
|
|
|
|
gmio_transfer_t *trsf,
|
2015-04-02 23:59:02 +08:00
|
|
|
const gmio_stl_mesh_t *mesh,
|
|
|
|
const gmio_stl_write_options_t *options)
|
2015-04-02 22:38:14 +08:00
|
|
|
{
|
|
|
|
int error = GMIO_ERROR_OK;
|
|
|
|
|
|
|
|
if (trsf != NULL) {
|
|
|
|
switch (format) {
|
|
|
|
case GMIO_STL_FORMAT_ASCII: {
|
2015-05-27 23:29:48 +08:00
|
|
|
error = gmio_stla_write(trsf, mesh, options);
|
2015-04-02 22:38:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GMIO_STL_FORMAT_BINARY_BE: {
|
2015-05-27 23:29:48 +08:00
|
|
|
error = gmio_stlb_write(trsf, mesh, options, GMIO_ENDIANNESS_BIG);
|
2015-04-02 22:38:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GMIO_STL_FORMAT_BINARY_LE: {
|
2015-05-27 23:29:48 +08:00
|
|
|
error = gmio_stlb_write(trsf, mesh, options, GMIO_ENDIANNESS_LITTLE);
|
2015-04-02 22:38:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GMIO_STL_FORMAT_UNKNOWN: {
|
|
|
|
error = GMIO_STL_ERROR_UNKNOWN_FORMAT;
|
|
|
|
}
|
|
|
|
} /* end switch() */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = GMIO_ERROR_NULL_TRANSFER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
2015-05-27 23:29:48 +08:00
|
|
|
|
2015-05-28 22:19:07 +08:00
|
|
|
static const gmio_stlb_header_t internal_stlb_zero_header = {0};
|
2015-05-27 23:29:48 +08:00
|
|
|
|
|
|
|
int gmio_stlb_write_header(
|
|
|
|
gmio_stream_t *stream,
|
|
|
|
gmio_endianness_t byte_order,
|
2015-05-28 22:19:07 +08:00
|
|
|
const gmio_stlb_header_t *header,
|
2015-05-27 23:29:48 +08:00
|
|
|
uint32_t facet_count)
|
|
|
|
{
|
|
|
|
uint8_t facet_count_bytes[sizeof(uint32_t)];
|
2015-05-28 22:19:07 +08:00
|
|
|
const gmio_stlb_header_t* non_null_header =
|
|
|
|
header != NULL ? header : &internal_stlb_zero_header;
|
2015-05-27 23:29:48 +08:00
|
|
|
|
|
|
|
/* Write 80-byte header */
|
|
|
|
if (gmio_stream_write(stream, non_null_header, GMIO_STLB_HEADER_SIZE, 1)
|
|
|
|
!= 1)
|
|
|
|
{
|
|
|
|
return GMIO_ERROR_STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write facet count */
|
|
|
|
if (byte_order == GMIO_ENDIANNESS_LITTLE)
|
|
|
|
gmio_encode_uint32_le(facet_count, &facet_count_bytes[0]);
|
|
|
|
else
|
|
|
|
gmio_encode_uint32_be(facet_count, &facet_count_bytes[0]);
|
|
|
|
if (gmio_stream_write(stream, &facet_count_bytes[0], sizeof(uint32_t), 1)
|
|
|
|
!= 1)
|
|
|
|
{
|
|
|
|
return GMIO_ERROR_STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GMIO_ERROR_OK;
|
|
|
|
}
|