Added basic traits

master
hheik 2026-04-23 19:41:24 +03:00
commit 28c57905a3
5 changed files with 63 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "wfc"
version = "0.1.0"

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "wfc"
version = "0.1.0"
edition = "2024"
[dependencies]

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# WFC - Wave Function Collapse
An algorithm for procedural generation of matrices or node graphs, based on a ruleset.
Instead of going with a restrictive ruleset, this implementation allows for rulesets that act dynamically on the state
of the graph, allowing more complex rules.
## Overview
Few main concepts:
### GraphLike
Trait for a struct that holds spatial data
### Ruleset
Trait for a ruleset
### Node
Trait for any data that

27
src/lib.rs Normal file
View File

@ -0,0 +1,27 @@
/// A trait for graph-like structures, where you can get a node by a unique value and find its neighbors.
///
/// **Node** represents some settled data type
///
/// **NodeId** represents something that can be used to find an Node inside the graph, such as
/// position, index, or key to a hashmap. It should allow O(1) access to the Node.
pub trait GraphLike<Node, NodeId> {
fn get_node(&self, id: NodeId) -> Option<&Node>;
fn iter_ids(&self) -> impl Iterator<Item = NodeId>;
fn iter_neighbor_ids(&self, id: NodeId) -> impl Iterator<Item = NodeId>;
fn iter_nodes<'a>(&'a self) -> impl Iterator<Item = (NodeId, Option<&'a Node>)> where Node: 'a, NodeId: Copy {
self.iter_ids().map(|id| (id, self.get_node(id)))
}
fn iter_empty(&self) -> impl Iterator<Item = NodeId> where NodeId: Copy {
self.iter_ids().filter(|id| self.get_node(*id).is_none())
}
fn iter_neighbour_nodes(&self, id: NodeId) -> Vec<(NodeId, Option<&Node>)> where NodeId: Copy {
self.iter_neighbor_ids(id).map(|id| (id, self.get_node(id))).collect()
}
}
pub trait Ruleset<Node, NodeId> {
fn entropy(&self, graph: &impl GraphLike<Node, NodeId>, id: NodeId) -> f32 where NodeId: Copy;
fn choose(&self, graph: &impl GraphLike<Node, NodeId>, id: NodeId) -> Node where NodeId: Copy;
}