star-enclosures-3d

Extract enclosures from 3D geometry
git clone git://git.meso-star.fr/star-enclosures-3d.git
Log | Files | Refs | README | LICENSE

commit 5afa312ad2caffe9cbae56084694848da46223a5
parent 424534f9cb6b262982371037ad9be360ca5971bf
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Fri,  2 Mar 2018 22:04:30 +0100

Add a new test.

Diffstat:
Mcmake/CMakeLists.txt | 2++
Asrc/test_senc_many_enclosures.c | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 149 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -120,11 +120,13 @@ if(NOT NO_TEST) new_test(test_senc_descriptor) new_test(test_senc_device) new_test(test_senc_enclosure) + new_test(test_senc_many_enclosures) new_test(test_senc_many_triangles) new_test(test_senc_sample_enclosure) new_test(test_senc_scene) target_link_libraries(test_senc_sample_enclosure StarSP) + target_link_libraries(test_senc_many_enclosures Star3DUT) target_link_libraries(test_senc_many_triangles Star3DUT) rcmake_copy_runtime_libraries(test_senc_sample_enclosure) rcmake_copy_runtime_libraries(test_senc_many_triangles) diff --git a/src/test_senc_many_enclosures.c b/src/test_senc_many_enclosures.c @@ -0,0 +1,147 @@ +/* Copyright (C) |Meso|Star> 2016-2018 (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 "senc.h" +#include "test_senc_utils.h" + +#include <star/s3dut.h> +#include <rsys/double3.h> +#include <rsys/clock_time.h> + +#include <limits.h> + +struct s3dut_context { + struct s3dut_mesh_data data; + struct context ctx; +}; + +static void +get_s3dut_indices(const unsigned itri, unsigned ids[3], void* context) +{ + 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 void +get_s3dut_position(const unsigned ivert, double pos[3], void* context) +{ + struct s3dut_context* ctx = context; + ASSERT(pos && ctx); + ASSERT(ivert < ctx->data.nvertices); + pos[0] + = ctx->data.positions[ivert * 3 + 0] * ctx->ctx.scale + ctx->ctx.offset[0]; + pos[1] + = ctx->data.positions[ivert * 3 + 1] * ctx->ctx.scale + ctx->ctx.offset[1]; + pos[2] + = ctx->data.positions[ivert * 3 + 2] * ctx->ctx.scale + ctx->ctx.offset[2]; +} + +static void +get_s3dut_media(const unsigned itri, unsigned medium[2], void* context) +{ + struct s3dut_context* ctx = context; + (void) itri; + ASSERT(medium && ctx); + medium[ctx->ctx.reverse_med ? 1 : 0] = ctx->ctx.front_media[0]; + medium[ctx->ctx.reverse_med ? 0 : 1] = ctx->ctx.back_media[0]; +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct senc_descriptor* desc = NULL; + struct senc_device* dev = NULL; + struct senc_scene* scn = NULL; + struct s3dut_mesh* cyl = NULL; + struct s3dut_context ctx; + unsigned count; + unsigned cyl_trg_count, cyl_vrtx_count, i; + char dump[64]; + struct time t0, t1; + (void) argc, (void) argv; + + CHK(mem_init_regular_allocator(&allocator) == RES_OK); + CHK(senc_device_create(NULL, &allocator, SENC_NTHREADS_DEFAULT, 1, &dev) + == RES_OK); + +#define NB_CYL 4096 + /* Create the scene */ + CHK(senc_scene_create(dev, NB_CYL + 1, &scn) == RES_OK); + + ctx.ctx.positions = NULL; + ctx.ctx.indices = NULL; + ctx.ctx.reverse_vrtx = 0; + ctx.ctx.reverse_med = 0; + /* Create a cylinder (320 K trg, 160 K vrtx). */ + CHK(s3dut_create_cylinder(&allocator, 1, 1, 16, 1, &cyl) == RES_OK); + 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; + FOR_EACH(i, 1, 1 + NB_CYL) { + unsigned m_in = i-1; + unsigned m_out = i; + ctx.ctx.front_media = &m_in; + ctx.ctx.back_media = &m_out; + ctx.ctx.scale = i; + d3(ctx.ctx.offset, -0.25 * i, -0.25 * i, -0.25 * i); + CHK(senc_scene_add_geometry(scn, cyl_trg_count, get_s3dut_indices, + get_s3dut_media, NULL, cyl_vrtx_count, get_s3dut_position, &ctx) + == RES_OK); + } + S3DUT(mesh_ref_put(cyl)); + + time_current(&t0); + CHK(senc_scene_analyze(scn, &desc) == RES_OK); + time_sub(&t0, time_current(&t1), &t0); + time_dump(&t0, TIME_MSEC | TIME_SEC | TIME_MIN, NULL, dump, sizeof(dump)); + printf("Scene analyzed in: %s\n", dump); + + CHK(senc_descriptor_get_global_vertices_count(desc, &count) == RES_OK); + CHK(count == NB_CYL * cyl_vrtx_count); + CHK(senc_descriptor_get_global_triangles_count(desc, &count) == RES_OK); + CHK(count == NB_CYL * cyl_trg_count); + + CHK(senc_descriptor_get_enclosure_count(desc, &count) == RES_OK); + CHK(count == 1 + NB_CYL); + + FOR_EACH(i, 0, count) { + struct senc_enclosure* enclosure; + const struct enclosure_header* header; + CHK(senc_descriptor_get_enclosure(desc, i, &enclosure) == RES_OK); + CHK(senc_enclosure_get_header(enclosure, &header) == RES_OK); + CHK(header->triangle_count == + i ? cyl_trg_count : NB_CYL * cyl_trg_count); + CHK(senc_enclosure_ref_put(enclosure) == RES_OK); + } + + CHK(senc_scene_ref_put(scn) == RES_OK); + CHK(senc_device_ref_put(dev) == RES_OK); + CHK(senc_descriptor_ref_put(desc) == RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_regular_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +}