Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/spark-hive/src/geometry.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / spark-hive / src / geometry.rs` 243 lines ยท 6995 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use serde::{Deserialize, Serialize}; 17 18 pub const DEFAULT_HEX_RADIUS: f64 = 88.0; 19 pub const SQRT_3: f64 = 1.`732050807568877293527446341505872367`; 20 21 /// 6 Axial Directions: 22 /// [1, 0] East, [1, -1] Northeast, [0, -1] Northwest, [-1, 0] West, [-1, 1] Southwest, [0, 1] Southeast 23 pub const HEX_DIRECTIONS: [(i32, i32); 6] = [ 24 (1, 0), // East 25 (1, -1), // Northeast 26 (0, -1), // Northwest 27 (-1, 0), // West 28 (-1, 1), // Southwest 29 (0, 1), // Southeast 30 ]; 31 32 pub const HEX_DIRECTION_LABELS: [&str; 6] = [ 33 "east", 34 "northeast", 35 "northwest", 36 "west", 37 "southwest", 38 "southeast", 39 ]; 40 41 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] 42 pub struct HexCoord { 43 pub q: i32, 44 pub r: i32, 45 } 46 47 impl HexCoord { 48 pub const fn new(q: i32, r: i32) -> Self { 49 Self { q, r } 50 } 51 52 pub fn distance(&self, other: HexCoord) -> i32 { 53 let dq = self.q - other.q; 54 let dr = self.r - other.r; 55 dq.abs().max(dr.abs()).max((dq + dr).abs()) 56 } 57 58 pub fn neighbors(&self) -> [HexCoord; 6] { 59 [ 60 HexCoord::new(self.q + 1, self.r), 61 HexCoord::new(self.q + 1, self.r - 1), 62 HexCoord::new(self.q, self.r - 1), 63 HexCoord::new(self.q - 1, self.r), 64 HexCoord::new(self.q - 1, self.r + 1), 65 HexCoord::new(self.q, self.r + 1), 66 ] 67 } 68 69 pub fn to_pixel(&self, radius: f64) -> (f64, f64) { 70 axial_to_pixel(self.q, self.r, radius) 71 } 72 73 pub fn from_pixel(x: f64, y: f64, radius: f64) -> Self { 74 pixel_to_axial(x, y, radius) 75 } 76 } 77 78 pub fn neighbors(q: i32, r: i32) -> [HexCoord; 6] { 79 HexCoord::new(q, r).neighbors() 80 } 81 82 pub fn axial_to_pixel(q: i32, r: i32, radius: f64) -> (f64, f64) { 83 let x = radius * SQRT_3 * (q as f64 + (r as f64) / 2.0); 84 let y = radius * 1.5 * (r as f64); 85 (x, y) 86 } 87 88 pub fn round_axial(cube_x: f64, cube_z: f64) -> HexCoord { 89 let cube_y = -cube_x - cube_z; 90 let mut rx = cube_x.round(); 91 let ry = cube_y.round(); 92 let mut rz = cube_z.round(); 93 94 let dx = (rx - cube_x).abs(); 95 let dy = (ry - cube_y).abs(); 96 let dz = (rz - cube_z).abs(); 97 98 if dx > dy && dx > dz { 99 rx = -ry - rz; 100 } else if dz > dy { 101 rz = -rx - ry; 102 } 103 104 HexCoord::new(rx as i32, rz as i32) 105 } 106 107 pub fn pixel_to_axial(x: f64, y: f64, radius: f64) -> HexCoord { 108 let q = (SQRT_3 / 3.0 * x - y / 3.0) / radius; 109 let r = (2.0 / 3.0 * y) / radius; 110 round_axial(q, r) 111 } 112 113 pub fn hex_distance(q1: i32, r1: i32, q2: i32, r2: i32) -> i32 { 114 let dq = q1 - q2; 115 let dr = r1 - r2; 116 dq.abs().max(dr.abs()).max((dq + dr).abs()) 117 } 118 119 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] 120 pub enum DetailTier { 121 Full, // >= 0.62 zoom 122 Compact, // >= 0.34 zoom 123 Dot, // < 0.34 zoom 124 } 125 126 impl DetailTier { 127 pub fn for_zoom(zoom: f64) -> Self { 128 if zoom >= 0.62 { 129 DetailTier::Full 130 } else if zoom >= 0.34 { 131 DetailTier::Compact 132 } else { 133 DetailTier::Dot 134 } 135 } 136 137 pub fn as_str(&self) -> &'static str { 138 match self { 139 DetailTier::Full => "full", 140 DetailTier::Compact => "compact", 141 DetailTier::Dot => "dot", 142 } 143 } 144 } 145 146 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] 147 pub enum PlacementIntent { 148 Independent, 149 Join, 150 Branch, 151 Meet, 152 } 153 154 impl PlacementIntent { 155 pub fn parse(s: &str) -> Self { 156 match s.trim().to_lowercase().as_str() { 157 "join" => PlacementIntent::Join, 158 "branch" => PlacementIntent::Branch, 159 "meet" => PlacementIntent::Meet, 160 _ => PlacementIntent::Independent, 161 } 162 } 163 164 pub fn as_str(&self) -> &'static str { 165 match self { 166 PlacementIntent::Independent => "independent", 167 PlacementIntent::Join => "join", 168 PlacementIntent::Branch => "branch", 169 PlacementIntent::Meet => "meet", 170 } 171 } 172 } 173 174 #[cfg(test)] 175 mod tests { 176 use super::*; 177 178 #[test] 179 fn test_hex_directions() { 180 assert_eq!(HEX_DIRECTIONS.len(), 6); 181 assert_eq!(HEX_DIRECTIONS[0], (1, 0)); // East 182 assert_eq!(HEX_DIRECTIONS[1], (1, -1)); // Northeast 183 assert_eq!(HEX_DIRECTIONS[2], (0, -1)); // Northwest 184 assert_eq!(HEX_DIRECTIONS[3], (-1, 0)); // West 185 assert_eq!(HEX_DIRECTIONS[4], (-1, 1)); // Southwest 186 assert_eq!(HEX_DIRECTIONS[5], (0, 1)); // Southeast 187 } 188 189 #[test] 190 fn test_axial_to_pixel_and_back_roundtrip() { 191 let test_points = [(0, 0), (1, 0), (1, -1), (-4, 3), (7, -2)]; 192 for (q, r) in test_points { 193 let (x, y) = axial_to_pixel(q, r, DEFAULT_HEX_RADIUS); 194 let back = pixel_to_axial(x, y, DEFAULT_HEX_RADIUS); 195 assert_eq!( 196 back, 197 HexCoord::new(q, r), 198 "Mismatch for point ({}, {})", 199 q, 200 r 201 ); 202 } 203 } 204 205 #[test] 206 fn test_hex_distance() { 207 assert_eq!(hex_distance(0, 0, 0, 0), 0); 208 assert_eq!(hex_distance(0, 0, 1, 0), 1); 209 assert_eq!(hex_distance(0, 0, 1, -1), 1); 210 assert_eq!(hex_distance(0, 0, 2, 2), 4); 211 } 212 213 #[test] 214 fn test_neighbors() { 215 let origin = HexCoord::new(3, -2); 216 let nbrs = neighbors(3, -2); 217 assert_eq!(nbrs.len(), 6); 218 for n in nbrs { 219 assert_eq!(origin.distance(n), 1); 220 } 221 } 222 223 #[test] 224 fn test_detail_tiers() { 225 assert_eq!(DetailTier::for_zoom(1.0), DetailTier::Full); 226 assert_eq!(DetailTier::for_zoom(0.62), DetailTier::Full); 227 assert_eq!(DetailTier::for_zoom(0.61), DetailTier::Compact); 228 assert_eq!(DetailTier::for_zoom(0.34), DetailTier::Compact); 229 assert_eq!(DetailTier::for_zoom(0.33), DetailTier::Dot); 230 assert_eq!(DetailTier::for_zoom(0.10), DetailTier::Dot); 231 } 232 233 #[test] 234 fn test_placement_intents() { 235 assert_eq!(PlacementIntent::parse("join"), PlacementIntent::Join); 236 assert_eq!(PlacementIntent::parse("BRANCH"), PlacementIntent::Branch); 237 assert_eq!(PlacementIntent::parse("meet"), PlacementIntent::Meet); 238 assert_eq!( 239 PlacementIntent::parse("other"), 240 PlacementIntent::Independent 241 ); 242 } 243 }