test_sgf_estimator.c (8603B)
1 /* Copyright (C) 2021, 2024 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2015-2018 EDF S.A., France (syrthes-support@edf.fr) 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 "sgf.h" 18 #include "test_sgf_utils.h" 19 20 #include <rsys/stretchy_array.h> 21 22 #include <star/s3d.h> 23 #include <star/ssp.h> 24 25 #define NSTEPS 10000L 26 27 static const float vertices[] = { 28 0.f, 0.f, 0.f, 29 1.f, 0.f, 0.f, 30 0.f, 1.f, 0.f, 31 1.f, 1.f, 0.f, 32 0.f, 0.f, 1.f, 33 1.f, 0.f, 1.f, 34 0.f, 1.f, 1.f, 35 1.f, 1.f, 1.f 36 }; 37 static const size_t nvertices = sizeof(vertices) / (3*sizeof(float)); 38 39 /* Front faces are CW. The normals point into the cube */ 40 static const unsigned indices[] = { 41 0, 2, 1, 1, 2, 3, /* Front */ 42 0, 4, 2, 2, 4, 6, /* Left */ 43 4, 5, 6, 6, 5, 7, /* Back */ 44 3, 7, 1, 1, 7, 5, /* Right */ 45 2, 6, 3, 3, 6, 7, /* Top */ 46 0, 1, 4, 4, 1, 5 /* Bottom */ 47 }; 48 static const size_t nprims = sizeof(indices) / (3*sizeof(unsigned)); 49 50 static const double emissivity[] = { 51 0.6, 0.6, /* Front */ 52 0.8, 0.8, /* Left */ 53 0.9, 0.9, /* Back */ 54 0.7, 0.7, /* Right */ 55 0.3, 0.3, /* Top */ 56 0.4, 0.4 /* Bottom */ 57 }; 58 59 static const double specularity[] = { 60 0.0, 0.0, /* Front */ 61 0.0, 0.0, /* Left */ 62 0.0, 0.0, /* Back */ 63 0.0, 0.0, /* Right */ 64 0.0, 0.0, /* Top */ 65 0.0, 0.0 /* Bottom */ 66 }; 67 68 int 69 main(int argc, char** argv) 70 { 71 struct mem_allocator allocator; 72 struct shape_context shape; 73 struct sgf_device* sgf = NULL; 74 struct sgf_estimator* estimator = NULL; 75 struct sgf_scene_desc desc = SGF_SCENE_DESC_NULL; 76 struct sgf_scene* scn; 77 struct sgf_status status; 78 struct ssp_rng* rng = NULL; 79 (void)argc, (void)argv; 80 81 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 82 83 CHK(ssp_rng_create(&allocator, SSP_RNG_THREEFRY, &rng) == RES_OK); 84 CHK(sgf_device_create(NULL, &allocator, 1, &sgf) == RES_OK); 85 CHK(sgf_scene_create(sgf, &scn) == RES_OK); 86 87 shape.vertices = vertices; 88 shape.nvertices = nvertices; 89 shape.indices = indices; 90 shape.nprimitives = nprims; 91 shape.emissivity = emissivity; 92 shape.specularity = specularity; 93 shape.dim = 3; 94 95 desc.get_position = get_position; 96 desc.get_indices = get_indices; 97 desc.get_emissivity = get_emissivity; 98 desc.get_specularity = get_specularity; 99 desc.get_reflectivity = get_reflectivity; 100 desc.nprims = (unsigned)nprims; 101 desc.nverts = (unsigned)nvertices; 102 desc.nbands = 1; 103 desc.context = &shape; 104 105 CHK(sgf_scene_setup_3d(scn, &desc) == RES_OK); 106 CHK(sgf_scene_begin_integration(scn) == RES_OK); 107 108 CHK(sgf_integrate(NULL, SIZE_MAX, NULL, 0, NULL) == RES_BAD_ARG); 109 CHK(sgf_integrate(scn, SIZE_MAX, NULL, 0, NULL) == RES_BAD_ARG); 110 CHK(sgf_integrate(NULL, 0, NULL, 0, NULL) == RES_BAD_ARG); 111 CHK(sgf_integrate(scn, 0, NULL, 0, NULL) == RES_BAD_ARG); 112 CHK(sgf_integrate(NULL, SIZE_MAX, rng, 0, NULL) == RES_BAD_ARG); 113 CHK(sgf_integrate(scn, SIZE_MAX, rng, 0, NULL) == RES_BAD_ARG); 114 CHK(sgf_integrate(NULL, 0, rng, 0, NULL) == RES_BAD_ARG); 115 CHK(sgf_integrate(scn, 0, rng, 0, NULL) == RES_BAD_ARG); 116 CHK(sgf_integrate(NULL, SIZE_MAX, NULL, NSTEPS, NULL) == RES_BAD_ARG); 117 CHK(sgf_integrate(scn, SIZE_MAX, NULL, NSTEPS, NULL) == RES_BAD_ARG); 118 CHK(sgf_integrate(NULL, 0, NULL, NSTEPS, NULL) == RES_BAD_ARG); 119 CHK(sgf_integrate(scn, 0, NULL, NSTEPS, NULL) == RES_BAD_ARG); 120 CHK(sgf_integrate(NULL, SIZE_MAX, rng, NSTEPS, NULL) == RES_BAD_ARG); 121 CHK(sgf_integrate(scn, SIZE_MAX, rng, NSTEPS, NULL) == RES_BAD_ARG); 122 CHK(sgf_integrate(NULL, 0, rng, NSTEPS, NULL) == RES_BAD_ARG); 123 CHK(sgf_integrate(scn, 0, rng, NSTEPS, NULL) == RES_BAD_ARG); 124 CHK(sgf_integrate(NULL, SIZE_MAX, NULL, 0, &estimator) == RES_BAD_ARG); 125 CHK(sgf_integrate(scn, SIZE_MAX, NULL, 0, &estimator) == RES_BAD_ARG); 126 CHK(sgf_integrate(NULL, 0, NULL, 0, &estimator) == RES_BAD_ARG); 127 CHK(sgf_integrate(scn, 0, NULL, 0, &estimator) == RES_BAD_ARG); 128 CHK(sgf_integrate(NULL, SIZE_MAX, rng, 0, &estimator) == RES_BAD_ARG); 129 CHK(sgf_integrate(scn, SIZE_MAX, rng, 0, &estimator) == RES_BAD_ARG); 130 CHK(sgf_integrate(NULL, 0, rng, 0, &estimator) == RES_BAD_ARG); 131 CHK(sgf_integrate(scn, 0, rng, 0, &estimator) == RES_BAD_ARG); 132 CHK(sgf_integrate(NULL, SIZE_MAX, NULL, NSTEPS, &estimator) == RES_BAD_ARG); 133 CHK(sgf_integrate(scn, SIZE_MAX, NULL, NSTEPS, &estimator) == RES_BAD_ARG); 134 CHK(sgf_integrate(NULL, 0, NULL, NSTEPS, &estimator) == RES_BAD_ARG); 135 CHK(sgf_integrate(scn, 0, NULL, NSTEPS, &estimator) == RES_BAD_ARG); 136 CHK(sgf_integrate(NULL, SIZE_MAX, rng, NSTEPS, &estimator) == RES_BAD_ARG); 137 CHK(sgf_integrate(scn, SIZE_MAX, rng, NSTEPS, &estimator) == RES_BAD_ARG); 138 CHK(sgf_integrate(NULL, 0, rng, NSTEPS, &estimator) == RES_BAD_ARG); 139 CHK(sgf_integrate(scn, 0, rng, NSTEPS, &estimator) == RES_OK); 140 141 CHK(sgf_estimator_get_status(NULL, SIZE_MAX, SIZE_MAX, NULL) == RES_BAD_ARG); 142 CHK(sgf_estimator_get_status(estimator, SIZE_MAX, SIZE_MAX, NULL) == RES_BAD_ARG); 143 CHK(sgf_estimator_get_status(NULL, 0, SIZE_MAX, NULL) == RES_BAD_ARG); 144 CHK(sgf_estimator_get_status(estimator, 0, SIZE_MAX, NULL) == RES_BAD_ARG); 145 CHK(sgf_estimator_get_status(NULL, SIZE_MAX, SIZE_MAX, &status) == RES_BAD_ARG); 146 CHK(sgf_estimator_get_status(estimator, SIZE_MAX, SIZE_MAX, &status) == RES_BAD_ARG); 147 CHK(sgf_estimator_get_status(NULL, 0, SIZE_MAX, &status) == RES_BAD_ARG); 148 CHK(sgf_estimator_get_status(estimator, 0, SIZE_MAX, &status) == RES_BAD_ARG); 149 CHK(sgf_estimator_get_status(NULL, SIZE_MAX, 0, NULL) == RES_BAD_ARG); 150 CHK(sgf_estimator_get_status(estimator, SIZE_MAX, 0, NULL) == RES_BAD_ARG); 151 CHK(sgf_estimator_get_status(NULL, 0, 0, NULL) == RES_BAD_ARG); 152 CHK(sgf_estimator_get_status(estimator, 0, 0, NULL) == RES_BAD_ARG); 153 CHK(sgf_estimator_get_status(NULL, SIZE_MAX, 0, &status) == RES_BAD_ARG); 154 CHK(sgf_estimator_get_status(estimator, SIZE_MAX, 0, &status) == RES_BAD_ARG); 155 CHK(sgf_estimator_get_status(NULL, 0, 0, &status) == RES_BAD_ARG); 156 CHK(sgf_estimator_get_status(estimator, 0, 0, &status) == RES_OK); 157 CHK(status.nsteps == NSTEPS); 158 159 CHK(sgf_estimator_get_status_infinity(NULL, SIZE_MAX, NULL) == RES_BAD_ARG); 160 CHK(sgf_estimator_get_status_infinity(estimator, SIZE_MAX, NULL) == RES_BAD_ARG); 161 CHK(sgf_estimator_get_status_infinity(NULL, 0, NULL) == RES_BAD_ARG); 162 CHK(sgf_estimator_get_status_infinity(estimator, 0, NULL) == RES_BAD_ARG); 163 CHK(sgf_estimator_get_status_infinity(NULL, SIZE_MAX, &status) == RES_BAD_ARG); 164 CHK(sgf_estimator_get_status_infinity(estimator, SIZE_MAX, &status) == RES_BAD_ARG); 165 CHK(sgf_estimator_get_status_infinity(NULL, 0, &status) == RES_BAD_ARG); 166 CHK(sgf_estimator_get_status_infinity(estimator, 0, &status) == RES_OK); 167 CHK(eq_eps(status.E, 0, status.SE) == 1); 168 CHK(status.nsteps == NSTEPS); 169 170 CHK(sgf_estimator_get_status_medium(NULL, SIZE_MAX, NULL) == RES_BAD_ARG); 171 CHK(sgf_estimator_get_status_medium(estimator, SIZE_MAX, NULL) == RES_BAD_ARG); 172 CHK(sgf_estimator_get_status_medium(NULL, 0, NULL) == RES_BAD_ARG); 173 CHK(sgf_estimator_get_status_medium(estimator, 0, NULL) == RES_BAD_ARG); 174 CHK(sgf_estimator_get_status_medium(NULL, SIZE_MAX, &status) == RES_BAD_ARG); 175 CHK(sgf_estimator_get_status_medium(estimator, SIZE_MAX,&status) == RES_BAD_ARG); 176 CHK(sgf_estimator_get_status_medium(NULL, 0, &status) == RES_BAD_ARG); 177 CHK(sgf_estimator_get_status_medium(estimator, 0, &status) == RES_OK); 178 CHK(status.E == 0); 179 CHK(status.V == 0); 180 CHK(status.SE == 0); 181 CHK(status.nsteps == NSTEPS); 182 183 CHK(sgf_estimator_ref_get(NULL) == RES_BAD_ARG); 184 CHK(sgf_estimator_ref_get(estimator) == RES_OK); 185 CHK(sgf_estimator_ref_put(NULL) == RES_BAD_ARG); 186 CHK(sgf_estimator_ref_put(estimator) == RES_OK); 187 CHK(sgf_estimator_ref_put(estimator) == RES_OK); 188 189 CHK(sgf_scene_end_integration(scn) == RES_OK); 190 CHK(sgf_integrate(scn, 0, rng, NSTEPS, &estimator) == RES_BAD_OP); 191 192 CHK(ssp_rng_ref_put(rng) == RES_OK); 193 CHK(sgf_device_ref_put(sgf) == RES_OK); 194 CHK(sgf_scene_ref_put(scn) == RES_OK); 195 196 check_memory_allocator(&allocator); 197 mem_shutdown_proxy_allocator(&allocator); 198 CHK(mem_allocated_size() == 0); 199 return 0; 200 } 201