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