Add PNMImage::perlin_noise_fill

This commit is contained in:
rdb 2010-05-08 14:49:41 +00:00
parent e2d7f9a871
commit 609a328946
3 changed files with 51 additions and 4 deletions

View File

@ -5,7 +5,7 @@
#begin lib_target
#define TARGET pnmimage
#define LOCAL_LIBS \
linmath putil express
linmath putil express mathutil
#define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx

View File

@ -17,6 +17,8 @@
#include "pnmWriter.h"
#include "pnmBrush.h"
#include "config_pnmimage.h"
#include "perlinNoise2.h"
#include "stackedPerlinNoise2.h"
#include <algorithm>
////////////////////////////////////////////////////////////////////
@ -1253,6 +1255,47 @@ make_histogram(PNMImage::Histogram &histogram) {
histogram.swap(pixels, hist_map);
}
////////////////////////////////////////////////////////////////////
// Function: PNMImage::perlin_noise_fill
// Access: Published
// Description: Fills the image with a grayscale perlin noise
// pattern based on the indicated parameters.
// Uses set_xel to set the grayscale values.
// The sx and sy parameters are in multiples
// of the size of this image.
// See also the PerlinNoise2 class in mathutil.
////////////////////////////////////////////////////////////////////
void PNMImage::
perlin_noise_fill(double sx, double sy, int table_size, unsigned long seed) {
double x, y;
double noise;
PerlinNoise2 perlin (sx * _x_size, sy * _y_size, table_size, seed);
for (x = 0; x < _x_size; ++x) {
for (y = 0; y < _y_size; ++y) {
noise = perlin.noise(x, y);
set_xel(x, y, 0.5 * (noise + 1.0));
}
}
}
////////////////////////////////////////////////////////////////////
// Function: PNMImage::perlin_noise_fill
// Access: Published
// Description: Variant of perlin_noise_fill that uses an
// existing StackedPerlinNoise2 object.
////////////////////////////////////////////////////////////////////
void PNMImage::
perlin_noise_fill(StackedPerlinNoise2 &perlin) {
double x, y;
double noise;
for (x = 0; x < _x_size; ++x) {
for (y = 0; y < _y_size; ++y) {
noise = perlin.noise(x / (double) _x_size, y / (double) _y_size);
set_xel(x, y, 0.5 * (noise + 1.0));
}
}
}
////////////////////////////////////////////////////////////////////
// Function: PNMImage::setup_rc
// Access: Private

View File

@ -25,6 +25,7 @@
class PNMReader;
class PNMWriter;
class PNMFileType;
class StackedPerlinNoise2;
////////////////////////////////////////////////////////////////////
// Class : PNMImage
@ -231,6 +232,9 @@ PUBLISHED:
int xborder = 0, int yborder = 0);
void make_histogram(Histogram &hist);
void perlin_noise_fill(double sx, double sy, int table_size = 256,
unsigned long seed = 0);
void perlin_noise_fill(StackedPerlinNoise2 &perlin);
private:
INLINE void allocate_array();