test_ssp_ran_spherical_zone.h (5029B)
1 /* Copyright (C) 2015-2025 |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 #ifndef TEST_SSP_RAN_SPHERICAL_ZONE_H 17 #define TEST_SSP_RAN_SPHERICAL_ZONE_H 18 19 #include "ssp.h" 20 #include "test_ssp_utils.h" 21 22 #define NSAMPS 1024 23 24 #endif /* TEST_SSP_RAN_SPHERE_H */ 25 26 #if TYPE_FLOAT==0 27 #define REAL double 28 #define TEST test_double 29 #define RAN_SPHERICAL_ZONE_UNIFORM_LOCAL ssp_ran_spherical_zone_uniform_local 30 #define RAN_SPHERICAL_ZONE_UNIFORM_PDF ssp_ran_spherical_zone_uniform_pdf 31 #define RAN_SPHERICAL_ZONE_UNIFORM ssp_ran_spherical_zone_uniform 32 #define RAN_SPHERE_UNIFORM ssp_ran_sphere_uniform 33 #define EQ_EPS eq_eps 34 #define R3_EQ_EPS d3_eq_eps 35 #define R3_IS_NORMALIZED d3_is_normalized 36 #define R3_NORMALIZE d3_normalize 37 #define R3_DOT d3_dot 38 #elif TYPE_FLOAT==1 39 #define REAL float 40 #define TEST test_float 41 #define RAN_SPHERICAL_ZONE_UNIFORM_LOCAL ssp_ran_spherical_zone_uniform_float_local 42 #define RAN_SPHERICAL_ZONE_UNIFORM_PDF ssp_ran_spherical_zone_uniform_float_pdf 43 #define RAN_SPHERICAL_ZONE_UNIFORM ssp_ran_spherical_zone_uniform_float 44 #define RAN_SPHERE_UNIFORM ssp_ran_sphere_uniform_float 45 #define EQ_EPS eq_epsf 46 #define R3_EQ_EPS f3_eq_eps 47 #define R3_IS_NORMALIZED f3_is_normalized 48 #define R3_NORMALIZE f3_normalize 49 #define R3_DOT f3_dot 50 #else 51 #error "TYPE_FLOAT must be defined either 0 or 1" 52 #endif 53 54 static void 55 TEST(void) 56 { 57 struct ssp_rng* rng; 58 struct mem_allocator allocator; 59 REAL pdf; 60 REAL samps[NSAMPS][3]; 61 REAL up[3]; 62 REAL heights[2]; 63 int i, j; 64 65 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 66 CHK(ssp_rng_create(&allocator, SSP_RNG_MT19937_64, &rng) == RES_OK); 67 68 heights[0] = heights[1] = 1; 69 CHK(RAN_SPHERICAL_ZONE_UNIFORM_LOCAL(rng, heights, samps[0], &pdf) == samps[0]); 70 CHK(samps[0][0] == 0); 71 CHK(samps[0][1] == 0); 72 CHK(samps[0][2] == 1); 73 CHK(IS_INF(pdf)); 74 75 heights[0] = -1; 76 CHK(RAN_SPHERICAL_ZONE_UNIFORM_LOCAL(rng, heights, samps[0], &pdf) == samps[0]); 77 CHK(EQ_EPS(pdf, 1/(4*(REAL)PI), (REAL)1.e-6)); 78 79 heights[0] = heights[1] = (REAL)0.4; 80 CHK(RAN_SPHERICAL_ZONE_UNIFORM_LOCAL(rng, heights, samps[0], &pdf) == samps[0]); 81 CHK(EQ_EPS(pdf, 1/(2*(REAL)PI), (REAL)1.e-6)); 82 83 /* Check NULL pdf */ 84 CHK(RAN_SPHERICAL_ZONE_UNIFORM_LOCAL(rng, heights, samps[0], NULL) == samps[0]); 85 86 FOR_EACH(i, 0, NSAMPS) { 87 heights[0] = (REAL)-0.7; heights[1] = 1; 88 CHK(RAN_SPHERICAL_ZONE_UNIFORM_LOCAL(rng, heights, samps[i], &pdf) == samps[i]); 89 CHK(R3_IS_NORMALIZED(samps[i])); 90 CHK(EQ_EPS(pdf, 1/(2*(REAL)PI*(heights[1]-heights[0])), (REAL)1.e-6)); 91 CHK(EQ_EPS(pdf, RAN_SPHERICAL_ZONE_UNIFORM_PDF(heights), (REAL)1.e-6)); 92 CHK(samps[i][2] >= heights[0] && samps[i][2] <= heights[1]); 93 FOR_EACH(j, 0, i) { 94 CHK(!R3_EQ_EPS(samps[j], samps[i], (REAL)1.e-6)); 95 } 96 } 97 98 /* Sample an up vector */ 99 RAN_SPHERE_UNIFORM(rng, up, NULL); 100 101 heights[0] = heights[1] = 1; 102 CHK(RAN_SPHERICAL_ZONE_UNIFORM(rng, up, heights, samps[0], &pdf) == samps[0]); 103 CHK(R3_EQ_EPS(samps[0], up, (REAL)1.e-6)); 104 CHK(IS_INF(pdf)); 105 106 heights[0] = -1; 107 CHK(RAN_SPHERICAL_ZONE_UNIFORM(rng, up, heights, samps[0], &pdf) == samps[0]); 108 CHK(EQ_EPS(pdf, 1/(4*(REAL)PI), (REAL)1.e-6)); 109 110 heights[0] = heights[1] = (REAL)0.9; 111 CHK(RAN_SPHERICAL_ZONE_UNIFORM(rng, up, heights, samps[0], &pdf) == samps[0]); 112 CHK(EQ_EPS(pdf, 1/(2*(REAL)PI), (REAL)1.e-6)); 113 114 /* Check NULL pdf */ 115 CHK(RAN_SPHERICAL_ZONE_UNIFORM(rng, up, heights, samps[0], NULL) == samps[0]); 116 117 FOR_EACH(i, 0, NSAMPS) { 118 heights[0] = (REAL)0.3; heights[1] = 1; 119 CHK(RAN_SPHERICAL_ZONE_UNIFORM(rng, up, heights, samps[i], &pdf) == samps[i]); 120 CHK(R3_IS_NORMALIZED(samps[i])); 121 CHK(EQ_EPS(pdf, 1/(2*(REAL)PI*(heights[1]-heights[0])), (REAL)1.e-6)); 122 CHK(EQ_EPS(pdf, RAN_SPHERICAL_ZONE_UNIFORM_PDF(heights), (REAL)1.e-6)); 123 CHK(R3_DOT(up, samps[i]) >= heights[0] && R3_DOT(up, samps[i]) <= heights[1]); 124 FOR_EACH(j, 0, i) { 125 CHK(!R3_EQ_EPS(samps[j], samps[i], (REAL)1.e-6)); 126 } 127 } 128 129 ssp_rng_ref_put(rng); 130 check_memory_allocator(&allocator); 131 mem_shutdown_proxy_allocator(&allocator); 132 133 CHK(mem_allocated_size() == 0); 134 } 135 136 #undef REAL 137 #undef TEST 138 #undef RAN_SPHERICAL_ZONE_UNIFORM_LOCAL 139 #undef RAN_SPHERICAL_ZONE_UNIFORM_PDF 140 #undef RAN_SPHERICAL_ZONE_UNIFORM 141 #undef RAN_SPHERE_UNIFORM 142 #undef EQ_EPS 143 #undef R3_EQ_EPS 144 #undef R3_IS_NORMALIZED 145 #undef R3_NORMALIZE 146 #undef R3_DOT 147 #undef TYPE_FLOAT