test_s3d_sphere.c (6151B)
1 /* Copyright (C) 2015-2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "s3d.h" 17 #include "test_s3d_utils.h" 18 19 #include <rsys/float2.h> 20 #include <rsys/float3.h> 21 22 int 23 main(int argc, char** argv) 24 { 25 struct mem_allocator allocator; 26 struct s3d_primitive prim0; 27 struct s3d_primitive prim1; 28 struct s3d_hit hit; 29 struct s3d_device* dev; 30 struct s3d_scene* scn; 31 struct s3d_shape* sphere0; 32 struct s3d_shape* sphere1; 33 struct s3d_scene_view* view; 34 float center[3] = {0, 0, 0}; 35 float radius; 36 float org[3]; 37 float dir[3]; 38 float range[2]; 39 float N[3], P[3], tmp[3]; 40 float lower[3]; 41 float upper[3]; 42 float area; 43 float volume; 44 size_t nprims; 45 unsigned sphere0_id; 46 unsigned sphere1_id; 47 (void)argc, (void)argv; 48 49 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 50 51 CHK(s3d_device_create(NULL, &allocator, 0, &dev) == RES_OK); 52 CHK(s3d_scene_create(dev, &scn) == RES_OK); 53 54 CHK(s3d_shape_create_sphere(NULL, NULL) == RES_BAD_ARG); 55 CHK(s3d_shape_create_sphere(dev, NULL) == RES_BAD_ARG); 56 CHK(s3d_shape_create_sphere(NULL, &sphere0) == RES_BAD_ARG); 57 CHK(s3d_shape_create_sphere(dev, &sphere0) == RES_OK); 58 59 CHK(s3d_sphere_setup(NULL, NULL, -1) == RES_BAD_ARG); 60 CHK(s3d_sphere_setup(sphere0, NULL, -1) == RES_BAD_ARG); 61 CHK(s3d_sphere_setup(NULL, center, -1) == RES_BAD_ARG); 62 CHK(s3d_sphere_setup(sphere0, center, -1) == RES_BAD_ARG); 63 CHK(s3d_sphere_setup(NULL, NULL, 1) == RES_BAD_ARG); 64 CHK(s3d_sphere_setup(sphere0, NULL, 1) == RES_BAD_ARG); 65 CHK(s3d_sphere_setup(NULL, center, 1) == RES_BAD_ARG); 66 CHK(s3d_sphere_setup(sphere0, center, 1) == RES_OK); 67 CHK(s3d_sphere_setup(sphere0, center, 0) == RES_BAD_ARG); 68 CHK(s3d_shape_ref_put(sphere0) == RES_OK); 69 70 CHK(s3d_shape_create_sphere(dev, &sphere0) == RES_OK); 71 CHK(s3d_scene_attach_shape(scn, sphere0) == RES_OK); 72 CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); 73 74 f3(org, 0, 0, 100); 75 f3(dir, 0, 0, -1); 76 f2(range, 0, FLT_MAX); 77 78 CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); 79 CHK(S3D_HIT_NONE(&hit)); 80 81 radius = 2; 82 CHK(s3d_sphere_setup(sphere0, center, 2) == RES_OK); 83 CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); 84 CHK(S3D_HIT_NONE(&hit)); 85 CHK(s3d_scene_view_ref_put(view) == RES_OK); 86 CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); 87 CHK(s3d_scene_view_trace_ray(view, org, dir, range, NULL, &hit) == RES_OK); 88 CHK(!S3D_HIT_NONE(&hit)); 89 CHK(eq_epsf(hit.distance, 100 - radius, 1.e-3f)); 90 f3_normalize(N, hit.normal); 91 f3_add(P, org, f3_mulf(P, dir, hit.distance)); 92 CHK(f3_eq_eps(N, f3(tmp, 0, 0, 1), 1.e-3f)); 93 CHK(f3_eq_eps(P, f3(tmp, 0, 0, radius), 1.e-3f)); 94 95 CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); 96 CHK(eq_epsf(area, (float)(4*PI*radius*radius), 1.e-6f)); 97 CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); 98 CHK(eq_epsf(volume, (float)(4.0/3.0*PI*radius*radius*radius), 1.e-6f)); 99 100 CHK(s3d_shape_flip_surface(sphere0) == RES_OK); 101 CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); 102 CHK(eq_epsf(area, (float)(4*PI*radius*radius), 1.e-6f)); 103 CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); 104 CHK(eq_epsf(volume, (float)(4.0/3.0*PI*radius*radius*radius), 1.e-6f)); 105 106 CHK(s3d_scene_view_ref_put(view) == RES_OK); 107 CHK(s3d_scene_view_create(scn, S3D_TRACE, &view) == RES_OK); 108 109 CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); 110 CHK(eq_epsf(area, (float)(4*PI*radius*radius), 1.e-6f)); 111 CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); 112 CHK(eq_epsf(volume, (float)(-4.0/3.0*PI*radius*radius*radius), 1.e-6f)); 113 114 CHK(s3d_shape_flip_surface(sphere0) == RES_OK); 115 CHK(s3d_scene_view_ref_put(view) == RES_OK); 116 117 center[0] = 4; 118 CHK(s3d_shape_create_sphere(dev, &sphere1) == RES_OK); 119 CHK(s3d_sphere_setup(sphere1, center, radius) == RES_OK); 120 CHK(s3d_scene_attach_shape(scn, sphere1) == RES_OK); 121 CHK(s3d_scene_view_create(scn, S3D_GET_PRIMITIVE, &view) == RES_OK); 122 123 CHK(s3d_scene_view_compute_area(view, &area) == RES_OK); 124 CHK(eq_epsf(area, (float)(2*4*PI*radius*radius), 1.e-6f)); 125 CHK(s3d_scene_view_compute_volume(view, &volume) == RES_OK); 126 CHK(eq_epsf(volume, (float)(2*4.0/3.0*PI*radius*radius*radius), 1.e-6f)); 127 128 CHK(s3d_shape_get_id(sphere0, &sphere0_id) == RES_OK); 129 CHK(s3d_shape_get_id(sphere1, &sphere1_id) == RES_OK); 130 CHK(sphere0_id != sphere1_id); 131 132 CHK(s3d_scene_view_primitives_count(view, &nprims) == RES_OK); 133 CHK(nprims == 2); 134 135 CHK(s3d_scene_view_get_aabb(view, lower, upper) == RES_OK); 136 CHK(f3_eq_eps(lower, f3_splat(tmp, -2), 1.e-6f)); 137 CHK(f3_eq_eps(upper, f3(tmp, 6, 2, 2), 1.e-6f)); 138 139 CHK(s3d_scene_view_get_primitive(view, 0, &prim0) == RES_OK); 140 CHK(s3d_scene_view_get_primitive(view, 1, &prim1) == RES_OK); 141 CHK(prim0.prim_id == 0); 142 CHK(prim1.prim_id == 0); 143 CHK(prim0.geom_id == sphere0_id || prim0.geom_id == sphere1_id); 144 CHK(prim1.geom_id == sphere0_id || prim1.geom_id == sphere1_id); 145 CHK(prim0.geom_id != prim1.geom_id); 146 CHK(prim0.inst_id == S3D_INVALID_ID); 147 CHK(prim1.inst_id == S3D_INVALID_ID); 148 CHK(prim0.scene_prim_id == 0); 149 CHK(prim1.scene_prim_id == 1); 150 151 CHK(s3d_shape_ref_put(sphere0) == RES_OK); 152 CHK(s3d_shape_ref_put(sphere1) == RES_OK); 153 CHK(s3d_scene_view_ref_put(view) == RES_OK); 154 CHK(s3d_scene_ref_put(scn) == RES_OK); 155 CHK(s3d_device_ref_put(dev) == RES_OK); 156 157 check_memory_allocator(&allocator); 158 mem_shutdown_proxy_allocator(&allocator); 159 CHK(mem_allocated_size() == 0); 160 return 0; 161 }