Optimization

This commit is contained in:
Thorsten Sommer 2020-01-11 23:34:10 +01:00
parent 124cac2740
commit c58d7c104b

View File

@ -16,7 +16,7 @@ namespace CubicNoise.Noisers
private readonly int periodY; private readonly int periodY;
private readonly int seed; private readonly int seed;
public CubicNoiseEngine(int seed, Dictionary<IParameterName, int> intParameters) public CubicNoiseEngine(int seed, IReadOnlyDictionary<IParameterName, int> intParameters)
{ {
this.seed = seed; this.seed = seed;
this.octave = intParameters?.ContainsKey(CubicNoiseIntParameters.OCTAVE) == true ? intParameters[CubicNoiseIntParameters.OCTAVE] : 16; this.octave = intParameters?.ContainsKey(CubicNoiseIntParameters.OCTAVE) == true ? intParameters[CubicNoiseIntParameters.OCTAVE] : 16;
@ -29,11 +29,11 @@ namespace CubicNoise.Noisers
var xi = (int)Math.Floor(x / this.octave); var xi = (int)Math.Floor(x / this.octave);
var octaveXFactor = x / this.octave - xi; var octaveXFactor = x / this.octave - xi;
return this.Interpolation( return Interpolation(
a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: 0), a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: 0),
b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: 0), b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: 0),
c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: 0), c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: 0),
d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: 0), d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: 0),
x: octaveXFactor) * 0.5f + 0.25f; x: octaveXFactor) * 0.5f + 0.25f;
} }
@ -45,33 +45,33 @@ namespace CubicNoise.Noisers
var octaveXFactor = x / octave - xi; var octaveXFactor = x / octave - xi;
var octaveYFactor = y / octave - yi; var octaveYFactor = y / octave - yi;
return this.Interpolation( return Interpolation(
a: this.Interpolation( a: Interpolation(
a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)),
x: octaveXFactor), x: octaveXFactor),
b: this.Interpolation( b: Interpolation(
a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)),
x: octaveXFactor), x: octaveXFactor),
c: this.Interpolation( c: Interpolation(
a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)),
x: octaveXFactor), x: octaveXFactor),
d: this.Interpolation( d: Interpolation(
a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)),
x: octaveXFactor), x: octaveXFactor),
x: octaveYFactor) * 0.5f + 0.25f; x: octaveYFactor) * 0.5f + 0.25f;
@ -82,12 +82,12 @@ namespace CubicNoise.Noisers
return (float)((((x ^ y) * RANDOM_NUMBER_A) ^ (this.seed + x)) * (((RANDOM_NUMBER_B * x) << 16) ^ (RANDOM_NUMBER_B * y) - RANDOM_NUMBER_A)) / int.MaxValue; return (float)((((x ^ y) * RANDOM_NUMBER_A) ^ (this.seed + x)) * (((RANDOM_NUMBER_B * x) << 16) ^ (RANDOM_NUMBER_B * y) - RANDOM_NUMBER_A)) / int.MaxValue;
} }
private int DeterminePatch(int coordinate, int period) private static int DeterminePatch(int coordinate, int period)
{ {
return coordinate % period; return coordinate % period;
} }
private float Interpolation(float a, float b, float c, float d, float x) private static float Interpolation(float a, float b, float c, float d, float x)
{ {
var p = (d - c) - (a - b); var p = (d - c) - (a - b);
return x * (x * (x * p + ((a - b) - p)) + (c - a)) + b; return x * (x * (x * p + ((a - b) - p)) + (c - a)) + b;