2015-09-11 16:51:25 +08:00
|
|
|
/****************************************************************************
|
2017-01-27 01:05:12 +08:00
|
|
|
** Copyright (c) 2017, Fougue Ltd. <http://www.fougue.pro>
|
2016-07-05 18:46:22 +08:00
|
|
|
** All rights reserved.
|
2015-09-11 16:51:25 +08:00
|
|
|
**
|
2016-07-05 18:46:22 +08:00
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
2015-09-11 16:51:25 +08:00
|
|
|
**
|
2016-07-05 18:46:22 +08:00
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
**
|
|
|
|
** 2. Redistributions in binary form must reproduce the above
|
|
|
|
** copyright notice, this list of conditions and the following
|
|
|
|
** disclaimer in the documentation and/or other materials provided
|
|
|
|
** with the distribution.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2015-09-11 16:51:25 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "stl_utils.h"
|
|
|
|
|
2016-03-11 17:33:13 +08:00
|
|
|
#include "core_utils.h"
|
2015-09-11 22:35:04 +08:00
|
|
|
#include "../src/gmio_core/internal/min_max.h"
|
2015-12-10 01:51:03 +08:00
|
|
|
#include "../src/gmio_core/internal/numeric_utils.h"
|
2015-09-11 22:35:04 +08:00
|
|
|
#include "../src/gmio_core/internal/safe_cast.h"
|
2016-03-04 00:34:20 +08:00
|
|
|
#include "../src/gmio_core/internal/string.h"
|
2015-09-11 22:35:04 +08:00
|
|
|
|
2015-09-11 16:51:25 +08:00
|
|
|
#include <stdlib.h>
|
2015-09-11 22:35:04 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2015-09-11 16:51:25 +08:00
|
|
|
|
|
|
|
void gmio_stl_nop_add_triangle(
|
2015-12-04 01:00:25 +08:00
|
|
|
void *cookie, uint32_t tri_id, const struct gmio_stl_triangle *triangle)
|
2015-09-11 16:51:25 +08:00
|
|
|
{
|
|
|
|
GMIO_UNUSED(cookie);
|
|
|
|
GMIO_UNUSED(tri_id);
|
|
|
|
GMIO_UNUSED(triangle);
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_triangle_array gmio_stl_triangle_array_malloc(size_t tri_count)
|
2015-09-11 22:35:04 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_triangle_array array = {0};
|
2015-09-11 22:35:04 +08:00
|
|
|
if (tri_count > 0) {
|
|
|
|
array.ptr =
|
2015-12-10 01:51:03 +08:00
|
|
|
(struct gmio_stl_triangle*)calloc(
|
|
|
|
tri_count, sizeof(struct gmio_stl_triangle));
|
2015-09-11 22:35:04 +08:00
|
|
|
}
|
|
|
|
array.count = gmio_size_to_uint32(tri_count);
|
|
|
|
array.capacity = array.count;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2016-02-24 23:43:40 +08:00
|
|
|
void gmio_stl_triangle_array_free(struct gmio_stl_triangle_array *array)
|
|
|
|
{
|
|
|
|
if (array != NULL) {
|
|
|
|
free(array->ptr);
|
|
|
|
array->ptr = NULL;
|
|
|
|
array->count = 0;
|
|
|
|
array->capacity = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 23:53:45 +08:00
|
|
|
static void gmio_stl_data__begin_solid(
|
|
|
|
void* cookie, const struct gmio_stl_mesh_creator_infos* infos)
|
2015-09-11 22:35:04 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_data* data = (struct gmio_stl_data*)cookie;
|
2015-09-11 22:35:04 +08:00
|
|
|
|
2016-02-01 23:53:45 +08:00
|
|
|
if (infos->format == GMIO_STL_FORMAT_ASCII) {
|
|
|
|
memset(data->solid_name, 0, sizeof(data->solid_name));
|
|
|
|
if (infos->stla_solid_name != NULL) {
|
2016-03-04 00:34:20 +08:00
|
|
|
gmio_cstr_copy(
|
|
|
|
data->solid_name,
|
|
|
|
sizeof(data->solid_name),
|
|
|
|
infos->stla_solid_name,
|
|
|
|
strlen(infos->stla_solid_name));
|
2016-02-01 23:53:45 +08:00
|
|
|
}
|
2015-09-11 22:35:04 +08:00
|
|
|
|
2016-02-01 23:53:45 +08:00
|
|
|
/* Try to guess how many vertices we could have assume we'll need
|
|
|
|
* 200 bytes for each face */
|
|
|
|
{
|
|
|
|
const size_t facet_size = 200;
|
|
|
|
const size_t facet_count =
|
|
|
|
gmio_streamsize_to_size(
|
|
|
|
GMIO_MAX(1, infos->stla_stream_size / facet_size));
|
|
|
|
data->tri_array = gmio_stl_triangle_array_malloc(facet_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(&data->header, infos->stlb_header, GMIO_STLB_HEADER_SIZE);
|
|
|
|
data->tri_array =
|
|
|
|
gmio_stl_triangle_array_malloc(infos->stlb_triangle_count);
|
2015-09-11 22:35:04 +08:00
|
|
|
}
|
2015-09-11 16:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gmio_stl_data__add_triangle(
|
2015-12-04 01:00:25 +08:00
|
|
|
void* cookie, uint32_t tri_id, const struct gmio_stl_triangle* triangle)
|
2015-09-11 16:51:25 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_data* data = (struct gmio_stl_data*)cookie;
|
2015-09-11 22:35:04 +08:00
|
|
|
if (tri_id >= data->tri_array.capacity) {
|
|
|
|
uint32_t cap = data->tri_array.capacity;
|
|
|
|
cap += cap >> 3; /* Add 12.5% more capacity */
|
|
|
|
data->tri_array.ptr =
|
2015-12-04 01:00:25 +08:00
|
|
|
realloc(data->tri_array.ptr, cap * sizeof(struct gmio_stl_triangle));
|
2015-09-11 22:35:04 +08:00
|
|
|
data->tri_array.capacity = cap;
|
|
|
|
}
|
2015-09-14 23:30:22 +08:00
|
|
|
memcpy(&data->tri_array.ptr[tri_id], triangle, GMIO_STLB_TRIANGLE_RAWSIZE);
|
2016-01-29 19:47:01 +08:00
|
|
|
data->tri_array.count = tri_id + 1;
|
2015-09-11 16:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gmio_stl_data__get_triangle(
|
2015-12-04 01:00:25 +08:00
|
|
|
const void* cookie, uint32_t tri_id, struct gmio_stl_triangle* triangle)
|
2015-09-11 16:51:25 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
const struct gmio_stl_data* data = (const struct gmio_stl_data*)cookie;
|
2015-09-11 16:51:25 +08:00
|
|
|
*triangle = data->tri_array.ptr[tri_id];
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_mesh_creator gmio_stl_data_mesh_creator(struct gmio_stl_data *data)
|
2015-09-11 16:51:25 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_mesh_creator creator = {0};
|
2015-09-11 16:51:25 +08:00
|
|
|
creator.cookie = data;
|
2016-02-01 23:53:45 +08:00
|
|
|
creator.func_begin_solid = &gmio_stl_data__begin_solid;
|
2015-09-11 16:51:25 +08:00
|
|
|
creator.func_add_triangle = &gmio_stl_data__add_triangle;
|
|
|
|
return creator;
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_mesh gmio_stl_data_mesh(const struct gmio_stl_data *data)
|
2015-09-11 16:51:25 +08:00
|
|
|
{
|
2015-12-04 01:00:25 +08:00
|
|
|
struct gmio_stl_mesh mesh = {0};
|
2015-09-11 16:51:25 +08:00
|
|
|
mesh.cookie = data;
|
|
|
|
mesh.triangle_count = data->tri_array.count;
|
|
|
|
mesh.func_get_triangle = &gmio_stl_data__get_triangle;
|
|
|
|
return mesh;
|
|
|
|
}
|
2015-09-11 22:35:04 +08:00
|
|
|
|
2016-01-27 00:03:58 +08:00
|
|
|
bool gmio_stl_triangle_equal(
|
2015-12-04 01:00:25 +08:00
|
|
|
const struct gmio_stl_triangle *lhs,
|
|
|
|
const struct gmio_stl_triangle *rhs,
|
2015-09-11 22:35:04 +08:00
|
|
|
uint32_t max_ulp_diff)
|
|
|
|
{
|
2016-03-14 23:31:48 +08:00
|
|
|
return gmio_vec3f_equal(&lhs->n, &rhs->n, max_ulp_diff)
|
|
|
|
&& gmio_vec3f_equal(&lhs->v1, &rhs->v1, max_ulp_diff)
|
|
|
|
&& gmio_vec3f_equal(&lhs->v2, &rhs->v2, max_ulp_diff)
|
|
|
|
&& gmio_vec3f_equal(&lhs->v3, &rhs->v3, max_ulp_diff)
|
2015-09-11 22:35:04 +08:00
|
|
|
&& lhs->attribute_byte_count == rhs->attribute_byte_count;
|
|
|
|
}
|
2015-09-14 16:42:38 +08:00
|
|
|
|
|
|
|
void gmio_stl_nop_get_triangle(
|
2015-12-04 01:00:25 +08:00
|
|
|
const void *cookie, uint32_t tri_id, struct gmio_stl_triangle *triangle)
|
2015-09-14 16:42:38 +08:00
|
|
|
{
|
|
|
|
GMIO_UNUSED(cookie);
|
|
|
|
GMIO_UNUSED(tri_id);
|
|
|
|
GMIO_UNUSED(triangle);
|
|
|
|
}
|