diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 573dfb063..459b4cc5a 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -11,7 +11,7 @@ publish = false autoexamples = false [dev-dependencies] -honeycomb-core = { workspace = true, features = ["io"] } +honeycomb-core = { workspace = true } honeycomb-render = { workspace = true } honeycomb-kernels = { workspace = true } rand = { workspace = true, features = ["small_rng"] } diff --git a/honeycomb-core/Cargo.toml b/honeycomb-core/Cargo.toml index b06939f7f..55c6f0c94 100644 --- a/honeycomb-core/Cargo.toml +++ b/honeycomb-core/Cargo.toml @@ -12,8 +12,7 @@ keywords.workspace = true authors.workspace = true publish = true -[features] -io = ["dep:vtkio"] +# [features] # deps @@ -24,7 +23,7 @@ loom.workspace= true num-traits.workspace = true stm.workspace = true thiserror.workspace = true -vtkio = { workspace = true, optional = true } +vtkio.workspace = true [build-dependencies] rustversion.workspace = true diff --git a/honeycomb-core/src/cmap/builder/io/mod.rs b/honeycomb-core/src/cmap/builder/io.rs similarity index 95% rename from honeycomb-core/src/cmap/builder/io/mod.rs rename to honeycomb-core/src/cmap/builder/io.rs index 1e19c31e2..5c4723180 100644 --- a/honeycomb-core/src/cmap/builder/io/mod.rs +++ b/honeycomb-core/src/cmap/builder/io.rs @@ -1,19 +1,13 @@ -//! Input/Output features implementation -//! -//! The support for I/O is currently very restricted since this is not the focus of this project. -//! Maps can be built from and serialized to VTK legacy files (both binary and ASCII). The -//! `DATASET` of the VTK file should be `UNSTRUCTURED_GRID`, and only a given set of `CELL_TYPES` -//! are supported, because of orientation and dimension restriction. - -// ------ IMPORTS use crate::prelude::{BuilderError, CMap2, CMapBuilder, DartIdType, Vertex2, VertexIdType}; use crate::{attributes::AttrStorageManager, geometry::CoordsFloat}; -use num_traits::Zero; + use std::collections::BTreeMap; + +use num_traits::Zero; use vtkio::model::{CellType, DataSet, VertexNumbers}; use vtkio::{IOBuffer, Vtk}; -// ------ CONTENT +// --- descriptor impls impl CMapBuilder { /// Import and set the VTK file that will be used when building the map. @@ -32,14 +26,6 @@ impl CMapBuilder { /// Create a [`CMapBuilder`] from an imported VTK file. /// -/// This implementation is roughly equivalent to the following: -/// -/// ```rust,no_run -/// # use honeycomb_core::prelude::CMapBuilder; -/// // `CMapBuilder::from_vtk_file("some/path/to/file.vtk")`, or: -/// let builder = CMapBuilder::::default().vtk_file("some/path/to/file.vtk"); -/// ``` -/// /// # Panics /// /// This function may panic if the file cannot be loaded. @@ -54,6 +40,8 @@ impl + std::fmt::Debug> From

