test_solparser4.c (7234B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016-2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "solparser.h" 18 #include "test_solstice_utils.h" 19 20 const struct solparser_geometry* geometry; 21 22 static const char* input[] = { 23 "- sun: \n", 24 " dni: 1\n", 25 " spectrum: [{wavelength: 1, data: 1}]\n", 26 "- material: &lambertian\n", 27 " mirror: { reflectivity: 0.2, slope_error: 0.1 }\n", 28 "- geometry: &cuboid\n", 29 " - cuboid: { size: [1, 2, 3] }\n", 30 " material: *lambertian\n", 31 "- template: &template\n", 32 " name: template0\n", 33 " primary: 1\n", 34 " geometry: *cuboid\n", 35 "- entity:\n", 36 " name: entity0\n", 37 " transform: { translation: [1, 2, 3] }\n", 38 " children: [ *template ]\n", 39 "- entity:\n", 40 " name: entity1\n", 41 " transform: { translation: [3, 4, 5] }\n", 42 " children: [ *template ]\n", 43 NULL 44 }; 45 46 static void 47 check_entity0 48 (struct solparser* parser, const struct solparser_entity* entity0) 49 { 50 const struct solparser_entity* entity; 51 struct solparser_entity_id entity_id; 52 double tmp[3]; 53 54 CHK(parser != NULL); 55 CHK(entity0 != NULL); 56 57 CHK(strcmp(str_cget(&entity0->name), "entity0") == 0); 58 CHK(d3_eq(entity0->rotation, d3_splat(tmp, 0)) == 1); 59 CHK(d3_eq(entity0->translation, d3(tmp, 1, 2, 3)) == 1); 60 CHK(entity0->type == SOLPARSER_ENTITY_EMPTY); 61 62 CHK(solparser_entity_get_children_count(entity0) == 1); 63 CHK(solparser_entity_get_anchors_count(entity0) == 0); 64 65 entity_id = solparser_entity_get_child(entity0, 0); 66 entity = solparser_get_entity(parser, entity_id); 67 CHK(strcmp(str_cget(&entity->name), "template0") == 0); 68 CHK(d3_eq(entity->translation, d3_splat(tmp, 0)) == 1); 69 CHK(d3_eq(entity->rotation, d3_splat(tmp, 0)) == 1); 70 71 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 72 CHK(solparser_get_geometry(parser, entity->data.geometry) == geometry); 73 CHK(solparser_find_entity(parser, "entity0.template0") == entity); 74 } 75 76 static void 77 check_entity1 78 (struct solparser* parser, const struct solparser_entity* entity1) 79 { 80 const struct solparser_entity* entity; 81 struct solparser_entity_id entity_id; 82 double tmp[3]; 83 84 CHK(parser != NULL); 85 CHK(entity1 != NULL); 86 87 CHK(strcmp(str_cget(&entity1->name), "entity1") == 0); 88 CHK(d3_eq(entity1->rotation, d3_splat(tmp, 0)) == 1); 89 CHK(d3_eq(entity1->translation, d3(tmp, 3, 4, 5)) == 1); 90 CHK(entity1->type == SOLPARSER_ENTITY_EMPTY); 91 92 CHK(solparser_entity_get_children_count(entity1) == 1); 93 CHK(solparser_entity_get_anchors_count(entity1) == 0); 94 95 entity_id = solparser_entity_get_child(entity1, 0); 96 entity = solparser_get_entity(parser, entity_id); 97 CHK(strcmp(str_cget(&entity->name), "template0") == 0); 98 CHK(d3_eq(entity->rotation, d3_splat(tmp, 0)) == 1); 99 CHK(d3_eq(entity->translation, d3_splat(tmp, 0)) == 1); 100 101 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 102 CHK(solparser_get_geometry(parser, entity->data.geometry) == geometry); 103 CHK(solparser_find_entity(parser, "entity1.template0") == entity); 104 } 105 106 int 107 main(int argc, char** argv) 108 { 109 struct solparser_entity_iterator it, it_end; 110 struct solparser_geometry_iterator it_geom, it_end_geom; 111 struct mem_allocator allocator; 112 struct solparser* parser; 113 const struct solparser_material* mtl; 114 const struct solparser_material_double_sided* mtl2; 115 const struct solparser_material_mirror* mirror; 116 const struct solparser_object* obj; 117 const struct solparser_shape* shape; 118 const struct solparser_shape_cuboid* cuboid; 119 struct solparser_geometry_id geom_id; 120 struct solparser_object_id obj_id; 121 double tmp[3]; 122 FILE* stream; 123 size_t i; 124 int entity0 = 0; 125 int entity1 = 0; 126 (void)argc, (void)argv; 127 128 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 129 solparser_create(&allocator, &parser); 130 131 stream = tmpfile(); 132 CHK(stream != NULL); 133 i = 0; 134 while(input[i]) { 135 const size_t len = strlen(input[i]); 136 CHK(fwrite(input[i], 1, len, stream) == len); 137 ++i; 138 } 139 rewind(stream); 140 141 CHK(solparser_setup(parser, NULL, stream) == RES_OK); 142 CHK(solparser_load(parser) == RES_OK); 143 144 solparser_geometry_iterator_begin(parser, &it_geom); 145 solparser_geometry_iterator_end(parser, &it_end_geom); 146 CHK(solparser_geometry_iterator_eq(&it_geom, &it_end_geom) == 0); 147 geom_id = solparser_geometry_iterator_get(&it_geom); 148 geometry = solparser_get_geometry(parser, geom_id); 149 solparser_geometry_iterator_next(&it_geom); 150 CHK(solparser_geometry_iterator_eq(&it_geom, &it_end_geom) == 1); 151 152 CHK(solparser_geometry_get_objects_count(geometry) == 1); 153 obj_id = solparser_geometry_get_object(geometry, 0); 154 obj = solparser_get_object(parser, obj_id); 155 CHK(d3_eq(obj->rotation, d3_splat(tmp, 0)) == 1); 156 CHK(d3_eq(obj->translation, d3_splat(tmp, 0)) == 1); 157 158 mtl2 = solparser_get_material_double_sided(parser, obj->mtl2); 159 CHK(mtl2->front.i == mtl2->back.i); 160 mtl = solparser_get_material(parser, mtl2->front); 161 CHK(mtl->type == SOLPARSER_MATERIAL_MIRROR); 162 mirror = solparser_get_material_mirror(parser, mtl->data.mirror); 163 CHK(mirror->reflectivity.type == SOLPARSER_MTL_DATA_REAL); 164 CHK(mirror->reflectivity.value.real == 0.2); 165 CHK(mirror->slope_error.type == SOLPARSER_MTL_DATA_REAL); 166 CHK(mirror->slope_error.value.real == 0.1); 167 CHK(mirror->ufacet_distrib == SOLPARSER_MICROFACET_BECKMANN); 168 169 shape = solparser_get_shape(parser, obj->shape); 170 CHK(shape->type == SOLPARSER_SHAPE_CUBOID); 171 cuboid = solparser_get_shape_cuboid(parser, shape->data.cuboid); 172 CHK(cuboid->size[0] == 1); 173 CHK(cuboid->size[1] == 2); 174 CHK(cuboid->size[2] == 3); 175 176 solparser_entity_iterator_begin(parser, &it); 177 solparser_entity_iterator_end(parser, &it_end); 178 CHK(solparser_entity_iterator_eq(&it, &it_end) == 0); 179 180 while(!solparser_entity_iterator_eq(&it, &it_end)) { 181 struct solparser_entity_id entity_id; 182 const struct solparser_entity* entity; 183 184 entity_id = solparser_entity_iterator_get(&it); 185 entity = solparser_get_entity(parser, entity_id); 186 187 if(!strcmp(str_cget(&entity->name), "entity0")) { 188 CHK(entity0 == 0); 189 entity0 = 1; 190 check_entity0(parser, entity); 191 } else if(!strcmp(str_cget(&entity->name), "entity1")) { 192 CHK(entity1 == 0); 193 entity1 = 1; 194 check_entity1(parser, entity); 195 } else { 196 FATAL("Unexpected entity name.\n"); 197 } 198 199 solparser_entity_iterator_next(&it); 200 } 201 CHK(entity0 == 1); 202 CHK(entity1 == 1); 203 204 CHK(solparser_load(parser) == RES_BAD_OP); 205 solparser_ref_put(parser); 206 fclose(stream); 207 208 check_memory_allocator(&allocator); 209 mem_shutdown_proxy_allocator(&allocator); 210 CHK(mem_allocated_size() == 0); 211 return 0; 212 } 213