use crate::IOBuffer;
use num_derive::FromPrimitive;
use std::any::TypeId;
use std::fmt;
#[derive(Clone, PartialEq, Debug)]
pub struct Vtk {
pub version: Version,
pub title: String,
pub data: DataSet,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Version {
pub major: u8,
pub minor: u8,
}
impl Version {
pub fn new(pair: (u8, u8)) -> Self {
Version {
major: pair.0,
minor: pair.1,
}
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct FieldArray {
pub name: String,
pub num_comp: u32,
pub data: IOBuffer,
}
#[derive(Clone, PartialEq, Debug)]
pub enum Attribute {
Scalars {
num_comp: u8,
lookup_table: Option<String>,
data: IOBuffer,
},
ColorScalars {
num_comp: u8,
data: IOBuffer,
},
LookupTable {
data: IOBuffer,
},
Vectors {
data: IOBuffer,
},
Normals {
data: IOBuffer,
},
TextureCoordinates {
dim: u8,
data: IOBuffer,
},
Tensors {
data: IOBuffer,
},
Field {
data_array: Vec<FieldArray>,
},
}
#[derive(Clone, PartialEq, Debug)]
pub struct Attributes {
pub point: Vec<(String, Attribute)>,
pub cell: Vec<(String, Attribute)>,
}
impl Attributes {
pub fn new() -> Self {
Attributes {
point: Vec::new(),
cell: Vec::new(),
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct Cells {
pub num_cells: u32,
pub vertices: Vec<u32>,
}
#[derive(Clone, PartialEq, Debug)]
pub enum PolyDataTopology {
Vertices(Cells),
Lines(Cells),
Polygons(Cells),
TriangleStrips(Cells),
}
#[derive(Copy, Clone, PartialEq, Debug, FromPrimitive)]
pub enum CellType {
Vertex = 1,
PolyVertex = 2,
Line = 3,
PolyLine = 4,
Triangle = 5,
TriangleStrip = 6,
Polygon = 7,
Pixel = 8,
Quad = 9,
Tetra = 10,
Voxel = 11,
Hexahedron = 12,
Wedge = 13,
Pyramid = 14,
QuadraticEdge = 21,
QuadraticTriangle = 22,
QuadraticQuad = 23,
QuadraticTetra = 24,
QuadraticHexahedron = 25,
}
#[derive(Clone, PartialEq, Debug)]
pub enum DataSet {
StructuredPoints {
dims: [u32; 3],
origin: [f32; 3],
spacing: [f32; 3],
data: Attributes,
},
StructuredGrid {
dims: [u32; 3],
points: IOBuffer,
data: Attributes,
},
UnstructuredGrid {
points: IOBuffer,
cells: Cells,
cell_types: Vec<CellType>,
data: Attributes,
},
PolyData {
points: IOBuffer,
topo: Vec<PolyDataTopology>,
data: Attributes,
},
RectilinearGrid {
dims: [u32; 3],
x_coords: IOBuffer,
y_coords: IOBuffer,
z_coords: IOBuffer,
data: Attributes,
},
Field {
name: String,
data_array: Vec<FieldArray>,
},
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum DataType {
Bit,
UnsignedChar,
Char,
UnsignedShort,
Short,
UnsignedInt,
Int,
UnsignedLong,
Long,
Float,
Double,
}
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DataType::Bit => write!(f, "bit"),
DataType::UnsignedChar => write!(f, "unsigned_char"),
DataType::Char => write!(f, "char"),
DataType::UnsignedShort => write!(f, "unsigned_short"),
DataType::Short => write!(f, "short"),
DataType::UnsignedInt => write!(f, "unsigned_int"),
DataType::Int => write!(f, "int"),
DataType::UnsignedLong => write!(f, "unsigned_long"),
DataType::Long => write!(f, "long"),
DataType::Float => write!(f, "float"),
DataType::Double => write!(f, "double"),
}
}
}
impl From<TypeId> for DataType {
fn from(dt: TypeId) -> Self {
match dt {
x if x == TypeId::of::<u8>() => DataType::UnsignedChar,
x if x == TypeId::of::<i8>() => DataType::Char,
x if x == TypeId::of::<u16>() => DataType::UnsignedShort,
x if x == TypeId::of::<i16>() => DataType::Short,
x if x == TypeId::of::<u32>() => DataType::UnsignedInt,
x if x == TypeId::of::<i32>() => DataType::Int,
x if x == TypeId::of::<u64>() => DataType::UnsignedLong,
x if x == TypeId::of::<i64>() => DataType::Long,
x if x == TypeId::of::<f32>() => DataType::Float,
x if x == TypeId::of::<f64>() => DataType::Double,
_ => panic!("Specified type is unsupported by VTK."),
}
}
}