def __init__(self, size, seed=0, population=1000000):
        self.size = size
        self.population = population

        noise = generate_perlin_noise_2d(size, (4, 4), (False, False))
        noise = np.where(noise < 0, 0, noise)
        noise *= population / np.sum(noise)

        self.population_distribution = noise
Exemplo n.º 2
0
    def _instantiate_grid_(self):
        self._hex_model = loader.loadModel("models/simple_hex.obj")

        # NOTE: higher resolution has steeper gradients per area.
        #   The generation of terrain with perlin noise should be done elsewhere.
        np.random.seed(12345)
        self.terrain_noise = generate_perlin_noise_2d((512, 512), (32, 32),
                                                      tileable=(True, True))

        # Set the initial position and scale.
        self._hex_model.setPos(0, 0, 0)
        self._hex_model.setScale(1)
        self._hex_model.setHpr(0, 0, 0)
        self._hex_model.setDepthOffset(1)

        center_chunk = np.array((0, 0, 0))
        chunk_id = self.data_model.chunk_vec_to_buf(center_chunk)
        starting_chunk = self.data_model.load_chunk(chunk_id)

        chunk_radius_to_render = 2
        radius_to_render = (self.data_model.chunk_radius * 3 +
                            1) * chunk_radius_to_render

        # BFS Queue
        neighbors_to_explore = [starting_chunk]
        seen_neighbors = set()
        loaded_chunks = {chunk_id}

        while len(neighbors_to_explore) != 0:
            chunk = neighbors_to_explore.pop(0)
            if chunk in seen_neighbors:
                continue

            seen_neighbors.add(chunk)

            # Only render chunk if the chunk's radius is less than or equal to R away
            origin_d = cubic_manhattan(starting_chunk.chunk_center,
                                       chunk.chunk_center)
            if origin_d > radius_to_render:
                continue

            # Convert the list of neighbor positions to ids, then load the neighbors.
            for neighbor_vector in chunk.neighbor_chunk_vec:
                neighbor_id = self.data_model.chunk_vec_to_buf(neighbor_vector)
                if neighbor_id not in loaded_chunks:
                    self.data_model.load_chunk(neighbor_id)
                    loaded_chunks.add(neighbor_id)

            self._render_chunk(chunk)
            neighbors_to_explore.extend(
                filter(
                    lambda n: n is not None and n not in neighbors_to_explore,
                    chunk.neighbors))
Exemplo n.º 3
0
    def __init__(self, shape, res, seed=0):
        """
        + shape: shape of the generated array (tuple of 2 ints)
        + res: number of periods of noise to generate along each axis (tuple of 2 ints)
        + seed: rng seed (numpy)
        Note: shape must be a multiple of res
        """
        random.seed(seed)
        noise = generate_perlin_noise_2d(shape, res, (True, True))
        # [-1, 1] -> [-pi, pi]
        # field = noise * 2 * pi
        field = noise
        super().__init__(field)

        self.shape = shape
        self.res = res
Exemplo n.º 4
0
def generate_z_vect_from_perlin_noise(seed=1001, size_z=1, scale=1.0):
    np.random.seed(seed)
    x = generate_perlin_noise_2d((size_z, VECTOR_DIM), (1, 1))
    x = x * scale
    return x
