commit 2cfa86f5719871b978b41bc70f43fd4fa64434f7
parent 8375b3d244cb0355e7cb17f25d6468f82bc958f7
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Tue, 20 May 2025 08:58:57 +0200
Set up accelerating structure for geometrical data
This commit introduces the code necessary to restructure the geometrical
data, adapting it to the algorithms that compute intersections between
rays and triangles.
In this step, the geometrical data, which was temporarily stored in a
dynamic array of triangles, is transformed into a structured scene view.
This structured view is a member variable of the sphor struct and can be
used during computation.
This commit also adds a dependency on star-3d, the library responsible
for the geometrical structuring. This same library also provides several
functions useful during Monte Carlo sampling.
Diffstat:
7 files changed, 138 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
@@ -59,6 +59,7 @@ $(LIBNAME): $(OBJ_LIB)
.config: config.mk
$(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys
$(PKG_CONFIG) --atleast-version $(SPHIN_VERSION) sphin
+ $(PKG_CONFIG) --atleast-version $(S3D_VERSION) s3d
echo "config done" > $@
.SUFFIXES: .c .d .o
@@ -111,7 +112,9 @@ clean_executable:
pkg:
sed -e 's#@PREFIX@#$(PREFIX)#g'\
-e 's#@VERSION@#$(VERSION)#g'\
+ -e 's#@RSYS_VERSION@#$(RSYS_VERSION)#g'\
-e 's#@SPHIN_VERSION@#$(SPHIN_VERSION)#g'\
+ -e 's#@S3D_VERSION@#$(S3D_VERSION)#g'\
sphor.pc.in > sphor.pc
star-phor.1: doc/star-phor.scd
diff --git a/README.md b/README.md
@@ -9,6 +9,7 @@ Solver for radiative transfer in photoreactors.
- pkg-config
- rsys
- star-phor-input
+- star-3d
## Installation
diff --git a/config.mk b/config.mk
@@ -24,9 +24,10 @@ RANLIB = ranlib
################################################################################
SPHIN_VERSION=0.0
RSYS_VERSION=0.14
+S3D_VERSION=0.10
-INCS = $$($(PKG_CONFIG) --cflags rsys sphin)
-LIBS = $$($(PKG_CONFIG) --libs rsys sphin)
+INCS = $$($(PKG_CONFIG) --cflags rsys sphin s3d)
+LIBS = $$($(PKG_CONFIG) --libs rsys sphin s3d)
################################################################################
# Compilation options
diff --git a/sphor.pc.in b/sphor.pc.in
@@ -2,8 +2,9 @@ prefix=@PREFIX@
includedir=${prefix}/include
libdir=${prefix}/lib
-Requires: rsys >= @RSYS_VERSION@
+Requires: rsys >= @RSYS_VERSION@
Requires.private: sphin >= @SPHIN_VERSION@
+Requires.private: s3d >= @S3D_VERSION@
Name: Star-Phor
Description: statistical solver for radiative transfer in photoreactors
Version: @VERSION@
diff --git a/src/sphor.c b/src/sphor.c
@@ -26,6 +26,7 @@
#include "sphor_c.h"
#include <star/sphin.h>
+#include <star/s3d.h>
#include <rsys/logger.h>
#include <rsys/mem_allocator.h>
@@ -54,6 +55,9 @@ release_sphor
if (NULL != sphor->config) {
sphin_config_ref_put(sphor->config);
}
+ if (NULL != sphor->scene_view) {
+ s3d_scene_view_ref_put(sphor->scene_view);
+ }
darray_interface_release(&sphor->interfaces);
MEM_RM(sphor->allocator, sphor);
}
diff --git a/src/sphor_c.h b/src/sphor_c.h
@@ -47,12 +47,16 @@ struct logger;
struct mem_allocator;
struct sphin_config;
struct sphor_create_args;
+struct s3d_scene_view;
struct sphor {
/* Physical configuration */
struct sphin_config* config;
struct darray_interface interfaces;
+ /* Geometrical data */
+ struct s3d_scene_view* scene_view;
+
/* Log */
struct logger* logger;
int verbose;
diff --git a/src/sphor_config.c b/src/sphor_config.c
@@ -26,6 +26,7 @@
#include "sphor_c.h"
#include <star/sphin.h>
+#include <star/s3d.h>
/*******************************************************************************
* Helper functions
@@ -244,6 +245,20 @@ setup_interface_volume_geometry_triangle
tri_sphor = darray_triangle_cdata_get(triangles) + itri_sphor;
interface = darray_interface_data_get(&sphor->interfaces) + itri_sphor;
+ /* Determine the side to append the surface based on whether the geometry is
+ * flipped and knowing the input side using the xor (^) operator.
+ * The value of the is_back variable is given by the truth table:
+ *
+ * +-----------+
+ * | flip |
+ * +-----+-----+
+ * | 0 | 1 |
+ * +------+-----+-----+-----+
+ * | side | 0 | 0 | 1 |
+ * | == |-----+-----+-----+
+ * | back | 1 | 1 | 0 |
+ * +------+-----+-----+-----+ */
+
flip = are_triangles_reversed(&tri, tri_sphor);
side = (size_t) flip ^ geom_desc->side;
@@ -533,6 +548,108 @@ error:
goto exit;
}
+static void
+get_triangle_indices
+ (const unsigned tri_id,
+ unsigned indices[3],
+ void* data)
+{
+ struct darray_triangle* triangles = data;
+
+ ASSERT(NULL != data);
+ ASSERT(NULL != indices);
+ ASSERT(tri_id < darray_triangle_size_get(triangles));
+
+ (void) triangles;
+
+ indices[0] = tri_id*3+0;
+ indices[1] = tri_id*3+1;
+ indices[2] = tri_id*3+2;
+}
+
+static void
+get_vertex_coords
+ (const unsigned vertex_id,
+ float coords[3],
+ void* data)
+{
+ struct darray_triangle* triangles = data;
+ const struct triangle* tri = NULL;
+ unsigned tri_id = vertex_id/3;
+ unsigned tri_vertex_id = vertex_id%3;
+ const double *vertices[3] = {NULL, NULL, NULL};
+
+ ASSERT(NULL != coords);
+ ASSERT(NULL != data);
+ ASSERT(tri_id < darray_triangle_size_get(triangles));
+
+ tri = darray_triangle_cdata_get(triangles) + tri_id;
+ vertices[0] = tri->v0;
+ vertices[1] = tri->v1;
+ vertices[2] = tri->v2;
+ coords[0] = (float)vertices[tri_vertex_id][0];
+ coords[1] = (float)vertices[tri_vertex_id][1];
+ coords[2] = (float)vertices[tri_vertex_id][2];
+}
+
+static res_T
+setup_geometry_accel_struct
+ (struct sphor* sphor,
+ struct darray_triangle* triangles)
+{
+ size_t triangle_count = 0;
+ size_t vertex_count = 0;
+ struct s3d_vertex_data vertex_data;
+ struct s3d_device* device = NULL;
+ struct s3d_scene* scene = NULL;
+ struct s3d_scene_view* view = NULL;
+ struct s3d_shape* shape = NULL;
+ res_T res = RES_OK;
+
+ ASSERT(NULL != sphor);
+ ASSERT(NULL != triangles);
+
+ triangle_count = darray_triangle_size_get(triangles);
+ vertex_count = triangle_count*3;
+
+ res = s3d_device_create
+ (sphor->logger, sphor->allocator, sphor->verbose, &device);
+ if (RES_OK != res) { goto error; }
+
+ res = s3d_shape_create_mesh(device, &shape);
+ if (RES_OK != res) { goto error; }
+
+ vertex_data.usage = S3D_POSITION;
+ vertex_data.type = S3D_FLOAT3;
+ vertex_data.get = get_vertex_coords;
+ res = s3d_mesh_setup_indexed_vertices
+ (shape, (unsigned)triangle_count, get_triangle_indices,
+ (unsigned)vertex_count, &vertex_data, 1, triangles);
+ if (RES_OK != res) { goto error; }
+
+ res = s3d_scene_create(device, &scene);
+ if (RES_OK != res) { goto error; }
+
+ res = s3d_scene_attach_shape(scene, shape);
+ if (RES_OK != res) { goto error; }
+
+ res = s3d_scene_view_create(scene, S3D_TRACE, &view);
+ if (RES_OK != res) { goto error; }
+
+exit:
+ if (NULL != device) { S3D(device_ref_put(device)); }
+ if (NULL != shape) { S3D(shape_ref_put(shape)); }
+ if (NULL != scene) { S3D(scene_ref_put(scene)); }
+ sphor->scene_view = view;
+ return res;
+error:
+ if (NULL != view) {
+ S3D(scene_view_ref_put(view));
+ view = NULL;
+ }
+ goto exit;
+}
+
static res_T
load_config
(struct sphor* sphor,
@@ -601,6 +718,10 @@ setup_config
res = setup_interfaces(sphor, &tri2id, &triangles);
if (RES_OK != res) { goto error; }
+ /* Build accelerating structure for geometrical data */
+ res = setup_geometry_accel_struct(sphor, &triangles);
+ if (RES_OK != res) { goto error; }
+
exit:
darray_triangle_release(&triangles);
htable_tri2id_release(&tri2id);