star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.fr/star-3d.git
Log | Files | Refs | README | LICENSE

commit 94bb0d5e6e2c4cb61eba8d16d971d9fdd1708c59
parent b6e5e41304d1b748fa5c7ec57e8fd3489f38edc2
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 22 Oct 2020 11:56:54 +0200

Test the closest point query on a rotated instance

Diffstat:
Msrc/test_s3d_closest_point.c | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+), 0 deletions(-)

diff --git a/src/test_s3d_closest_point.c b/src/test_s3d_closest_point.c @@ -35,6 +35,7 @@ #include "test_s3d_utils.h" #include <rsys/float3.h> +#include <rsys/float33.h> #include <limits.h> #define ON_EDGE_EPSILON 1.e-4f @@ -951,6 +952,90 @@ test_single_triangle(struct s3d_device* dev) CHK(s3d_scene_ref_put(scn) == RES_OK); } +static void +test_single_triangle_instantiated(struct s3d_device* dev) +{ + union { float f; uint32_t u32; } ucast; + struct s3d_scene* scn = NULL; + struct s3d_shape* shape = NULL; + struct s3d_scene_view* view0 = NULL; + struct s3d_scene_view* view1 = NULL; + struct s3d_vertex_data vdata = S3D_VERTEX_DATA_NULL; + struct s3d_hit hit0 = S3D_HIT_NULL; + struct s3d_hit hit1 = S3D_HIT_NULL; + float transform[12]; + float vertices[9]; + float transformed_vertices[9]; + float query_pos[3]; + + vdata.usage = S3D_POSITION; + vdata.type = S3D_FLOAT3; + vdata.get = triangle_get_pos; + + /* Setup the query position. The following data are retrieved from a user + * case and are thus setuped as it, in its raw binary format */ + query_pos[0] = (ucast.u32 = 0xc1dc7a9e, ucast.f); + query_pos[1] = (ucast.u32 = 0xc382179f, ucast.f); + query_pos[2] = (ucast.u32 = 0xc32181b0, ucast.f); + + f3(vertices+0, -28.5f, -298.5f, 69.964429f); + f3(vertices+3, -27.0f, -298.5f, 69.899651f); + f3(vertices+6, -27.0f, -297.0f, 69.204593f); + + /* Setup the triangle transformation */ + f33_rotation(transform, (float)MDEG2RAD(45.0), 0, 0); + f3_splat(transform+9, 0); + + /* Transform the triangle directly */ + f33_mulf3(transformed_vertices+0, transform, vertices+0); + f33_mulf3(transformed_vertices+3, transform, vertices+3); + f33_mulf3(transformed_vertices+6, transform, vertices+6); + f3_add(transformed_vertices+0, transformed_vertices+0, transform+9); + f3_add(transformed_vertices+1, transformed_vertices+1, transform+9); + f3_add(transformed_vertices+2, transformed_vertices+2, transform+9); + + /* Setup the scene with the pre-transformed triangle */ + CHK(s3d_scene_create(dev, &scn) == RES_OK); + CHK(s3d_shape_create_mesh(dev, &shape) == RES_OK); + CHK(s3d_scene_attach_shape(scn, shape) == RES_OK); + CHK(s3d_mesh_setup_indexed_vertices + (shape, 1, triangle_get_ids, 3, &vdata, 1, transformed_vertices) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_TRACE, &view0) == RES_OK); + CHK(s3d_scene_ref_put(scn) == RES_OK); + CHK(s3d_shape_ref_put(shape) == RES_OK); + + /* Setup the same scene with the transformation performed by Star-3D through + * instantiation */ + CHK(s3d_scene_create(dev, &scn) == RES_OK); + CHK(s3d_shape_create_mesh(dev, &shape) == RES_OK); + CHK(s3d_scene_attach_shape(scn, shape) == RES_OK); + CHK(s3d_mesh_setup_indexed_vertices + (shape, 1, triangle_get_ids, 3, &vdata, 1, vertices) == RES_OK); + CHK(s3d_shape_ref_put(shape) == RES_OK); + CHK(s3d_scene_instantiate(scn, &shape) == RES_OK); + CHK(s3d_instance_set_transform(shape, transform) == RES_OK); + CHK(s3d_scene_ref_put(scn) == RES_OK); + CHK(s3d_scene_create(dev, &scn) == RES_OK); + CHK(s3d_scene_attach_shape(scn, shape) == RES_OK); + CHK(s3d_scene_view_create(scn, S3D_TRACE, &view1) == RES_OK); + CHK(s3d_scene_ref_put(scn) == RES_OK); + CHK(s3d_shape_ref_put(shape) == RES_OK); + + /* Find the closest point */ + CHK(s3d_scene_view_closest_point + (view0, query_pos, (float)INF, NULL, &hit0) == RES_OK); + CHK(s3d_scene_view_closest_point + (view1, query_pos, (float)INF, NULL, &hit1) == RES_OK); + + /* Check that the found hits are the same */ + CHK(!S3D_HIT_NONE(&hit0)); + CHK(!S3D_HIT_NONE(&hit1)); + CHK(eq_epsf(hit0.distance, hit1.distance, 1.e-6f)); + + CHK(s3d_scene_view_ref_put(view0) == RES_OK); + CHK(s3d_scene_view_ref_put(view1) == RES_OK); +} + /******************************************************************************* * Miscellaneous test ******************************************************************************/ @@ -995,6 +1080,7 @@ main(int argc, char** argv) test_api(dev); test_single_triangle(dev); + test_single_triangle_instantiated(dev); test_cbox(dev); test_sphere(dev); test_cbox_sphere(dev);