Exemplo n.º 5
0
def random_image(array_shape,
                 shapes_number,
                 shapes_min_size=(1, 1),
                 shapes_max_size=(None, None)):
    """
    return an image filled with random shapes with noise and texture.
    to avoid using to much parameters in the function, most of them are fixed at the start of the function.
    """
    background_noise = True
    shape_texture = True
    shape_noise = True
    final_noise = True

    fill_range = (0, 255)
    background_noise_mean = 128
    background_noise_std = 32

    texture_noise_res_min = 2
    texture_noise_res_max = 16
    texture_noise_amp = 42
    tnrmin = texture_noise_res_min
    tnrmax = texture_noise_res_max

    shape_gaussian_noise_mean = 0
    shape_gaussian_noise_std = 10

    final_added_gaussian_noise_mean = 0
    final_added_gaussian_noise_std = 20

    # check and set shapes_max_size
    sms = np.asarray(array_shape)
    for i in range(len(shapes_max_size)):
        if shapes_max_size[i] is not None:
            sms[i] = min(shapes_max_size[i], array_shape[i])
    shapes_max_size = tuple(sms)

    # check and set shapes_min_size
    sms = np.asarray(shapes_max_size)
    for i in range(len(shapes_min_size)):
        if shapes_min_size[i] is not None:
            sms[i] = min(shapes_min_size[i], array_shape[i])
    shapes_min_size = tuple(sms)

    # background noise
    if background_noise:
        array = (
            np.random.standard_normal(array_shape) * background_noise_std +
            background_noise_mean).astype(dtype)
        array = scipy.ndimage.gaussian_filter(array, 0.5)
    else:
        array = np.zeros(array_shape, dtype=dtype)
    print(array.dtype)

    # add random shapes
    for _ in range(shapes_number):
        # create new shape
        fill = randint(fill_range[0], fill_range[1])
        mask = elastic_deformation(
            random_ellipsoid(array.shape, shapes_min_size, shapes_max_size))
        # mask = (elasticdeform.deform_random_grid(random_spheroid(array.shape, shapes_max_size), sigma=4, points=3) > 0) * 1
        # mask = random_shape_multi_resolution(array.shape, shapes_max_size, resolution=2, iteration=3)
        # mask = random_shape(array.shape, shapes_max_size, iteration=10)

        # skip if void shape
        if np.sum(mask) == 0:
            continue

        shape = np.copy(mask)

        if shape_texture:
            # add perlin / fractal noise to create texture for the shape
            # get mask bounding box for texture and noise
            x, y = np.where(mask)

            x_min, y_min = x.min(), y.min()
            x_max, y_max = x.max(), y.max()
            x_size, y_size = x_max - x_min, y_max - y_min

            # apply noise to mask
            noise_res_x, noise_res_y = randint(tnrmin, tnrmax), \
                                       randint(tnrmin, tnrmax)

            x_size, y_size = ((x_size//noise_res_x)+1)*noise_res_x,\
                            ((y_size//noise_res_y)+1)*noise_res_y

            try:
                texture_noise = generate_perlin_noise_2d(
                    (x_size, y_size), (noise_res_x, noise_res_y),
                    tileable=(False, False))

            except:
                print("generate_perlin_noise_2d failed, alternative used")
                sX, sY = randint(1, 50) / 100, randint(1, 50) / 100
                rX, rY = (x_size * sX) // 2, (y_size * sY) // 2
                arX, arY = np.arange(-rX - 1, rX + 1, sX), \
                           np.arange(-rY - 1, rY + 1, sY)
                xx, yy = np.meshgrid(arY, arX)
                texture_noise = (np.sin(xx) / 2 + np.sin(yy) / 2 +
                                 np.tanh(xx + yy) / 2) / 4
                texture_noise = texture_noise[0:x_size, 0:y_size]

            shape_subpart = shape[x_min:x_min + x_size,
                                  y_min:y_min + y_size].shape

            shape[x_min:x_min+x_size, y_min:y_min+y_size] = \
                np.clip(((texture_noise[0:shape_subpart[0], 0:shape_subpart[1]] * 2)
                         * texture_noise_amp) + fill, 0, 255)
        else:
            shape = shape * fill

        if shape_noise:
            # add gaussian noise
            gaussian_noise = np.random.normal(shape_gaussian_noise_mean,
                                              shape_gaussian_noise_std,
                                              shape.shape)
            shape[mask == 1] = np.clip(
                shape[mask == 1] + gaussian_noise[mask == 1], 0, 255)

        array[mask == 1] = shape[mask == 1]

    if final_noise:
        # add gaussian noise
        array = np.clip(
            array +
            np.random.normal(final_added_gaussian_noise_mean,
                             final_added_gaussian_noise_std, array.shape), 0,
            255)

    return array, mask