48 return std::sqrt(
x*
x +
y*
y);
68 unsigned int n_height,
unsigned int n_width,
unsigned long int seed)
87 unsigned int n_height,
unsigned int n_width,
unsigned long int seed)
101 std::uniform_real_distribution<double> distribution(-1, 1);
102 std::mt19937 random_generator;
107 random_generator = std::mt19937(
seed_);
111 for (
int i = 0; i < 256; i++) {
115 v.
x = distribution(random_generator);
116 v.
y = distribution(random_generator);
117 }
while (v.
norm2() > 1.0 && v.
norm2() == 0.0);
131 std::vector<unsigned int> permut(256);
132 for (
int i = 0; i < 256; i++) {
141 std::random_shuffle(permut.begin(), permut.end());
144 for (
unsigned int i = 0; i <=
n_height_; i++) {
145 for (
unsigned int j = 0; j <=
n_width_; j++) {
155 std::cout <<
"[PerlinNoiseGenerator] Error: specified coordinates not valid\n";
161 unsigned int i1 = i0 + 1;
162 unsigned int j1 = j0 + 1;
163 float x0 = float(i0)/float(n_height_)*
height_;
164 float y0 = float(j0)/float(n_width_)*
width_;
176 float t_x = (x - x0) / (
height_/n_height_);
177 float t_y = (y - y0) / (
width_/n_width_);
184 if (output < -1) output = -1;
185 if (output > 1) output = 1;
186 output = output/2 + 0.5;
206 return u.
x*v.
x + u.
y*v.
y;
211 float x,
float y)
const 222 return (1-t)*a + t*b;
float norm2()
Computes the square of the euclidean norm of the vector.
float fade(float t) const
Sixth order polynomial to smooth the noise.
Declaration of 2D perlin noise generator.
std::vector< std::vector< Vec2d > > gradients_
Gradients evaluated at nodes of the grid.
float dot_product(Vec2d u, Vec2d v) const
Computes the dot product between two vectors.
void operator=(const Vec2d &a)
void init_random(unsigned long int seed)
Initialises random generation.
float height_
Height of the 2D space in which to generate the noise.
float width_
Width of the 2D space in which to generate the noise.
float interpolate(float a, float b, float t) const
Linear interpolation between two values.
void configure(float height, float width, unsigned int n_height, unsigned int n_width, unsigned long int seed=0)
Configures the generator.
unsigned int n_height_
Size of the discretised grid in the first dimension.
bool randomise_seed_
Whether to randomise seeding for random numbers.
float y
Second coordinate.
std::random_device random_device_
Seed initialiser for generation of random numbers.
void generate(unsigned long int seed=0)
Populates the grid with random gradients from the hash list.
unsigned int n_width_
Size of the discretised grid in the second dimension.
unsigned long int seed_
Seed for random numbers when no random seeding.
void randomise_gradients()
Fills the hash list with random gradients.
std::vector< Vec2d > hash_gradients_
Hash list of random gradients.
float dot_dist_grad(unsigned int i, unsigned int j, float x, float y) const
Compute the dot product of the distance vector to the node an the gradient at this node...
PerlinNoiseGenerator()
Default constructor.
float norm()
Computes the euclidean norm of the vector.
double evaluate(float x, float y) const
Evaluates the perlin noise at a given position.