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
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-13 23:43:19 +08:00
|
|
|
#include "stl_io.h"
|
2013-03-03 05:05:17 +08:00
|
|
|
|
2014-01-29 02:06:24 +08:00
|
|
|
#include "stl_error.h"
|
2015-03-30 16:18:18 +08:00
|
|
|
#include "internal/helper_stl_mesh_creator.h"
|
2015-10-15 00:17:03 +08:00
|
|
|
#include "internal/stl_funptr_typedefs.h"
|
2014-03-14 00:49:39 +08:00
|
|
|
#include "internal/stl_rw_common.h"
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
#include "../gmio_core/error.h"
|
2015-03-24 01:21:04 +08:00
|
|
|
#include "../gmio_core/internal/helper_stream.h"
|
2015-03-13 18:04:14 +08:00
|
|
|
#include "../gmio_core/internal/helper_transfer.h"
|
2015-07-10 16:55:02 +08:00
|
|
|
#include "../gmio_core/internal/min_max.h"
|
2015-11-20 18:53:57 +08:00
|
|
|
#include "../gmio_core/internal/stringstream.h"
|
2015-03-31 22:16:04 +08:00
|
|
|
#include "../gmio_core/internal/string_utils.h"
|
2013-03-05 08:04:29 +08:00
|
|
|
|
2014-01-27 22:28:12 +08:00
|
|
|
#include <ctype.h>
|
2014-01-28 05:57:10 +08:00
|
|
|
#include <stdlib.h>
|
2013-03-03 05:05:17 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-01-29 23:59:19 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-04-29 18:24:51 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* STL ASCII grammar:
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
|
|
|
|
CONTENTS -> SOLID
|
|
|
|
CONTENTS -> SOLID SOLID
|
|
|
|
|
|
|
|
SOLID -> BEG_SOLID FACETS END_SOLID
|
|
|
|
BEG_SOLID -> solid SOLID_NAME
|
|
|
|
END_SOLID -> endsolid SOLID_NAME
|
|
|
|
|
|
|
|
SOLID_NAME ->
|
|
|
|
SOLID_NAME -> [id] (Note: [id] == facet is forbidden)
|
|
|
|
|
|
|
|
FACETS ->
|
|
|
|
FACETS -> F
|
|
|
|
FACETS -> FF
|
|
|
|
F -> facet N outer loop V V V endloop endfacet
|
|
|
|
|
|
|
|
V -> vertex XYZ
|
|
|
|
N -> normal XYZ
|
|
|
|
XYZ -> [float] [float] [float]
|
|
|
|
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* Nullable, FIRST and FOLLOW:
|
|
|
|
* \code
|
|
|
|
| Nullable | FIRST | FOLLOW
|
2013-04-29 19:21:39 +08:00
|
|
|
----------+----------+--------------+-----------------------------
|
2013-04-29 18:24:51 +08:00
|
|
|
CONTENTS | N solid
|
|
|
|
SOLID | N solid solid
|
|
|
|
BEG_SOLID | N solid facet
|
|
|
|
END_SOLID | N endsolid solid
|
|
|
|
SOLID_NAME | Y [id] facet, endsolid, solid
|
|
|
|
FACETS | Y facet
|
|
|
|
F | N facet facet, endsolid
|
|
|
|
N | N normal outer
|
|
|
|
V | N vertex vertex, endloop
|
|
|
|
XYZ | N [float] outer, vertex, endloop
|
|
|
|
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
/* gmio_stla_token */
|
2015-11-20 18:20:06 +08:00
|
|
|
typedef enum
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-10-22 00:35:09 +08:00
|
|
|
null_token = 0,
|
|
|
|
ENDFACET_token,
|
|
|
|
ENDLOOP_token,
|
|
|
|
ENDSOLID_token,
|
|
|
|
FACET_token,
|
|
|
|
LOOP_token,
|
|
|
|
NORMAL_token,
|
|
|
|
OUTER_token,
|
|
|
|
SOLID_token,
|
|
|
|
VERTEX_token,
|
|
|
|
ID_token,
|
2015-03-03 17:35:36 +08:00
|
|
|
FLOAT_token = ID_token,
|
2015-10-22 00:35:09 +08:00
|
|
|
empty_token,
|
|
|
|
unknown_token
|
2015-11-20 18:20:06 +08:00
|
|
|
} gmio_stla_token_t;
|
2015-10-22 00:35:09 +08:00
|
|
|
|
2015-11-20 18:53:57 +08:00
|
|
|
/* gmio_stringstream_stla_cookie_t */
|
2015-10-29 22:23:21 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* Copy of gmio_stla_read() corresponding argument */
|
|
|
|
gmio_transfer_t* transfer;
|
|
|
|
/* Cache for gmio_stream_size(&transfer->stream) */
|
2015-11-06 20:43:03 +08:00
|
|
|
gmio_streamsize_t stream_size;
|
2015-10-29 22:23:21 +08:00
|
|
|
/* Offset (in bytes) from beginning of stream : current position */
|
2015-11-06 20:43:03 +08:00
|
|
|
gmio_streamoffset_t stream_offset;
|
2015-10-29 22:23:21 +08:00
|
|
|
/* Cache for gmio_transfer::func_is_stop_requested() */
|
|
|
|
gmio_bool_t is_stop_requested;
|
2015-11-20 18:53:57 +08:00
|
|
|
} gmio_stringstream_stla_cookie_t;
|
2015-10-29 22:23:21 +08:00
|
|
|
|
2014-03-28 23:33:35 +08:00
|
|
|
/* gmio_stla_parse_data */
|
2014-01-27 22:03:50 +08:00
|
|
|
typedef struct
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
gmio_stla_token_t token;
|
2015-03-20 00:31:08 +08:00
|
|
|
gmio_bool_t error;
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_t stream_iterator;
|
|
|
|
gmio_stringstream_stla_cookie_t stream_iterator_cookie;
|
2015-10-22 15:58:38 +08:00
|
|
|
gmio_string_t string_buffer;
|
2015-03-03 23:54:13 +08:00
|
|
|
gmio_stl_mesh_creator_t* creator;
|
2014-03-28 23:33:35 +08:00
|
|
|
} gmio_stla_parse_data_t;
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
/* Fixed maximum length of any gmio_string in this source file */
|
|
|
|
enum { GMIO_STLA_READ_STRING_MAX_LEN = 1024 };
|
|
|
|
|
|
|
|
/* Callback used for gmio_string_stream_fwd_iterator::func_stream_read_hook */
|
2015-11-20 18:53:57 +08:00
|
|
|
static void gmio_stringstream_stla_read_hook(
|
2015-11-20 18:20:06 +08:00
|
|
|
void* cookie, const gmio_string_t* strbuff)
|
2013-03-05 21:54:11 +08:00
|
|
|
{
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_stla_cookie_t* tcookie =
|
|
|
|
(gmio_stringstream_stla_cookie_t*)(cookie);
|
2015-03-13 00:46:40 +08:00
|
|
|
const gmio_transfer_t* trsf = tcookie != NULL ? tcookie->transfer : NULL;
|
2015-03-13 17:32:18 +08:00
|
|
|
if (tcookie != NULL) {
|
2015-11-20 18:20:06 +08:00
|
|
|
tcookie->stream_offset += strbuff->len;
|
2015-03-13 17:32:18 +08:00
|
|
|
tcookie->is_stop_requested = gmio_transfer_is_stop_requested(trsf);
|
|
|
|
gmio_transfer_handle_progress(
|
|
|
|
trsf, tcookie->stream_offset, tcookie->stream_size);
|
|
|
|
}
|
2013-03-03 05:05:17 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
/* Root function, parses a whole solid */
|
|
|
|
static void parse_solid(gmio_stla_parse_data_t* data);
|
2013-03-03 05:05:17 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
int gmio_stla_read(gmio_transfer_t* trsf, gmio_stl_mesh_creator_t* creator)
|
2015-03-31 21:56:36 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
char fixed_buffer[GMIO_STLA_READ_STRING_MAX_LEN];
|
|
|
|
gmio_stla_parse_data_t parse_data;
|
2015-03-31 21:56:36 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
{ /* Check validity of input parameters */
|
|
|
|
int error = GMIO_ERROR_OK;
|
|
|
|
if (!gmio_check_transfer(&error, trsf))
|
|
|
|
return error;
|
|
|
|
}
|
2015-03-31 21:56:36 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
parse_data.token = unknown_token;
|
|
|
|
parse_data.error = GMIO_FALSE;
|
|
|
|
|
|
|
|
parse_data.stream_iterator_cookie.transfer = trsf;
|
|
|
|
parse_data.stream_iterator_cookie.stream_offset = 0;
|
|
|
|
parse_data.stream_iterator_cookie.stream_size =
|
|
|
|
gmio_stream_size(&trsf->stream);
|
|
|
|
parse_data.stream_iterator_cookie.is_stop_requested = GMIO_FALSE;
|
|
|
|
|
|
|
|
parse_data.stream_iterator.stream = &trsf->stream;
|
|
|
|
parse_data.stream_iterator.strbuff.ptr = trsf->memblock.ptr;
|
|
|
|
parse_data.stream_iterator.strbuff.max_len = trsf->memblock.size;
|
|
|
|
parse_data.stream_iterator.cookie = &parse_data.stream_iterator_cookie;
|
|
|
|
parse_data.stream_iterator.func_stream_read_hook =
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_stla_read_hook;
|
|
|
|
gmio_stringstream_init(&parse_data.stream_iterator);
|
2015-11-20 18:20:06 +08:00
|
|
|
|
|
|
|
parse_data.string_buffer.ptr = &fixed_buffer[0];
|
|
|
|
parse_data.string_buffer.len = 0;
|
|
|
|
parse_data.string_buffer.max_len = GMIO_STLA_READ_STRING_MAX_LEN;
|
|
|
|
|
|
|
|
parse_data.creator = creator;
|
|
|
|
|
|
|
|
parse_solid(&parse_data);
|
|
|
|
|
|
|
|
if (parse_data.error)
|
|
|
|
return GMIO_STL_ERROR_PARSING;
|
|
|
|
if (parse_data.stream_iterator_cookie.is_stop_requested)
|
|
|
|
return GMIO_ERROR_TRANSFER_STOPPED;
|
|
|
|
return GMIO_ERROR_OK;
|
2015-03-31 21:56:36 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* __________________________________________________________________________
|
|
|
|
*
|
|
|
|
* Private API
|
|
|
|
* __________________________________________________________________________ */
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* STLA token utils
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static const char stla_tokstr_ENDFACET[] = "endfacet";
|
|
|
|
static const char stla_tokstr_ENDLOOP[] = "endloop";
|
|
|
|
static const char stla_tokstr_ENDSOLID[] = "endsolid";
|
|
|
|
static const char stla_tokstr_FACET[] = "facet";
|
|
|
|
static const char stla_tokstr_LOOP[] = "loop";
|
|
|
|
static const char stla_tokstr_NORMAL[] = "normal";
|
|
|
|
static const char stla_tokstr_OUTER[] = "outer";
|
|
|
|
static const char stla_tokstr_SOLID[] = "solid";
|
|
|
|
static const char stla_tokstr_VERTEX[] = "vertex";
|
|
|
|
|
|
|
|
static const gmio_const_string_t stla_tokcstr[] = {
|
|
|
|
{0}, /* null_token */
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_ENDFACET),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_ENDLOOP),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_ENDSOLID),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_FACET),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_LOOP),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_NORMAL),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_OUTER),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_SOLID),
|
|
|
|
GMIO_CONST_STRING_FROM_ARRAY(stla_tokstr_VERTEX),
|
|
|
|
{ "ID", 2 }, /* ID_token */
|
|
|
|
{ "", 0 }, /* empty_token */
|
|
|
|
{ "?", 1 } /* unknown_token */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Returns the string corresponding to token */
|
|
|
|
GMIO_INLINE const char* stla_token_to_string(gmio_stla_token_t token);
|
|
|
|
|
|
|
|
/* Qualifies input string as a token */
|
|
|
|
static gmio_stla_token_t stla_find_token(const char* word, size_t word_len);
|
|
|
|
|
|
|
|
/* Same as parsing_find_token() but takes a gmio_string_t object as input */
|
|
|
|
GMIO_INLINE gmio_stla_token_t stla_find_token_from_string(const gmio_string_t* str);
|
|
|
|
|
|
|
|
/* Returns true if \p token matches one of the candidate tokens
|
|
|
|
*
|
|
|
|
* Array \p candidates must end with a "null_token" item
|
|
|
|
*/
|
|
|
|
GMIO_INLINE gmio_bool_t stla_token_match_candidate(
|
|
|
|
gmio_stla_token_t token, const gmio_stla_token_t* candidates);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* Error message functions
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Makes the parsing fails and print error message */
|
|
|
|
static void stla_error_msg(
|
|
|
|
gmio_stla_parse_data_t* data, const char* msg);
|
|
|
|
|
|
|
|
/* Makes the parsing fails with message showing token mismatch */
|
|
|
|
static void stla_error_token_expected(
|
|
|
|
gmio_stla_parse_data_t* data, gmio_stla_token_t token);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* Parsing helper functions
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Eats next token string and checks it against an expected token
|
|
|
|
*
|
|
|
|
* This procedure copies the token string into internal
|
|
|
|
* gmio_stla_parse_data_t::string_buffer
|
|
|
|
*/
|
|
|
|
static int stla_eat_next_token(
|
|
|
|
gmio_stla_parse_data_t* data, gmio_stla_token_t expected_token);
|
|
|
|
|
|
|
|
/* Eats next token string and checks it against an expected token
|
|
|
|
*
|
|
|
|
* This procedure does the same thing as parsing_eat_next_token() but is faster
|
|
|
|
* as it does not copy the token string(it just "reads" it).
|
|
|
|
* It performs "in-place" case insensitive string comparison of the current
|
|
|
|
* token string against expected
|
|
|
|
*/
|
|
|
|
static int stla_eat_next_token_inplace(
|
|
|
|
gmio_stla_parse_data_t* data, gmio_stla_token_t expected_token);
|
|
|
|
|
|
|
|
/* Eats contents until some expected "end" token is matched
|
|
|
|
*
|
|
|
|
* Array \p end_tokens must end with a "null_token" item
|
|
|
|
*/
|
|
|
|
static int stla_eat_until_token(
|
|
|
|
gmio_stla_parse_data_t* data, const gmio_stla_token_t* end_tokens);
|
|
|
|
|
|
|
|
/* Returns true if parsing can continue */
|
|
|
|
GMIO_INLINE gmio_bool_t stla_parsing_can_continue(
|
|
|
|
const gmio_stla_parse_data_t* data);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* STLA parsing functions
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Used as general STLA parsing error code */
|
|
|
|
enum { GMIO_STLA_PARSE_ERROR = 1 };
|
|
|
|
|
|
|
|
/* Parses the (optional) solid name that appears after token "solid" */
|
|
|
|
static int parse_solidname_beg(gmio_stla_parse_data_t* data);
|
|
|
|
|
|
|
|
/* Parses the (optional) solid name that appears after token "endsolid"
|
|
|
|
*
|
|
|
|
* It should be the same name as the one parsed with parse_solidname_beg()
|
|
|
|
*/
|
|
|
|
static int parse_solidname_end(gmio_stla_parse_data_t* data);
|
|
|
|
|
|
|
|
/* Parses "solid <name>" */
|
|
|
|
static int parse_beginsolid(gmio_stla_parse_data_t* data);
|
|
|
|
|
|
|
|
/* Parses "endsolid <name>" */
|
|
|
|
static gmio_bool_t parse_endsolid(gmio_stla_parse_data_t* data);
|
|
|
|
|
|
|
|
/* Parses STL (x,y,z) coords, each coord being separated by whitespaces */
|
|
|
|
GMIO_INLINE int parse_xyz_coords(
|
|
|
|
gmio_stla_parse_data_t* data, gmio_stl_coords_t* coords);
|
|
|
|
|
|
|
|
/* Parses a STL facet, ie. facet ... endfacet */
|
|
|
|
static int parse_facet(
|
|
|
|
gmio_stla_parse_data_t* data, gmio_stl_triangle_t* facet);
|
|
|
|
|
|
|
|
/* Parses a list of facets */
|
|
|
|
static void parse_facets(gmio_stla_parse_data_t* data);
|
|
|
|
|
|
|
|
/* __________________________________________________________________________
|
|
|
|
*
|
|
|
|
* Private API implementation
|
|
|
|
* __________________________________________________________________________ */
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* STLA token utils
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const char* stla_token_to_string(gmio_stla_token_t token)
|
2015-03-31 21:56:36 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
return stla_tokcstr[token].ptr;
|
2015-03-31 21:56:36 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
gmio_stla_token_t stla_find_token(const char* word, size_t word_len)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
/* Get rid of ill-formed token */
|
|
|
|
if (word_len == 0)
|
|
|
|
return empty_token;
|
|
|
|
|
|
|
|
/* Try to find non "endXxx" token */
|
|
|
|
if (word_len >= 4) {
|
|
|
|
switch (word[0]) {
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 1, "acet") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return FACET_token;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 1, "oop") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return LOOP_token;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 1, "ormal") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return NORMAL_token;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
case 'O':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 1, "uter") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return OUTER_token;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 1, "olid") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return SOLID_token;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
case 'V':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 1, "ertex") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return VERTEX_token;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
2015-03-03 17:35:36 +08:00
|
|
|
|
|
|
|
/* Might be "end..." token */
|
2015-10-29 22:23:21 +08:00
|
|
|
if (word_len >= 7 && gmio_ascii_istarts_with(word, "end")) {
|
2015-03-03 17:35:36 +08:00
|
|
|
switch (word[3]) {
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 4, "acet") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return ENDFACET_token;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 4, "oop") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return ENDLOOP_token;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
2015-10-29 22:23:21 +08:00
|
|
|
if (gmio_ascii_stricmp(word + 4, "olid") == 0)
|
2015-03-03 17:35:36 +08:00
|
|
|
return ENDSOLID_token;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
2014-01-29 23:59:19 +08:00
|
|
|
|
2015-03-03 17:35:36 +08:00
|
|
|
return ID_token;
|
2014-01-29 23:59:19 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
gmio_stla_token_t stla_find_token_from_string(const gmio_string_t* str)
|
2015-07-10 16:55:02 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
return stla_find_token(str->ptr, str->len);
|
2015-07-10 16:55:02 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
gmio_bool_t stla_token_match_candidate(
|
|
|
|
gmio_stla_token_t token, const gmio_stla_token_t* candidates)
|
|
|
|
{
|
|
|
|
gmio_bool_t found = GMIO_FALSE;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; !found && candidates[i] != null_token; ++i)
|
|
|
|
found = token == candidates[i];
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* Error message functions
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void stla_error_msg(gmio_stla_parse_data_t* data, const char* msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n"
|
|
|
|
"gmio_stla_read() parsing error: %s\n"
|
|
|
|
" current token: <%s>\n"
|
|
|
|
" current token string: \"%s\"\n",
|
|
|
|
msg,
|
|
|
|
stla_token_to_string(data->token),
|
|
|
|
data->string_buffer.ptr);
|
|
|
|
data->error = GMIO_TRUE;
|
|
|
|
data->token = unknown_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stla_error_token_expected(
|
|
|
|
gmio_stla_parse_data_t* data, gmio_stla_token_t token)
|
|
|
|
{
|
|
|
|
char msg[256] = {0};
|
|
|
|
sprintf(msg,
|
|
|
|
"token <%s> expected, got <%s>",
|
|
|
|
stla_token_to_string(token),
|
|
|
|
stla_token_to_string(data->token));
|
|
|
|
stla_error_msg(data, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* Parsing helper functions
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int stla_eat_next_token(
|
|
|
|
gmio_stla_parse_data_t* data,
|
|
|
|
gmio_stla_token_t expected_token)
|
2014-01-29 23:59:19 +08:00
|
|
|
{
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_string_t* strbuff = &data->string_buffer;
|
2015-10-22 00:35:09 +08:00
|
|
|
gmio_eat_word_error_t eat_error;
|
|
|
|
|
2015-11-20 18:53:57 +08:00
|
|
|
strbuff->len = 0;
|
|
|
|
eat_error = gmio_stringstream_eat_word(&data->stream_iterator, strbuff);
|
2015-10-22 00:35:09 +08:00
|
|
|
if (eat_error == GMIO_EAT_WORD_ERROR_OK) {
|
2015-11-20 18:20:06 +08:00
|
|
|
const char* expected_token_str = stla_token_to_string(expected_token);
|
2015-11-20 18:53:57 +08:00
|
|
|
if (gmio_ascii_stricmp(strbuff->ptr, expected_token_str) == 0) {
|
2015-11-20 18:20:06 +08:00
|
|
|
data->token = expected_token;
|
|
|
|
return 0;
|
2015-10-22 00:35:09 +08:00
|
|
|
}
|
2015-03-03 17:35:36 +08:00
|
|
|
data->token = unknown_token;
|
2015-11-20 18:20:06 +08:00
|
|
|
if (expected_token == unknown_token)
|
|
|
|
return 0;
|
|
|
|
stla_error_token_expected(data, expected_token);
|
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
2015-10-22 00:35:09 +08:00
|
|
|
}
|
|
|
|
else {
|
2015-11-20 18:20:06 +08:00
|
|
|
stla_error_msg(data, "failure to get next word with gmio_eat_word()");
|
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
2015-10-22 00:35:09 +08:00
|
|
|
}
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
2013-03-03 05:05:17 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
int stla_eat_next_token_inplace(
|
|
|
|
gmio_stla_parse_data_t* data,
|
|
|
|
gmio_stla_token_t expected_token)
|
2015-11-02 17:47:11 +08:00
|
|
|
{
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_t* it = &data->stream_iterator;
|
2015-11-20 18:20:06 +08:00
|
|
|
const char* stream_char = NULL;
|
|
|
|
const char* expected_token_str = stla_token_to_string(expected_token);
|
|
|
|
gmio_bool_t error = GMIO_FALSE;
|
|
|
|
|
2015-11-02 17:47:11 +08:00
|
|
|
data->token = unknown_token;
|
2015-11-20 18:53:57 +08:00
|
|
|
stream_char = gmio_stringstream_skip_ascii_spaces(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
while (!error) {
|
|
|
|
if (stream_char == NULL || gmio_ascii_isspace(*stream_char)) {
|
|
|
|
if (*expected_token_str == 0) {
|
|
|
|
data->token = expected_token;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
error = GMIO_TRUE;
|
|
|
|
}
|
|
|
|
else if (!gmio_ascii_char_iequals(*stream_char, *expected_token_str)
|
|
|
|
|| *expected_token_str == 0)
|
|
|
|
{
|
|
|
|
error = GMIO_TRUE;
|
|
|
|
}
|
2015-11-20 18:53:57 +08:00
|
|
|
stream_char = gmio_stringstream_next_char(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
++expected_token_str;
|
|
|
|
}
|
2015-11-02 17:47:11 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
if (error) {
|
|
|
|
stla_error_token_expected(data, expected_token);
|
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
2013-03-03 05:05:17 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
int stla_eat_until_token(
|
|
|
|
gmio_stla_parse_data_t* data, const gmio_stla_token_t* end_tokens)
|
2015-07-10 16:55:02 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
if (!stla_token_match_candidate(data->token, end_tokens)) {
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_t* stream_it = &data->stream_iterator;
|
|
|
|
gmio_string_t* strbuff = &data->string_buffer;
|
2015-10-22 00:35:09 +08:00
|
|
|
gmio_bool_t end_token_found = GMIO_FALSE;
|
2015-07-10 16:55:02 +08:00
|
|
|
|
|
|
|
do {
|
2015-11-20 18:53:57 +08:00
|
|
|
const size_t previous_buff_len = strbuff->len;
|
2015-10-22 00:35:09 +08:00
|
|
|
gmio_eat_word_error_t eat_word_err = 0;
|
2015-07-10 16:55:02 +08:00
|
|
|
const char* next_word = NULL; /* Pointer on next word string */
|
|
|
|
size_t next_word_len = 0; /* Length of next word string */
|
|
|
|
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_copy_ascii_spaces(stream_it, strbuff);
|
2015-07-10 16:55:02 +08:00
|
|
|
/* Next word */
|
2015-11-20 18:53:57 +08:00
|
|
|
next_word = strbuff->ptr + strbuff->len;
|
|
|
|
eat_word_err = gmio_stringstream_eat_word(stream_it, strbuff);
|
|
|
|
next_word_len = (strbuff->ptr + strbuff->len) - next_word;
|
2015-07-10 16:55:02 +08:00
|
|
|
/* Qualify token */
|
|
|
|
data->token =
|
2015-10-22 00:35:09 +08:00
|
|
|
eat_word_err == GMIO_EAT_WORD_ERROR_OK ?
|
2015-11-20 18:20:06 +08:00
|
|
|
stla_find_token(next_word, next_word_len) :
|
2015-07-10 16:55:02 +08:00
|
|
|
unknown_token;
|
|
|
|
/* End token found ? */
|
2015-11-20 18:20:06 +08:00
|
|
|
end_token_found = stla_token_match_candidate(data->token, end_tokens);
|
2015-07-10 16:55:02 +08:00
|
|
|
/* True ?
|
|
|
|
* trim string_buf so it contains only contents before end token */
|
|
|
|
if (end_token_found) {
|
2015-11-20 18:53:57 +08:00
|
|
|
strbuff->len = previous_buff_len;
|
|
|
|
strbuff->ptr[previous_buff_len] = 0;
|
2015-07-10 16:55:02 +08:00
|
|
|
}
|
2015-11-20 18:53:57 +08:00
|
|
|
} while (!end_token_found && strbuff->len < strbuff->max_len);
|
2015-07-10 16:55:02 +08:00
|
|
|
|
|
|
|
if (!end_token_found) {
|
2015-11-20 18:20:06 +08:00
|
|
|
stla_error_msg(
|
2015-10-22 00:35:09 +08:00
|
|
|
data, "end token not found in parse_eat_until_token()");
|
2015-11-20 18:20:06 +08:00
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
2015-07-10 16:55:02 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-20 18:20:06 +08:00
|
|
|
return 0;
|
2015-07-10 16:55:02 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
gmio_bool_t stla_parsing_can_continue(const gmio_stla_parse_data_t* data)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
return !data->error && !data->stream_iterator_cookie.is_stop_requested;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------
|
|
|
|
* STLA parsing functions
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int parse_solidname_beg(gmio_stla_parse_data_t* data)
|
|
|
|
{
|
|
|
|
if (stla_eat_next_token(data, unknown_token) == 0) {
|
|
|
|
data->token = stla_find_token_from_string(&data->string_buffer);
|
2015-10-22 00:35:09 +08:00
|
|
|
if (data->token == FACET_token || data->token == ENDSOLID_token) {
|
2015-10-22 15:58:38 +08:00
|
|
|
gmio_string_clear(&data->string_buffer);
|
2015-11-20 18:20:06 +08:00
|
|
|
return 0;
|
2015-10-22 00:35:09 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Solid name can be made of multiple words */
|
|
|
|
const gmio_stla_token_t end_tokens[] = {
|
|
|
|
FACET_token, ENDSOLID_token, null_token };
|
2015-11-20 18:20:06 +08:00
|
|
|
return stla_eat_until_token(data, end_tokens);
|
2015-10-22 00:35:09 +08:00
|
|
|
}
|
2015-03-03 17:35:36 +08:00
|
|
|
}
|
2015-11-20 18:20:06 +08:00
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
2013-03-03 05:05:17 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
int parse_solidname_end(gmio_stla_parse_data_t* data)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-10-29 22:23:21 +08:00
|
|
|
GMIO_UNUSED(data);
|
2015-10-22 00:35:09 +08:00
|
|
|
/* TODO: parse according to retrieved solid name */
|
2015-11-20 18:20:06 +08:00
|
|
|
return 0;
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
int parse_beginsolid(gmio_stla_parse_data_t* data)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
if (stla_eat_next_token(data, SOLID_token) == 0) {
|
|
|
|
if (parse_solidname_beg(data) == 0) {
|
2015-03-30 16:18:18 +08:00
|
|
|
gmio_stl_mesh_creator_ascii_begin_solid(
|
|
|
|
data->creator,
|
2015-03-24 01:21:04 +08:00
|
|
|
data->stream_iterator_cookie.stream_size,
|
2015-07-10 16:55:02 +08:00
|
|
|
data->string_buffer.ptr);
|
2015-11-20 18:20:06 +08:00
|
|
|
return 0;
|
2015-03-03 17:35:36 +08:00
|
|
|
}
|
2014-01-29 23:59:19 +08:00
|
|
|
}
|
2015-11-20 18:20:06 +08:00
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
gmio_bool_t parse_endsolid(gmio_stla_parse_data_t* data)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-10-22 00:35:09 +08:00
|
|
|
if (data->token == ENDSOLID_token
|
2015-11-20 18:20:06 +08:00
|
|
|
|| stla_eat_next_token(data, ENDSOLID_token) == 0)
|
2015-10-22 00:35:09 +08:00
|
|
|
{
|
2015-03-03 17:35:36 +08:00
|
|
|
parse_solidname_end(data);
|
2015-10-22 00:35:09 +08:00
|
|
|
gmio_stl_mesh_creator_end_solid(data->creator);
|
2015-11-20 18:20:06 +08:00
|
|
|
return 0;
|
2014-01-29 23:59:19 +08:00
|
|
|
}
|
2015-11-20 18:20:06 +08:00
|
|
|
return GMIO_STLA_PARSE_ERROR;
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
/* Returns true if first char of input string is valid for float numbers */
|
|
|
|
GMIO_INLINE int is_float_char(const char* str)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
const char c = str != NULL ? *str : 0;
|
|
|
|
return gmio_ascii_isdigit(c)
|
|
|
|
|| c == '-'
|
|
|
|
|| c == '.'
|
|
|
|
|| c == 'e'
|
|
|
|
|| c == 'E'
|
|
|
|
|| c == '+';
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
int parse_xyz_coords(gmio_stla_parse_data_t* data, gmio_stl_coords_t* coords)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-11-20 18:20:06 +08:00
|
|
|
int errc = 0;
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_t* it = &data->stream_iterator;
|
2015-11-20 18:20:06 +08:00
|
|
|
const char* strbuff = NULL;
|
|
|
|
|
2015-11-20 18:53:57 +08:00
|
|
|
strbuff = gmio_stringstream_skip_ascii_spaces(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += !is_float_char(strbuff);
|
2015-11-20 18:53:57 +08:00
|
|
|
coords->x = gmio_stringstream_parse_float32(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
|
2015-11-20 18:53:57 +08:00
|
|
|
strbuff = gmio_stringstream_skip_ascii_spaces(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += !is_float_char(strbuff);
|
2015-11-20 18:53:57 +08:00
|
|
|
coords->y = gmio_stringstream_parse_float32(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
|
2015-11-20 18:53:57 +08:00
|
|
|
strbuff = gmio_stringstream_skip_ascii_spaces(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += !is_float_char(strbuff);
|
2015-11-20 18:53:57 +08:00
|
|
|
coords->z = gmio_stringstream_parse_float32(it);
|
2015-11-20 18:20:06 +08:00
|
|
|
|
|
|
|
data->string_buffer.len = 0;
|
|
|
|
data->token = unknown_token;
|
|
|
|
|
|
|
|
return errc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_facet(gmio_stla_parse_data_t* data, gmio_stl_triangle_t* facet)
|
|
|
|
{
|
|
|
|
int errc = 0;
|
2015-10-22 00:35:09 +08:00
|
|
|
if (data->token != FACET_token)
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += stla_eat_next_token_inplace(data, FACET_token);
|
2015-10-22 00:35:09 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += stla_eat_next_token_inplace(data, NORMAL_token);
|
2015-03-03 17:35:36 +08:00
|
|
|
parse_xyz_coords(data, &facet->normal);
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += stla_eat_next_token_inplace(data, OUTER_token);
|
|
|
|
errc += stla_eat_next_token_inplace(data, LOOP_token);
|
|
|
|
|
|
|
|
errc += stla_eat_next_token_inplace(data, VERTEX_token);
|
|
|
|
errc += parse_xyz_coords(data, &facet->v1);
|
|
|
|
errc += stla_eat_next_token_inplace(data, VERTEX_token);
|
|
|
|
errc += parse_xyz_coords(data, &facet->v2);
|
|
|
|
errc += stla_eat_next_token_inplace(data, VERTEX_token);
|
|
|
|
errc += parse_xyz_coords(data, &facet->v3);
|
2015-03-03 17:35:36 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
errc += stla_eat_next_token_inplace(data, ENDLOOP_token);
|
|
|
|
errc += stla_eat_next_token_inplace(data, ENDFACET_token);
|
2015-03-03 17:35:36 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
return errc;
|
2014-01-28 05:57:10 +08:00
|
|
|
}
|
2013-04-27 06:10:28 +08:00
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
void parse_facets(gmio_stla_parse_data_t* data)
|
2014-01-28 05:57:10 +08:00
|
|
|
{
|
2015-10-15 00:17:03 +08:00
|
|
|
const gmio_stl_mesh_creator_func_add_triangle_t func_add_triangle =
|
|
|
|
data->creator->func_add_triangle;
|
|
|
|
void* creator_cookie = data->creator->cookie;
|
2015-11-20 18:20:06 +08:00
|
|
|
gmio_string_t* strbuff = &data->string_buffer;
|
2015-03-30 16:18:18 +08:00
|
|
|
uint32_t i_facet = 0;
|
2015-03-03 17:35:36 +08:00
|
|
|
gmio_stl_triangle_t facet;
|
|
|
|
|
2015-09-11 22:34:18 +08:00
|
|
|
facet.attribute_byte_count = 0;
|
2015-11-20 18:20:06 +08:00
|
|
|
while (data->token == FACET_token && stla_parsing_can_continue(data)) {
|
|
|
|
if (parse_facet(data, &facet) == 0) {
|
|
|
|
/* Add triangle to user mesh */
|
|
|
|
if (func_add_triangle != NULL)
|
|
|
|
func_add_triangle(creator_cookie, i_facet, &facet);
|
|
|
|
/* Eat next unknown token */
|
|
|
|
strbuff->len = 0;
|
2015-11-20 18:53:57 +08:00
|
|
|
gmio_stringstream_eat_word(&data->stream_iterator, strbuff);
|
2015-11-20 18:20:06 +08:00
|
|
|
data->token = stla_find_token_from_string(strbuff);
|
|
|
|
++i_facet;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stla_error_msg(data, "Invalid facet");
|
|
|
|
}
|
2015-03-03 17:35:36 +08:00
|
|
|
}
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:20:06 +08:00
|
|
|
void parse_solid(gmio_stla_parse_data_t* data)
|
2013-04-27 06:10:28 +08:00
|
|
|
{
|
2015-10-22 00:35:09 +08:00
|
|
|
parse_beginsolid(data);
|
|
|
|
parse_facets(data);
|
|
|
|
parse_endsolid(data);
|
2013-04-27 06:10:28 +08:00
|
|
|
}
|