star-3dut

Generate meshes of simple geometric shapes
git clone git://git.meso-star.fr/star-3dut.git
Log | Files | Refs | README | LICENSE

commit 8345c18ab1655ba90baf49b77722f32d829d8b9e
parent 2217591af474fa4845bea68f95bc2e9b115f1eba
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 30 Nov 2016 12:04:12 +0100

Add and test the s3dut_create_cuboid function

Diffstat:
Mcmake/CMakeLists.txt | 2++
Msrc/s3dut.h | 11+++++++++++
Asrc/s3dut_cuboid.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/s3dut_mesh.h | 1+
Asrc/test_s3dut_cuboid.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 164 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -42,6 +42,7 @@ set(VERSION_PATCH 0) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) set(S3DUT_FILES_SRC + s3dut_cuboid.c s3dut_cylinder.c s3dut_mesh.c s3dut_sphere.c) @@ -77,6 +78,7 @@ if(NOT NO_TEST) add_test(${_name} ${_name}) endfunction() + new_test(test_s3dut_cuboid) new_test(test_s3dut_cylinder) new_test(test_s3dut_sphere) endif() diff --git a/src/s3dut.h b/src/s3dut.h @@ -86,5 +86,16 @@ s3dut_create_cylinder const unsigned nstacks, /* # subdivision along Z axis in [1, INF) */ struct s3dut_mesh** cylinder); +/* Create a triangulated cuboid centered in 0. Face vertices are CCW ordered + * with respect to the cylinder center, i.e. they are CW ordered from the + * outside point of view. */ +S3DUT_API res_T +s3dut_create_cuboid + (struct mem_allocator* allocator, + const double width, + const double height, + const double depth, + struct s3dut_mesh** cuboid); + #endif /* S3DUT_H */ diff --git a/src/s3dut_cuboid.c b/src/s3dut_cuboid.c @@ -0,0 +1,85 @@ +/* Copyright (C) |Meso|Star> 2016 (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 "s3dut.h" +#include "s3dut_mesh.h" + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +s3dut_create_cuboid + (struct mem_allocator* allocator, + const double width, + const double height, + const double depth, + struct s3dut_mesh** mesh) +{ + struct s3dut_mesh* cuboid = NULL; + double x, y, z; + double* coords; + size_t* ids; + size_t i; + res_T res = RES_OK; + + if(width <= 0 || height <= 0 || depth <= 0 || !mesh) { + res = RES_BAD_ARG; + goto error; + } + + res = mesh_create(allocator, S3DUT_MESH_CUBOID, 8, 12, &cuboid); + if(res != RES_OK) goto error; + + coords = darray_double_data_get(&cuboid->coords); + ids = darray_size_t_data_get(&cuboid->ids); + x = width * 0.5; + y = height * 0.5; + z = depth * 0.5; + + i = 0; + coords[i++]=-x; coords[i++]=-y; coords[i++]=-z; + coords[i++]= x; coords[i++]=-y; coords[i++]=-z; + coords[i++]= x; coords[i++]= y; coords[i++]=-z; + coords[i++]=-x; coords[i++]= y; coords[i++]=-z; + coords[i++]=-x; coords[i++]=-y; coords[i++]= z; + coords[i++]= x; coords[i++]=-y; coords[i++]= z; + coords[i++]= x; coords[i++]= y; coords[i++]= z; + coords[i++]=-x; coords[i++]= y; coords[i++]= z; + + i = 0; + ids[i++]=0; ids[i++]=2; ids[i++]=3; + ids[i++]=0; ids[i++]=1; ids[i++]=2; + ids[i++]=0; ids[i++]=3; ids[i++]=7; + ids[i++]=0; ids[i++]=7; ids[i++]=4; + ids[i++]=0; ids[i++]=4; ids[i++]=1; + ids[i++]=4; ids[i++]=5; ids[i++]=1; + ids[i++]=5; ids[i++]=6; ids[i++]=1; + ids[i++]=1; ids[i++]=6; ids[i++]=2; + ids[i++]=2; ids[i++]=6; ids[i++]=7; + ids[i++]=2; ids[i++]=7; ids[i++]=3; + ids[i++]=7; ids[i++]=6; ids[i++]=4; + ids[i++]=6; ids[i++]=5; ids[i++]=4; + +exit: + if(mesh) *mesh = cuboid; + return res; +error: + if(cuboid) { + S3DUT(mesh_ref_put(cuboid)); + cuboid = NULL; + } + goto exit; +} + diff --git a/src/s3dut_mesh.h b/src/s3dut_mesh.h @@ -21,6 +21,7 @@ #include <rsys/ref_count.h> enum s3dut_mesh_type { + S3DUT_MESH_CUBOID, S3DUT_MESH_CYLINDER, S3DUT_MESH_SPHERE }; diff --git a/src/test_s3dut_cuboid.c b/src/test_s3dut_cuboid.c @@ -0,0 +1,65 @@ +/* Copyright (C) |Meso|Star> 2016 (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 "s3dut.h" +#include "test_s3dut_utils.h" + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct s3dut_mesh* msh; + struct s3dut_mesh_data data; + (void)argc, (void)argv; + + CHECK(mem_init_proxy_allocator(&allocator, &mem_default_allocator), RES_OK); + + CHECK(s3dut_create_cuboid(NULL, 0, 0, 0, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 0, 0, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 1, 0, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 1, 0, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 0, 1, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 0, 1, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 1, 1, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 1, 1, NULL), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 0, 0, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 0, 0, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 1, 0, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 1, 0, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 0, 1, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 0, 1, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 0, 1, 1, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(NULL, 1, 1, 1, &msh), RES_OK); + + CHECK(s3dut_mesh_ref_put(msh), RES_OK); + + CHECK(s3dut_create_cuboid(&allocator, 1, 1, 2, &msh), RES_OK); + CHECK(s3dut_mesh_ref_put(msh), RES_OK); + + CHECK(s3dut_create_cuboid(&allocator,-1, 1, 2, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(&allocator, 1,-1, 2, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(&allocator, 1, 1,-2, &msh), RES_BAD_ARG); + CHECK(s3dut_create_cuboid(&allocator, 1, 1, 2, &msh), RES_OK); + + CHECK(s3dut_mesh_get_data(msh, &data), RES_OK); + dump_mesh_data(stdout, &data); + CHECK(s3dut_mesh_ref_put(msh), RES_OK); + + check_memory_allocator(&allocator); + mem_shutdown_proxy_allocator(&allocator); + CHECK(mem_allocated_size(), 0); + return 0; +} +