commit 2e0382e154d5e3dc8cc37a6ad3810dd83cf9f63d
parent 8142de9329ec00ef8881f5fcd9a2e54ad7f60519
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Tue, 11 Apr 2017 17:11:40 +0200
Add hemispheres; version is now 0.2.
Diffstat:
5 files changed, 269 insertions(+), 4 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -37,7 +37,7 @@ include(rcmake_runtime)
# Define targets
################################################################################
set(VERSION_MAJOR 0)
-set(VERSION_MINOR 1)
+set(VERSION_MINOR 2)
set(VERSION_PATCH 0)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
@@ -45,7 +45,8 @@ set(S3DUT_FILES_SRC
s3dut_cuboid.c
s3dut_cylinder.c
s3dut_mesh.c
- s3dut_sphere.c)
+ s3dut_sphere.c
+ s3dut_hemisphere.c)
set(S3DUT_FILES_INC s3dut_mesh.h)
set(S3DUT_FILES_INC_API s3dut.h)
set(S3DUT_FILES_DOC COPYING README.md)
@@ -66,6 +67,7 @@ set_target_properties(s3dut PROPERTIES
VERSION ${VERSION}
SOVERSION ${VERSION_MAJOR})
target_link_libraries(s3dut RSys ${MATH_LIB})
+rcmake_copy_runtime_libraries(s3dut)
rcmake_setup_devel(s3dut Star3DUT ${VERSION} star/s3dut_version.h)
@@ -83,8 +85,8 @@ if(NOT NO_TEST)
new_test(test_s3dut_cuboid)
new_test(test_s3dut_cylinder)
new_test(test_s3dut_sphere)
+ new_test(test_s3dut_hemisphere)
- rcmake_copy_runtime_libraries(test_s3dut_cuboid)
endif()
################################################################################
diff --git a/src/s3dut.h b/src/s3dut.h
@@ -73,6 +73,20 @@ s3dut_create_sphere
const unsigned nstacks, /* # subdivisions along Z axis in [2, INF) */
struct s3dut_mesh** sphere);
+/* Create a triangulated UV hemisphere discretized in `nslices' around the Z
+ * axis and `nstacks' along the Z axis. Face vertices are CCW ordered with
+ * respect to the hemisphere center, i.e. they are CW ordered from the outside
+ * point of view.
+ * The base of the hemisphere is located in 0,0,0 and the hemisphere is
+ * the lower half of a sphere. */
+S3DUT_API res_T
+s3dut_create_hemisphere
+ (struct mem_allocator* allocator, /* May be NULL <=> use default allocator */
+ const double radius, /* In ]0, INF) */
+ const unsigned nslices, /* # subdivisions around Z axis in [3, INF) */
+ const unsigned nstacks, /* # subdivisions along Z axis in [1, INF) */
+ struct s3dut_mesh** hemisphere);
+
/* Create a triangulated cylinder centered in 0 discretized in `nslices' around
* the Z axis and `nstacks' along the Z axis. Face vertices are CCW ordered
* with respect to the cylinder center, i.e. they are CW ordered from the
diff --git a/src/s3dut_hemisphere.c b/src/s3dut_hemisphere.c
@@ -0,0 +1,170 @@
+/* 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"
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static res_T
+setup_hemisphere_coords
+ (struct mem_allocator* allocator,
+ double* coords,
+ const double radius,
+ const unsigned nslices, /* # subdivisions around the Z axis */
+ const unsigned nstacks) /* # subdivisions along the Z axis */
+{
+ enum { SIN, COS };
+ struct darray_double sincos_theta;
+ struct darray_double sincos_phi;
+ double step_theta;
+ double step_phi;
+ size_t itheta;
+ size_t iphi;
+ size_t i;
+ res_T res = RES_OK;
+ ASSERT(coords && radius > 0 && nslices >= 3 && nstacks >= 1);
+
+ darray_double_init(allocator, &sincos_theta);
+ darray_double_init(allocator, &sincos_phi);
+
+ res = darray_double_resize(&sincos_theta, nslices*2/*sin & cos*/);
+ if(res != RES_OK) goto error;
+ res = darray_double_resize(&sincos_phi, nstacks*2/*sin & cos*/);
+ if(res != RES_OK) goto error;
+
+ /* Precompute the sinus/cosine of the theta/phi angles */
+ step_theta = 2*PI / (double)nslices;
+ FOR_EACH(itheta, 0, nslices) {
+ const double theta = -PI + (double)itheta * step_theta;
+ darray_double_data_get(&sincos_theta)[itheta*2 + SIN] = sin(theta);
+ darray_double_data_get(&sincos_theta)[itheta*2 + COS] = cos(theta);
+ }
+ step_phi = PI/2/(double)nstacks;
+ FOR_EACH(iphi, 0, nstacks) {
+ const double phi = -PI/2 + (double)(iphi+1) * step_phi;
+ darray_double_data_get(&sincos_phi)[iphi*2 + SIN] = sin(phi);
+ darray_double_data_get(&sincos_phi)[iphi*2 + COS] = cos(phi);
+ }
+
+ /* Setup the contour vertices */
+ i = 0;
+ FOR_EACH(itheta, 0, nslices) {
+ const double* theta = darray_double_cdata_get(&sincos_theta) + itheta*2;
+ FOR_EACH(iphi, 0, nstacks) {
+ const double* phi = darray_double_cdata_get(&sincos_phi) + iphi*2;
+ coords[i++] = radius * COS[theta] * COS[phi];
+ coords[i++] = radius * SIN[theta] * COS[phi];
+ coords[i++] = radius + radius * SIN[phi];
+ }
+ }
+
+ /* Setup the bottom polar vertex */
+ coords[i++] = 0;
+ coords[i++] = 0;
+ coords[i++] = 0;
+
+exit:
+ darray_double_release(&sincos_theta);
+ darray_double_release(&sincos_phi);
+ return res;
+error:
+ goto exit;
+}
+
+static void
+setup_hemisphere_indices
+ (size_t* ids,
+ const unsigned nslices, /* # subdivisions around the Z axis */
+ const unsigned nstacks) /* # subdivisions along the Z axis */
+{
+ size_t i, itheta, iphi;
+ ASSERT(ids && nslices && nstacks);
+
+ /* Define the indices of the contour primitives */
+ i = 0;
+ FOR_EACH(itheta, 0, nslices) {
+ const size_t itheta0 = itheta * nstacks;
+ const size_t itheta1 = ((itheta + 1) % nslices) * nstacks;
+ FOR_EACH(iphi, 0, nstacks-1) {
+ const size_t iphi0 = iphi + 0;
+ const size_t iphi1 = iphi + 1;
+
+ ids[i++] = itheta0 + iphi0;
+ ids[i++] = itheta0 + iphi1;
+ ids[i++] = itheta1 + iphi0;
+
+ ids[i++] = itheta1 + iphi0;
+ ids[i++] = itheta0 + iphi1;
+ ids[i++] = itheta1 + iphi1;
+ }
+ }
+
+ /* Define the indices of the polar primitives */
+ FOR_EACH(itheta, 0, nslices) {
+ const size_t itheta0 = itheta * nstacks;
+ const size_t itheta1 = ((itheta + 1) % nslices) * nstacks;
+
+ ids[i++] = nslices * nstacks;
+ ids[i++] = itheta0;
+ ids[i++] = itheta1;
+ }
+}
+
+/*******************************************************************************
+ * Exported function
+ ******************************************************************************/
+res_T
+s3dut_create_hemisphere
+ (struct mem_allocator* allocator,
+ const double radius,
+ const unsigned nslices, /* # subdivisions around the Z axis */
+ const unsigned nstacks, /* # subdivisions along the Z axis */
+ struct s3dut_mesh** mesh)
+{
+ struct s3dut_mesh* hemisphere = NULL;
+ size_t nverts;
+ size_t ntris;
+ res_T res = RES_OK;
+
+ if(radius <= 0 || nslices < 3 || nstacks < 1 || !mesh) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ nverts = nslices*nstacks/* #contour verts*/ + 1/*polar vert*/;
+ ntris = 2*nslices*(nstacks-1)/* #contour tris*/ + nslices/* #polar tris*/;
+
+ res = mesh_create(allocator, S3DUT_MESH_HEMISPHERE, nverts, ntris, &hemisphere);
+ if(res != RES_OK) goto error;
+
+ res = setup_hemisphere_coords(allocator, darray_double_data_get(&hemisphere->coords),
+ radius, nslices, nstacks);
+ if(res != RES_OK) goto error;
+
+ setup_hemisphere_indices(darray_size_t_data_get(&hemisphere->ids), nslices, nstacks);
+
+exit:
+ if(mesh) *mesh = hemisphere;
+ return res;
+error:
+ if(hemisphere) {
+ S3DUT(mesh_ref_put(hemisphere));
+ hemisphere = NULL;
+ }
+ goto exit;
+}
+
diff --git a/src/s3dut_mesh.h b/src/s3dut_mesh.h
@@ -23,7 +23,8 @@
enum s3dut_mesh_type {
S3DUT_MESH_CUBOID,
S3DUT_MESH_CYLINDER,
- S3DUT_MESH_SPHERE
+ S3DUT_MESH_SPHERE,
+ S3DUT_MESH_HEMISPHERE
};
struct s3dut_mesh {
diff --git a/src/test_s3dut_hemisphere.c b/src/test_s3dut_hemisphere.c
@@ -0,0 +1,78 @@
+/* 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_hemisphere(NULL, 0, 0, 0, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 0, 0, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 3, 0, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 3, 0, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 0, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 0, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 3, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 3, 1, NULL), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 0, 0, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 0, 0, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 3, 0, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 3, 0, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 0, 1, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 0, 1, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 0, 3, 1, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(NULL, 1, 3, 1, &msh), RES_OK);
+
+ CHECK(s3dut_mesh_ref_get(NULL), RES_BAD_ARG);
+ CHECK(s3dut_mesh_ref_get(msh), RES_OK);
+ CHECK(s3dut_mesh_ref_put(NULL), RES_BAD_ARG);
+ CHECK(s3dut_mesh_ref_put(msh), RES_OK);
+ CHECK(s3dut_mesh_ref_put(msh), RES_OK);
+
+ CHECK(s3dut_create_hemisphere(&allocator, 1, 3, 1, &msh), RES_OK);
+ CHECK(s3dut_mesh_ref_put(msh), RES_OK);
+
+ CHECK(s3dut_create_hemisphere(&allocator, 1, 2, 1, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(&allocator, 1, 3, 0, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(&allocator,-1, 3, 0, &msh), RES_BAD_ARG);
+ CHECK(s3dut_create_hemisphere(&allocator, 1, 32, 16, &msh), RES_OK);
+
+ CHECK(s3dut_mesh_get_data(NULL, NULL), RES_BAD_ARG);
+ CHECK(s3dut_mesh_get_data(msh, NULL), RES_BAD_ARG);
+ CHECK(s3dut_mesh_get_data(NULL, &data), RES_BAD_ARG);
+ CHECK(s3dut_mesh_get_data(msh, &data), RES_OK);
+ NCHECK(data.positions, NULL);
+ NCHECK(data.indices, NULL);
+ CHECK(data.nvertices >= (32*16+1), 1);
+ CHECK(data.nprimitives, (32*(16-1)*2) + 32);
+
+ 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;
+}
+