commit 38eac9d4227b52ebedb20b5f06a771f72dd4526a
parent 2bd9dcdd439f288642ea4c9248b4d22108b2bbd5
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Wed, 21 May 2025 18:15:01 +0200
Add cube geometry to sphor API test and disallow empty geometries
The previous test was failing because it used an empty input file. While
this is currently accepted by star-phor-input, the s3d library
explicitly disallows it by checking the number of triangles before
creating the acceleration structure and returning an error if none are
present. This commit resolves that inconsistency by adopting the s3d
behavior: a valid system must contain at least one triangle, and an
error is now returned if the geometry is empty.
Moreover, using an empty input limited the usefulness of the test, as it
prevented the sphor struct from being instantiated. To address this, the
test has been updated to include a minimal input file defining a cube
geometry. This simple yet complete input allows the sphor API to be
tested in several contexts.
Diffstat:
2 files changed, 68 insertions(+), 6 deletions(-)
diff --git a/src/sphor_config.c b/src/sphor_config.c
@@ -620,6 +620,13 @@ setup_geometry_accel_struct
triangle_count = darray_triangle_size_get(triangles);
vertex_count = triangle_count*3;
+ if (0 == triangle_count) {
+ ERROR(sphor,
+ "Could not start simulation: no geometries found in the input file.\n");
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
res = s3d_device_create
(sphor->logger, sphor->allocator, sphor->verbose, &device);
if (RES_OK != res) { goto error; }
diff --git a/src/test_sphor_lib.c b/src/test_sphor_lib.c
@@ -37,14 +37,61 @@ log_stream(const char* msg, void* ctx)
}
static void
-write_input_file(void)
+write_cube(const char* filename)
{
- char* path = "input";
+ const float cube_verts[][3] = {
+ {5.f, 5.f, 5.f},
+ {6.f, 5.f, 5.f},
+ {5.f, 6.f, 5.f},
+ {6.f, 6.f, 5.f},
+ {5.f, 5.f, 6.f},
+ {6.f, 5.f, 6.f},
+ {5.f, 6.f, 6.f},
+ {6.f, 6.f, 6.f}
+ };
+
+ /* Front faces are CW. The normals point into the cube */
+ const unsigned cube_tris[][3] = {
+ {0, 2, 1}, {1, 2, 3}, /* Front */
+ {0, 4, 2}, {2, 4, 6}, /* Left */
+ {4, 5, 6}, {6, 5, 7}, /* Back */
+ {3, 7, 1}, {1, 7, 5}, /* Right */
+ {2, 6, 3}, {3, 6, 7}, /* Top */
+ {0, 1, 4}, {4, 1, 5} /* Bottom */
+ };
+ const unsigned cube_ntris = sizeof(cube_tris) / (sizeof(unsigned)*3);
+
+ unsigned int i =0;
FILE* fp = NULL;
- CHK(fp = fopen(path, "w+"));
- fclose(fp);
+
+ fp = fopen(filename, "w");
+ CHK(NULL != fp);
+
+ fprintf(fp, "solid\n");
+ FOR_EACH(i, 0, cube_ntris) {
+ fprintf(fp, "facet normal 0.0 0.0 0.0\n");
+ fprintf(fp, " outer loop\n");
+ fprintf(fp, " vertex %f %f %f\n", SPLIT3(cube_verts[cube_tris[i][0]]));
+ fprintf(fp, " vertex %f %f %f\n", SPLIT3(cube_verts[cube_tris[i][1]]));
+ fprintf(fp, " vertex %f %f %f\n", SPLIT3(cube_verts[cube_tris[i][2]]));
+ fprintf(fp, " endloop\n");
+ fprintf(fp, "endfacet\n");
+ }
+ fprintf(fp, "endsolid\n");
+ CHK(fclose(fp) == 0);
}
+static void
+write_input_file(FILE* fp)
+{
+ const char* geom_filename = "cube.stl";
+
+ write_cube(geom_filename);
+
+ fprintf(fp, "volume: \"test volume\"\n");
+ fprintf(fp, "\tgeometry: BACK %s\n", geom_filename);
+ CHK(fflush(fp) == 0);
+}
int
main(int argc, char** argv)
@@ -54,8 +101,16 @@ main(int argc, char** argv)
struct sphor* sphor = NULL;
struct sphor_create_args args = SPHOR_CREATE_ARGS_DEFAULT;
- write_input_file();
- args.input_filename = "input";
+ char filename[] = "input";
+ FILE* fp = NULL;
+
+ fp = fopen(filename, "w+");
+ CHK(NULL != fp);
+
+ write_input_file(fp);
+ CHK(fclose(fp) == 0);
+
+ args.input_filename = filename;
(void)argc;
(void)argv;