commit 1fef41acfc0c1c71282f65233461383f77323615
parent 72cb802c1c7fa44839f666feeed8ee1fcf4c8ea4
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 15 Dec 2020 14:39:59 +0100
Test the suvm_polyhedron_intersect_aabb function
Diffstat:
2 files changed, 131 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -96,6 +96,7 @@ if(NOT NO_TEST)
new_test(test_suvm_device)
new_test(test_suvm_volume)
+ new_test(test_suvm_primitive_intersection)
endif()
################################################################################
diff --git a/src/test_suvm_primitive_intersection.c b/src/test_suvm_primitive_intersection.c
@@ -0,0 +1,130 @@
+/* Copyright (C) 2020 |Meso|Star> (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 "suvm.h"
+#include "test_suvm_utils.h"
+
+#include <rsys/float3.h>
+
+struct mesh {
+ const double* vertices; /* List of double[3] */
+ size_t nvertices;
+ const size_t* tetrahedra; /* List of size_t[4] */
+ size_t ntetrahedra;
+};
+static const struct mesh MESH_NULL;
+
+/*******************************************************************************
+ * Helper functions
+ *************v*****************************************************************/
+static void
+get_indices(const size_t itetra, size_t ids[4], void* ctx)
+{
+ struct mesh* msh = ctx;
+ CHK(itetra < msh->ntetrahedra);
+ CHK(ids != NULL);
+ ids[0] = msh->tetrahedra[itetra*4+0];
+ ids[1] = msh->tetrahedra[itetra*4+1];
+ ids[2] = msh->tetrahedra[itetra*4+2];
+ ids[3] = msh->tetrahedra[itetra*4+3];
+}
+
+static void
+get_position(const size_t ivert, double pos[3], void* ctx)
+{
+ struct mesh* msh = ctx;
+ CHK(ctx != NULL);
+ CHK(pos != NULL);
+ CHK(ivert < msh->nvertices);
+ pos[0] = msh->vertices[ivert*3+0];
+ pos[1] = msh->vertices[ivert*3+1];
+ pos[2] = msh->vertices[ivert*3+2];
+}
+
+static void
+test_tetra_aabb_intersection(struct suvm_device* dev)
+{
+ const double vertices[] = {
+ 0.0, 0.0, 0.0,
+ 0.0, 0.0, 1.0,
+ 1.0, 0.0, 1.0,
+ 0.0, 1.0, 1.0
+ };
+ const size_t tetra[] = { 0, 1, 2, 3 };
+ struct mesh msh = MESH_NULL;
+ struct suvm_tetrahedral_mesh_args args = SUVM_TETRAHEDRAL_MESH_ARGS_NULL;
+ struct suvm_polyhedron poly;
+ struct suvm_primitive prim = SUVM_PRIMITIVE_NULL;
+ struct suvm_volume* vol= NULL;
+ float low[3];
+ float upp[3];
+ size_t nprims;
+
+ args.ntetrahedra = 1;
+ args.nvertices = 4;
+ args.get_indices = get_indices;
+ args.get_position = get_position;
+ args.context = &msh;
+
+ msh.vertices = vertices;
+ msh.nvertices = 4;
+ msh.tetrahedra = tetra;
+ msh.ntetrahedra = 1;
+
+ CHK(suvm_tetrahedral_mesh_create(dev, &args, &vol) == RES_OK);
+
+ CHK(suvm_volume_get_primitives_count(vol, &nprims) == RES_OK);
+ CHK(nprims == 1);
+
+ CHK(suvm_volume_get_primitive(vol, 0, &prim) == RES_OK);
+ CHK(suvm_primitive_setup_polyhedron(&prim, &poly) == RES_OK);
+
+ f3(low, 0.f, 0.f, 0.f);
+ f3(upp, 1.f, 1.f, 1.f);
+ CHK(suvm_polyhedron_intersect_aabb(&poly, low, upp) == SUVM_INTERSECT_IS_INCLUDED);
+
+ f3(low, 0.f, 0.f, 0.5f);
+ f3(upp, 0.5f, 0.5f, 1.f);
+ CHK(suvm_polyhedron_intersect_aabb(&poly, low, upp) == SUVM_INTERSECT_PARTIAL);
+
+ f3(low, 0.f, 0.f, 0.66667f /* ~1-1/3 */);
+ f3(upp, 0.33332f/*~1/3*/, 0.33332f/*~1-1/3*/, 1.f);
+ CHK(suvm_polyhedron_intersect_aabb(&poly, low, upp) == SUVM_INTERSECT_INCLUDE);
+
+ f3(low, 0.33334f/*~1.3*/, 0.33334f/* ~1/3 */, 0);
+ f3(upp, 1.f, 1.f, 0.66665f /*~ 1-1/3 */);
+ CHK(suvm_polyhedron_intersect_aabb(&poly, low, upp) == SUVM_INTERSECT_NONE);
+
+ CHK(suvm_volume_ref_put(vol) == RES_OK);
+}
+
+/*******************************************************************************
+ * Main function
+ ******************************************************************************/
+int
+main(int argc, char** argv)
+{
+ struct suvm_device* dev = NULL;
+ (void)argc, (void)argv;
+
+ CHK(suvm_device_create(NULL, &mem_default_allocator, 1, &dev) == RES_OK);
+
+ test_tetra_aabb_intersection(dev);
+
+ CHK(suvm_device_ref_put(dev) == RES_OK);
+ check_memory_allocator(&mem_default_allocator);
+ CHK(mem_allocated_size() == 0);
+ return 0;
+}