test_sg3d_invalid_models.c (4116B)
1 /* Copyright (C) 2019, 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 #include "sg3d.h" 17 #include "test_sg3d_utils.h" 18 19 #include <rsys/double3.h> 20 21 #include <stdio.h> 22 23 /* The following array lists the indices toward the 3D vertices of each 24 * triangle. 25 * ,2---,3 ,2----3 26 * ,' | ,'/| ,'/| \ | 27 * 6----7' / | 6' / | \ | Y 28 * |', | / ,1 | / ,0---,1 | 29 * | ',|/,' |/,' | ,' o--X 30 * 4----5' 4----5' / 31 * Front, right Back, left and Z 32 * and Top faces bottom faces 33 * 34 * The right-handed geometrical normal is outside the cube */ 35 static const unsigned 36 cube_indices2[12/*#triangles*/ * 3/*#indices per triangle*/] = { 37 0, 3, 1, 0, 2, 3, /* Front face */ 38 0, 6, 2, 0, 4, 6, /* Left face*/ 39 4, 5, 7, 6, 4, 7, /* Back face */ 40 3, 7, 5, 1, 3, 5, /* Right face */ 41 2, 6, 7, 3, 2, 7, /* Top face */ 42 0, 1, 5, 4, 0, 5 /* Bottom face */ 43 }; 44 45 int 46 main(int argc, char** argv) 47 { 48 struct mem_allocator allocator; 49 struct sg3d_device* dev; 50 struct sg3d_geometry* geom; 51 struct context ctx = CONTEXT_NULL__; 52 struct sg3d_geometry_add_callbacks callbacks = SG3D_ADD_CALLBACKS_NULL__; 53 unsigned count; 54 (void)argc, (void)argv; 55 56 OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); 57 OK(sg3d_device_create(NULL, &allocator, 1, &dev)); 58 OK(sg3d_geometry_create(dev, &geom)); 59 SG3D(device_ref_put(dev)); 60 61 callbacks.get_indices = get_indices; 62 callbacks.get_properties = get_properties; 63 callbacks.get_position = get_position; 64 65 ctx.positions = cube_vertices; 66 ctx.indices = cube_indices; 67 ctx.front_media = medium0; 68 ctx.back_media = medium1; 69 ctx.intface = intface0; 70 71 /* First cube (front: 0, back: 1), right-handed normal outside */ 72 OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx)); 73 74 ctx.indices = cube_indices2; 75 76 /* Second cube (front: 0, back: 1), right-handed normal outside 77 * Same cube location, but opposite diagonals on sides than first cube */ 78 OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx)); 79 80 OK(sg3d_geometry_get_unique_triangles_with_merge_conflict_count(geom, &count)); 81 CHK(count == 0); 82 OK(sg3d_geometry_get_unique_triangles_with_unspecified_interface_count(geom, &count)); 83 CHK(count == 0); 84 OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count)); 85 CHK(count == 0); 86 OK(sg3d_geometry_dump_as_c_code(geom, stdout, "invalid_1", 87 SG3D_C_DUMP_CONST | SG3D_C_DUMP_STATIC)); 88 89 d3(ctx.offset, -4, -4, -4); 90 d3_splat(ctx.scale, 10); 91 ctx.reverse_vrtx = 1; 92 ctx.reverse_med = 1; 93 ctx.front_media = medium1; 94 ctx.back_media = medium0; 95 96 /* Third cube (front: 0, back: 1), right-handed normal inside */ 97 OK(sg3d_geometry_add(geom, nvertices, ntriangles, &callbacks, &ctx)); 98 99 OK(sg3d_geometry_get_unique_triangles_with_merge_conflict_count(geom, &count)); 100 CHK(count == 0); 101 OK(sg3d_geometry_get_unique_triangles_with_unspecified_interface_count(geom, &count)); 102 CHK(count == 0); 103 OK(sg3d_geometry_get_unique_triangles_with_unspecified_side_count(geom, &count)); 104 CHK(count == 0); 105 OK(sg3d_geometry_dump_as_c_code(geom, stdout, "invalid_2", 106 SG3D_C_DUMP_CONST | SG3D_C_DUMP_STATIC)); 107 108 OK(sg3d_geometry_dump_as_vtk(geom, stdout)); 109 110 SG3D(geometry_ref_put(geom)); 111 112 check_memory_allocator(&allocator); 113 mem_shutdown_proxy_allocator(&allocator); 114 CHK(mem_allocated_size() == 0); 115 return 0; 116 }