test_sg2d_unspecified_properties.c (5743B)
1 /* Copyright (C) 2019, 2020, 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 "sg2d.h" 17 #include "test_sg2d_utils.h" 18 19 #include <stdio.h> 20 21 int 22 main(int argc, char** argv) 23 { 24 struct mem_allocator allocator; 25 struct sg2d_device* dev; 26 struct sg2d_geometry* geom; 27 struct context ctx = CONTEXT_NULL__; 28 struct sg2d_geometry_add_callbacks callbacks = SG2D_ADD_CALLBACKS_NULL__; 29 unsigned property[12]; 30 unsigned i; 31 const unsigned property_count = sizeof(property) / sizeof(*property); 32 unsigned count; 33 (void)argc, (void)argv; 34 35 OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator)); 36 OK(sg2d_device_create(NULL, &allocator, 1, &dev)); 37 OK(sg2d_geometry_create(dev, &geom)); 38 39 FOR_EACH(i, 0, property_count) property[i] = SG2D_UNSPECIFIED_PROPERTY; 40 41 callbacks.get_indices = get_indices; 42 callbacks.get_position = get_position; 43 44 /* A 3D square. 45 * 2 enclosures (inside, outside) sharing the same segments, 46 * but opposite sides */ 47 ctx.positions = box_vertices; 48 ctx.indices = box_indices; 49 50 /* Add geometry with no properties */ 51 ctx.front_media = property; 52 ctx.back_media = medium1; 53 ctx.intface = property; 54 OK(sg2d_geometry_add(geom, nvertices, nsegments, &callbacks, &ctx)); 55 OK(sg2d_geometry_get_unique_segments_with_unspecified_side_count(geom, &count)); 56 CHK(count == nsegments); 57 OK(sg2d_geometry_get_added_segments_count(geom, &count)); 58 CHK(count == nsegments); 59 60 /* Add same geometry with no properties on front/intface */ 61 callbacks.get_properties = get_properties; 62 OK(sg2d_geometry_add(geom, nvertices, nsegments, &callbacks, &ctx)); 63 OK(sg2d_geometry_get_unique_segments_with_unspecified_side_count(geom, &count)); 64 CHK(count == nsegments); 65 OK(sg2d_geometry_get_added_segments_count(geom, &count)); 66 CHK(count == 2 * nsegments); 67 68 OK(sg2d_geometry_dump_as_c_code(geom, stdout, "front_unspecified", 69 SG2D_C_DUMP_STATIC | SG2D_C_DUMP_CONST)); 70 71 /* Add same geometry, front/intface properties are defined for odd segments */ 72 FOR_EACH(i, 0, sizeof(property) / sizeof(*property)) 73 property[i] = (i % 2) ? 0 : SG2D_UNSPECIFIED_PROPERTY; 74 OK(sg2d_geometry_add(geom, nvertices, nsegments, &callbacks, &ctx)); 75 OK(sg2d_geometry_get_unique_segments_with_unspecified_side_count(geom, &count)); 76 CHK(count == nsegments / 2); 77 OK(sg2d_geometry_get_unique_vertices_count(geom, &count)); 78 CHK(count == nvertices); 79 OK(sg2d_geometry_get_added_segments_count(geom, &count)); 80 CHK(count == 3 * nsegments); 81 OK(sg2d_geometry_get_unique_segments_count(geom, &count)); 82 CHK(count == nsegments); 83 FOR_EACH(i, 0, count) { 84 unsigned prop[SG2D_PROP_TYPES_COUNT__]; 85 OK(sg2d_geometry_get_unique_segment_properties(geom, i, prop)); 86 CHK(prop[SG2D_FRONT] == ((i % 2) ? 0 : SG2D_UNSPECIFIED_PROPERTY) 87 && prop[SG2D_BACK] == 1 88 && prop[SG2D_INTFACE] == ((i % 2) ? 0 : SG2D_UNSPECIFIED_PROPERTY)); 89 } 90 91 /* Same information again, using a reversed box */ 92 ctx.reverse_vrtx = 1; 93 SWAP(const unsigned*, ctx.front_media, ctx.back_media); 94 OK(sg2d_geometry_add(geom, nvertices, nsegments, &callbacks, &ctx)); 95 OK(sg2d_geometry_get_unique_segments_with_unspecified_side_count(geom, &count)); 96 CHK(count == nsegments / 2); 97 OK(sg2d_geometry_get_added_segments_count(geom, &count)); 98 CHK(count == 4 * nsegments); 99 100 OK(sg2d_geometry_dump_as_c_code(geom, stdout, "front_half_unspecified", 101 SG2D_C_DUMP_STATIC | SG2D_C_DUMP_CONST)); 102 103 /* Define properties for remaining segments, using reversed box */ 104 FOR_EACH(i, 0, sizeof(property) / sizeof(*property)) 105 property[i] = (i % 2) ? SG2D_UNSPECIFIED_PROPERTY : 0; 106 OK(sg2d_geometry_add(geom, nvertices, nsegments, &callbacks, &ctx)); 107 OK(sg2d_geometry_get_unique_segments_with_unspecified_side_count(geom, &count)); 108 CHK(count == 0); 109 OK(sg2d_geometry_get_unique_vertices_count(geom, &count)); 110 CHK(count == nvertices); 111 OK(sg2d_geometry_get_added_segments_count(geom, &count)); 112 CHK(count == 5 * nsegments); 113 OK(sg2d_geometry_get_unique_segments_count(geom, &count)); 114 CHK(count == nsegments); 115 FOR_EACH(i, 0, count) { 116 unsigned prop[SG2D_PROP_TYPES_COUNT__]; 117 OK(sg2d_geometry_get_unique_segment_properties(geom, i, prop)); 118 CHK(prop[SG2D_FRONT] == 0 && prop[SG2D_BACK] == 1 119 && prop[SG2D_INTFACE] == 0); 120 } 121 122 OK(sg2d_geometry_dump_as_c_code(geom, stdout, "all_defined", 123 SG2D_C_DUMP_STATIC | SG2D_C_DUMP_CONST)); 124 125 /* Define incoherent properties for some segments */ 126 FOR_EACH(i, 0, sizeof(property) / sizeof(*property)) 127 property[i] = (i % 2); 128 OK(sg2d_geometry_add(geom, nvertices, nsegments, &callbacks, &ctx)); 129 OK(sg2d_geometry_get_unique_segments_with_merge_conflict_count(geom, &count)); 130 CHK(count == nsegments / 2); 131 OK(sg2d_geometry_get_unique_segments_with_properties_conflict_count(geom, &count)); 132 CHK(count == 0); 133 OK(sg2d_geometry_get_added_segments_count(geom, &count)); 134 CHK(count == 6 * nsegments); 135 136 OK(sg2d_geometry_ref_put(geom)); 137 OK(sg2d_device_ref_put(dev)); 138 139 check_memory_allocator(&allocator); 140 mem_shutdown_proxy_allocator(&allocator); 141 CHK(mem_allocated_size() == 0); 142 return 0; 143 }