designs.py
2.1 kB · python · 55 lines
1import numpy as np2from typing import TYPE_CHECKING3from mrlypy.core import binary, rules4from mrlypy.core.state import state56if TYPE_CHECKING:7 from .models import Cell2d89def build_2d(pattern: np.ndarray, level: int = 1, rotation: int = 0) -> "Cell2d":10 from .models import Cell2d11 cell = Cell2d(types=pattern)12 if level > 1:13 cell.fractal(level)14 if rotation != 0:15 cell.rotate(rotation)16 return cell1718def zeros_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":19 return build_2d(binary.zeros_2d(number), level, rotation)2021def ones_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":22 return build_2d(binary.ones_2d(number), level, rotation)2324def noise_2d(number: int, level: int = 1, density: float = 0.5, rotation: int = 0) -> "Cell2d":25 pattern = binary.noise_2d(number, density, state.rng)26 return build_2d(pattern, level, rotation)2728def carpet_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":29 return build_2d(binary.carpet_2d(number), level, rotation)3031def net_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":32 return build_2d(binary.net_2d(number), level, rotation)3334def tree_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":35 return build_2d(binary.tree_2d(number), level, rotation)3637def void_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":38 return build_2d(binary.void_2d(number), level, rotation)3940def level_set_2d(number: int, levels, level: int = 1, rotation: int = 0) -> "Cell2d":41 return build_2d(rules.level_set(number, 2, levels), level, rotation)4243def vtree_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":44 return build_2d(rules.tree(number, 2, free_axis=0), level, rotation)4546def htree_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":47 return build_2d(rules.tree(number, 2, free_axis=1), level, rotation)4849def anti_2d(design: "Cell2d") -> "Cell2d":50 return design.invert()5152def random_2d(number: int, level: int = 1, rotation: int = 0) -> "Cell2d":53 choices = [carpet_2d, net_2d, tree_2d, void_2d]54 idx = state.rng.choice(len(choices))55 return choices[idx](number, level, rotation)