Fixed noise range and normalization (with unit testing)

This commit is contained in:
Michaël Lemaire 2013-06-09 14:56:45 +02:00
parent f3ddf1917f
commit 701faf6ece
3 changed files with 84 additions and 13 deletions

View file

@ -202,17 +202,8 @@ void noiseValidate(NoiseGenerator* generator)
generator->_max_value = generator->height_offset; generator->_max_value = generator->height_offset;
for (x = 0; x < generator->level_count; x++) for (x = 0; x < generator->level_count; x++)
{ {
double min_value = generator->levels[x].minvalue; generator->_min_value += generator->levels[x].minvalue;
double max_value = min_value + generator->levels[x].amplitude; generator->_max_value += generator->levels[x].minvalue + generator->levels[x].amplitude;
if (min_value < generator->_min_value)
{
generator->_min_value = min_value;
}
if (max_value > generator->_max_value)
{
generator->_max_value = max_value;
}
} }
} }
@ -394,14 +385,14 @@ void noiseNormalizeAmplitude(NoiseGenerator* generator, double minvalue, double
for (level = 0; level < generator->level_count; level++) for (level = 0; level < generator->level_count; level++)
{ {
generator->levels[level].minvalue = minvalue + (generator->levels[level].minvalue - current_minvalue) * factor; generator->levels[level].minvalue *= factor;
generator->levels[level].amplitude *= factor; generator->levels[level].amplitude *= factor;
if (adjust_scaling) if (adjust_scaling)
{ {
generator->levels[level].wavelength *= factor; generator->levels[level].wavelength *= factor;
} }
} }
/*generator->height_offset = minvalue + (generator->height_offset - current_minvalue) * factor;*/ generator->height_offset = minvalue + (generator->height_offset - current_minvalue) * factor;
noiseValidate(generator); noiseValidate(generator);
} }

View file

@ -6,6 +6,7 @@
extern void test_euclid_case(Suite* s); extern void test_euclid_case(Suite* s);
extern void test_camera_case(Suite* s); extern void test_camera_case(Suite* s);
extern void test_clouds_case(Suite* s); extern void test_clouds_case(Suite* s);
extern void test_noise_case(Suite* s);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -18,6 +19,7 @@ int main(int argc, char** argv)
test_euclid_case(s); test_euclid_case(s);
test_camera_case(s); test_camera_case(s);
test_clouds_case(s); test_clouds_case(s);
test_noise_case(s);
SRunner *sr = srunner_create(s); SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL); srunner_run_all(sr, CK_NORMAL);

78
src/testing/test_noise.c Normal file
View file

@ -0,0 +1,78 @@
#include "testing/common.h"
#include "rendering/noise.h"
START_TEST(test_noise_range)
{
NoiseGenerator* noise;
double minvalue, maxvalue;
noise = noiseCreateGenerator();
noiseAddLevelSimple(noise, 0.1, -1.0, 1.0);
noiseAddLevelSimple(noise, 0.2, -0.5, 0.2);
noiseAddLevelSimple(noise, 0.4, -0.3, 1.2);
noiseValidate(noise);
noiseGetRange(noise, &minvalue, &maxvalue);
ck_assert_double_eq(minvalue, -1.8);
ck_assert_double_eq(maxvalue, 2.4);
}
END_TEST
START_TEST(test_noise_normalize)
{
int x;
NoiseGenerator* noise;
noise = noiseCreateGenerator();
/* Symmetric case */
noiseAddLevelsSimple(noise, 10, 1.0, -4.0, 4.0, 0.5);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, -1.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, -1.0, 1.0);
}
/* Target center differs from current center */
noiseClearLevels(noise);
noiseAddLevelsSimple(noise, 10, 1.0, -5.0, 5.0, 0.5);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, 0.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, 0.0, 1.0);
}
/* Asymmetric range */
noiseClearLevels(noise);
noiseAddLevelsSimple(noise, 10, 1.0, 0.0, 10.0, 0.0);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, 0.0, 1.0, 0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, 0.0, 1.0);
}
/* Complex asymmetric case */
noiseClearLevels(noise);
noiseAddLevelsSimple(noise, 3, 1.0, -2.0, 8.0, 0.3);
noiseValidate(noise);
noiseNormalizeAmplitude(noise, -2.0, 4.0, 0.0);
for (x = 0; x < 1000; x++)
{
double value = noiseGet1DTotal(noise, 0.01 * (double)x);
ck_assert_double_in_range(value, -2.0, 4.0);
}
noiseDeleteGenerator(noise);
}
END_TEST
TEST_CASE(noise, test_noise_range, test_noise_normalize)