-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.py
226 lines (184 loc) · 8.45 KB
/
noise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from abc import ABC, abstractmethod
import opensimplex
from random import randint
import numpy as np
from util import ensure_np_array
class Noise(ABC):
_frequency: np.ndarray
_amplitude: float
_n_wrapped_dimensions: int
_radii: np.ndarray
@ensure_np_array
def __init__(
self,
frequency: np.ndarray,
amplitude: float = 1.0,
n_wrapped_dimensions: int = 0,
radii: np.ndarray = np.array([]),
seed: int | None = None
):
if frequency.shape == (1,):
frequency = np.repeat(frequency[0], self.N_DIMENSIONS)
elif frequency.shape != (self.N_DIMENSIONS,):
raise ValueError(f"frequency must have shape ({self.N_DIMENSIONS},)")
self._frequency = frequency
self._amplitude = amplitude
if n_wrapped_dimensions < 0 or n_wrapped_dimensions > self.MAX_N_WRAPPED_DIMENSIONS:
raise ValueError(f"n_wrapped_dimensions must be between 0 and {self.MAX_N_WRAPPED_DIMENSIONS}")
self._n_wrapped_dimensions = n_wrapped_dimensions
if radii.shape[0] != n_wrapped_dimensions:
raise ValueError(f"radii must have shape ({n_wrapped_dimensions},)")
self._radii = radii
if seed is None:
seed = randint(0, 2 ** 64 - 1)
opensimplex.seed(seed)
@ensure_np_array
@abstractmethod
def get_value(self, p: np.ndarray) -> float:
raise NotImplementedError("@abstractmethod get_value")
@ensure_np_array
def __call__(self, p: np.ndarray) -> float:
return self.get_value(p)
class Noise1D(Noise):
N_DIMENSIONS: int = 1
MAX_N_WRAPPED_DIMENSIONS: int = 1
@classmethod
@ensure_np_array
def line(cls, frequency: np.ndarray = np.array([1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([]), seed: int | None = None):
return cls(frequency, amplitude, 0, radii, seed)
@classmethod
@ensure_np_array
def circle(cls, frequency: np.ndarray = np.array([1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([1.0]), seed: int | None = None):
return cls(frequency, amplitude, 1, radii, seed)
@ensure_np_array
def __init__(
self,
frequency: np.ndarray = np.array([1.0]),
amplitude: float = 1.0,
n_wrapped_dimensions: int = 0,
radii: np.ndarray = np.array([]),
seed: int | None = None
):
super().__init__(frequency, amplitude, n_wrapped_dimensions, radii, seed)
@ensure_np_array
def get_value(self, p: np.ndarray) -> float:
if self._n_wrapped_dimensions == 0: # line
x = p[0] * self._frequency[0]
y = 0
elif self._n_wrapped_dimensions == 1: # circle
x = np.cos(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[0]
y = np.sin(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[0]
v = opensimplex.noise2(x, y)
return self._amplitude * v
class Noise2D(Noise):
N_DIMENSIONS: int = 2
MAX_N_WRAPPED_DIMENSIONS: int = 2
@ensure_np_array
@classmethod
def plane(cls, frequency: np.ndarray = np.array([1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([]), seed: int | None = None):
return cls(frequency, amplitude, 0, radii, seed)
@classmethod
@ensure_np_array
def cylinder(cls, frequency: np.ndarray = np.array([1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([1.0]), seed: int | None = None):
return cls(frequency, amplitude, 1, radii, seed)
@classmethod
@ensure_np_array
def torus(cls, frequency: np.ndarray = np.array([1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([1.0, 1.0]), seed: int | None = None):
return cls(frequency, amplitude, 2, radii, seed)
@ensure_np_array
def __init__(
self,
frequency: np.ndarray = np.array([1.0, 1.0]),
amplitude: float = 1.0,
n_wrapped_dimensions: int = 0,
radii: np.ndarray = np.array([]),
seed: int | None = None
):
super().__init__(frequency, amplitude, n_wrapped_dimensions, radii, seed)
@ensure_np_array
def get_value(self, p: np.ndarray) -> float:
if self._n_wrapped_dimensions == 0: # plane
x = p[0] * self._frequency[0]
y = p[1] * self._frequency[1]
v = opensimplex.noise2(x, y)
elif self._n_wrapped_dimensions == 1: # cylinder
x = np.cos(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[0]
y = np.sin(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[0]
z = p[1] * self._frequency[1]
v = opensimplex.noise3(x, y, z)
elif self._n_wrapped_dimensions == 2: # torus
x = (1 + np.cos(p[0] * self._frequency[0] * np.pi * 2.0)) * np.cos(p[1] * self._frequency[1] * np.pi * 2.0) * self._radii[0]
y = (1 + np.cos(p[0] * self._frequency[0] * np.pi * 2.0)) * np.sin(p[1] * self._frequency[1] * np.pi * 2.0) * self._radii[0]
z = np.sin(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[1]
v = opensimplex.noise3(x, y, z)
return self._amplitude * v
class Noise3D(Noise):
N_DIMENSIONS: int = 3
MAX_N_WRAPPED_DIMENSIONS: int = 2
@classmethod
@ensure_np_array
def space(cls, frequency: np.ndarray = np.array([1.0, 1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([]), seed: int | None = None):
return cls(frequency, amplitude, 0, radii, seed)
@classmethod
@ensure_np_array
def cylindrical(cls, frequency: np.ndarray = np.array([1.0, 1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([1.0]), seed: int | None = None):
return cls(frequency, amplitude, 1, radii, seed)
@classmethod
@ensure_np_array
def toroidal(cls, frequency: np.ndarray = np.array([1.0, 1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([1.0, 1.0]), seed: int | None = None):
return cls(frequency, amplitude, 2, radii, seed)
@ensure_np_array
def __init__(
self,
frequency: np.ndarray = np.array([1.0, 1.0, 1.0]),
amplitude: float = 1.0,
n_wrapped_dimensions: int = 0,
radii: np.ndarray = np.array([]),
seed: int | None = None
):
super().__init__(frequency, amplitude, n_wrapped_dimensions, radii, seed)
@ensure_np_array
def get_value(self, p: np.ndarray) -> float:
if self._n_wrapped_dimensions == 0: # space
x = p[0] * self._frequency[0]
y = p[1] * self._frequency[1]
z = p[2] * self._frequency[2]
v = opensimplex.noise3(x, y, z)
elif self._n_wrapped_dimensions == 1: # cylindrical
x = np.cos(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[0]
y = np.sin(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[0]
z = p[1] * self._frequency[1]
w = p[2] * self._frequency[2]
v = opensimplex.noise4(x, y, z, w)
elif self._n_wrapped_dimensions == 2: # toroidal
x = (1 + np.cos(p[0] * self._frequency[0] * np.pi * 2.0)) * np.cos(p[1] * self._frequency[1] * np.pi * 2.0) * self._radii[0]
y = (1 + np.cos(p[0] * self._frequency[0] * np.pi * 2.0)) * np.sin(p[1] * self._frequency[1] * np.pi * 2.0) * self._radii[0]
z = np.sin(p[0] * self._frequency[0] * np.pi * 2.0) * self._radii[1]
w = p[2] * self._frequency[2]
v = opensimplex.noise4(x, y, z, w)
return self._amplitude * v
class Noise4D(Noise):
N_DIMENSIONS: int = 4
MAX_N_WRAPPED_DIMENSIONS: int = 0
@classmethod
@ensure_np_array
def hyperspace(cls, frequency: np.ndarray = np.array([1.0, 1.0, 1.0, 1.0]), amplitude: float = 1.0, radii: np.ndarray = np.array([]), seed: int | None = None):
return cls(frequency, amplitude, 0, radii, seed)
@ensure_np_array
def __init__(
self,
frequency: np.ndarray = np.array([1.0, 1.0, 1.0, 1.0]),
amplitude: float = 1.0,
n_wrapped_dimensions: int = 0,
radii: np.ndarray = np.array([]),
seed: int | None = None
):
super().__init__(frequency, amplitude, n_wrapped_dimensions, radii, seed)
@ensure_np_array
def get_value(self, p: np.ndarray) -> float:
x = p[0] * self._frequency[0]
y = p[1] * self._frequency[1]
z = p[2] * self._frequency[2]
w = p[3] * self._frequency[3]
v = opensimplex.noise4(x, y, z, w)
return self._amplitude * v