test_senc3d_cube_in_cube.c (7423B)
1 /* Copyright (C) 2018-2020, 2023, 2024 |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 /* This test has been created using the sg3_geometry_dump_as_C_code feature 17 * of star-geometry. It uses output from test_sg3_cube_in_cube. */ 18 19 #include "senc3d.h" 20 #include "test_senc3d_utils.h" 21 22 #include <rsys/double3.h> 23 24 /* 25 cube_2 cube_3 26 27 +-------------------------+ 28 | B 29 +-------------+ | +-------------+ | 30 m1 | m0 M m1 | m1 | m0 M | 31 | +------+ | | m0 | +------+ | | 32 | | m1 S | | | | m1 S | | 33 | | N <--| | | | | N <--| | | 34 | +------+ | | | +------+ | | 35 | N <--| | | N <--| | 36 +-------------+ | +-------------+ | 37 | N <--| 38 +-------------------------+ 39 */ 40 41 42 /* Dump of star-geometry 'cube_in_cube_2'. */ 43 static const unsigned cube_in_cube_2_vertices_count = 16; 44 static const double cube_in_cube_2_vertices[48] = 45 { 46 0.1, 0, 0, 47 1, 0, 0, 48 0, 1, 0, 49 1, 1, 0, 50 0, 0, 1.1, 51 1, 0, 1, 52 0, 1, 1, 53 1, 1.1, 1, 54 -0.7, -1, -1, 55 2, -1, -1, 56 -1, 2, -1, 57 2, 2, -1, 58 -1, -1, 2.3, 59 2, -1, 2, 60 -1, 2, 2, 61 2, 2.3, 2 62 }; 63 static const unsigned cube_in_cube_2_triangles_count = 24; 64 static const unsigned cube_in_cube_2_triangles[72] = 65 { 66 0, 2, 1, 67 1, 2, 3, 68 0, 4, 2, 69 2, 4, 6, 70 4, 5, 6, 71 6, 5, 7, 72 3, 7, 1, 73 1, 7, 5, 74 2, 6, 3, 75 3, 6, 7, 76 0, 1, 4, 77 4, 1, 5, 78 8, 9, 10, 79 9, 11, 10, 80 8, 10, 12, 81 10, 14, 12, 82 12, 14, 13, 83 14, 15, 13, 84 11, 9, 15, 85 9, 13, 15, 86 10, 11, 14, 87 11, 15, 14, 88 8, 12, 9, 89 12, 13, 9 90 }; 91 static const unsigned cube_in_cube_2_properties[72] = 92 { 93 0, 1, 0, 94 0, 1, 0, 95 0, 1, 0, 96 0, 1, 0, 97 0, 1, 0, 98 0, 1, 0, 99 0, 1, 0, 100 0, 1, 0, 101 0, 1, 0, 102 0, 1, 0, 103 0, 1, 0, 104 0, 1, 0, 105 0, 1, 0, 106 0, 1, 0, 107 0, 1, 0, 108 0, 1, 0, 109 0, 1, 0, 110 0, 1, 0, 111 0, 1, 0, 112 0, 1, 0, 113 0, 1, 0, 114 0, 1, 0, 115 0, 1, 0, 116 0, 1, 0 117 }; 118 /* Dump of star-geometry 'cube_in_cube_3'. */ 119 static const unsigned cube_in_cube_3_vertices_count = 24; 120 static const double cube_in_cube_3_vertices[72] = 121 { 122 0.1, 0, 0, 123 1, 0, 0, 124 0, 1, 0, 125 1, 1, 0, 126 0, 0, 1.1, 127 1, 0, 1, 128 0, 1, 1, 129 1, 1.1, 1, 130 -0.7, -1, -1, 131 2, -1, -1, 132 -1, 2, -1, 133 2, 2, -1, 134 -1, -1, 2.3, 135 2, -1, 2, 136 -1, 2, 2, 137 2, 2.3, 2, 138 -3, -4, -4, 139 6, -4, -4, 140 -4, 6, -4, 141 6, 6, -4, 142 -4, -4, 7, 143 6, -4, 6, 144 -4, 6, 6, 145 6, 7, 6 146 }; 147 static const unsigned cube_in_cube_3_triangles_count = 36; 148 static const unsigned cube_in_cube_3_triangles[108] = 149 { 150 0, 2, 1, 151 1, 2, 3, 152 0, 4, 2, 153 2, 4, 6, 154 4, 5, 6, 155 6, 5, 7, 156 3, 7, 1, 157 1, 7, 5, 158 2, 6, 3, 159 3, 6, 7, 160 0, 1, 4, 161 4, 1, 5, 162 8, 9, 10, 163 9, 11, 10, 164 8, 10, 12, 165 10, 14, 12, 166 12, 14, 13, 167 14, 15, 13, 168 11, 9, 15, 169 9, 13, 15, 170 10, 11, 14, 171 11, 15, 14, 172 8, 12, 9, 173 12, 13, 9, 174 16, 17, 18, 175 17, 19, 18, 176 16, 18, 20, 177 18, 22, 20, 178 20, 22, 21, 179 22, 23, 21, 180 19, 17, 23, 181 17, 21, 23, 182 18, 19, 22, 183 19, 23, 22, 184 16, 20, 17, 185 20, 21, 17 186 }; 187 static const unsigned cube_in_cube_3_properties[108] = 188 { 189 0, 1, 0, 190 0, 1, 0, 191 0, 1, 0, 192 0, 1, 0, 193 0, 1, 0, 194 0, 1, 0, 195 0, 1, 0, 196 0, 1, 0, 197 0, 1, 0, 198 0, 1, 0, 199 0, 1, 0, 200 0, 1, 0, 201 0, 1, 0, 202 0, 1, 0, 203 0, 1, 0, 204 0, 1, 0, 205 0, 1, 0, 206 0, 1, 0, 207 0, 1, 0, 208 0, 1, 0, 209 0, 1, 0, 210 0, 1, 0, 211 0, 1, 0, 212 0, 1, 0, 213 0, 1, 0, 214 0, 1, 0, 215 0, 1, 0, 216 0, 1, 0, 217 0, 1, 0, 218 0, 1, 0, 219 0, 1, 0, 220 0, 1, 0, 221 0, 1, 0, 222 0, 1, 0, 223 0, 1, 0, 224 0, 1, 0 225 }; 226 227 int 228 main(int argc, char** argv) 229 { 230 struct mem_allocator allocator; 231 struct senc3d_device* dev = NULL; 232 struct senc3d_scene* scn = NULL; 233 struct context ctx = CONTEXT_NULL__; 234 unsigned e, ecount, maxm, e2; 235 (void)argc, (void)argv; 236 237 OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); 238 OK(senc3d_device_create(NULL, &allocator, SENC3D_NTHREADS_DEFAULT, 1, &dev)); 239 240 /* Create a scene with the first and second cubes. 241 * The enclosure in the small contains medium 1, the external enclosure 242 * contains medium 1, the enclosure between the small and medium cubes 243 * contains medium 0. */ 244 ctx.positions = cube_in_cube_2_vertices; 245 ctx.indices = cube_in_cube_2_triangles; 246 ctx.properties = cube_in_cube_2_properties; 247 OK(senc3d_scene_create(dev, 248 SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE, 249 cube_in_cube_2_triangles_count, get_indices, get_media_from_properties, 250 cube_in_cube_2_vertices_count, get_position, &ctx, &scn)); 251 252 OK(senc3d_scene_get_enclosure_count(scn, &ecount)); 253 CHK(ecount == 3); 254 255 FOR_EACH(e, 0, ecount) { 256 struct senc3d_enclosure* enclosure; 257 struct senc3d_enclosure_header header; 258 unsigned m; 259 OK(senc3d_scene_get_enclosure(scn, e, &enclosure)); 260 OK(senc3d_enclosure_get_header(enclosure, &header)); 261 CHK(header.enclosed_media_count == 1); 262 OK(senc3d_enclosure_get_medium(enclosure, 0, &m)); 263 CHK(m <= 1); 264 CHK((m == 0) == (header.primitives_count == cube_in_cube_2_triangles_count)); 265 OK(senc3d_enclosure_ref_put(enclosure)); 266 } 267 268 OK(senc3d_scene_get_max_medium(scn, &maxm)); 269 CHK(maxm == 1); 270 OK(senc3d_scene_ref_put(scn)); 271 272 /* Create a scene with the 3 cubes, same 2 first cubes as above. 273 * The enclosure in the small cube contains medium 1, the external enclosure 274 * contains medium 1, the enclosure between the small and medium cubes 275 * contains medium 0 and the enclosure between the medium and big cubes 276 * contains both media 0 and 1. */ 277 ctx.positions = cube_in_cube_3_vertices; 278 ctx.indices = cube_in_cube_3_triangles; 279 ctx.properties = cube_in_cube_3_properties; 280 OK(senc3d_scene_create(dev, 281 SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE, 282 cube_in_cube_3_triangles_count, get_indices, get_media_from_properties, 283 cube_in_cube_3_vertices_count, get_position, &ctx, &scn)); 284 285 OK(senc3d_scene_get_enclosure_count(scn, &ecount)); 286 CHK(ecount == 4); 287 288 e2 = ecount; 289 FOR_EACH(e, 0, ecount) { 290 struct senc3d_enclosure* enclosure; 291 struct senc3d_enclosure_header header; 292 OK(senc3d_scene_get_enclosure(scn, e, &enclosure)); 293 OK(senc3d_enclosure_get_header(enclosure, &header)); 294 CHK(header.enclosed_media_count <= 2); 295 if(header.enclosed_media_count == 2) { 296 /* A single internal enclosure has 2 media */ 297 CHK(!header.is_infinite); 298 CHK(e2 == ecount); 299 e2 = e; 300 } 301 OK(senc3d_enclosure_ref_put(enclosure)); 302 } 303 304 OK(senc3d_scene_get_max_medium(scn, &maxm)); 305 CHK(maxm == 1); 306 307 OK(senc3d_scene_ref_put(scn)); 308 OK(senc3d_device_ref_put(dev)); 309 310 check_memory_allocator(&allocator); 311 mem_shutdown_proxy_allocator(&allocator); 312 CHK(mem_allocated_size() == 0); 313 return 0; 314 }