star-phor-input

File format for describing photoreactor configurations
git clone https://www.edstar.cnrs.fr/git/star-phor-input.git
Log | Files | Refs | README | LICENSE

test_sphin_load_surface.c (8123B)


      1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2024-2026 Clermont Auvergne INP
      3  * Copyright (C) 2024-2026 INSA Lyon
      4  * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux
      5  * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse
      6  * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com)
      7  * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com)
      8  * Copyright (C) 2024-2026 Université de Lorraine
      9  * Copyright (C) 2024-2026 Université Paul Sabatier
     10  * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès
     11  *
     12  * This program is free software: you can redistribute it and/or modify
     13  * it under the terms of the GNU General Public License as published by
     14  * the Free Software Foundation, either version 3 of the License, or
     15  * (at your option) any later version.
     16  *
     17  * This program is distributed in the hope that it will be useful,
     18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     20  * GNU General Public License for more details.
     21  *
     22  * You should have received a copy of the GNU General Public License
     23  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     24 
     25 #include "sphin.h"
     26 
     27 #include <rsys/math.h>
     28 #include <rsys/mem_allocator.h>
     29 #include <rsys/rsys.h>
     30 
     31 #include <stdio.h>
     32 #include <string.h>
     33 
     34 struct sphin;
     35 
     36 static void
     37 write_stl_test_files
     38   (void)
     39 {
     40   FILE* file;
     41   static const char* test0 =
     42     "solid\n"
     43     "  facet normal 0.0 -1.0 0.0\n"
     44     "    outer loop\n"
     45     "      vertex 0.0 0.0 0.0\n"
     46     "      vertex 1.0 0.0 0.0\n"
     47     "      vertex 0.0 0.0 1.0\n"
     48     "    endloop\n"
     49     "  endfacet\n"
     50     "endsolid";
     51   static const char* test1 =
     52     "solid my_solid\n"
     53     "endsolid my_solid\n";
     54   static const char* test2 = /* Bad geometry => sstl should return
     55                                 RES_BAD_ARG */
     56     "solid\n"
     57     "  facet normal 0.0 -1.0 0.0\n"
     58     "    outer loop\n"
     59     "      vertex 0.0 0.0 0.0\n"
     60     "      vertex 1.0 0.0\n"
     61     "      vertex 0.0 0.0 1.0\n"
     62     "    endloop\n"
     63     "  endfacet\n"
     64     "endsolid";
     65   static const char* test3 = /* Noisy geometry => sstl should accept it */
     66     "solid my_solid\n"
     67     "\n"
     68     "  facet normal 0.0 -1.0 0.0\n"
     69     "    outer loop     hophophophophop\n"
     70     "      vertex\t  0.0      0.0 0.0\n"
     71     "      vertex 1.0   0.0 0.0     \taaa\n"
     72     "      vertex 0.0   0.0\t 1.0\n"
     73     "    endloop   \n"
     74     "  endfacet   \t\t\t noise\n"
     75     "endsolid pouet\n";
     76   file = fopen("test_0.stl", "w");
     77   CHK(file != NULL);
     78   fwrite(test0, sizeof(char), strlen(test0), file);
     79   fclose(file);
     80   file = fopen("test_1.stl", "w");
     81   CHK(file != NULL);
     82   fwrite(test1, sizeof(char), strlen(test1), file);
     83   fclose(file);
     84   file = fopen("test_2.stl", "w");
     85   CHK(file != NULL);
     86   fwrite(test2, sizeof(char), strlen(test2), file);
     87   fclose(file);
     88   file = fopen("test_3.stl", "w");
     89   CHK(file != NULL);
     90   fwrite(test3, sizeof(char), strlen(test3), file);
     91   fclose(file);
     92 }
     93 
     94 static void
     95 write_spec_rad_test_files
     96   (void)
     97 {
     98   FILE* file;
     99   static const char* test0 =
    100     "100 1\n"
    101     "200 2\n"
    102     "300 3\n"
    103     "400 4\n";
    104   file = fopen("spectral_property.txt", "w");
    105   CHK(file != NULL);
    106   fwrite(test0, sizeof(char), strlen(test0), file);
    107   fclose(file);
    108 }
    109 
    110 static void
    111 test_surface_api
    112   (struct sphin* sphin)
    113 {
    114   char* name = NULL;
    115   const char* path = "file.txt";
    116   double area, power, response_function;
    117   enum sphin_brdf_direction_distribution dir_dist
    118     = SPHIN_BRDF_DIRECTION_NONE__;
    119   size_t nsurfaces;
    120   struct sphin_brdf* brdf = NULL;
    121   struct sphin_config* config = NULL;
    122   struct sphin_sensor_surface* sensor_surface = NULL;
    123   struct sphin_surface* surface = NULL;
    124   FILE* fp = NULL;
    125 
    126   CHK(fp = fopen(path, "w+"));
    127   fprintf(fp, "#Mot Clé   Nom\n");
    128   fprintf(fp, "\t\t surface : \"tube walls\"\n");
    129   fprintf(fp, "\tbrdf: LAMBERT spectral_property.txt # my comment\n");
    130   fprintf(fp, "\tgeometry: FRONT test_0.stl\n");
    131   fprintf(fp, "\tgeometry: BACK test_3.stl\n");
    132   fprintf(fp, " \n");
    133   fprintf(fp, " # A line with a comment\n");
    134   fprintf(fp, " \n");
    135   fprintf(fp, "surface: \"other_tube walls\"\n");
    136   fprintf(fp, "\tgeometry: FRONT test_0.stl\n");
    137   fprintf(fp, "\tbrdf\t : SPECULAR spectral_property.txt \n");
    138   fprintf(fp, "\tsensor: \n");
    139   fprintf(fp, "\t\tresponse_function: 1\n");
    140   fclose(fp);
    141 
    142   CHK(sphin_load(sphin, path, &config) == RES_OK);
    143   /* Test surface 0 and all possible arg values */
    144   CHK(sphin_config_get_surface_count(NULL, NULL) == RES_BAD_ARG);
    145   CHK(sphin_config_get_surface_count(NULL, &nsurfaces) == RES_BAD_ARG);
    146   CHK(sphin_config_get_surface_count(config, NULL) == RES_BAD_ARG);
    147   CHK(sphin_config_get_surface_count(config, &nsurfaces) == RES_OK);
    148   CHK(nsurfaces == 2);
    149   CHK(sphin_config_get_surface(NULL, 0,NULL) == RES_BAD_ARG);
    150   CHK(sphin_config_get_surface(NULL, 0, &surface) == RES_BAD_ARG);
    151   CHK(sphin_config_get_surface(config, 0, NULL) == RES_BAD_ARG);
    152   CHK(sphin_config_get_surface(config, 10, &surface) == RES_BAD_ARG);
    153   CHK(sphin_config_get_surface(config, (size_t)-1, &surface) == RES_BAD_ARG);
    154   CHK(sphin_config_get_surface(config, 0, &surface) == RES_OK);
    155   CHK(sphin_surface_get_name(NULL, NULL) == RES_BAD_ARG);
    156   CHK(sphin_surface_get_name(surface, NULL) == RES_BAD_ARG);
    157   CHK(sphin_surface_get_name(NULL, &name) == RES_BAD_ARG);
    158   CHK(sphin_surface_get_name(surface, &name) == RES_OK);
    159   CHK(strcmp(name, "tube walls") == 0);
    160 
    161   /* Test surface 1 */
    162   CHK(sphin_config_get_surface(config, 1, &surface) == RES_OK);
    163   CHK(sphin_surface_get_brdf(surface, &brdf) == RES_OK);
    164   CHK(sphin_brdf_get_direction_distribution(brdf, &dir_dist) == RES_OK);
    165   CHK(dir_dist == SPHIN_BRDF_DIRECTION_SPECULAR);
    166   CHK(sphin_surface_source_get_power(surface, &power) == RES_BAD_ARG);
    167   CHK(sphin_surface_compute_total_area(surface, &area) == RES_OK);
    168   CHK(eq_eps(area, 0.5, 1e-15));
    169   CHK(sphin_brdf_ref_get(brdf) == RES_OK);
    170   CHK(sphin_brdf_ref_put(brdf) == RES_OK);
    171   CHK(sphin_surface_get_sensor(NULL, NULL) == RES_BAD_ARG);
    172   CHK(sphin_surface_get_sensor(NULL, &sensor_surface) == RES_BAD_ARG);
    173   CHK(sphin_surface_get_sensor(surface, NULL) == RES_BAD_ARG);
    174   CHK(sphin_surface_get_sensor(surface, &sensor_surface) == RES_OK);
    175   CHK(sphin_sensor_surface_get_response_function(NULL, NULL) == RES_BAD_ARG);
    176   CHK(sphin_sensor_surface_get_response_function
    177       (NULL, &response_function) == RES_BAD_ARG);
    178   CHK(sphin_sensor_surface_get_response_function
    179       (sensor_surface, NULL) == RES_BAD_ARG);
    180   CHK(sphin_sensor_surface_get_response_function
    181       (sensor_surface, &response_function) == RES_OK);
    182   CHK(eq_eps(response_function, 1, 1e-15));
    183   CHK(sphin_surface_ref_get(surface) == RES_OK);
    184   CHK(sphin_surface_ref_get(surface) == RES_OK);
    185   CHK(sphin_surface_ref_put(surface) == RES_OK);
    186   CHK(sphin_config_ref_put(config) == RES_OK);
    187   CHK(surface != NULL);
    188 
    189   CHK(sphin_surface_ref_put(surface) == RES_OK);
    190 }
    191 
    192 static void
    193 test_surface_api_bad_name
    194   (struct sphin* sphin)
    195 {
    196   struct sphin_config* config = NULL;
    197   const char* path = "file.txt";
    198   FILE* fp = NULL;
    199 
    200   CHK(fp = fopen(path, "w+"));
    201   fprintf(fp, "#Mot Clé   Nom\n");
    202   fprintf(fp, " surface\t: \"tube_walls\" blabla\n");
    203   fclose(fp);
    204 
    205   CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG);
    206 }
    207 
    208 static void
    209 test_surface_api_bad_geometry
    210   (struct sphin* sphin)
    211 {
    212   struct sphin_config* config = NULL;
    213   const char* path = "file.txt";
    214   FILE* fp = NULL;
    215 
    216   CHK(fp = fopen(path, "w+"));
    217   fprintf(fp, "#Mot Clé   Nom\n");
    218   fprintf(fp, " surface\t: \"tube_walls\"\n");
    219   fprintf(fp, "\tgeometry: BACK test_2.stl\n");
    220   fclose(fp);
    221 
    222   CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG);
    223 }
    224 
    225 int
    226 main(int argc, char** argv)
    227 {
    228   struct sphin* sphin = NULL;
    229   struct sphin_create_args args = SPHIN_CREATE_ARGS_DEFAULT;
    230 
    231   (void)argc;
    232   (void)argv;
    233 
    234   args.verbose = 1;
    235   sphin_create(&args, &sphin);
    236 
    237   write_stl_test_files();
    238   write_spec_rad_test_files();
    239   test_surface_api(sphin);
    240   test_surface_api_bad_name(sphin);
    241   test_surface_api_bad_geometry(sphin);
    242 
    243   CHK(sphin_ref_put(sphin) == RES_OK);
    244 
    245   CHK(mem_allocated_size() == 0);
    246   return 0;
    247 }