| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.geometry.noise; |
| 23 |
|
| 24 |
import java.awt.geom.Rectangle2D; |
| 25 |
|
| 26 |
import org.jetbrains.annotations.Nullable; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public final class PerlinTurbulence { |
| 43 |
private static final int RAND_m = 2147483647; |
| 44 |
private static final int RAND_a = 16807; |
| 45 |
private static final int RAND_q = 127773; |
| 46 |
private static final int RAND_r = 2836; |
| 47 |
|
| 48 |
private static final int BSize = 0x100; |
| 49 |
private static final int BM = 0xff; |
| 50 |
private static final double PerlinN = 0x1000; |
| 51 |
|
| 52 |
private final int[] uLatticeSelector = new int[BSize + 1]; |
| 53 |
private final double[] fGradient = new double[(BSize + 1) * 8]; |
| 54 |
private final int numOctaves; |
| 55 |
private final double xFrequency; |
| 56 |
private final double yFrequency; |
| 57 |
|
| 58 |
public PerlinTurbulence(int seed, int numOctaves, double xFrequency, double yFrequency) { |
| 59 |
this.numOctaves = numOctaves; |
| 60 |
this.xFrequency = xFrequency; |
| 61 |
this.yFrequency = yFrequency; |
| 62 |
init(seed); |
| 63 |
} |
| 64 |
|
| 65 |
public static final class StitchInfo { |
| 66 |
private int width; |
| 67 |
private int height; |
| 68 |
private int wrapX; |
| 69 |
private int wrapY; |
| 70 |
} |
| 71 |
|
| 72 |
private static int setupSeed(int seed) { |
| 73 |
if (seed <= 0) seed = -(seed % (RAND_m - 1)) + 1; |
| 74 |
if (seed > RAND_m - 1) seed = RAND_m - 1; |
| 75 |
return seed; |
| 76 |
} |
| 77 |
|
| 78 |
private static int random(int seed) { |
| 79 |
int result = RAND_a * (seed % RAND_q) - RAND_r * (seed / RAND_q); |
| 80 |
if (result <= 0) result += RAND_m; |
| 81 |
return result; |
| 82 |
} |
| 83 |
|
| 84 |
private void init(int seed) { |
| 85 |
int lSeed = setupSeed(seed); |
| 86 |
|
| 87 |
int i, j, k; |
| 88 |
|
| 89 |
for (k = 0; k < 4; k++) { |
| 90 |
for (i = 0; i < BSize; i++) { |
| 91 |
double u, v; |
| 92 |
do { |
| 93 |
u = ((lSeed = random(lSeed)) % (BSize + BSize)) - (double) BSize; |
| 94 |
v = ((lSeed = random(lSeed)) % (BSize + BSize)) - (double) BSize; |
| 95 |
} while (u == 0 && v == 0); |
| 96 |
|
| 97 |
double s = Math.sqrt(u * u + v * v); |
| 98 |
double si = 1 / s; |
| 99 |
fGradient[i * 8 + k * 2] = u * si; |
| 100 |
fGradient[i * 8 + k * 2 + 1] = v * si; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
for (i = 0; i < BSize; i++) |
| 105 |
uLatticeSelector[i] = i; |
| 106 |
|
| 107 |
while (--i > 0) { |
| 108 |
k = uLatticeSelector[i]; |
| 109 |
j = (lSeed = random(lSeed)) % BSize; |
| 110 |
uLatticeSelector[i] = uLatticeSelector[j]; |
| 111 |
uLatticeSelector[j] = k; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
int s1 = i << 3; |
| 116 |
int s2 = j << 3; |
| 117 |
for (j = 0; j < 8; j++) { |
| 118 |
double s = fGradient[s1 + j]; |
| 119 |
fGradient[s1 + j] = fGradient[s2 + j]; |
| 120 |
fGradient[s2 + j] = s; |
| 121 |
} |
| 122 |
} |
| 123 |
uLatticeSelector[BSize] = uLatticeSelector[0]; |
| 124 |
for (j = 0; j < 8; j++) |
| 125 |
fGradient[(BSize * 8) + j] = fGradient[j]; |
| 126 |
} |
| 127 |
|
| 128 |
private static double curve(double t) { |
| 129 |
return t * t * (3. - 2. * t); |
| 130 |
} |
| 131 |
|
| 132 |
private static double lerp(double t, double a, double b) { |
| 133 |
return a + t * (b - a); |
| 134 |
} |
| 135 |
|
| 136 |
private void noise2(double[] noiseChannels, double vec0, double vec1, @Nullable StitchInfo stitchInfo) { |
| 137 |
double t = vec0 + PerlinN; |
| 138 |
int bx0 = (int) t; |
| 139 |
int bx1 = bx0 + 1; |
| 140 |
final double rx0 = t - bx0; |
| 141 |
final double rx1 = rx0 - 1.0f; |
| 142 |
final double sx = curve(rx0); |
| 143 |
|
| 144 |
t = vec1 + PerlinN; |
| 145 |
int by0 = (int) t; |
| 146 |
int by1 = by0 + 1; |
| 147 |
final double ry0 = t - (int) t; |
| 148 |
final double ry1 = ry0 - 1.0f; |
| 149 |
final double sy = curve(ry0); |
| 150 |
|
| 151 |
|
| 152 |
if (stitchInfo != null) { |
| 153 |
if (bx0 >= stitchInfo.wrapX) |
| 154 |
bx0 -= stitchInfo.width; |
| 155 |
if (bx1 >= stitchInfo.wrapX) |
| 156 |
bx1 -= stitchInfo.width; |
| 157 |
|
| 158 |
if (by0 >= stitchInfo.wrapY) |
| 159 |
by0 -= stitchInfo.height; |
| 160 |
if (by1 >= stitchInfo.wrapY) |
| 161 |
by1 -= stitchInfo.height; |
| 162 |
} |
| 163 |
|
| 164 |
bx0 &= BM; |
| 165 |
bx1 &= BM; |
| 166 |
by0 &= BM; |
| 167 |
by1 &= BM; |
| 168 |
|
| 169 |
final int i = uLatticeSelector[bx0]; |
| 170 |
final int j = uLatticeSelector[bx1]; |
| 171 |
|
| 172 |
final int b00 = ((i + by0) & BM) << 3; |
| 173 |
final int b10 = ((j + by0) & BM) << 3; |
| 174 |
final int b01 = ((i + by1) & BM) << 3; |
| 175 |
final int b11 = ((j + by1) & BM) << 3; |
| 176 |
|
| 177 |
for (int channelIndex = 0; channelIndex < noiseChannels.length; channelIndex++) { |
| 178 |
int offset = 2 * channelIndex; |
| 179 |
noiseChannels[channelIndex] = |
| 180 |
lerp(sy, |
| 181 |
lerp(sx, |
| 182 |
rx0 * fGradient[b00 + offset] + ry0 * fGradient[b00 + offset + 1], |
| 183 |
rx1 * fGradient[b10 + offset] + ry0 * fGradient[b10 + offset + 1]), |
| 184 |
lerp(sx, |
| 185 |
rx0 * fGradient[b01 + offset] + ry1 * fGradient[b01 + offset + 1], |
| 186 |
rx1 * fGradient[b11 + offset] + ry1 * fGradient[b11 + offset + 1])); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public void turbulence(double[] turbulenceChannels, double pointX, double pointY, |
| 191 |
boolean fractalSum, @Nullable StitchInfo stitchInfo, @Nullable Rectangle2D.Double tile) { |
| 192 |
double baseFrequencyX = xFrequency; |
| 193 |
double baseFrequencyY = yFrequency; |
| 194 |
if (stitchInfo != null) { |
| 195 |
assert tile != null; |
| 196 |
if (baseFrequencyX != 0) { |
| 197 |
baseFrequencyX = adjustFrequency(baseFrequencyX, tile.width); |
| 198 |
} |
| 199 |
if (baseFrequencyY != 0) { |
| 200 |
baseFrequencyY = adjustFrequency(baseFrequencyY, tile.height); |
| 201 |
} |
| 202 |
|
| 203 |
stitchInfo.width = (int) (tile.width * baseFrequencyX + 0.5f); |
| 204 |
stitchInfo.wrapX = (int) (tile.x * baseFrequencyX + PerlinN + stitchInfo.width); |
| 205 |
|
| 206 |
stitchInfo.height = (int) (tile.height * baseFrequencyY + 0.5f); |
| 207 |
stitchInfo.wrapY = (int) (tile.y * baseFrequencyY + PerlinN + stitchInfo.height); |
| 208 |
} |
| 209 |
|
| 210 |
final double[] fSum = fractalSum |
| 211 |
? new double[] {127.5, 127.5, 127.5, 127.5} |
| 212 |
: new double[] {0, 0, 0, 0}; |
| 213 |
|
| 214 |
double vec0 = pointX * baseFrequencyX; |
| 215 |
double vec1 = pointY * baseFrequencyY; |
| 216 |
|
| 217 |
double ratio = fractalSum ? 127.5 : 255; |
| 218 |
|
| 219 |
for (int nOctave = 0; nOctave < numOctaves; nOctave++) { |
| 220 |
noise2(turbulenceChannels, vec0, vec1, stitchInfo); |
| 221 |
if (fractalSum) { |
| 222 |
for (int i = 0; i < turbulenceChannels.length; i++) { |
| 223 |
fSum[i] += turbulenceChannels[i] * ratio; |
| 224 |
} |
| 225 |
} else { |
| 226 |
for (int i = 0; i < turbulenceChannels.length; i++) { |
| 227 |
fSum[i] += Math.abs(turbulenceChannels[i]) * ratio; |
| 228 |
} |
| 229 |
} |
| 230 |
vec0 *= 2; |
| 231 |
vec1 *= 2; |
| 232 |
ratio *= 0.5; |
| 233 |
if (stitchInfo != null) { |
| 234 |
|
| 235 |
|
| 236 |
stitchInfo.width *= 2; |
| 237 |
stitchInfo.wrapX *= 2; |
| 238 |
stitchInfo.wrapX += (int) PerlinN; |
| 239 |
|
| 240 |
stitchInfo.height *= 2; |
| 241 |
stitchInfo.wrapY *= 2; |
| 242 |
stitchInfo.wrapY += (int) PerlinN; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
System.arraycopy(fSum, 0, turbulenceChannels, 0, fSum.length); |
| 247 |
} |
| 248 |
|
| 249 |
private double adjustFrequency(double frequency, double tileSize) { |
| 250 |
double fLoFreq = Math.floor(tileSize * frequency) / tileSize; |
| 251 |
double fHiFreq = Math.ceil(tileSize * frequency) / tileSize; |
| 252 |
if (frequency / fLoFreq < fHiFreq / frequency) { |
| 253 |
return fLoFreq; |
| 254 |
} else { |
| 255 |
return fHiFreq; |
| 256 |
} |
| 257 |
} |
| 258 |
} |