fimama

 1from importlib.resources import path as resource_path
 2from pathlib import Path
 3
 4import matplotlib
 5import matplotlib.pyplot as plt
 6from matplotlib.colors import LinearSegmentedColormap
 7import numpy as np
 8import yaml
 9
10from fimama.configuration import MapConfiguration
11from fimama.perlin import perlin_map
12from fimama.voronoi import voronoi_map
13
14matplotlib.use('qtagg')
15
16
17def plot_map(
18    heightmap: np.ndarray, colormap: LinearSegmentedColormap,
19):
20    """
21    Plot a heightmap as a field of Voronoi cells.
22
23    Parameters
24    ----------
25    heightmap : np.ndarray
26        Heightmap to plot
27    colormap : LinearSegmentedColormap
28        Colormap to use in the plotting.
29
30    Returns
31    -------
32    tuple[matplotlib.figure.Figure, matplotlib.Axes.axes]
33        The containing Figure and the Axes the map was plotted on.
34    """
35    fig, (ax1) = plt.subplots(nrows=1, ncols=1, layout="constrained")
36    ax1.set_aspect('equal', 'box')
37    # ax2.set_aspect('equal', 'box')
38
39    voronoi_map(fig=fig, axes=ax1, heightmap=heightmap, colormap=colormap)
40
41    # Colored elevation map
42    # ax2.imshow(X=terrain, cmap=colormap)
43    # plt.tight_layout()
44    plt.show()
45    return fig, ax1
46
47
48def build_map(
49    config_path: Path | None = None
50) -> tuple[np.ndarray, LinearSegmentedColormap]:
51    """
52    Build a map based on settings in the configuration file.
53
54    Parameters
55    ----------
56    config_path : Path | None, optional
57        Path to the configuration file. If this is None the default parameters
58        from the fimama package will be loaded, by default None.
59
60    Returns
61    -------
62    tuple[np.ndarray, LinearSegmentedColormap]
63        The heightmap and the colormap.
64    """
65    anchor = 'fimama.resources'
66
67    # read the config
68    with resource_path(anchor, "default.yaml") as config_path:
69        with open(config_path, 'r', encoding='utf-8') as config_file:
70            raw_config = yaml.safe_load(config_file)
71            config = MapConfiguration(**raw_config)
72
73    heightmap = perlin_map(
74        width=config.width,
75        height=config.height,
76        params=config.perlin_parameters)
77    heightmap = heightmap.T
78    # terrain = np.arange(width*height).reshape((width,height))
79
80    # Read the colormap
81    with resource_path(anchor, f"{config.colormap_name}.gpf") as colormap_path:
82        tmp = []
83        for row in np.loadtxt(colormap_path):
84            tmp.append([row[0], row[1:4]])
85        colormap = LinearSegmentedColormap.from_list(
86            config.colormap_name, tmp)
87
88    return heightmap, colormap
89
90
91def main() -> None:
92    heightmap, colormap = build_map()
93    fig, ax1 = plot_map(heightmap=heightmap, colormap=colormap)
def plot_map( heightmap: numpy.ndarray, colormap: matplotlib.colors.LinearSegmentedColormap):
19def plot_map(
20    heightmap: np.ndarray, colormap: LinearSegmentedColormap,
21):
22    """
23    Plot a heightmap as a field of Voronoi cells.
24
25    Parameters
26    ----------
27    heightmap : np.ndarray
28        Heightmap to plot
29    colormap : LinearSegmentedColormap
30        Colormap to use in the plotting.
31
32    Returns
33    -------
34    tuple[matplotlib.figure.Figure, matplotlib.Axes.axes]
35        The containing Figure and the Axes the map was plotted on.
36    """
37    fig, (ax1) = plt.subplots(nrows=1, ncols=1, layout="constrained")
38    ax1.set_aspect('equal', 'box')
39    # ax2.set_aspect('equal', 'box')
40
41    voronoi_map(fig=fig, axes=ax1, heightmap=heightmap, colormap=colormap)
42
43    # Colored elevation map
44    # ax2.imshow(X=terrain, cmap=colormap)
45    # plt.tight_layout()
46    plt.show()
47    return fig, ax1

Plot a heightmap as a field of Voronoi cells.

Parameters
  • heightmap (np.ndarray): Heightmap to plot
  • colormap (LinearSegmentedColormap): Colormap to use in the plotting.
Returns
  • tuple[matplotlib.figure.Figure, matplotlib.Axes.axes]: The containing Figure and the Axes the map was plotted on.
def build_map( config_path: pathlib._local.Path | None = None) -> tuple[numpy.ndarray, matplotlib.colors.LinearSegmentedColormap]:
50def build_map(
51    config_path: Path | None = None
52) -> tuple[np.ndarray, LinearSegmentedColormap]:
53    """
54    Build a map based on settings in the configuration file.
55
56    Parameters
57    ----------
58    config_path : Path | None, optional
59        Path to the configuration file. If this is None the default parameters
60        from the fimama package will be loaded, by default None.
61
62    Returns
63    -------
64    tuple[np.ndarray, LinearSegmentedColormap]
65        The heightmap and the colormap.
66    """
67    anchor = 'fimama.resources'
68
69    # read the config
70    with resource_path(anchor, "default.yaml") as config_path:
71        with open(config_path, 'r', encoding='utf-8') as config_file:
72            raw_config = yaml.safe_load(config_file)
73            config = MapConfiguration(**raw_config)
74
75    heightmap = perlin_map(
76        width=config.width,
77        height=config.height,
78        params=config.perlin_parameters)
79    heightmap = heightmap.T
80    # terrain = np.arange(width*height).reshape((width,height))
81
82    # Read the colormap
83    with resource_path(anchor, f"{config.colormap_name}.gpf") as colormap_path:
84        tmp = []
85        for row in np.loadtxt(colormap_path):
86            tmp.append([row[0], row[1:4]])
87        colormap = LinearSegmentedColormap.from_list(
88            config.colormap_name, tmp)
89
90    return heightmap, colormap

Build a map based on settings in the configuration file.

Parameters
  • config_path (Path | None, optional): Path to the configuration file. If this is None the default parameters from the fimama package will be loaded, by default None.
Returns
  • tuple[np.ndarray, LinearSegmentedColormap]: The heightmap and the colormap.
def main() -> None:
93def main() -> None:
94    heightmap, colormap = build_map()
95    fig, ax1 = plot_map(heightmap=heightmap, colormap=colormap)