star-geometry-3d

Clean and decorate 3D geometries
git clone git://git.meso-star.fr/star-geometry-3d.git
Log | Files | Refs | README | LICENSE

commit 0706bf60ecd51c0378f21db798dd2da48e6828c9
parent f113dd5534b8bcc4e4565a2b2bbc0842a5212eba
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Tue,  7 Jan 2020 15:22:57 +0100

Add 2 new tests from star-enclosures

Diffstat:
Mcmake/CMakeLists.txt | 27+++++++++++++++++++++++----
Asrc/test_sg3_many_enclosures.c | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_sg3_many_triangles.c | 128+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 294 insertions(+), 4 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -21,6 +21,7 @@ include(CMakeDependentOption) set(STAR_GEOM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) option(NO_TEST "Do not build tests" OFF) +option(ADDITIONAL_TESTS "Build additional tests originally written for star-enclosures" OFF) CMAKE_DEPENDENT_OPTION(ALL_TESTS "Perform basic and advanced tests" OFF "NOT NO_TEST" OFF) @@ -31,6 +32,10 @@ CMAKE_DEPENDENT_OPTION(ALL_TESTS find_package(RCMake 0.4 REQUIRED) find_package(RSys 0.8.1 REQUIRED) +if(ADDITIONAL_TESTS) + find_package(Star3DUT 0.3.1 REQUIRED) +endif() + set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) include(rcmake) include(rcmake_runtime) @@ -38,9 +43,12 @@ include(rcmake_runtime) include_directories( ${RSys_INCLUDE_DIR} ${StarEnc_INCLUDE_DIR}) - -rcmake_append_runtime_dirs(_runtime_dirs - RSys StarEnc) + +if(ADDITIONAL_TESTS) + rcmake_append_runtime_dirs(_runtime_dirs RSys StarEnc Star3DUT) +else() + rcmake_append_runtime_dirs(_runtime_dirs RSys StarEnc) +endif() ################################################################################ # Configure and define targets @@ -119,10 +127,21 @@ if(NOT NO_TEST) register_test(${_name} ${_name}) endfunction() - new_test(test_sg3_cube_behind_cube) new_test(test_sg3_device) new_test(test_sg3_geometry) new_test(test_sg3_geometry_2) + + if(ADDITIONAL_TESTS) + new_test(test_sg3_cube_behind_cube) + new_test(test_sg3_many_triangles) + new_test(test_sg3_many_enclosures) + + target_link_libraries(test_sg3_many_triangles Star3DUT) + target_link_libraries(test_sg3_many_enclosures Star3DUT) + rcmake_copy_runtime_libraries(test_sg3_many_triangles) + rcmake_copy_runtime_libraries(test_sg3_many_enclosures) + endif() + endif() ################################################################################ diff --git a/src/test_sg3_many_enclosures.c b/src/test_sg3_many_enclosures.c @@ -0,0 +1,142 @@ +/* Copyright (C) 2016-2019 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "sg3.h" +#include "test_sg3_utils.h" + +#include <rsys/double3.h> + +#include <star/s3dut.h> + +#include <stdio.h> +#include <limits.h> + +#define NB_CYL_1 64 +#define NB_CYL (NB_CYL_1 * NB_CYL_1 * NB_CYL_1) + +struct s3dut_context { + struct s3dut_mesh_data data; + struct context ctx; +}; + +static INLINE void +get_s3dut_indices(const unsigned itri, unsigned ids[3], void* context) +{ + const struct s3dut_context* ctx = context; + ASSERT(ids && ctx); + ASSERT(itri < ctx->data.nprimitives); + ASSERT(ctx->data.indices[itri * 3 + 0] < UINT_MAX + && ctx->data.indices[itri * 3 + 1] < UINT_MAX + && ctx->data.indices[itri * 3 + 2] < UINT_MAX); + ids[0] = (unsigned)ctx->data.indices[itri * 3 + 0]; + ids[ctx->ctx.reverse_vrtx ? 2 : 1] = (unsigned)ctx->data.indices[itri * 3 + 1]; + ids[ctx->ctx.reverse_vrtx ? 1 : 2] = (unsigned)ctx->data.indices[itri * 3 + 2]; +} + +static INLINE void +get_s3dut_position(const unsigned ivert, double pos[3], void* context) +{ + const struct s3dut_context* ctx = context; + double tmp[3]; + ASSERT(pos && ctx); + ASSERT(ivert < ctx->data.nvertices); + (void)ivert; + d3_add(pos, d3_muld(tmp, ctx->data.positions + ivert * 3, ctx->ctx.scale), + ctx->ctx.offset); +} + +static INLINE void +get_s3dut_properties(const unsigned itri, unsigned property[3], void* context) +{ + const struct s3dut_context* ctx = context; + ASSERT(property && ctx); + (void)itri; + property[ctx->ctx.reverse_med ? SG3_BACK : SG3_FRONT] = *ctx->ctx.front_media; + property[ctx->ctx.reverse_med ? SG3_FRONT : SG3_BACK] = *ctx->ctx.back_media; + property[SG3_INTFACE] = *ctx->ctx.intface; +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct sg3_device* dev; + struct sg3_geometry* geom; + struct sg3_geometry_add_callbacks callbacks = SG3_ADD_CALLBACKS_NULL__; + unsigned cyl_trg_count, cyl_vrtx_count, count; + int i, j, k; + unsigned m_in, m_out, itf = 0; + struct s3dut_context ctx = { {NULL,NULL,0,0}, CONTEXT_NULL__ }; + struct s3dut_mesh* cyl = NULL; + (void)argc, (void)argv; + + OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); + OK(sg3_device_create(NULL, &allocator, 1, &dev)); + OK(sg3_geometry_create(dev, &geom)); + SG3(device_ref_put(dev)); + + callbacks.get_indices = get_s3dut_indices; + callbacks.get_properties = get_s3dut_properties; + callbacks.get_position = get_s3dut_position; + + ctx.ctx.positions = box_vertices; + ctx.ctx.indices = cube_indices; + ctx.ctx.front_media = &m_in; + ctx.ctx.back_media = &m_out; + ctx.ctx.intface = &itf; + + /* A 20 triangles 12 vertices cylinder template */ + S3DUT(create_cylinder(&allocator, 1, 1, 5, 1, &cyl)); + S3DUT(mesh_get_data(cyl, &ctx.data)); + ASSERT(ctx.data.nprimitives <= UINT_MAX); + ASSERT(ctx.data.nvertices <= UINT_MAX); + cyl_trg_count = (unsigned)ctx.data.nprimitives; + cyl_vrtx_count = (unsigned)ctx.data.nvertices; + OK(sg3_geometry_reserve(geom, NB_CYL * cyl_vrtx_count, NB_CYL * cyl_trg_count, 0)); + FOR_EACH(i, 0, NB_CYL_1) { + double center_x = 2 * (1 + NB_CYL_1) * (i - NB_CYL_1 / 2); + FOR_EACH(j, 0, NB_CYL_1) { + double misalignment = 0.01; + FOR_EACH(k, 0, NB_CYL_1) { + double center_y = 2 * (1 + NB_CYL_1) * (j - NB_CYL_1 / 2); + m_in = (unsigned)k; + m_out = (unsigned)(k + 1); + ctx.ctx.scale = k + 1; + /* Mitigate Embree issue #181 + * We cannot keep perfect alignment of cylinders + * or some hits are missed */ + misalignment *= -1; + d3(ctx.ctx.offset, center_x + misalignment, center_y + misalignment, 0); + OK(sg3_geometry_add(geom, cyl_trg_count, cyl_vrtx_count, &callbacks, &ctx)); + } + } + } + S3DUT(mesh_ref_put(cyl)); + + OK(sg3_geometry_get_merge_conflict_count(geom, &count)); + CHK(count == 0); + OK(sg3_geometry_get_triangle_with_undefined_interface_count(geom, &count)); + CHK(count == 0); + OK(sg3_geometry_get_triangle_with_undefined_side_count(geom, &count)); + CHK(count == 0); + OK(sg3_geometry_dump_as_obj(geom, stdout, SG3_ALL_TRIANGLES)); + + SG3(geometry_ref_put(geom)); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} +\ No newline at end of file diff --git a/src/test_sg3_many_triangles.c b/src/test_sg3_many_triangles.c @@ -0,0 +1,127 @@ +/* Copyright (C) 2016-2019 |Meso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "sg3.h" +#include "test_sg3_utils.h" + +#include <rsys/double3.h> + +#include <star/s3dut.h> + +#include <stdio.h> +#include <limits.h> + +#define NB_CYL 4 + +struct s3dut_context { + struct s3dut_mesh_data data; + struct context ctx; +}; + +static INLINE void +get_s3dut_indices(const unsigned itri, unsigned ids[3], void* context) +{ + const struct s3dut_context* ctx = context; + ASSERT(ids && ctx); + ASSERT(itri < ctx->data.nprimitives); + ASSERT(ctx->data.indices[itri * 3 + 0] < UINT_MAX + && ctx->data.indices[itri * 3 + 1] < UINT_MAX + && ctx->data.indices[itri * 3 + 2] < UINT_MAX); + ids[0] = (unsigned)ctx->data.indices[itri * 3 + 0]; + ids[ctx->ctx.reverse_vrtx ? 2 : 1] = (unsigned)ctx->data.indices[itri * 3 + 1]; + ids[ctx->ctx.reverse_vrtx ? 1 : 2] = (unsigned)ctx->data.indices[itri * 3 + 2]; +} + +static INLINE void +get_s3dut_position(const unsigned ivert, double pos[3], void* context) +{ + const struct s3dut_context* ctx = context; + double tmp[3]; + ASSERT(pos && ctx); + ASSERT(ivert < ctx->data.nvertices); + (void)ivert; + d3_add(pos, d3_muld(tmp, ctx->data.positions + ivert * 3, ctx->ctx.scale), + ctx->ctx.offset); +} + +static INLINE void +get_s3dut_properties(const unsigned itri, unsigned property[3], void* context) +{ + const struct s3dut_context* ctx = context; + ASSERT(property && ctx); + (void)itri; + property[ctx->ctx.reverse_med ? SG3_BACK : SG3_FRONT] = *ctx->ctx.front_media; + property[ctx->ctx.reverse_med ? SG3_FRONT : SG3_BACK] = *ctx->ctx.back_media; + property[SG3_INTFACE] = *ctx->ctx.intface; +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct sg3_device* dev; + struct sg3_geometry* geom; + struct sg3_geometry_add_callbacks callbacks = SG3_ADD_CALLBACKS_NULL__; + unsigned cyl_trg_count, cyl_vrtx_count, i, count; + unsigned m0 = 0, m1, itf = 0; + struct s3dut_context ctx = { {NULL,NULL,0,0}, CONTEXT_NULL__ }; + struct s3dut_mesh* cyl = NULL; + (void)argc, (void)argv; + + OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); + OK(sg3_device_create(NULL, &allocator, 1, &dev)); + OK(sg3_geometry_create(dev, &geom)); + SG3(device_ref_put(dev)); + + callbacks.get_indices = get_s3dut_indices; + callbacks.get_properties = get_s3dut_properties; + callbacks.get_position = get_s3dut_position; + + ctx.ctx.positions = box_vertices; + ctx.ctx.indices = cube_indices; + ctx.ctx.front_media = &m1; + ctx.ctx.back_media = &m0; + ctx.ctx.intface = &itf; + + /* A 2,562,560 triangles 1,281,282 vertices cylinder template */ + S3DUT(create_cylinder(&allocator, 1, 2, 1280, 1000, &cyl)); + S3DUT(mesh_get_data(cyl, &ctx.data)); + ASSERT(ctx.data.nprimitives <= UINT_MAX); + ASSERT(ctx.data.nvertices <= UINT_MAX); + cyl_trg_count = (unsigned)ctx.data.nprimitives; + cyl_vrtx_count = (unsigned)ctx.data.nvertices; + OK(sg3_geometry_reserve(geom, NB_CYL * cyl_vrtx_count, NB_CYL * cyl_trg_count, 0)); + FOR_EACH(i, 0, NB_CYL) { + m1 = i; + d3(ctx.ctx.offset, 0, 0, i * 10); + OK(sg3_geometry_add(geom, cyl_trg_count, cyl_vrtx_count, &callbacks, &ctx)); + } + S3DUT(mesh_ref_put(cyl)); + + OK(sg3_geometry_get_merge_conflict_count(geom, &count)); + CHK(count == 0); + OK(sg3_geometry_get_triangle_with_undefined_interface_count(geom, &count)); + CHK(count == 0); + OK(sg3_geometry_get_triangle_with_undefined_side_count(geom, &count)); + CHK(count == 0); + OK(sg3_geometry_dump_as_obj(geom, stdout, SG3_ALL_TRIANGLES)); + + SG3(geometry_ref_put(geom)); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} +\ No newline at end of file