Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated so code is Python 3 compatible #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/1D_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
pn = PerlinNoise(num_octaves=7, persistence=0.1)
data = []

t = [i for i in xrange(length)]
for i in xrange(length):
t = [i for i in range(length)]
for i in range(length):
data.append(normalize(pn.fractal(x=i, hgrid=length)))

fig = plt.figure()
Expand Down
6 changes: 3 additions & 3 deletions examples/fbm_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
sn = SimplexNoise(num_octaves=7, persistence=0.1, dimensions=2)
data = []

for i in xrange(size):
for i in range(size):
data.append([])
for j in xrange(size):
for j in range(size):
noise = normalize(sn.fractal(i, j, hgrid=size))
data[i].append(noise * 255.0)

# Cast to numpy array so we can save
data = np.array(data).astype(np.uint8)
img = Image.fromarray(data, mode='L')
img.save('./fbm_example.png')
img.save('./fbm_example.png')
6 changes: 3 additions & 3 deletions examples/noise_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
sn = SimplexNoise(num_octaves=7, persistence=0.1, dimensions=2, noise_scale=noise_scale)
data = []

for i in xrange(size):
for i in range(size):
data.append([])
for j in xrange(size):
for j in range(size):
noise = normalize(sn.noise(i, j))
data[i].append(noise * 255.0)

# Cast to numpy array so we can save
data = np.array(data).astype(np.uint8)
img = Image.fromarray(data, mode='L')
img.save('./noise_example.png')
img.save('./noise_example.png')
40 changes: 20 additions & 20 deletions simplexnoise/noise.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import division
import math
import random
from geometry import Point
from .geometry import Point

# Constants to avoid magic numbers
DEFAULT_NOISE_SCALE = -1 # Check noise_scale against this
Expand Down Expand Up @@ -41,17 +41,17 @@ def __init__(self, num_octaves, persistence, noise_scale=DEFAULT_NOISE_SCALE):
else:
self.noise_scale = noise_scale

self.octaves = [PerlinNoiseOctave() for i in xrange(self.num_octaves)]
self.frequencies = [1.0 / pow(2, i) for i in xrange(self.num_octaves)]
self.octaves = [PerlinNoiseOctave() for i in range(self.num_octaves)]
self.frequencies = [1.0 / pow(2, i) for i in range(self.num_octaves)]
self.amplitudes = [pow(persistence, len(self.octaves) - i)
for i in xrange(self.num_octaves)]
for i in range(self.num_octaves)]

def noise(self, x):
noise = [
self.octaves[i].noise(
xin=x * self.frequencies[i],
noise_scale=self.noise_scale
) * self.amplitudes[i] for i in xrange(self.num_octaves)]
) * self.amplitudes[i] for i in range(self.num_octaves)]

return sum(noise)

Expand All @@ -61,7 +61,7 @@ def fractal(self, x, hgrid, lacunarity=DEFAULT_LACUNARITY, gain=DEFAULT_GAIN):
frequency = 1.0 / hgrid
amplitude = gain

