commit bd52e9b29762aaf79b1262e7ceb9fafad65db3a1
parent 4f403b04ecbac79a0fae222e0051812ed0b6ca3b
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 27 Jul 2018 16:52:26 +0200
Add the sample_sw_spectral_interval_CIE<X|Y|Z> functions
Diffstat:
| M | src/htgop.c | | | 83 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | src/htgop.h | | | 26 | ++++++++++++++++++++++---- |
2 files changed, 105 insertions(+), 4 deletions(-)
diff --git a/src/htgop.c b/src/htgop.c
@@ -17,6 +17,7 @@
#include "htgop_c.h"
#include "htgop_reader.h"
+#include <rsys/algorithm.h>
#include <rsys/cstr.h>
#include <rsys/logger.h>
#include <rsys/mem_allocator.h>
@@ -49,6 +50,14 @@ log_msg
}
}
+static INLINE int
+cmp_dbl(const void* a, const void* b)
+{
+ const double d0 = *((const double*)a);
+ const double d1 = *((const double*)b);
+ return d0 < d1 ? -1 : (d0 > d1 ? 1 : 0);
+}
+
static FINLINE double
trapezoidal_integration
(const double lambda_lo, /* Integral lower bound. In nanometer */
@@ -221,6 +230,41 @@ error:
goto exit;
}
+static INLINE res_T
+sample_sw_spectral_interval_CIE
+ (const struct htgop* htgop,
+ const char* caller,
+ const double* cdf,
+ const size_t cdf_length,
+ const double r, /* Canonical number in [0, 1[ */
+ size_t* id)
+{
+ double* find;
+ size_t i;
+ ASSERT(htgop && cdf && cdf_length);
+
+ if(!id) return RES_BAD_ARG;
+ if(r < 0 || r >= 1) {
+ log_err(htgop, "%s: invalid canonical random number `%g'.\n", caller, r);
+ return RES_BAD_ARG;
+ }
+
+ find = search_lower_bound(&r, &cdf, cdf_length, sizeof(double), cmp_dbl);
+ if(!find) {
+ log_err(htgop,
+ "%s: could not sample a spectral interval in the CIE XYZ interval.\n",
+ caller);
+ return RES_BAD_OP;
+ }
+
+ ASSERT(find);
+ i = (size_t)(find - cdf);
+ ASSERT(i < cdf_length && cdf[i] > r && (!i || cdf[i-1] < r));
+ *id = i;
+ return RES_OK;
+}
+
+
static res_T
parse_spectral_intervals
(struct htgop* htgop,
@@ -780,6 +824,45 @@ htgop_get_sw_spectral_interval
return RES_OK;
}
+res_T
+htgop_sample_sw_spectral_interval_CIE_X
+ (const struct htgop* htgop,
+ const double r, /* Canonical number in [0, 1[ */
+ size_t* ispecint)
+{
+ if(!htgop) return RES_BAD_ARG;
+ return sample_sw_spectral_interval_CIE(htgop, FUNC_NAME,
+ darray_double_cdata_get(&htgop->sw_X_cdf),
+ darray_double_size_get(&htgop->sw_X_cdf),
+ r, ispecint);
+}
+
+res_T
+htgop_sample_sw_spectral_interval_CIE_Y
+ (const struct htgop* htgop,
+ const double r, /* Canonical number in [0, 1[ */
+ size_t* ispecint)
+{
+ if(!htgop) return RES_BAD_ARG;
+ return sample_sw_spectral_interval_CIE(htgop, FUNC_NAME,
+ darray_double_cdata_get(&htgop->sw_Y_cdf),
+ darray_double_size_get(&htgop->sw_Y_cdf),
+ r, ispecint);
+}
+
+res_T
+htgop_sample_sw_spectral_interval_CIE_Z
+ (const struct htgop* htgop,
+ const double r, /* Canonical number in [0, 1[ */
+ size_t* ispecint)
+{
+ if(!htgop) return RES_BAD_ARG;
+ return sample_sw_spectral_interval_CIE(htgop, FUNC_NAME,
+ darray_double_cdata_get(&htgop->sw_Y_cdf),
+ darray_double_size_get(&htgop->sw_Y_cdf),
+ r, ispecint);
+}
+
/*******************************************************************************
* Local functions
******************************************************************************/
diff --git a/src/htgop.h b/src/htgop.h
@@ -50,8 +50,8 @@ struct htgop_ground {
};
struct htgop_spectral_interval {
- double wave_numbers[2]; /* Lower/Upper wave number of the interval */
- const double* quadrature; /* Weights of the spectral interval quadrature */
+ double wave_numbers[2]; /* Lower/Upper wave number of the interval in cm^-1 */
+ const double* quadrature; /* Weights of the quadrature points */
size_t quadrature_length; /* #quadrature points of the interval */
};
@@ -83,14 +83,14 @@ struct htgop_layer_lw_spectral_interval_tab {
struct htgop_layer_sw_spectral_interval {
const double* ka_nominal; /* Per quadrature point absorption coef */
- const double* ks_nominal; /* Per quadrature point scatteriing coef */
+ const double* ks_nominal; /* Per quadrature point scattering coef */
size_t quadrature_length;
const void* data__; /* Internal data */
};
struct htgop_layer_sw_spectral_interval_tab {
const double* ka_tab; /* Tabulated absorption coef */
- const double* ks_tab; /* Tabulated scatteriing coef */
+ const double* ks_tab; /* Tabulated scattering coef */
size_t tab_length;
};
@@ -162,6 +162,24 @@ htgop_get_sw_spectral_intervals_count
size_t* nspecints);
HTGOP_API res_T
+htgop_sample_sw_spectral_interval_CIE_X
+ (const struct htgop* htgop,
+ const double r, /* Canonical random number in [0, 1[ */
+ size_t* ispecint); /* Id of the sampled interval */
+
+HTGOP_API res_T
+htgop_sample_sw_spectral_interval_CIE_Y
+ (const struct htgop* htgop,
+ const double r, /* Canonical random number in [0, 1[ */
+ size_t* ispecint); /* Id of the sampled interval */
+
+HTGOP_API res_T
+htgop_sample_sw_spectral_interval_CIE_Z
+ (const struct htgop* htgop,
+ const double r, /* Canonical random number in [0, 1[ */
+ size_t* ispecint); /* Id of the sampled interval */
+
+HTGOP_API res_T
htgop_get_lw_spectral_intervals_wave_numbers
(const struct htgop* htgop,
const double* wave_numbers[]);