| 40 | }; |
| 41 | |
| 42 | class SimplexIslands { // Simplex noise in 2D, 3D and 4D |
| 43 | // Inner class to speed upp gradient computations |
| 44 | // (In Java, array access is a lot slower than member access) |
| 45 | public: |
| 46 | std::vector<Grad> grad3; |
| 47 | std::vector<Grad> grad4; |
| 48 | |
| 49 | static short pbak[]; |
| 50 | static short p[]; |
| 51 | |
| 52 | // To remove the need for index wrapping, double the permutation table length |
| 53 | short perm[512]; |
| 54 | short permMod12[512]; |
| 55 | |
| 56 | double F2; |
| 57 | double G2; |
| 58 | double F3; |
| 59 | double G3; |
| 60 | double F4; |
| 61 | double G4; |
| 62 | |
| 63 | SimplexIslands(); |
| 64 | |
| 65 | // This method is a *lot* faster than using (int)Math.floor(x) |
| 66 | static inline int32_t fastfloor(double fp) { |
| 67 | int32_t i = static_cast<int32_t>(fp); |
| 68 | return (fp < i) ? (i - 1) : (i); |
| 69 | } |
| 70 | |
| 71 | double dot(Grad g, double x, double y) { |
| 72 | return g.x*x + g.y*y; |
| 73 | } |
| 74 | |
| 75 | // 2D simplex noise |
| 76 | double noise(double xin, double yin) { |
| 77 | double n0, n1, n2; // Noise contributions from the three corners |
| 78 | // Skew the input space to determine which simplex cell we're in |
| 79 | double s = (xin + yin)*F2; // Hairy factor for 2D |
| 80 | int i = fastfloor(xin + s); |
| 81 | int j = fastfloor(yin + s); |
| 82 | double t = (i + j)*G2; |
| 83 | double X0 = i - t; // Unskew the cell origin back to (x,y) space |
| 84 | double Y0 = j - t; |
| 85 | double x0 = xin - X0; // The x,y distances from the cell origin |
| 86 | double y0 = yin - Y0; |
| 87 | // For the 2D case, the simplex shape is an equilateral triangle. |
| 88 | // Determine which simplex we are in. |
| 89 | int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords |
| 90 | if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1) |
| 91 | else { i1 = 0; j1 = 1; } // upper triangle, YX order: (0,0)->(0,1)->(1,1) |
| 92 | // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and |
| 93 | // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where |
| 94 | // c = (3-sqrt(3))/6 |
| 95 | double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords |
| 96 | double y1 = y0 - j1 + G2; |
| 97 | double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords |
| 98 | double y2 = y0 - 1.0 + 2.0 * G2; |
| 99 | // Work out the hashed gradient indices of the three simplex corners |
nothing calls this directly
no outgoing calls
no test coverage detected