From 28c57905a33afb5e1b7900f3d7e7db7ceeaa1bd6 Mon Sep 17 00:00:00 2001 From: hheik <4469778+hheik@users.noreply.github.com> Date: Thu, 23 Apr 2026 19:41:24 +0300 Subject: [PATCH] Added basic traits --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 6 ++++++ README.md | 22 ++++++++++++++++++++++ src/lib.rs | 27 +++++++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ab96e98 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d974d89 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "wfc" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/README.md b/README.md new file mode 100644 index 0000000..f4bcab8 --- /dev/null +++ b/README.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e62c9aa --- /dev/null +++ b/src/lib.rs @@ -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 { + fn get_node(&self, id: NodeId) -> Option<&Node>; + fn iter_ids(&self) -> impl Iterator; + fn iter_neighbor_ids(&self, id: NodeId) -> impl Iterator; + + fn iter_nodes<'a>(&'a self) -> impl Iterator)> where Node: 'a, NodeId: Copy { + self.iter_ids().map(|id| (id, self.get_node(id))) + } + fn iter_empty(&self) -> impl Iterator 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 { + fn entropy(&self, graph: &impl GraphLike, id: NodeId) -> f32 where NodeId: Copy; + fn choose(&self, graph: &impl GraphLike, id: NodeId) -> Node where NodeId: Copy; +}