Fixed NoiseGenerator.forceValue

This commit is contained in:
Michaël Lemaire 2015-12-16 00:36:30 +01:00
parent c5d73f96a2
commit 62a7f746ea
2 changed files with 38 additions and 35 deletions

View file

@ -164,7 +164,6 @@ void NoiseGenerator::setFunctionParams(NoiseFunctionAlgorithm algorithm, double
void NoiseGenerator::forceValue(double value) { void NoiseGenerator::forceValue(double value) {
clearLevels(); clearLevels();
height_offset = value; height_offset = value;
addLevelSimple(1.0, 0.0, 0.0); /* FIXME Should not be needed */
} }
void NoiseGenerator::getRange(double *minvalue, double *maxvalue) const { void NoiseGenerator::getRange(double *minvalue, double *maxvalue) const {

View file

@ -3,67 +3,71 @@
#include "NoiseGenerator.h" #include "NoiseGenerator.h"
TEST(NoiseGenerator, getRange) { TEST(NoiseGenerator, getRange) {
NoiseGenerator *noise; NoiseGenerator noise;
double minvalue, maxvalue; double minvalue, maxvalue;
noise = new NoiseGenerator(); noise.addLevelSimple(0.1, -1.0, 1.0);
noise.addLevelSimple(0.2, -0.5, 0.2);
noise->addLevelSimple(0.1, -1.0, 1.0); noise.addLevelSimple(0.4, -0.3, 1.2);
noise->addLevelSimple(0.2, -0.5, 0.2); noise.validate();
noise->addLevelSimple(0.4, -0.3, 1.2); noise.getRange(&minvalue, &maxvalue);
noise->validate();
noise->getRange(&minvalue, &maxvalue);
EXPECT_DOUBLE_EQ(minvalue, -1.8); EXPECT_DOUBLE_EQ(minvalue, -1.8);
EXPECT_DOUBLE_EQ(maxvalue, 2.4); EXPECT_DOUBLE_EQ(maxvalue, 2.4);
delete noise;
} }
TEST(NoiseGenerator, normalizeAmplitude) { TEST(NoiseGenerator, normalizeAmplitude) {
int x; int x;
NoiseGenerator *noise; NoiseGenerator noise;
noise = new NoiseGenerator();
/* Symmetric case */ /* Symmetric case */
noise->addLevelsSimple(10, 1.0, -4.0, 4.0, 0.5); noise.addLevelsSimple(10, 1.0, -4.0, 4.0, 0.5);
noise->validate(); noise.validate();
noise->normalizeAmplitude(-1.0, 1.0, 0); noise.normalizeAmplitude(-1.0, 1.0, 0);
for (x = 0; x < 1000; x++) { for (x = 0; x < 1000; x++) {
double value = noise->get1DTotal(0.01 * (double)x); double value = noise.get1DTotal(0.01 * to_double(x));
ASSERT_DOUBLE_IN_RANGE(value, -1.0, 1.0); ASSERT_DOUBLE_IN_RANGE(value, -1.0, 1.0);
} }
/* Target center differs from current center */ /* Target center differs from current center */
noise->clearLevels(); noise.clearLevels();
noise->addLevelsSimple(10, 1.0, -5.0, 5.0, 0.5); noise.addLevelsSimple(10, 1.0, -5.0, 5.0, 0.5);
noise->validate(); noise.validate();
noise->normalizeAmplitude(0.0, 1.0, 0); noise.normalizeAmplitude(0.0, 1.0, 0);
for (x = 0; x < 1000; x++) { for (x = 0; x < 1000; x++) {
double value = noise->get1DTotal(0.01 * (double)x); double value = noise.get1DTotal(0.01 * to_double(x));
ASSERT_DOUBLE_IN_RANGE(value, 0.0, 1.0); ASSERT_DOUBLE_IN_RANGE(value, 0.0, 1.0);
} }
/* Asymmetric range */ /* Asymmetric range */
noise->clearLevels(); noise.clearLevels();
noise->addLevelsSimple(10, 1.0, 0.0, 10.0, 0.0); noise.addLevelsSimple(10, 1.0, 0.0, 10.0, 0.0);
noise->validate(); noise.validate();
noise->normalizeAmplitude(0.0, 1.0, 0); noise.normalizeAmplitude(0.0, 1.0, 0);
for (x = 0; x < 1000; x++) { for (x = 0; x < 1000; x++) {
double value = noise->get1DTotal(0.01 * (double)x); double value = noise.get1DTotal(0.01 * to_double(x));
ASSERT_DOUBLE_IN_RANGE(value, 0.0, 1.0); ASSERT_DOUBLE_IN_RANGE(value, 0.0, 1.0);
} }
/* Complex asymmetric case */ /* Complex asymmetric case */
noise->clearLevels(); noise.clearLevels();
noise->addLevelsSimple(3, 1.0, -2.0, 8.0, 0.3); noise.addLevelsSimple(3, 1.0, -2.0, 8.0, 0.3);
noise->validate(); noise.validate();
noise->normalizeAmplitude(-2.0, 4.0, 0.0); noise.normalizeAmplitude(-2.0, 4.0, 0.0);
for (x = 0; x < 1000; x++) { for (x = 0; x < 1000; x++) {
double value = noise->get1DTotal(0.01 * (double)x); double value = noise.get1DTotal(0.01 * to_double(x));
ASSERT_DOUBLE_IN_RANGE(value, -2.0, 4.0); ASSERT_DOUBLE_IN_RANGE(value, -2.0, 4.0);
} }
}
delete noise;
TEST(NoiseGenerator, forceValue) {
NoiseGenerator noise;
noise.addLevelsSimple(10, 1.0, -4.0, 4.0, 0.5);
noise.forceValue(2.3);
noise.validate();
EXPECT_DOUBLE_EQ(2.3, noise.get1DTotal(0.5));
EXPECT_DOUBLE_EQ(2.3, noise.get2DTotal(-6.3, 1.4));
EXPECT_DOUBLE_EQ(2.3, noise.get3DTotal(12.9, 0.0, -5.0));
} }