def setNextPositionPerlin(self, *args, **kwargs): self.updateFunction = self.updatePerlin if('slow' in args): self.currentSpeedLimit = StewartPlatform.SERVO_SPEED_LIMIT/2 else: self.currentSpeedLimit = StewartPlatform.SERVO_SPEED_LIMIT*2 t = (time()-StewartPlatform.INIT_TIME) * StewartPlatform.PERLIN_TIME_SCALE (x,y,z) = self.currentPosition.getTranslationAsList() # direction u = snoise4(x*StewartPlatform.PERLIN_POSITION_SCALE + 1*StewartPlatform.PERLIN_PHASE, y*StewartPlatform.PERLIN_POSITION_SCALE + 1*StewartPlatform.PERLIN_PHASE, z*StewartPlatform.PERLIN_POSITION_SCALE + 1*StewartPlatform.PERLIN_PHASE, t + 1*StewartPlatform.PERLIN_PHASE) v = snoise4(x*StewartPlatform.PERLIN_POSITION_SCALE + 2*StewartPlatform.PERLIN_PHASE, y*StewartPlatform.PERLIN_POSITION_SCALE + 2*StewartPlatform.PERLIN_PHASE, z*StewartPlatform.PERLIN_POSITION_SCALE + 2*StewartPlatform.PERLIN_PHASE, t + 2*StewartPlatform.PERLIN_PHASE) w = snoise4(x*StewartPlatform.PERLIN_POSITION_SCALE + 3*StewartPlatform.PERLIN_PHASE, y*StewartPlatform.PERLIN_POSITION_SCALE + 3*StewartPlatform.PERLIN_PHASE, z*StewartPlatform.PERLIN_POSITION_SCALE + 3*StewartPlatform.PERLIN_PHASE, t + 3*StewartPlatform.PERLIN_PHASE) #magnitude thisSpeedScale = StewartPlatform.PERLIN_SPEED_SCALE/3 if ('slow' in args) else StewartPlatform.PERLIN_SPEED_SCALE speed = min(StewartPlatform.PERLIN_MAX_SPEED, max(StewartPlatform.PERLIN_MIN_SPEED, thisSpeedScale*(snoise4(u,v,w,t)*0.5+0.5))) # result deltaDistances = ( u*speed, v*speed, w*speed) deltaAngles = ( snoise2(v,t)*StewartPlatform.PERLIN_ANGLE_SCALE, snoise2(w,t)*StewartPlatform.PERLIN_ANGLE_SCALE, snoise2(u,t)*StewartPlatform.PERLIN_ANGLE_SCALE) # pick new valid position translateArg = kwargs.get('translate', '') rotateArg = kwargs.get('rotate', '') done = False while not done: translation = Vector3( deltaDistances[0] if 'x' in translateArg else 0, deltaDistances[1] if 'y' in translateArg else 0, deltaDistances[2] if 'z' in translateArg else 0) + self.currentPosition.translation translation.constrain(-StewartPlatform.PERLIN_DISTANCE_LIMIT, StewartPlatform.PERLIN_DISTANCE_LIMIT) rotation = Vector3( deltaAngles[0] if 'x' in rotateArg else 0, deltaAngles[1] if 'y' in rotateArg else 0, deltaAngles[2] if 'z' in rotateArg else 0) + self.currentPosition.rotation rotation.constrain(-StewartPlatform.PERLIN_ANGLE_LIMIT, StewartPlatform.PERLIN_ANGLE_LIMIT) done = self.setTargetAnglesSuccessfully(translation, rotation) deltaDistances = map(lambda x:0.9*x, deltaDistances) deltaAngles = map(lambda x:0.9*x, deltaAngles)
def generateNoise(): logging.info("Recreating texture") d = np.zeros((textWidth, textHeight, 4), dtype=np.float32) # How many units? dx = 20 dy = 20 for i in tqdm.trange(textWidth, leave=False): for j in range(textHeight): s = float(i) / textWidth t = float(j) / textHeight x1 = 1 y1 = 1 nx = x1+np.cos(s*2*np.pi)*dx / (2*np.pi) ny = y1+np.cos(t*2*np.pi)*dy / (2*np.pi) nz = x1+np.sin(s*2*np.pi)*dx / (2*np.pi) nw = y1+np.sin(t*2*np.pi)*dy / (2*np.pi) t = noise.snoise4(nx, ny, nz, nw, octaves=10, persistence=0.5) d[i, j] = t d -= np.min(d) d /= np.max(d)-np.min(d) d *= 2 d -= 1 for i in tqdm.trange(textWidth, leave=False): for j in range(textHeight): v1 = np.array([float(i)/textWidth, d[i, j][3], float(j)/textWidth]) v2 = np.array([float(i+1)/textWidth, d[(i+1)%textWidth, j][3], float(j)/textWidth]) v3 = np.array([float(i)/textWidth, d[i, (j+1)%textHeight][3], float(j+1)/textWidth]) d[i, j][:3] = np.cross(v3-v1, v2-v1) d[i, j][:3] /= np.linalg.norm(d[i, j][:3]) d[i, j][0], d[i, j][2] = d[i, j][2], d[i, j][0] return d
def _get_force(self, x, y, t, magnitude=1): cos_value = self.radius * math.cos(2 * math.pi * t) sin_value = self.radius * math.sin(2 * math.pi * t) angle = snoise4(x * self.scale[0], y * self.scale[1], cos_value, sin_value) return Vec2D(np.cos(angle) * magnitude, np.sin(angle) * magnitude)
def generate_noise(x, y=0, z=None, w=None, scale=1, offset_x=0.0, offset_y=0.0, octaves=1, persistence=0.5, lacunarity=2.0): """Generate simplex noise. :param x: The x coordinate of the noise value :param y: The y coordinate of the noise value :param z: The z coordinate of the noise value :param w: A fourth dimensional coordinate :param scale: The scale of the base plane :param float offset_x: How much to offset `x` by on the base plane :param float offset_y: How much to offset `y` by on the base plane :param int octaves: The number of passes to make calculating noise :param float persistence: The amplitude multiplier per octave :param float lacunarity: The frequency multiplier per octave """ x = (x + offset_x) / scale y = (y + offset_y) / scale if z is not None: z /= scale if w is not None: w /= scale if z is None and w is None: return noise.snoise2(x, y, octaves=octaves, lacunarity=lacunarity, persistence=persistence) elif w is None: return noise.snoise3(x, y, z, octaves=octaves, lacunarity=lacunarity, persistence=persistence) else: return noise.snoise4(x, y, z, w, octaves=octaves, lacunarity=lacunarity, persistence=persistence)
def test_simplex_4d_range(self): from noise import snoise4 for i in range(-10000, 10000): x = i * 0.88 y = -i * 0.11 z = -i * 0.57 w = i * 0.666 n = snoise4(x, y, z, w) self.assertTrue(-1.0 <= n <= 1.0, (x, y, z, w, n))
def test_simplex_4d_octaves_range(self): from noise import snoise4 for i in range(-1000, 1000): x = -i * 0.12 y = i * 0.55 z = i * 0.34 w = i * 0.21 for o in range(10): n = snoise4(x, y, z, w, octaves=o + 1) self.assertTrue(-1.0 <= n <= 1.0, (x, y, z, w, o + 1, n))
def step(self, amt): for y in range(self.x): for x in range(self.y): for z in range(self.z): v = int(snoise4(x / self._freq, y / self._freq, z / self._freq, self._step / self._freq, octaves=self._octaves) * 127.0 + 128.0) c = self.palette(v) self.layout.set(x, y, z, c) self._step += amt
def test_simplex_4d_octaves_range(self): from noise import snoise4 for i in range(-1000, 1000): x = -i * 0.12 y = i * 0.55 z = i * 0.34 w = i * 0.21 for o in range(10): n = snoise4(x, y, z, w, octaves=o + 1) self.assertTrue(-1.0 <= n <= 1.0, (x, y, z, w, o+1, n))
def snoise4(x, y=None, z=None, w=None, octaves=1, gain=0.5, lacunarity=2, amp=1, repeat=1): if y is not None: return amp * fit_11_to_01( noise.snoise4(x * repeat, y * repeat, z * repeat, w * repeat, octaves, gain, lacunarity)) else: r = np.empty((len(x), ), dtype=np.float64) for i, d in enumerate(x): r[i] = amp * fit_11_to_01( noise.snoise4(d[0] * repeat, d[1] * repeat, d[2] * repeat, d[3] * repeat, octaves, gain, lacunarity)) return r
def simplex_noise_3d(x, y, z, seed=0, octaves=1, freq_ratio=16.0) -> float: """ Create simplex noise between 0 and 1 :param x: x coord for the nose :param y: y coord for the noise :param z: z coord for the noise :param seed: seed to use :param octaves: higher number makes the noise smoother :param freq_ratio: how often to sample as the ratio changes :return: float between 0 and 1 """ freq = octaves * freq_ratio return (noise.snoise4(seed, x / freq, y / freq, z / freq, octaves) + 1) / 2
def update(self, dt): self.phase += dt * 0.05 for i, (x, y, z) in zip(self.disruptors, self.vertices): i.update(snoise4(x, y, z, self.phase) * 0.2 + 0.9) radii = np.array([ i.position() for i in self.disruptors ], dtype=np.float32) data = np.transpose(np.transpose(self.vertices) * radii) with self.vertex_bo: self.vertex_bo.set_array(data) self.vertex_bo.copy_data()
def step(self, amt): for y in range(self.x): for x in range(self.y): for z in range(self.z): v = int( snoise4(x / self._freq, y / self._freq, z / self._freq, self._step / self._freq, octaves=self._octaves) * 127.0 + 128.0) c = self.palette(v) self.layout.set(x, y, z, c) self._step += amt
def step(self, amt): for y in range(self.x): for x in range(self.y): for z in range(self.z): v = int( snoise4(x / self._freq, y / self._freq, z / self._freq, self._step / self._freq, octaves=self._octaves) * 127.0 + 128.0) c = colors.hue2rgb(v) self._led.set(x, y, z, c) self._step += amt
def flow_field(self): # generate vector field flow_field = np.zeros( (*shape, 2)) # if 256, 256, 30 -> 256, 256, 30, 2 radius = 0.1 scale = (0.1, 0.1) for i in range(shape[2]): cos_value = self.radius * math.cos(2 * math.pi * (i / shape[2])) sin_value = self.radius * math.sin(2 * math.pi * (i / shape[2])) for x in range(shape[0]): for y in range(shape[1]): angle = snoise4(x * self.scale[0], y * self.scale[1], cos_value, sin_value) flow_field[x, y, i, 0] = np.cos(angle) * 10 flow_field[x, y, i, 1] = np.sin(angle) * 10
def calculateWorldValues(self): """ Generate and normalize a new random world using snoise4 """ # use snoise4 to generate a random noise field pNoiseSeed = random.random() for x in range(self.worldSize): for y in range(self.worldSize): for z in range(self.worldSize): self.world[x][y][z] = snoise4(x/self.worldSize,y/self.worldSize,z/self.worldSize, pNoiseSeed) # normalize noise worldMax = np.max(self.world) worldMin = np.min(self.world) for x in range(self.worldSize): for y in range(self.worldSize): for z in range(self.worldSize): self.world[x][y][z] = (self.world[x][y][z] - worldMin) / (worldMax-worldMin)
def getWorldValues(self): """ Generate random world """ pNoiseSeed = random.random() for x in range(self.worldSize): for y in range(self.worldSize): for z in range(self.worldSize): self.world[x][y][z] = snoise4(x / self.worldSize, y / self.worldSize, z / self.worldSize, pNoiseSeed) # normalize worldMax = np.max(self.world) worldMin = np.min(self.world) for x in range(self.worldSize): for y in range(self.worldSize): for z in range(self.worldSize): self.world[x][y][z] = (self.world[x][y][z] - worldMin) / (worldMax - worldMin)
def _simplex_noise4d(shape, scale, octaves, radius, random): img = np.zeros(shape) if random: offset = np.random.rand() * 100 else: offset = 0 for i in range(shape[2]): cos_value = radius * math.cos(2 * math.pi * (i / shape[2])) sin_value = radius * math.sin(2 * math.pi * (i / shape[2])) for x in range(shape[0]): for y in range(shape[1]): img[x, y, i] = snoise4(x * scale[0] + offset, y * scale[1] + offset, cos_value, sin_value) img = ((img - img.min()) * (1 / (img.max() - img.min()) * 255)).astype('uint8') return img