wfc

    可复现的邻接约束传播、回溯和矛盾报告

    Download zip
    Version
    0.5.1
    License
    MIT
    Last updated
    4 hours ago
    Downloads
    3

    #可执行 API 示例

    增加解的尺寸、tile、pin 和四方向邻接约束验证。这些例子调用公开 API,并随 moon test 执行。

    ///|
    test "validate solver result and externally altered tiles" {
    let m = @wfc.Model::{
    labels: ["a", "b"],
    allowed: [[3, 3, 3, 3], [3, 3, 3, 3]],
    }
    match @wfc.solve(m, 3, 3, pins=[(0, 1)]) {
    Some(s) => {
    assert_true(s.validate(m, pins=[(0, 1)]))
    assert_true(!s.validate(m, pins=[(0, 0)]))
    s.tiles[0] = 99
    assert_true(!s.validate(m))
    }
    None => fail("expected solution")
    }
    }

    此例保留旧 Model 兼容入口。0.4 新增 RuleModel / learn_patterns / expand_tiles;PNG/XML、规模与剩余差距见 README 和 FEATURES。

    SolveError

    pub suberror SolveError {
    Invalid(String)
    BudgetExhausted
    } derive(
    Debug
    )

    Model

    pub(all) struct Model {
    labels : Array[String]
    allowed : Array[Array[Int]]
    } derive(
    Debug
    )

    allowed[tile][direction], directions east, south, west, north.

    Model::to_repr

    Model::to_rules

    fn Model::to_rules(self : Model, weights? : Array[Int]) -> RuleModel raise SolveError

    OverlapModel

    pub struct OverlapModel {
    size : Int
    patterns : Array[Array[Int]]
    weights : Array[Int]
    model : Model
    } derive(
    Debug
    )

    Learned square patterns and their observed frequencies. Symbols may be colors or tiles.

    OverlapModel::generate

    fn OverlapModel::generate(self : OverlapModel, width : Int, height : Int, seed? : UInt, periodic? : Bool, budget? : Int) -> Array[Int]? raise SolveError

    Return exactly width*height symbols. Nonperiodic output includes the full edge patches.

    PatternModel

    pub struct PatternModel {
    size : Int
    patterns : Array[Array[Int]]
    rules : RuleModel
    } derive(ToJson,
    Debug
    )

    PatternModel::generate

    fn PatternModel::generate(self : PatternModel, width : Int, height : Int, seed? : UInt, periodic? : Bool, ground? : Bool, pins? : Array[(Int, Int)], budget? : Int) -> Array[Int]? raise SolveError

    PatternModel::to_json

    RuleModel

    pub(all) struct RuleModel {
    labels : Array[String]
    neighbors : Array[Array[Array[Int]]]
    weights : Array[Double]
    } derive(ToJson,
    Debug
    )

    Explicit neighbor lists, indexed [tile][east,south,west,north]. Unlike Model, tile IDs are not packed into a signed machine integer.

    RuleModel::to_json

    fn RuleModel::to_json(RuleModel) -> Json

    Solution

    pub struct Solution {
    width : Int
    height : Int
    tiles : Array[Int]
    decisions : Int
    backtracks : Int
    } derive(Eq, ToJson,
    Debug
    )

    Solution::equal

    fn Solution::equal(Solution, Solution) -> Bool

    Solution::not_equal

    fn Solution::not_equal(x : Solution, y : Solution) -> Bool

    Solution::render

    fn Solution::render(self : Solution, model : Model) -> String raise SolveError

    Solution::to_json

    fn Solution::to_json(Solution) -> Json

    Solution::to_repr

    Solution::validate

    fn Solution::validate(self : Solution, model : Model, pins? : Array[(Int, Int)], periodic? : Bool) -> Bool

    Legacy model validation adapter.

    Solution::validate_rules

    fn Solution::validate_rules(self : Solution, model : RuleModel, pins? : Array[(Int, Int)], restrictions? : Array[(Int, Array[Int])], periodic? : Bool) -> Bool

    TileDefinition

    pub(all) struct TileDefinition {
    name : String
    symmetry : String
    weight : Double
    pixels : Array[Array[Int]]
    } derive(ToJson,
    Debug
    )

    D4 symmetry names and orientations follow mxgmn/WaveFunctionCollapse (MIT). Neighbor expansion below applies group actions to a directed spatial edge.

    TileDefinition::to_json

    TiledModel

    pub struct TiledModel {
    rules : RuleModel
    tiles : Array[Array[Int]]
    tile_size : Int
    } derive(ToJson,
    Debug
    )

    TiledModel::render

    fn TiledModel::render(self : TiledModel, solution : Solution) -> Array[Int] raise SolveError

    TiledModel::to_json

    fn TiledModel::to_json(TiledModel) -> Json

    evaluate_json

    fn evaluate_json(input : String) -> String

    Numeric-pixel/file-independent request interface shared by the Node worker and browser worker. File decoding belongs to the host; solving remains MoonBit.

    expand_tiles

    fn expand_tiles(definitions : Array[TileDefinition], tile_size : Int, neighbors : Array[(String, String)], subset? : Array[String]) -> TiledModel raise SolveError

    learn_overlap

    fn learn_overlap(sample : Array[Int], width : Int, height : Int, size : Int, periodic_input? : Bool, symmetry? : Bool) -> OverlapModel raise SolveError

    Learn patterns, optionally with all eight rotations/reflections. Current bit-mask solver supports at most 30 unique patterns; excess raises.

    learn_patterns

    fn learn_patterns(sample : Array[Int], width : Int, height : Int, size : Int, periodic_input? : Bool, symmetry? : Int) -> PatternModel raise SolveError

    Learn collision-free integer patterns with 1..8 dihedral transforms. Transform order is identity, reflection, quarter-turn, reflection, half-turn, ... .

    solve

    fn solve(model : Model, width : Int, height : Int, seed? : UInt, pins? : Array[(Int, Int)], budget? : Int, weights? : Array[Int], periodic? : Bool) -> Solution? raise SolveError

    Legacy integer-mask model adapter; use RuleModel for more than 30 tiles.

    solve_rules

    fn solve_rules(model : RuleModel, width : Int, height : Int, seed? : UInt, pins? : Array[(Int, Int)], restrictions? : Array[(Int, Array[Int])], periodic? : Bool, budget? : Int) -> Solution? raise SolveError

    Iterative, reversible constraint propagation with weighted entropy. Limits are 4096 tiles, 65536 cells and two million cell/tile states; budget counts actual removal/counter work. None means complete exhaustion, not a timeout.