1 module dosimplex.ext.layer; 2 3 /// Layer interface used by all kinds of layers 4 interface Layer 5 { 6 @nogc @safe pure double eval(D...)(D dimensions); 7 } 8 9 /// Default layer for open simplex noise 10 class OpenSimplexNoiseLayer : Layer 11 { 12 import dosimplex.generator; 13 import dosimplex.seed; 14 15 this(Seed s) 16 { 17 _gen = SNoiseGenerator(s); 18 } 19 20 @nogc @safe pure double eval(D...)(D dimensions) 21 if (dimensions.length > 0 && dimensions.length < 5) 22 { 23 static if (dimensions.length == 1) { 24 return _gen.noise2D(dimensions,0.0); // TODO: im unhappy with this. 25 } else static if (dimensions.length == 2) { 26 return _gen.noise2D(dimensions); 27 } else static if (dimensions.length == 3) { 28 return _gen.noise3D(dimensions); 29 } else static if (dimensions.length == 4) { 30 return _gen.noise4D(dimensions); 31 } 32 33 assert(0); 34 } 35 36 private: 37 SNoiseGenerator _gen; 38 } 39 40 /// Default layer for sine function 41 class SineLayer : Layer 42 { 43 @nogc @safe pure double eval(D...)(D dimensions) 44 if (dimensions.length > 0 && dimensions.length < 5) 45 { 46 import std.math; 47 double result = 0.0; // we dont set to 1.0 and then just multiply to avoid this tiny bit of error it might cause - and its probably a tiny bit faster 48 foreach(int i,d; dimensions) { 49 static if(i < 1) { 50 result = sin(d); 51 } else { 52 result = result*sin(d); 53 } 54 } 55 56 return result; 57 } 58 } 59