spark-hive
Multi-agent swarm coordination and peer routing engine
Brought by drakestapleton
spark-hive / src / geometry.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | |
| 3 | pub const DEFAULT_HEX_RADIUS: f64 = 88.0; |
| 4 | pub const SQRT_3: f64 = 1.732050807568877293527446341505872367; |
| 5 | |
| 6 | /// 6 Axial Directions: |
| 7 | /// [1, 0] East, [1, -1] Northeast, [0, -1] Northwest, [-1, 0] West, [-1, 1] Southwest, [0, 1] Southeast |
| 8 | pub const HEX_DIRECTIONS: [(i32, i32); 6] = [ |
| 9 | (1, 0), // East |
| 10 | (1, -1), // Northeast |
| 11 | (0, -1), // Northwest |
| 12 | (-1, 0), // West |
| 13 | (-1, 1), // Southwest |
| 14 | (0, 1), // Southeast |
| 15 | ]; |
| 16 | |
| 17 | pub const HEX_DIRECTION_LABELS: [&str; 6] = [ |
| 18 | "east", |
| 19 | "northeast", |
| 20 | "northwest", |
| 21 | "west", |
| 22 | "southwest", |
| 23 | "southeast", |
| 24 | ]; |
| 25 | |
| 26 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 27 | pub struct HexCoord { |
| 28 | pub q: i32, |
| 29 | pub r: i32, |
| 30 | } |
| 31 | |
| 32 | impl HexCoord { |
| 33 | pub const fn new(q: i32, r: i32) -> Self { |
| 34 | Self { q, r } |
| 35 | } |
| 36 | |
| 37 | pub fn distance(&self, other: HexCoord) -> i32 { |
| 38 | let dq = self.q - other.q; |
| 39 | let dr = self.r - other.r; |
| 40 | dq.abs().max(dr.abs()).max((dq + dr).abs()) |
| 41 | } |
| 42 | |
| 43 | pub fn neighbors(&self) -> [HexCoord; 6] { |
| 44 | [ |
| 45 | HexCoord::new(self.q + 1, self.r), |
| 46 | HexCoord::new(self.q + 1, self.r - 1), |
| 47 | HexCoord::new(self.q, self.r - 1), |
| 48 | HexCoord::new(self.q - 1, self.r), |
| 49 | HexCoord::new(self.q - 1, self.r + 1), |
| 50 | HexCoord::new(self.q, self.r + 1), |
| 51 | ] |
| 52 | } |
| 53 | |
| 54 | pub fn to_pixel(&self, radius: f64) -> (f64, f64) { |
| 55 | axial_to_pixel(self.q, self.r, radius) |
| 56 | } |
| 57 | |
| 58 | pub fn from_pixel(x: f64, y: f64, radius: f64) -> Self { |
| 59 | pixel_to_axial(x, y, radius) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn neighbors(q: i32, r: i32) -> [HexCoord; 6] { |
| 64 | HexCoord::new(q, r).neighbors() |
| 65 | } |
| 66 | |
| 67 | pub fn axial_to_pixel(q: i32, r: i32, radius: f64) -> (f64, f64) { |
| 68 | let x = radius * SQRT_3 * (q as f64 + (r as f64) / 2.0); |
| 69 | let y = radius * 1.5 * (r as f64); |
| 70 | (x, y) |
| 71 | } |
| 72 | |
| 73 | pub fn round_axial(cube_x: f64, cube_z: f64) -> HexCoord { |
| 74 | let cube_y = -cube_x - cube_z; |
| 75 | let mut rx = cube_x.round(); |
| 76 | let ry = cube_y.round(); |
| 77 | let mut rz = cube_z.round(); |
| 78 | |
| 79 | let dx = (rx - cube_x).abs(); |
| 80 | let dy = (ry - cube_y).abs(); |
| 81 | let dz = (rz - cube_z).abs(); |
| 82 | |
| 83 | if dx > dy && dx > dz { |
| 84 | rx = -ry - rz; |
| 85 | } else if dz > dy { |
| 86 | rz = -rx - ry; |
| 87 | } |
| 88 | |
| 89 | HexCoord::new(rx as i32, rz as i32) |
| 90 | } |
| 91 | |
| 92 | pub fn pixel_to_axial(x: f64, y: f64, radius: f64) -> HexCoord { |
| 93 | let q = (SQRT_3 / 3.0 * x - y / 3.0) / radius; |
| 94 | let r = (2.0 / 3.0 * y) / radius; |
| 95 | round_axial(q, r) |
| 96 | } |
| 97 | |
| 98 | pub fn hex_distance(q1: i32, r1: i32, q2: i32, r2: i32) -> i32 { |
| 99 | let dq = q1 - q2; |
| 100 | let dr = r1 - r2; |
| 101 | dq.abs().max(dr.abs()).max((dq + dr).abs()) |
| 102 | } |
| 103 | |
| 104 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 105 | pub enum DetailTier { |
| 106 | Full, // >= 0.62 zoom |
| 107 | Compact, // >= 0.34 zoom |
| 108 | Dot, // < 0.34 zoom |
| 109 | } |
| 110 | |
| 111 | impl DetailTier { |
| 112 | pub fn for_zoom(zoom: f64) -> Self { |
| 113 | if zoom >= 0.62 { |
| 114 | DetailTier::Full |
| 115 | } else if zoom >= 0.34 { |
| 116 | DetailTier::Compact |
| 117 | } else { |
| 118 | DetailTier::Dot |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | pub fn as_str(&self) -> &'static str { |
| 123 | match self { |
| 124 | DetailTier::Full => "full", |
| 125 | DetailTier::Compact => "compact", |
| 126 | DetailTier::Dot => "dot", |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 132 | pub enum PlacementIntent { |
| 133 | Independent, |
| 134 | Join, |
| 135 | Branch, |
| 136 | Meet, |
| 137 | } |
| 138 | |
| 139 | impl PlacementIntent { |
| 140 | pub fn parse(s: &str) -> Self { |
| 141 | match s.trim().to_lowercase().as_str() { |
| 142 | "join" => PlacementIntent::Join, |
| 143 | "branch" => PlacementIntent::Branch, |
| 144 | "meet" => PlacementIntent::Meet, |
| 145 | _ => PlacementIntent::Independent, |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | pub fn as_str(&self) -> &'static str { |
| 150 | match self { |
| 151 | PlacementIntent::Independent => "independent", |
| 152 | PlacementIntent::Join => "join", |
| 153 | PlacementIntent::Branch => "branch", |
| 154 | PlacementIntent::Meet => "meet", |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[cfg(test)] |
| 160 | mod tests { |
| 161 | use super::*; |
| 162 | |
| 163 | #[test] |
| 164 | fn test_hex_directions() { |
| 165 | assert_eq!(HEX_DIRECTIONS.len(), 6); |
| 166 | assert_eq!(HEX_DIRECTIONS[0], (1, 0)); // East |
| 167 | assert_eq!(HEX_DIRECTIONS[1], (1, -1)); // Northeast |
| 168 | assert_eq!(HEX_DIRECTIONS[2], (0, -1)); // Northwest |
| 169 | assert_eq!(HEX_DIRECTIONS[3], (-1, 0)); // West |
| 170 | assert_eq!(HEX_DIRECTIONS[4], (-1, 1)); // Southwest |
| 171 | assert_eq!(HEX_DIRECTIONS[5], (0, 1)); // Southeast |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn test_axial_to_pixel_and_back_roundtrip() { |
| 176 | let test_points = [(0, 0), (1, 0), (1, -1), (-4, 3), (7, -2)]; |
| 177 | for (q, r) in test_points { |
| 178 | let (x, y) = axial_to_pixel(q, r, DEFAULT_HEX_RADIUS); |
| 179 | let back = pixel_to_axial(x, y, DEFAULT_HEX_RADIUS); |
| 180 | assert_eq!(back, HexCoord::new(q, r), "Mismatch for point ({}, {})", q, r); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn test_hex_distance() { |
| 186 | assert_eq!(hex_distance(0, 0, 0, 0), 0); |
| 187 | assert_eq!(hex_distance(0, 0, 1, 0), 1); |
| 188 | assert_eq!(hex_distance(0, 0, 1, -1), 1); |
| 189 | assert_eq!(hex_distance(0, 0, 2, 2), 4); |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn test_neighbors() { |
| 194 | let origin = HexCoord::new(3, -2); |
| 195 | let nbrs = neighbors(3, -2); |
| 196 | assert_eq!(nbrs.len(), 6); |
| 197 | for n in nbrs { |
| 198 | assert_eq!(origin.distance(n), 1); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn test_detail_tiers() { |
| 204 | assert_eq!(DetailTier::for_zoom(1.0), DetailTier::Full); |
| 205 | assert_eq!(DetailTier::for_zoom(0.62), DetailTier::Full); |
| 206 | assert_eq!(DetailTier::for_zoom(0.61), DetailTier::Compact); |
| 207 | assert_eq!(DetailTier::for_zoom(0.34), DetailTier::Compact); |
| 208 | assert_eq!(DetailTier::for_zoom(0.33), DetailTier::Dot); |
| 209 | assert_eq!(DetailTier::for_zoom(0.10), DetailTier::Dot); |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn test_placement_intents() { |
| 214 | assert_eq!(PlacementIntent::parse("join"), PlacementIntent::Join); |
| 215 | assert_eq!(PlacementIntent::parse("BRANCH"), PlacementIntent::Branch); |
| 216 | assert_eq!(PlacementIntent::parse("meet"), PlacementIntent::Meet); |
| 217 | assert_eq!(PlacementIntent::parse("other"), PlacementIntent::Independent); |
| 218 | } |
| 219 | } |