|
Toshihiro Shimizu |
890ddd |
/*--------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
Simplex Noise C++ implementation
|
|
Toshihiro Shimizu |
890ddd |
Based on a public domain code by Stefan Gustavson.
|
|
Toshihiro Shimizu |
890ddd |
(The original header is below)
|
|
Toshihiro Shimizu |
890ddd |
--------------------------------------------------------------------*/
|
|
Toshihiro Shimizu |
890ddd |
/*-- start of the original header --*/
|
|
Toshihiro Shimizu |
890ddd |
/*
|
|
Toshihiro Shimizu |
890ddd |
* A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
|
|
Toshihiro Shimizu |
890ddd |
*
|
|
Toshihiro Shimizu |
890ddd |
* Based on example code by Stefan Gustavson (stegu@itn.liu.se).
|
|
Toshihiro Shimizu |
890ddd |
* Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
|
|
Toshihiro Shimizu |
890ddd |
* Better rank ordering method by Stefan Gustavson in 2012.
|
|
Toshihiro Shimizu |
890ddd |
*
|
|
Toshihiro Shimizu |
890ddd |
* This could be speeded up even further, but it's useful as it is.
|
|
Toshihiro Shimizu |
890ddd |
*
|
|
Toshihiro Shimizu |
890ddd |
* Version 2012-03-09
|
|
Toshihiro Shimizu |
890ddd |
*
|
|
Toshihiro Shimizu |
890ddd |
* This code was placed in the public domain by its original author,
|
|
Toshihiro Shimizu |
890ddd |
* Stefan Gustavson. You may use it as you see fit, but
|
|
Toshihiro Shimizu |
890ddd |
* attribution is appreciated.
|
|
Toshihiro Shimizu |
890ddd |
*
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
/*-- end of the original header --*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef IWA_SIMPLEXNOISE_H
|
|
Toshihiro Shimizu |
890ddd |
#define IWA_SIMPLEXNOISE_H
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
struct Grad {
|
|
Toshihiro Shimizu |
890ddd |
int x, y, z, w;
|
|
Toshihiro Shimizu |
890ddd |
Grad(int x_, int y_, int z_)
|
|
Toshihiro Shimizu |
890ddd |
: x(x_), y(y_), z(z_), w(0) {}
|
|
Toshihiro Shimizu |
890ddd |
Grad(int x_, int y_, int z_, int w_)
|
|
Toshihiro Shimizu |
890ddd |
: x(x_), y(y_), z(z_), w(w_) {}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
struct CellIds {
|
|
Toshihiro Shimizu |
890ddd |
int i, j, k;
|
|
Toshihiro Shimizu |
890ddd |
int i1, j1, k1;
|
|
Toshihiro Shimizu |
890ddd |
int i2, j2, k2;
|
|
Toshihiro Shimizu |
890ddd |
CellIds(int _i = 0, int _j = 0, int _k = 0,
|
|
Toshihiro Shimizu |
890ddd |
int _i1 = 0, int _j1 = 0, int _k1 = 0,
|
|
Toshihiro Shimizu |
890ddd |
int _i2 = 0, int _j2 = 0, int _k2 = 0)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
i = _i;
|
|
Toshihiro Shimizu |
890ddd |
j = _j;
|
|
Toshihiro Shimizu |
890ddd |
k = _k;
|
|
Toshihiro Shimizu |
890ddd |
i1 = _i1;
|
|
Toshihiro Shimizu |
890ddd |
j1 = _j1;
|
|
Toshihiro Shimizu |
890ddd |
k1 = _k1;
|
|
Toshihiro Shimizu |
890ddd |
i2 = _i2;
|
|
Toshihiro Shimizu |
890ddd |
j2 = _j2;
|
|
Toshihiro Shimizu |
890ddd |
k2 = _k2;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool operator==(const CellIds &right) const
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return i == right.i &&
|
|
Toshihiro Shimizu |
890ddd |
j == right.j &&
|
|
Toshihiro Shimizu |
890ddd |
k == right.k &&
|
|
Toshihiro Shimizu |
890ddd |
i1 == right.i1 &&
|
|
Toshihiro Shimizu |
890ddd |
j1 == right.j1 &&
|
|
Toshihiro Shimizu |
890ddd |
k1 == right.k1 &&
|
|
Toshihiro Shimizu |
890ddd |
i2 == right.i2 &&
|
|
Toshihiro Shimizu |
890ddd |
j2 == right.j2 &&
|
|
Toshihiro Shimizu |
890ddd |
k2 == right.k2;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
class SimplexNoise
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// This method is a *lot* faster than using (int)Math.floor(x)
|
|
Toshihiro Shimizu |
890ddd |
static int fastfloor(double x)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int xi = (int)x;
|
|
Toshihiro Shimizu |
890ddd |
return x < xi ? xi - 1 : xi;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
static double dot(Grad g, double x, double y)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return g.x * x + g.y * y;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
static double dot(Grad g, double x, double y, double z)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return g.x * x + g.y * y + g.z * z;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
static double dot(Grad g, double x, double y, double z, double w)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
return g.x * x + g.y * y + g.z * z + g.w * w;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
SimplexNoise();
|
|
Toshihiro Shimizu |
890ddd |
// 2D simplex noise
|
|
Toshihiro Shimizu |
890ddd |
static double noise(double xin, double yin);
|
|
Toshihiro Shimizu |
890ddd |
// 3D simplex noise
|
|
Toshihiro Shimizu |
890ddd |
static double noise(double xin, double yin, double zin);
|
|
Toshihiro Shimizu |
890ddd |
// 4D simplex noise
|
|
Toshihiro Shimizu |
890ddd |
static double noise(double x, double y, double z, double w);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
/*- セルまたぎを防ぐために、現在の所属セルを得る -*/
|
|
Toshihiro Shimizu |
890ddd |
static CellIds getCellIds(double xin, double yin, double zin);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif
|