sbb.h (5039B)
1 /* Copyright (C) 2018-2023 |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 SBB_H 17 #define SBB_H 18 19 #include <rsys/rsys.h> 20 21 /* Library symbol management */ 22 #if defined(SBB_SHARED_BUILD) /* Build shared library */ 23 #define SBB_API extern EXPORT_SYM 24 #elif defined(SBB_STATIC) /* Use/build static library */ 25 #define SBB_API extern LOCAL_SYM 26 #else /* Use shared library */ 27 #define SBB_API extern IMPORT_SYM 28 #endif 29 30 /* Helper macro that asserts if the invocation of the sbb function 31 * `Func' returns an error. One should use this macro on smc function 32 * calls for which no explicit error checking is performed */ 33 #ifndef NDEBUG 34 #define SBB(Func) ASSERT(sbb_ ## Func == RES_OK) 35 #else 36 #define SBB(Func) sbb_ ## Func 37 #endif 38 39 /* Forward declarations of opaque types */ 40 struct sbb_ran_planck; 41 42 /* Forward declarations of external types */ 43 struct logger; 44 struct mem_allocator; 45 46 struct sbb_ran_planck_create_args { 47 struct mem_allocator* allocator; /* NULL <=> use default allocator */ 48 struct logger* logger; /* NULL <=> user default logger */ 49 double range[2]; /* Spectral range [m] */ 50 double ref_temperature; /* Reference temperature [K] */ 51 52 /* Indication of the number of bands used to discretize the CDF. These bands 53 * are only used to speed up sampling and do not introduce any approximation: 54 * once a band is sampled, the wavelength in the band is sampled continuously. 55 * To disable this feature, set nbands to 0 */ 56 size_t nbands; 57 int verbose; /* Verbosity level */ 58 }; 59 #define SBB_RAN_PLANCK_CREATE_ARGS_DEFAULT__ {\ 60 NULL, /* Allocator */\ 61 NULL, /* Logger */\ 62 {9.6e-6, 11.5e-6}, /* Spectral range [m] */\ 63 300, /* Reference temperature [K] */\ 64 1900, /* #bands used to speed up sampling, actually 1 band per nanometers */\ 65 0 /* Verbosity */\ 66 } 67 static const struct sbb_ran_planck_create_args 68 SBB_RAN_PLANCK_CREATE_ARGS_DEFAULT = SBB_RAN_PLANCK_CREATE_ARGS_DEFAULT__; 69 70 BEGIN_DECLS 71 72 /******************************************************************************* 73 * Blackbody API 74 ******************************************************************************/ 75 SBB_API double 76 sbb_blackbody_fraction 77 (const double lambda0, /* [m] */ 78 const double lambda1, /* [m] */ 79 const double temperature); /* [K] */ 80 81 /* Return the Planck value at a given wavelength */ 82 SBB_API double /* [W/m^2/sr/m] */ 83 sbb_planck_monochromatic 84 (const double lambda, /* [m] */ 85 const double temperature); /* [K] */ 86 87 /* Return the average Planck value over the [lambda_min, lambda_max] interval */ 88 SBB_API double /* [W/m^2/sr/m] */ 89 sbb_planck_interval 90 (const double lambda_min, /* [m] */ 91 const double lambda_max, /* [m] */ 92 const double temperature); /* [K] */ 93 94 /* Invoke planck_monochromatic or planck_interval whether the submitted 95 * interval is null or not, respectively */ 96 static INLINE double /* [W/m^2/sr/m] */ 97 sbb_planck 98 (const double lambda_min, /* [m] */ 99 const double lambda_max, /* [m] */ 100 const double temperature) /* [K] */ 101 { 102 ASSERT(lambda_min >= 0 && lambda_max >= 0); 103 ASSERT(lambda_min <= lambda_max); 104 ASSERT(temperature >0); 105 if(lambda_min == lambda_max) { 106 return sbb_planck_monochromatic(lambda_min, temperature); 107 } else { 108 return sbb_planck_interval(lambda_min, lambda_max, temperature); 109 } 110 } 111 112 SBB_API res_T 113 sbb_brightness_temperature 114 (const double lambda_min, /* [m] */ 115 const double lambda_max, /* [m] */ 116 const double radiance, /* Averaged over the spectral range [W/m^2/sr/m] */ 117 double* temperature); /* [K] */ 118 119 SBB_API res_T 120 sbb_radiance_temperature 121 (const double lambda_min, /* [m] */ 122 const double lambda_max, /* [m] */ 123 const double radiance, /* [W/m^2/sr] */ 124 double* temperature); /* [K] */ 125 126 /******************************************************************************* 127 * Planck distribution 128 ******************************************************************************/ 129 SBB_API res_T 130 sbb_ran_planck_create 131 (struct sbb_ran_planck_create_args* args, 132 struct sbb_ran_planck** planck); 133 134 SBB_API res_T 135 sbb_ran_planck_ref_get 136 (struct sbb_ran_planck* planck); 137 138 SBB_API res_T 139 sbb_ran_planck_ref_put 140 (struct sbb_ran_planck* planck); 141 142 SBB_API double /* Wavelength [m] */ 143 sbb_ran_planck_sample 144 (struct sbb_ran_planck* planck, 145 const double r0, /* Random number in [0, 1[ */ 146 const double r1, /* Random number in [0, 1[ */ 147 double* pdf); /* [m^-1]. May be NULL */ 148 149 END_DECLS 150 151 #endif /* SBB_H */