for i in xrange(self.num_octaves):
for i in range(self.num_octaves):
noise.append(
self.octaves[i].noise(
xin=x * frequency,
Expand All @@ -78,9 +78,9 @@ def fractal(self, x, hgrid, lacunarity=DEFAULT_LACUNARITY, gain=DEFAULT_GAIN):
class PerlinNoiseOctave(object):

def __init__(self, num_shuffles=DEFAULT_SHUFFLES):
self.p_supply = [i for i in xrange(0, 256)]
self.p_supply = [i for i in range(0, 256)]

for i in xrange(num_shuffles):
for i in range(num_shuffles):
random.shuffle(self.p_supply)

self.perm = self.p_supply * 2
Expand Down Expand Up @@ -121,12 +121,12 @@ def __init__(self, num_octaves, persistence, dimensions, noise_scale=DEFAULT_NOI

if DIMENSIONS_2D == dimensions:
self.octaves = [SimplexNoiseOctave2D()
for i in xrange(self.num_octaves)]
for i in range(self.num_octaves)]
self.noise_scale = DEFAULT_2D_NOISE_SCALE

elif DIMENSIONS_3D == dimensions:
self.octaves = [SimplexNoiseOctave3D()
for i in xrange(self.num_octaves)]
for i in range(self.num_octaves)]
self.noise_scale = DEFAULT_2D_NOISE_SCALE

else:
Expand All @@ -137,9 +137,9 @@ def __init__(self, num_octaves, persistence, dimensions, noise_scale=DEFAULT_NOI
if DEFAULT_NOISE_SCALE != noise_scale:
self.noise_scale = noise_scale

self.frequencies = [pow(2, i) for i in xrange(self.num_octaves)]
self.frequencies = [pow(2, i) for i in range(self.num_octaves)]
self.amplitudes = [pow(persistence, len(self.octaves) - i)
for i in xrange(self.num_octaves)]
for i in range(self.num_octaves)]

def noise(self, x=0, y=0, z=0):
noise = [
Expand All @@ -148,7 +148,7 @@ def noise(self, x=0, y=0, z=0):
yin=y / self.frequencies[i],
zin=z / self.frequencies[i],
noise_scale=self.noise_scale
) * self.amplitudes[i] for i in xrange(self.num_octaves)]
) * self.amplitudes[i] for i in range(self.num_octaves)]

return sum(noise)

Expand All @@ -158,7 +158,7 @@ def fractal(self, x=0, y=0, z=0, hgrid=0, lacunarity=DEFAULT_LACUNARITY, gain=DE
frequency = 1.0 / hgrid
amplitude = gain

for i in xrange(self.num_octaves):
for i in range(self.num_octaves):
noise.append(
self.octaves[i].noise(
xin=x * frequency,
Expand All @@ -182,7 +182,7 @@ class SimplexNoiseOctave2D(object):
unskew_factor = (3.0 - math.sqrt(3.0)) / 6.0

def __init__(self, num_shuffles=DEFAULT_SHUFFLES):
self.p_supply = [i for i in xrange(0, 256)]
self.p_supply = [i for i in range(0, 256)]

self.grads = [
Point(1, 1, 0),
Expand All @@ -191,7 +191,7 @@ def __init__(self, num_shuffles=DEFAULT_SHUFFLES):
Point(-1, -1, 0)
]

for i in xrange(num_shuffles):
for i in range(num_shuffles):
random.shuffle(self.p_supply)

self.perm = self.p_supply * 2
Expand Down Expand Up @@ -277,7 +277,7 @@ def hashed_gradient_indices(self, points_ij):
def calc_noise_contributions(self, grad_index_hash, points_xy):
""" Calculates the contribution from each corner (in 2D there are three!) """
contribs = []
for i in xrange(len(grad_index_hash)):
for i in range(len(grad_index_hash)):
x = points_xy[i].x
y = points_xy[i].y
grad = self.grads[grad_index_hash[i]]
Expand All @@ -299,15 +299,15 @@ class SimplexNoiseOctave3D(object):
unskew_factor = 1.0 / 6.0

def __init__(self, num_shuffles=DEFAULT_SHUFFLES):
self.p_supply = [i for i in xrange(0, 256)]
self.p_supply = [i for i in range(0, 256)]

self.grads = [
Point(1, 1, 0), Point(-1, 1, 0), Point(1, -1, 0), Point(-1, -1, 0),
Point(1, 0, 1), Point(-1, 0, 1), Point(1, 0, -1), Point(-1, 0, -1),
Point(0, 1, 1), Point(0, -1, 1), Point(0, 1, -1), Point(0, -1, -1),
]

for i in xrange(num_shuffles):
for i in range(num_shuffles):
random.shuffle(self.p_supply)

self.perm = self.p_supply * 2
Expand Down Expand Up @@ -412,7 +412,7 @@ def hashed_gradient_indices(self, points_ijk):
def calc_noise_contributions(self, grad_index_hash, points_xyz):
""" Calculates the contribution from each corner (in 2D there are three!) """
contribs = []
for i in xrange(len(grad_index_hash)):
for i in range(len(grad_index_hash)):
x = points_xyz[i].x
y = points_xyz[i].y
z = points_xyz[i].z
Expand Down