Fixed possible out-of-bounds panic in XYZ -> Spectrum code.

This commit is contained in:
Nathan Vegdahl 2016-07-04 01:26:28 -07:00
parent e56ac418da
commit a8324669cc
2 changed files with 10 additions and 0 deletions

View File

@ -79,6 +79,11 @@ pub fn spectrum_xyz_to_p(lambda: f32, xyz: (f32, f32, f32)) -> f32
let idx = &cell.idx; let idx = &cell.idx;
let num: i32 = cell.num_points; let num: i32 = cell.num_points;
// if the cell has no points, nothing we can do, so return 0
if num == 0 {
return 0.0;
}
// get linearly interpolated spectral power for the corner vertices: // get linearly interpolated spectral power for the corner vertices:
// this clamping is only necessary if lambda is not sure to be >= SPECTRUM_SAMPLE_MIN and <= SPECTRUM_SAMPLE_MAX: // this clamping is only necessary if lambda is not sure to be >= SPECTRUM_SAMPLE_MIN and <= SPECTRUM_SAMPLE_MAX:
let sb: f32 = /*(SPECTRUM_NUM_SAMPLES as f32 - 1e-4).min(0.0.max(*/ (lambda - SPECTRUM_SAMPLE_MIN) / (SPECTRUM_SAMPLE_MAX - SPECTRUM_SAMPLE_MIN) * (SPECTRUM_NUM_SAMPLES as f32 - 1.0)/*))*/; let sb: f32 = /*(SPECTRUM_NUM_SAMPLES as f32 - 1e-4).min(0.0.max(*/ (lambda - SPECTRUM_SAMPLE_MIN) / (SPECTRUM_SAMPLE_MAX - SPECTRUM_SAMPLE_MIN) * (SPECTRUM_NUM_SAMPLES as f32 - 1.0)/*))*/;

View File

@ -45,6 +45,11 @@ pub fn spectrum_xyz_to_p(lambda: f32, xyz: (f32, f32, f32)) -> f32
let idx = &cell.idx; let idx = &cell.idx;
let num: i32 = cell.num_points; let num: i32 = cell.num_points;
// if the cell has no points, nothing we can do, so return 0
if num == 0 {
return 0.0;
}
// get linearly interpolated spectral power for the corner vertices: // get linearly interpolated spectral power for the corner vertices:
// this clamping is only necessary if lambda is not sure to be >= SPECTRUM_SAMPLE_MIN and <= SPECTRUM_SAMPLE_MAX: // this clamping is only necessary if lambda is not sure to be >= SPECTRUM_SAMPLE_MIN and <= SPECTRUM_SAMPLE_MAX:
let sb: f32 = /*(SPECTRUM_NUM_SAMPLES as f32 - 1e-4).min(0.0.max(*/ (lambda - SPECTRUM_SAMPLE_MIN) / (SPECTRUM_SAMPLE_MAX - SPECTRUM_SAMPLE_MIN) * (SPECTRUM_NUM_SAMPLES as f32 - 1.0)/*))*/; let sb: f32 = /*(SPECTRUM_NUM_SAMPLES as f32 - 1e-4).min(0.0.max(*/ (lambda - SPECTRUM_SAMPLE_MIN) / (SPECTRUM_SAMPLE_MAX - SPECTRUM_SAMPLE_MIN) * (SPECTRUM_NUM_SAMPLES as f32 - 1.0)/*))*/;