for CM } } +// --- building routine + macro_rules! if_predicate_return_err { ($pr: expr, $er: expr) => { if $pr { @@ -315,7 +303,3 @@ pub fn build_2d_from_vtk( } Ok(cmap) } - -// ------ TESTS -#[cfg(test)] -mod tests; diff --git a/honeycomb-core/src/cmap/builder/io/tests.rs b/honeycomb-core/src/cmap/builder/io/tests.rs deleted file mode 100644 index d4463a43c..000000000 --- a/honeycomb-core/src/cmap/builder/io/tests.rs +++ /dev/null @@ -1,93 +0,0 @@ -// ------ IMPORTS - -use crate::attributes::AttrStorageManager; -use crate::prelude::{CMap2, DartIdType, Orbit2, OrbitPolicy}; -use vtkio::Vtk; - -// ------ CONTENT - -#[test] -fn io_read() { - let vtk = Vtk::parse_legacy_be(VTK_ASCII).unwrap(); - // unwrap is fine since we know the VTK_ASCII const is correct - let cmap: CMap2 = super::build_2d_from_vtk(vtk, AttrStorageManager::default()).unwrap(); - - // check result - let faces: Vec<_> = cmap.iter_faces().collect(); - assert_eq!(faces.len(), 4); - assert_eq!(cmap.iter_edges().count(), 12); - assert_eq!(cmap.iter_vertices().count(), 9); - - let mut n_vertices_per_face: Vec = faces - .iter() - .map(|id| Orbit2::new(&cmap, OrbitPolicy::Face, *id as DartIdType).count()) - .collect(); - let (mut three_count, mut four_count, mut six_count): (usize, usize, usize) = (0, 0, 0); - while let Some(n) = n_vertices_per_face.pop() { - match n { - 3 => three_count += 1, - 4 => four_count += 1, - 6 => six_count += 1, - _ => panic!("cmap was built incorrectly"), - } - } - assert_eq!(three_count, 2); - assert_eq!(four_count, 1); - assert_eq!(six_count, 1); -} - -#[cfg(all(test, feature = "io"))] -const VTK_ASCII: &[u8] = b" -# vtk DataFile Version 2.0 -cmap -ASCII - -DATASET UNSTRUCTURED_GRID -POINTS 9 float -0 0 0 1 0 0 1 1 0 -0 1 0 2 0 0 2 1 0 -2 2 0 1 3 0 0 2 0 - -CELLS 17 54 -1 0 -1 4 -1 6 -1 7 -1 8 -2 0 1 -2 3 0 -2 1 4 -2 4 5 -2 5 6 -2 6 7 -2 7 8 -2 8 3 -4 0 1 2 3 -3 1 4 5 -3 1 5 2 -6 3 2 5 6 7 8 - -CELL_TYPES 17 -1 -1 -1 -1 -1 -3 -3 -3 -3 -3 -3 -3 -3 -9 -5 -5 -7 - - -POINT_DATA 9 - -CELL_DATA 17 -"; diff --git a/honeycomb-core/src/cmap/builder/mod.rs b/honeycomb-core/src/cmap/builder/mod.rs index dd4e59c57..53d350138 100644 --- a/honeycomb-core/src/cmap/builder/mod.rs +++ b/honeycomb-core/src/cmap/builder/mod.rs @@ -3,10 +3,8 @@ // ------ MODULE DECLARATIONS pub mod grid; -pub mod structure; - -#[cfg(feature = "io")] pub mod io; +pub mod structure; // ------ RE-EXPORTS diff --git a/honeycomb-core/src/cmap/builder/structure.rs b/honeycomb-core/src/cmap/builder/structure.rs index a75fba371..1b5bda44f 100644 --- a/honeycomb-core/src/cmap/builder/structure.rs +++ b/honeycomb-core/src/cmap/builder/structure.rs @@ -4,7 +4,6 @@ use crate::prelude::{AttributeBind, CMap2, GridDescriptor}; use crate::{attributes::AttrStorageManager, geometry::CoordsFloat}; use thiserror::Error; -#[cfg(feature = "io")] use vtkio::Vtk; // ------ CONTENT @@ -53,7 +52,6 @@ pub struct CMapBuilder where T: CoordsFloat, { - #[cfg(feature = "io")] pub(super) vtk_file: Option, pub(super) grid_descriptor: Option>, pub(super) attributes: AttrStorageManager, @@ -177,7 +175,6 @@ impl CMapBuilder { /// /// See [`CMapBuilder`] example. pub fn build(self) -> Result, BuilderError> { - #[cfg(feature = "io")] if let Some(vfile) = self.vtk_file { // build from vtk // this routine should return a Result instead of the map directly diff --git a/honeycomb-core/src/cmap/builder/tests.rs b/honeycomb-core/src/cmap/builder/tests.rs index a80e213b8..52184e8b5 100644 --- a/honeycomb-core/src/cmap/builder/tests.rs +++ b/honeycomb-core/src/cmap/builder/tests.rs @@ -1,4 +1,7 @@ -use crate::prelude::{CMap2, CMapBuilder, GridDescriptor}; +use crate::attributes::AttrStorageManager; +use crate::prelude::{CMap2, CMapBuilder, DartIdType, GridDescriptor, Orbit2, OrbitPolicy}; + +use vtkio::Vtk; // --- basic @@ -357,3 +360,92 @@ fn splitsquare_cmap2_correctness() { assert_eq!(cmap.beta::<2>(23), 0); assert_eq!(cmap.beta::<2>(24), 0); } + +// --- IO + +#[test] +fn io_read() { + let vtk = Vtk::parse_legacy_be(VTK_ASCII).unwrap(); + // unwrap is fine since we know the VTK_ASCII const is correct + let cmap: CMap2 = + super::io::build_2d_from_vtk(vtk, AttrStorageManager::default()).unwrap(); + + // check result + let faces: Vec<_> = cmap.iter_faces().collect(); + assert_eq!(faces.len(), 4); + assert_eq!(cmap.iter_edges().count(), 12); + assert_eq!(cmap.iter_vertices().count(), 9); + + let mut n_vertices_per_face: Vec = faces + .iter() + .map(|id| Orbit2::new(&cmap, OrbitPolicy::Face, *id as DartIdType).count()) + .collect(); + let (mut three_count, mut four_count, mut six_count): (usize, usize, usize) = (0, 0, 0); + while let Some(n) = n_vertices_per_face.pop() { + match n { + 3 => three_count += 1, + 4 => four_count += 1, + 6 => six_count += 1, + _ => panic!("cmap was built incorrectly"), + } + } + assert_eq!(three_count, 2); + assert_eq!(four_count, 1); + assert_eq!(six_count, 1); +} + +#[cfg(test)] +const VTK_ASCII: &[u8] = b" +# vtk DataFile Version 2.0 +cmap +ASCII + +DATASET UNSTRUCTURED_GRID +POINTS 9 float +0 0 0 1 0 0 1 1 0 +0 1 0 2 0 0 2 1 0 +2 2 0 1 3 0 0 2 0 + +CELLS 17 54 +1 0 +1 4 +1 6 +1 7 +1 8 +2 0 1 +2 3 0 +2 1 4 +2 4 5 +2 5 6 +2 6 7 +2 7 8 +2 8 3 +4 0 1 2 3 +3 1 4 5 +3 1 5 2 +6 3 2 5 6 7 8 + +CELL_TYPES 17 +1 +1 +1 +1 +1 +3 +3 +3 +3 +3 +3 +3 +3 +9 +5 +5 +7 + + +POINT_DATA 9 + +CELL_DATA 17 +"; diff --git a/honeycomb-core/src/cmap/dim2/mod.rs b/honeycomb-core/src/cmap/dim2/mod.rs index 6d5c57a9e..f51fd9450 100644 --- a/honeycomb-core/src/cmap/dim2/mod.rs +++ b/honeycomb-core/src/cmap/dim2/mod.rs @@ -11,13 +11,11 @@ pub mod basic_ops; pub mod embed; pub mod links; pub mod orbits; +pub mod serialize; pub mod sews; pub mod structure; pub mod utils; -#[cfg(feature = "io")] -pub mod io; - // ------ CONTENT /// Number of beta functions defined for [`CMap2`]. diff --git a/honeycomb-core/src/cmap/dim2/io.rs b/honeycomb-core/src/cmap/dim2/serialize.rs similarity index 91% rename from honeycomb-core/src/cmap/dim2/io.rs rename to honeycomb-core/src/cmap/dim2/serialize.rs index fb2418366..c22c3d372 100644 --- a/honeycomb-core/src/cmap/dim2/io.rs +++ b/honeycomb-core/src/cmap/dim2/serialize.rs @@ -1,12 +1,3 @@ -//! Input/Output features implementation -//! -//! The support for I/O is currently very restricted since this is not the focus of this project. -//! Maps can be built from and serialized to VTK legacy files (both binary and ASCII). The -//! `DATASET` of the VTK file should be `UNSTRUCTURED_GRID`, and only a given set of `CELL_TYPES` -//! are supported, because of orientation and dimension restriction. - -// ------ IMPORTS - use crate::cmap::{EdgeIdType, FaceIdType}; use crate::geometry::CoordsFloat; use crate::prelude::{CMap2, DartIdType, Orbit2, OrbitPolicy, VertexIdType, NULL_DART_ID}; @@ -20,9 +11,9 @@ use vtkio::{ IOBuffer, }; -// ------ CONTENT +// --- VTK -/// **Serializing methods** +/// **Serialization methods** impl CMap2 { /// Generate a legacy VTK file from the map. /// @@ -77,9 +68,7 @@ impl CMap2 { } } -// --- internals - -/// Internal building routine for [`CMap2::to_vtk_file`]. +/// Internal building routine for VTK serialization. fn build_unstructured_piece(map: &CMap2) -> UnstructuredGridPiece where T: CoordsFloat + 'static, diff --git a/honeycomb-kernels/Cargo.toml b/honeycomb-kernels/Cargo.toml index 2b7c9416c..658db82e6 100644 --- a/honeycomb-kernels/Cargo.toml +++ b/honeycomb-kernels/Cargo.toml @@ -13,7 +13,7 @@ authors.workspace = true publish = true [dependencies] -honeycomb-core = { workspace = true, features = ["io"] } +honeycomb-core.workspace = true num-traits.workspace = true rayon = "1.10.0" thiserror.workspace = true diff --git a/honeycomb/Cargo.toml b/honeycomb/Cargo.toml index a4655b30c..998b6c250 100644 --- a/honeycomb/Cargo.toml +++ b/honeycomb/Cargo.toml @@ -18,6 +18,6 @@ kernels = ["dep:honeycomb-kernels"] render = ["dep:honeycomb-render"] [dependencies] -honeycomb-core = { workspace = true, features = ["io"] } +honeycomb-core = { workspace = true } honeycomb-kernels = { workspace = true, optional = true } honeycomb-render = { workspace = true, optional = true }