1use bytemuck::{Pod, Zeroable};
9use serde::{Deserialize, Serialize};
10
11pub const GTOP_MAGIC: [u8; 4] = *b"GTOP";
13
14#[repr(u8)]
16#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
17pub enum GeometryComplexKind {
18 #[default]
20 MetricPointCloud10D = 0x01,
21 PlanarDcel2D = 0x02,
23 SurfaceManifold2D = 0x03,
25 SimplicialComplex = 0x04,
27 MixedCellComplex3D = 0x05,
29 CsgArrangement3D = 0x06,
31 HierarchicalTreeGrid = 0x07,
33 StatisticalSimplex = 0x08,
35 ToroidalWaveLattice = 0x09,
37 HyperCanvasLayout = 0x0A,
39}
40
41impl GeometryComplexKind {
42 #[inline]
43 pub const fn from_u8(v: u8) -> Option<Self> {
44 match v {
45 0x01 => Some(Self::MetricPointCloud10D),
46 0x02 => Some(Self::PlanarDcel2D),
47 0x03 => Some(Self::SurfaceManifold2D),
48 0x04 => Some(Self::SimplicialComplex),
49 0x05 => Some(Self::MixedCellComplex3D),
50 0x06 => Some(Self::CsgArrangement3D),
51 0x07 => Some(Self::HierarchicalTreeGrid),
52 0x08 => Some(Self::StatisticalSimplex),
53 0x09 => Some(Self::ToroidalWaveLattice),
54 0x0A => Some(Self::HyperCanvasLayout),
55 _ => None,
56 }
57 }
58
59 #[inline]
60 pub fn from_name(name: &str) -> Option<Self> {
61 match name.trim() {
62 "MetricPointCloud10D" | "PointCloud" => Some(Self::MetricPointCloud10D),
63 "PlanarDcel2D" | "DCEL" => Some(Self::PlanarDcel2D),
64 "SurfaceManifold2D" | "SurfaceMesh" => Some(Self::SurfaceManifold2D),
65 "SimplicialComplex" => Some(Self::SimplicialComplex),
66 "MixedCellComplex3D" | "MixedCell" => Some(Self::MixedCellComplex3D),
67 "CsgArrangement3D" | "CSG" => Some(Self::CsgArrangement3D),
68 "HierarchicalTreeGrid" | "OctreeGrid" => Some(Self::HierarchicalTreeGrid),
69 "StatisticalSimplex" => Some(Self::StatisticalSimplex),
70 "ToroidalWaveLattice" | "ToroidalLattice" => Some(Self::ToroidalWaveLattice),
71 "HyperCanvasLayout" | "HyperCanvas" => Some(Self::HyperCanvasLayout),
72 _ => None,
73 }
74 }
75}
76
77#[repr(u8)]
79#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
80pub enum GeometryMetricKind {
81 #[default]
82 Euclidean = 0x00,
83 ToroidalCyclic = 0x01,
84 HyperbolicPoincare = 0x02,
85 SphericalGeodesic = 0x03,
86 FisherRaoSimplex = 0x04,
87 LorentzianCausal = 0x05,
88}
89
90impl GeometryMetricKind {
91 #[inline]
92 pub const fn from_u8(v: u8) -> Option<Self> {
93 match v {
94 0x00 => Some(Self::Euclidean),
95 0x01 => Some(Self::ToroidalCyclic),
96 0x02 => Some(Self::HyperbolicPoincare),
97 0x03 => Some(Self::SphericalGeodesic),
98 0x04 => Some(Self::FisherRaoSimplex),
99 0x05 => Some(Self::LorentzianCausal),
100 _ => None,
101 }
102 }
103}
104
105#[repr(u8)]
107#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
108pub enum GraphEmbedRole {
109 #[default]
111 Primal1Skeleton = 0x01,
112 PoincareDual = 0x02,
114 BoundaryIncidence = 0x03,
116 CombinatorialDartMap = 0x04,
118 SpatialHierarchy = 0x05,
120 MetricProximity = 0x06,
122 TopologicalSkeleton = 0x07,
124 SemanticOverlay = 0x08,
126 DeonticGovernance = 0x09,
128 CausalFlux = 0x0A,
130}
131
132impl GraphEmbedRole {
133 #[inline]
134 pub const fn from_u8(v: u8) -> Option<Self> {
135 match v {
136 0x01 => Some(Self::Primal1Skeleton),
137 0x02 => Some(Self::PoincareDual),
138 0x03 => Some(Self::BoundaryIncidence),
139 0x04 => Some(Self::CombinatorialDartMap),
140 0x05 => Some(Self::SpatialHierarchy),
141 0x06 => Some(Self::MetricProximity),
142 0x07 => Some(Self::TopologicalSkeleton),
143 0x08 => Some(Self::SemanticOverlay),
144 0x09 => Some(Self::DeonticGovernance),
145 0x0A => Some(Self::CausalFlux),
146 _ => None,
147 }
148 }
149
150 #[inline]
151 pub fn from_name(name: &str) -> Option<Self> {
152 match name.trim() {
153 "Primal1Skeleton" | "Primal" => Some(Self::Primal1Skeleton),
154 "PoincareDual" | "Dual" => Some(Self::PoincareDual),
155 "BoundaryIncidence" | "Incidence" => Some(Self::BoundaryIncidence),
156 "CombinatorialDartMap" | "DartMap" => Some(Self::CombinatorialDartMap),
157 "SpatialHierarchy" | "Hierarchy" => Some(Self::SpatialHierarchy),
158 "MetricProximity" | "Proximity" => Some(Self::MetricProximity),
159 "TopologicalSkeleton" | "Skeleton" => Some(Self::TopologicalSkeleton),
160 "SemanticOverlay" | "Semantic" => Some(Self::SemanticOverlay),
161 "DeonticGovernance" | "Deontic" => Some(Self::DeonticGovernance),
162 "CausalFlux" | "Causal" => Some(Self::CausalFlux),
163 _ => None,
164 }
165 }
166}
167
168#[repr(u8)]
170#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
171pub enum GraphMorphology {
172 #[default]
174 DirectedMultigraph = 0x01,
175 DeterministicDAG = 0x02,
177 HyperGraph = 0x03,
179 SymmetricGraph = 0x04,
181 BipartiteFactor = 0x05,
183 InvolutionMap = 0x06,
185}
186
187impl GraphMorphology {
188 #[inline]
189 pub const fn from_u8(v: u8) -> Option<Self> {
190 match v {
191 0x01 => Some(Self::DirectedMultigraph),
192 0x02 => Some(Self::DeterministicDAG),
193 0x03 => Some(Self::HyperGraph),
194 0x04 => Some(Self::SymmetricGraph),
195 0x05 => Some(Self::BipartiteFactor),
196 0x06 => Some(Self::InvolutionMap),
197 _ => None,
198 }
199 }
200
201 #[inline]
202 pub fn from_name(name: &str) -> Option<Self> {
203 match name.trim() {
204 "DirectedMultigraph" | "Multigraph" => Some(Self::DirectedMultigraph),
205 "DeterministicDAG" | "DAG" => Some(Self::DeterministicDAG),
206 "HyperGraph" | "Hypergraph" => Some(Self::HyperGraph),
207 "SymmetricGraph" | "Symmetric" | "SimplicialComplex" => Some(Self::SymmetricGraph),
208 "BipartiteFactor" | "Bipartite" => Some(Self::BipartiteFactor),
209 "InvolutionMap" | "ToroidalLattice" => Some(Self::InvolutionMap),
210 _ => None,
211 }
212 }
213}
214
215#[repr(u8)]
217#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
218pub enum AnchorDomain {
219 #[default]
221 SemanticQuin = 0xFF,
222 Cell0Vertex = 0,
224 Cell1Edge = 1,
226 Cell2Face = 2,
228 Cell3Volume = 3,
230 CellKSimplex = 4,
232}
233
234impl AnchorDomain {
235 #[inline]
236 pub const fn from_u8(v: u8) -> Option<Self> {
237 match v {
238 0 => Some(Self::Cell0Vertex),
239 1 => Some(Self::Cell1Edge),
240 2 => Some(Self::Cell2Face),
241 3 => Some(Self::Cell3Volume),
242 4 => Some(Self::CellKSimplex),
243 0xFF => Some(Self::SemanticQuin),
244 _ => None,
245 }
246 }
247
248 #[inline]
249 pub fn from_name(name: &str) -> Option<Self> {
250 match name.trim() {
251 "Cell0Vertex" | "Vertex" | "0" => Some(Self::Cell0Vertex),
252 "Cell1Edge" | "Edge" | "1" => Some(Self::Cell1Edge),
253 "Cell2Face" | "Face" | "2" => Some(Self::Cell2Face),
254 "Cell3Volume" | "Volume" | "3" => Some(Self::Cell3Volume),
255 "CellKSimplex" | "Simplex" | "4" => Some(Self::CellKSimplex),
256 "SemanticQuin" | "NQuin" | "Quin" => Some(Self::SemanticQuin),
257 _ => None,
258 }
259 }
260}
261
262#[repr(transparent)]
264#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Pod, Zeroable, Serialize, Deserialize)]
265pub struct TopologyInvariants(pub u16);
266
267impl TopologyInvariants {
268 pub const EMPTY: Self = Self(0);
269 pub const ACYCLIC: Self = Self(1 << 0);
271 pub const TRANSITIVE: Self = Self(1 << 1);
273 pub const SYMMETRIC: Self = Self(1 << 2);
275 pub const REFLEXIVE: Self = Self(1 << 3);
277 pub const REIFIED_EDGES: Self = Self(1 << 4);
279 pub const SPATIAL_METRIC: Self = Self(1 << 5);
281 pub const TEMPORAL_MONOTONIC: Self = Self(1 << 6);
283 pub const DEONTIC_GUARDED: Self = Self(1 << 7);
285 pub const PARACONSISTENT_ISOLATED: Self = Self(1 << 8);
287 pub const MANIFOLD_CLOSED: Self = Self(1 << 9);
289 pub const SENSITIVITY_MONOTONIC: Self = Self(1 << 10);
291 pub const CHAIN_BOUNDARY_EXACT: Self = Self(1 << 11);
293
294 #[inline]
295 pub const fn contains(self, other: Self) -> bool { (self.0 & other.0) == other.0 }
296 #[inline]
297 pub const fn union(self, other: Self) -> Self { Self(self.0 | other.0) }
298 #[inline]
299 pub const fn bits(self) -> u16 { self.0 }
300 #[inline]
301 pub const fn from_bits_truncate(bits: u16) -> Self { Self(bits) }
302
303 pub fn from_name(name: &str) -> Option<Self> {
304 match name.trim() {
305 "ACYCLIC" => Some(Self::ACYCLIC),
306 "TRANSITIVE" => Some(Self::TRANSITIVE),
307 "SYMMETRIC" => Some(Self::SYMMETRIC),
308 "REFLEXIVE" => Some(Self::REFLEXIVE),
309 "REIFIED_EDGES" => Some(Self::REIFIED_EDGES),
310 "SPATIAL_METRIC" => Some(Self::SPATIAL_METRIC),
311 "TEMPORAL_MONOTONIC" => Some(Self::TEMPORAL_MONOTONIC),
312 "DEONTIC_GUARDED" => Some(Self::DEONTIC_GUARDED),
313 "PARACONSISTENT_ISOLATED" => Some(Self::PARACONSISTENT_ISOLATED),
314 "MANIFOLD_CLOSED" => Some(Self::MANIFOLD_CLOSED),
315 "SENSITIVITY_MONOTONIC" => Some(Self::SENSITIVITY_MONOTONIC),
316 "CHAIN_BOUNDARY_EXACT" | "BOUNDARY_EXACT" => Some(Self::CHAIN_BOUNDARY_EXACT),
317 _ => None,
318 }
319 }
320}
321
322impl core::ops::BitOr for TopologyInvariants {
323 type Output = Self;
324 #[inline]
325 fn bitor(self, rhs: Self) -> Self::Output {
326 Self(self.0 | rhs.0)
327 }
328}
329
330impl core::ops::BitOrAssign for TopologyInvariants {
331 #[inline]
332 fn bitor_assign(&mut self, rhs: Self) {
333 self.0 |= rhs.0;
334 }
335}
336
337#[repr(C, align(16))]
340#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Pod, Zeroable, Serialize, Deserialize)]
341pub struct CellAnchor {
342 pub entity_hash: u64,
344 pub cell_index: u32,
346 pub anchor_domain: u8,
348 pub orientation_sign: i8,
350 pub material_or_stratum: u16,
352}
353
354const _: () = assert!(core::mem::size_of::<CellAnchor>() == 16);
355const _: () = assert!(core::mem::align_of::<CellAnchor>() == 16);
356
357impl CellAnchor {
358 #[inline]
359 pub const fn domain(&self) -> Option<AnchorDomain> {
360 AnchorDomain::from_u8(self.anchor_domain)
361 }
362}
363
364#[repr(C, align(64))]
368#[derive(Copy, Clone, Debug, PartialEq, Eq, Pod, Zeroable, Serialize, Deserialize)]
369pub struct PoetGeometryHeader {
370 pub geometry_id: u64,
371 pub construct_schema_hash: u64,
372 pub manifold_id: u32,
373 pub cell_0_count: u32,
374 pub cell_1_count: u32,
375 pub cell_2_count: u32,
376 pub cell_3_count: u32,
377 pub geometry_payload_offset: u32,
378 pub ddg_operators_offset: u32,
379 pub graph_dir_offset: u32,
380 pub harboured_graph_count: u16,
381 pub active_graph_slot: u16,
382 pub euler_characteristic: i32,
383 pub complex_kind: u8,
384 pub metric_kind: u8,
385 pub ambient_dim: u8,
386 pub intrinsic_dim: u8,
387 pub crc32c: u32,
388}
389
390const _: () = assert!(core::mem::size_of::<PoetGeometryHeader>() == 64);
391const _: () = assert!(core::mem::align_of::<PoetGeometryHeader>() == 64);
392
393impl Default for PoetGeometryHeader {
394 fn default() -> Self { Self::zeroed() }
395}
396
397impl PoetGeometryHeader {
398 #[inline]
399 pub const fn complex_kind_enum(&self) -> Option<GeometryComplexKind> { GeometryComplexKind::from_u8(self.complex_kind) }
400 #[inline]
401 pub const fn metric_kind_enum(&self) -> Option<GeometryMetricKind> { GeometryMetricKind::from_u8(self.metric_kind) }
402 #[inline]
403 pub const fn computed_euler_characteristic(&self) -> i32 {
404 (self.cell_0_count as i32).wrapping_sub(self.cell_1_count as i32)
405 .wrapping_add(self.cell_2_count as i32).wrapping_sub(self.cell_3_count as i32)
406 }
407}
408
409#[repr(C, align(64))]
411#[derive(Copy, Clone, Debug, PartialEq, Eq, Pod, Zeroable, Serialize, Deserialize)]
412pub struct HarbouredGraphHeader {
413 pub graph_id: u64,
414 pub parent_geometry_id: u64,
415 pub anchor_table_offset: u32,
416 pub csr_row_ptr_offset: u32,
417 pub csr_col_idx_offset: u32,
418 pub edge_anchor_offset: u32,
419 pub csc_col_ptr_offset: u32,
420 pub csc_row_idx_offset: u32,
421 pub edge_weight_offset: u32,
422 pub vertex_count: u32,
423 pub edge_count: u32,
424 pub context_and_state_mask: u32,
425 pub embed_role: u8,
426 pub morphology: u8,
427 pub invariants: TopologyInvariants,
428 pub crc32c: u32,
429}
430
431const _: () = assert!(core::mem::size_of::<HarbouredGraphHeader>() == 64);
432const _: () = assert!(core::mem::align_of::<HarbouredGraphHeader>() == 64);
433
434impl Default for HarbouredGraphHeader {
435 fn default() -> Self {
436 Self::zeroed()
437 }
438}
439
440impl HarbouredGraphHeader {
441 #[inline]
442 pub const fn embed_role_enum(&self) -> Option<GraphEmbedRole> {
443 GraphEmbedRole::from_u8(self.embed_role)
444 }
445
446 #[inline]
447 pub const fn morphology_enum(&self) -> Option<GraphMorphology> {
448 GraphMorphology::from_u8(self.morphology)
449 }
450
451 #[inline]
452 pub const fn max_sensitivity_class(&self) -> u8 {
453 (self.context_and_state_mask & 0xFF) as u8
454 }
455
456 #[inline]
457 pub const fn max_depth(&self) -> u8 {
458 ((self.context_and_state_mask >> 24) & 0xFF) as u8
459 }
460}
461
462#[derive(Copy, Clone, Debug, PartialEq, Eq)]
464pub enum TopologyError {
465 BufferTooSmall,
466 UnalignedBuffer,
467 InvalidHeaderSize,
468 InvalidComplexKind(u8),
469 InvalidMorphology(u8),
470 InvalidEmbedRole(u8),
471 GraphSlotOutOfRange { slot: u16, count: u16 },
472 VertexOutOfRange { vertex: u32, count: u32 },
473 CellOutOfRange { domain: u8, index: u32, count: u32 },
474 NonMonotonicRowPtr { vertex: u32 },
475 ChecksumMismatch { expected: u32, actual: u32 },
476 DeltaRingFull { capacity: usize },
477 CycleDetected { source: u32, target: u32 },
478 TemporalMonotonicityViolation { source: u32, target: u32 },
479 DeonticForbidViolation { edge_anchor_idx: u32 },
480 ParaconsistentContagion { edge_anchor_idx: u32 },
481 SensitivityEscalation { source_class: u8, target_class: u8 },
482 ChainBoundaryNotZero { non_zero_terms: usize },
483 BudgetExhausted { requested: usize, budget: usize },
484}
485
486impl core::fmt::Display for TopologyError {
487 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
488 write!(f, "{:?}", self)
489 }
490}
491
492impl std::error::Error for TopologyError {}