test_solparser_normal_map.c (12950B)
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 static void 21 test_dielectric(struct solparser* parser) 22 { 23 struct solparser_entity_iterator it, end; 24 struct solparser_entity_id entity_id; 25 struct solparser_object_id obj_id; 26 const struct solparser_entity* entity; 27 const struct solparser_image* img; 28 const struct solparser_geometry* geom; 29 const struct solparser_material_double_sided* mtl2; 30 const struct solparser_material* mtl; 31 const struct solparser_material_dielectric* dielectric; 32 const struct solparser_object* obj; 33 const struct solparser_shape* shape; 34 FILE* stream; 35 36 stream = tmpfile(); 37 CHK(stream != NULL); 38 39 fprintf(stream, "- sun: {dni: 1, spectrum: [{wavelength: 1, data: 1} ]}\n"); 40 fprintf(stream, "\n"); 41 fprintf(stream, "- material: &glass\n"); 42 fprintf(stream, " front:\n"); 43 fprintf(stream, " dielectric:\n"); 44 fprintf(stream, " medium_i: &out {refractive_index: 1, extinction: 0}\n"); 45 fprintf(stream, " medium_t: &in {refractive_index: 1.5, extinction: 20}\n"); 46 fprintf(stream, " normal_map: {path: my_normal_map}\n"); 47 fprintf(stream, " back: {dielectric: {medium_i: *in, medium_t: *out}}\n"); 48 fprintf(stream, "\n"); 49 fprintf(stream, "- entity:\n"); 50 fprintf(stream, " name: foo\n"); 51 fprintf(stream, " primary: 1\n"); 52 fprintf(stream, " geometry:\n"); 53 fprintf(stream, " - cylinder: { radius: 2, height: 2 }\n"); 54 fprintf(stream, " material: *glass\n"); 55 rewind(stream); 56 57 CHK(solparser_setup(parser, NULL, stream) == RES_OK); 58 CHK(solparser_load(parser) == RES_OK); 59 60 solparser_entity_iterator_begin(parser, &it); 61 solparser_entity_iterator_end(parser, &end); 62 CHK(solparser_entity_iterator_eq(&it, &end) == 0); 63 64 entity_id = solparser_entity_iterator_get(&it); 65 entity = solparser_get_entity(parser, entity_id); 66 67 CHK(strcmp("foo", str_cget(&entity->name)) == 0); 68 CHK(solparser_entity_get_children_count(entity) == 0); 69 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 70 CHK(entity->primary == 1); 71 geom = solparser_get_geometry(parser, entity->data.geometry); 72 CHK(solparser_geometry_get_objects_count(geom) == 1); 73 obj_id = solparser_geometry_get_object(geom, 0); 74 obj = solparser_get_object(parser, obj_id); 75 shape = solparser_get_shape(parser, obj->shape); 76 CHK(shape->type == SOLPARSER_SHAPE_CYLINDER); 77 mtl2 = solparser_get_material_double_sided(parser, obj->mtl2); 78 CHK(mtl2->front.i != mtl2->back.i); 79 80 mtl = solparser_get_material(parser, mtl2->front); 81 CHK(mtl->type == SOLPARSER_MATERIAL_DIELECTRIC); 82 dielectric = solparser_get_material_dielectric(parser, mtl->data.dielectric); 83 CHK(SOLPARSER_ID_IS_VALID(dielectric->normal_map) == 1); 84 img = solparser_get_image(parser, dielectric->normal_map); 85 CHK(strcmp(str_cget(&img->filename), "my_normal_map") == 0); 86 87 mtl = solparser_get_material(parser, mtl2->back); 88 CHK(mtl->type == SOLPARSER_MATERIAL_DIELECTRIC); 89 dielectric = solparser_get_material_dielectric(parser, mtl->data.dielectric); 90 CHK(SOLPARSER_ID_IS_VALID(dielectric->normal_map) == 0); 91 92 solparser_entity_iterator_next(&it); 93 CHK(solparser_entity_iterator_eq(&it, &end) == 1); 94 95 CHK(solparser_load(parser) == RES_BAD_OP); 96 fclose(stream); 97 } 98 99 static void 100 test_matte(struct solparser* parser) 101 { 102 struct solparser_entity_iterator it, end; 103 struct solparser_entity_id entity_id; 104 struct solparser_object_id obj_id; 105 const struct solparser_entity* entity; 106 const struct solparser_image* img; 107 const struct solparser_geometry* geom; 108 const struct solparser_material_double_sided* mtl2; 109 const struct solparser_material* mtl; 110 const struct solparser_material_matte* matte; 111 const struct solparser_object* obj; 112 const struct solparser_shape* shape; 113 FILE* stream; 114 115 stream = tmpfile(); 116 CHK(stream != NULL); 117 118 fprintf(stream, "- sun: { dni: 1, spectrum: [{wavelength: 1, data: 1} ] }\n"); 119 fprintf(stream, "- entity:\n"); 120 fprintf(stream, " name: test\n"); 121 fprintf(stream, " primary: 0\n"); 122 fprintf(stream, " geometry:\n"); 123 fprintf(stream, " - sphere: { radius: 1 }\n"); 124 fprintf(stream, " material:\n"); 125 fprintf(stream, " matte:\n"); 126 fprintf(stream, " reflectivity: 0.123\n"); 127 fprintf(stream, " normal_map: { path: \"path to normal map\" }\n"); 128 rewind(stream); 129 130 CHK(solparser_setup(parser, NULL, stream) == RES_OK); 131 CHK(solparser_load(parser) == RES_OK); 132 133 solparser_entity_iterator_begin(parser, &it); 134 solparser_entity_iterator_end(parser, &end); 135 CHK(solparser_entity_iterator_eq(&it, &end) == 0); 136 137 entity_id = solparser_entity_iterator_get(&it); 138 entity = solparser_get_entity(parser, entity_id); 139 140 CHK(strcmp("test", str_cget(&entity->name)) == 0); 141 CHK(solparser_entity_get_children_count(entity) == 0); 142 CHK(entity->primary == 0); 143 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 144 geom = solparser_get_geometry(parser, entity->data.geometry); 145 CHK(solparser_geometry_get_objects_count(geom) == 1); 146 obj_id = solparser_geometry_get_object(geom, 0); 147 obj = solparser_get_object(parser, obj_id); 148 shape = solparser_get_shape(parser, obj->shape); 149 CHK(shape->type == SOLPARSER_SHAPE_SPHERE); 150 mtl2 = solparser_get_material_double_sided(parser, obj->mtl2); 151 CHK(mtl2->front.i == mtl2->back.i); 152 153 mtl = solparser_get_material(parser, mtl2->front); 154 CHK(mtl->type == SOLPARSER_MATERIAL_MATTE); 155 matte = solparser_get_material_matte(parser, mtl->data.matte); 156 CHK(matte->reflectivity.type == SOLPARSER_MTL_DATA_REAL); 157 CHK(matte->reflectivity.value.real == 0.123); 158 CHK(SOLPARSER_ID_IS_VALID(matte->normal_map) == 1); 159 img = solparser_get_image(parser, matte->normal_map); 160 CHK(strcmp(str_cget(&img->filename), "path to normal map") == 0); 161 162 solparser_entity_iterator_next(&it); 163 CHK(solparser_entity_iterator_eq(&it, &end) == 1); 164 165 CHK(solparser_load(parser) == RES_BAD_OP); 166 fclose(stream); 167 } 168 169 static void 170 test_mirror(struct solparser* parser) 171 { 172 struct solparser_entity_iterator it, end; 173 struct solparser_entity_id entity_id; 174 struct solparser_object_id obj_id; 175 const struct solparser_entity* entity; 176 const struct solparser_image* img; 177 const struct solparser_geometry* geom; 178 const struct solparser_material_double_sided* mtl2; 179 const struct solparser_material* mtl; 180 const struct solparser_material_mirror* mirror; 181 const struct solparser_object* obj; 182 const struct solparser_shape* shape; 183 FILE* stream; 184 185 stream = tmpfile(); 186 CHK(stream != NULL); 187 188 fprintf(stream, "- sun: { dni: 1, spectrum: [{wavelength: 1, data: 1} ] }\n"); 189 fprintf(stream, "- entity: \n"); 190 fprintf(stream, " name: my_entity\n"); 191 fprintf(stream, " primary: 0\n"); 192 fprintf(stream, " geometry: \n"); 193 fprintf(stream, " - cuboid: { size: [1, 1, 1] }\n"); 194 fprintf(stream, " material: \n"); 195 fprintf(stream, " mirror:\n"); 196 fprintf(stream, " reflectivity: 1\n"); 197 fprintf(stream, " slope_error: 0.1\n"); 198 fprintf(stream, " normal_map: { path: Normal map } \n"); 199 rewind(stream); 200 201 CHK(solparser_setup(parser, NULL, stream) == RES_OK); 202 CHK(solparser_load(parser) == RES_OK); 203 204 solparser_entity_iterator_begin(parser, &it); 205 solparser_entity_iterator_end(parser, &end); 206 CHK(solparser_entity_iterator_eq(&it, &end) == 0); 207 208 entity_id = solparser_entity_iterator_get(&it); 209 entity = solparser_get_entity(parser, entity_id); 210 211 CHK(strcmp("my_entity", str_cget(&entity->name)) == 0); 212 CHK(solparser_entity_get_children_count(entity) == 0); 213 CHK(entity->primary == 0); 214 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 215 geom = solparser_get_geometry(parser, entity->data.geometry); 216 CHK(solparser_geometry_get_objects_count(geom) == 1); 217 obj_id = solparser_geometry_get_object(geom, 0); 218 obj = solparser_get_object(parser, obj_id); 219 shape = solparser_get_shape(parser, obj->shape); 220 CHK(shape->type == SOLPARSER_SHAPE_CUBOID); 221 mtl2 = solparser_get_material_double_sided(parser, obj->mtl2); 222 CHK(mtl2->front.i == mtl2->back.i); 223 224 mtl = solparser_get_material(parser, mtl2->front); 225 CHK(mtl->type == SOLPARSER_MATERIAL_MIRROR); 226 mirror = solparser_get_material_mirror(parser, mtl->data.mirror); 227 CHK(mirror->reflectivity.type == SOLPARSER_MTL_DATA_REAL); 228 CHK(mirror->reflectivity.value.real == 1); 229 CHK(mirror->slope_error.type == SOLPARSER_MTL_DATA_REAL); 230 CHK(mirror->slope_error.value.real == 0.1); 231 CHK(SOLPARSER_ID_IS_VALID(mirror->normal_map) == 1); 232 img = solparser_get_image(parser, mirror->normal_map); 233 CHK(strcmp(str_cget(&img->filename), "Normal map") == 0); 234 235 solparser_entity_iterator_next(&it); 236 CHK(solparser_entity_iterator_eq(&it, &end) == 1); 237 238 CHK(solparser_load(parser) == RES_BAD_OP); 239 fclose(stream); 240 } 241 242 static void 243 test_thin_dielectric(struct solparser* parser) 244 { 245 struct solparser_entity_iterator it, end; 246 struct solparser_entity_id entity_id; 247 struct solparser_object_id obj_id; 248 const struct solparser_entity* entity; 249 const struct solparser_image* img; 250 const struct solparser_geometry* geom; 251 const struct solparser_material_double_sided* mtl2; 252 const struct solparser_material* mtl; 253 const struct solparser_material_thin_dielectric* thin; 254 const struct solparser_object* obj; 255 const struct solparser_shape* shape; 256 FILE* stream; 257 258 stream = tmpfile(); 259 CHK(stream != NULL); 260 fprintf(stream, "- sun: { dni: 1, spectrum: [{wavelength: 1, data: 1} ] }\n"); 261 fprintf(stream, "- entity:\n"); 262 fprintf(stream, " name: test\n"); 263 fprintf(stream, " primary: 0\n"); 264 fprintf(stream, " geometry:\n"); 265 fprintf(stream, " - sphere: { radius: 1 }\n"); 266 fprintf(stream, " material:\n"); 267 fprintf(stream, " thin_dielectric:\n"); 268 fprintf(stream, " thickness: 0.1\n"); 269 fprintf(stream, " medium_i:\n"); 270 fprintf(stream, " refractive_index: 1\n"); 271 fprintf(stream, " extinction: 0\n"); 272 fprintf(stream, " medium_t:\n"); 273 fprintf(stream, " refractive_index: 1.5\n"); 274 fprintf(stream, " extinction: 20\n"); 275 fprintf(stream, " normal_map: { path: Bump }\n"); 276 rewind(stream); 277 278 CHK(solparser_setup(parser, NULL, stream) == RES_OK); 279 CHK(solparser_load(parser) == RES_OK); 280 281 solparser_entity_iterator_begin(parser, &it); 282 solparser_entity_iterator_end(parser, &end); 283 CHK(solparser_entity_iterator_eq(&it, &end) == 0); 284 285 entity_id = solparser_entity_iterator_get(&it); 286 entity = solparser_get_entity(parser, entity_id); 287 288 CHK(strcmp("test", str_cget(&entity->name)) == 0); 289 CHK(solparser_entity_get_children_count(entity) == 0); 290 CHK(entity->primary == 0); 291 CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY); 292 geom = solparser_get_geometry(parser, entity->data.geometry); 293 CHK(solparser_geometry_get_objects_count(geom) == 1); 294 obj_id = solparser_geometry_get_object(geom, 0); 295 obj = solparser_get_object(parser, obj_id); 296 shape = solparser_get_shape(parser, obj->shape); 297 CHK(shape->type == SOLPARSER_SHAPE_SPHERE); 298 mtl2 = solparser_get_material_double_sided(parser, obj->mtl2); 299 CHK(mtl2->front.i == mtl2->back.i); 300 301 mtl = solparser_get_material(parser, mtl2->front); 302 CHK(mtl->type == SOLPARSER_MATERIAL_THIN_DIELECTRIC); 303 thin = solparser_get_material_thin_dielectric(parser, mtl->data.thin_dielectric); 304 CHK(thin->thickness == 0.1); 305 CHK(thin->medium_i.i != thin->medium_t.i); 306 CHK(SOLPARSER_ID_IS_VALID(thin->normal_map) == 1); 307 img = solparser_get_image(parser, thin->normal_map); 308 CHK(strcmp(str_cget(&img->filename), "Bump") == 0); 309 310 solparser_entity_iterator_next(&it); 311 CHK(solparser_entity_iterator_eq(&it, &end) == 1); 312 313 CHK(solparser_load(parser) == RES_BAD_OP); 314 fclose(stream); 315 } 316 317 int 318 main(int argc, char** argv) 319 { 320 struct mem_allocator allocator; 321 struct solparser* parser; 322 (void)argc, (void)argv; 323 324 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 325 CHK(solparser_create(&allocator, &parser) == RES_OK); 326 327 test_dielectric(parser); 328 test_matte(parser); 329 test_mirror(parser); 330 test_thin_dielectric(parser); 331 332 solparser_ref_put(parser); 333 334 check_memory_allocator(&allocator); 335 mem_shutdown_proxy_allocator(&allocator); 336 CHK(mem_allocated_size() == 0); 337 return 0; 338 } 339