public void Transform1D() { // Transform forward and inverse and compare with initial values. var random = new Random(1234567); var s = new Vector2F[16]; var t = new Vector2F[16]; for (int i = 0; i < s.Length; i++) { s[i] = random.NextVector2F(-10, 10); t[i] = s[i]; } FastFourierTransformF.Transform1D(t, true); FastFourierTransformF.Transform1D(t, false); for (int i = 0; i < s.Length; i++) Assert.IsTrue(Vector2F.AreNumericallyEqual(s[i], t[i])); }
public void Transform1DAs2DColumn() { // Result of 2D with one row/column must be the same as 1D. var fft = new FastFourierTransformF(16); var random = new Random(1234567); var s1D = new Vector2F[16]; var s2D = new Vector2F[16, 1]; for (int i = 0; i < s1D.Length; i++) { s1D[i] = random.NextVector2F(-10, 10); s2D[i, 0] = s1D[i]; } FastFourierTransformF.Transform1D(s1D, true); fft.Transform2D(s2D, true); for (int i = 0; i < s1D.Length; i++) Assert.AreEqual(s1D[i], s2D[i, 0]); }
/// <summary> /// Initializes a new instance of the <see cref="ProceduralTerrainCreator"/> class. /// </summary> /// <param name="seed">The seed for the random number generator (e.g. 7777).</param> /// <param name="permutationTableSize"> /// The size of the permutation table (e.g. 256). Must be a power of two. /// </param> /// <param name="mu"> /// The constant µ that defines the exponential distribution. Use a value of 1 to get standard /// Perlin noise. Use a value greater than 1 (e.g. 1.02) to get Perlin noise with exponentially /// distributed gradients. For a <paramref name="permutationTableSize"/> of 256, µ should be in /// the range [1, 1.16]. /// </param> public ProceduralTerrainCreator(int seed, int permutationTableSize, float mu) { if (!MathHelper.IsPowerOf2(permutationTableSize)) throw new ArgumentException("The permutation table size must be a power of 2 (e.g. 256)."); _maskB = permutationTableSize - 1; var random = new Random(seed); // Create table of random gradient vectors (normalized). _gradients = new Vector2F[permutationTableSize]; for (int i = 0; i < _gradients.Length; i++) { var direction = random.NextVector2F(-1, 1); if (!direction.TryNormalize()) direction = new Vector2F(1, 0); _gradients[i] = direction; } // Create table with a permutation of the values 0 to permutationTableSize. _permutations = new int[permutationTableSize]; for (int i = 0; i < _permutations.Length; i++) _permutations[i] = i; for (int i = _permutations.Length - 1; i > 0; i--) MathHelper.Swap(ref _permutations[i], ref _permutations[random.NextInteger(0, i)]); // Create table with gradient magnitudes. _magnitudes = new float[permutationTableSize]; float s = 1; // First magnitude. for (int i = 0; i < _magnitudes.Length; i++) { _magnitudes[i] = s; s /= mu; } }
public void NoiseRange2D() { double min = double.MaxValue; double max = double.MinValue; var random = new Random(15485863); for (int i = 0; i < 10000000; i++) { var v = random.NextVector2F(0, 255); var n = PerlinNoise.Compute(v.X, v.Y); min = Math.Min(min, n); max = Math.Max(max, n); } Assert.IsTrue(min < 0 && min >= -1); Assert.IsTrue(max > 0 && max <= 1); }
public void Transform2D() { // Transform forward and inverse and compare with initial values. var random = new Random(1234567); var s = new Vector2F[16, 8]; var t = new Vector2F[16, 8]; for (int i = 0; i < s.GetLength(0); i++) { for (int j = 0; j < s.GetLength(1); j++) { s[i, j] = random.NextVector2F(-10, 10); t[i, j] = s[i, j]; } } var fft = new FastFourierTransformF(16); fft.Transform2D(t, true); Assert.IsFalse(Vector2F.AreNumericallyEqual(s[0, 0], t[0, 0])); fft.Transform2D(t, false); for (int i = 0; i < s.GetLength(0); i++) for (int j = 0; j < s.GetLength(1); j++) Assert.IsTrue(Vector2F.AreNumericallyEqual(s[i, j], t[i, j])); }