star-camera

Camera models
git clone git://git.meso-star.fr/star-camera.git
Log | Files | Refs | README | LICENSE

commit 6934852821e419ec9cfbb02ee02ae2a2051bb2d5
parent 77d3e43d661487f4e017ca1ecb4220ac2c2433e1
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 21 Jul 2021 14:58:59 +0200

Test the ray generation for the pinhole camera

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

diff --git a/src/test_scam_pinhole.c b/src/test_scam_pinhole.c @@ -22,6 +22,13 @@ #include <rsys/mem_allocator.h> #include <math.h> +#include <stdlib.h> + +static INLINE double +rand_canonical(void) +{ + return (double)rand() / (double)((int64_t)RAND_MAX + 1); +} static void log_stream(const char* msg, void* ctx) @@ -36,7 +43,18 @@ main(int argc, char** argv) { struct logger logger; struct scam_pinhole_args args = SCAM_PINHOLE_ARGS_DEFAULT; + struct scam_sample sample = SCAM_SAMPLE_NULL; + struct scam_ray ray = SCAM_RAY_NULL; struct scam* cam = NULL; + const size_t nsamps = 1000; + double axis_x[3]; + double axis_y[3]; + double axis_z[3]; + double hori_hfov; /* horizontal half field of view */ + double sin_hori_hfov; /* Sinus of the horizontal half field of view */ + double sin_vert_hfov; /* Sinos of the vertical half field of view */ + double img_depth; + size_t i = 0; enum scam_type type = SCAM_NONE; (void)argc, (void)argv; @@ -90,6 +108,53 @@ main(int argc, char** argv) CHK(scam_create_pinhole(NULL, NULL, 1, &args, &cam) == RES_OK); CHK(scam_ref_put(cam) == RES_OK); + args.position[0] = rand_canonical(); + args.position[1] = rand_canonical(); + args.position[2] = rand_canonical(); + args.target[0] = rand_canonical(); + args.target[1] = rand_canonical(); + args.target[2] = rand_canonical(); + args.up[0] = rand_canonical(); + args.up[1] = rand_canonical(); + args.up[2] = rand_canonical(); + args.field_of_view = PI/2.0; + args.aspect_ratio = 4.0/3.0; + + CHK(scam_create_pinhole(NULL, NULL, 1, &args, &cam) == RES_OK); + + /* Precompute some view frustum constants */ + d3_normalize(axis_z, d3_sub(axis_z, args.target, args.position)); + d3_normalize(axis_x, d3_cross(axis_x, axis_z, args.up)); + d3_normalize(axis_y, d3_cross(axis_y, axis_z, axis_x)); + img_depth = 1.0/tan(args.field_of_view*0.5); + hori_hfov = atan(args.aspect_ratio / img_depth); + sin_hori_hfov = sin(hori_hfov); + sin_vert_hfov = sin(args.field_of_view*0.5); + + CHK(scam_generate_ray(NULL, &sample, &ray) == RES_BAD_ARG); + CHK(scam_generate_ray(cam, NULL, &ray) == RES_BAD_ARG); + CHK(scam_generate_ray(cam, &sample, NULL) == RES_BAD_ARG); + FOR_EACH(i, 0, nsamps) { + double cos_ray_axis_x; + double cos_ray_axis_y; + double cos_ray_axis_z; + sample.film[0] = rand_canonical(); + sample.film[1] = rand_canonical(); + CHK(scam_generate_ray(cam, &sample, &ray) == RES_OK); + + /* Check the ray origin */ + CHK(d3_eq(ray.org, args.position)); + + /* Check that the generated ray is in the view frustum */ + cos_ray_axis_x = d3_dot(ray.dir, axis_x); + cos_ray_axis_y = d3_dot(ray.dir, axis_y); + cos_ray_axis_z = d3_dot(ray.dir, axis_z); + CHK(cos_ray_axis_z >= 0); + CHK(cos_ray_axis_y <= sin_vert_hfov); + CHK(cos_ray_axis_x <= sin_hori_hfov); + } + CHK(scam_ref_put(cam) == RES_OK); + logger_release(&logger); CHK(mem_allocated_size() == 0); return 0;