1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::fmt::Debug;

use nalgebra::Vector3;
use vtkio::model::{Attribute, Attributes, CellType, Cells, DataSet};

use crate::Real;

/// A triangle (surface) mesh in 3D
#[derive(Clone, Debug)]
pub struct TriMesh3d<R: Real> {
    /// Coordinates of all vertices of the mesh
    pub vertices: Vec<Vector3<R>>,
    /// The triangles of the mesh identified by their vertex indices
    pub triangles: Vec<[usize; 3]>,
}

/// A hexahedral (volumetric) mesh in 3D
#[derive(Clone, Debug)]
pub struct HexMesh3d<R: Real> {
    /// Coordinates of all vertices of the mesh
    pub vertices: Vec<Vector3<R>>,
    /// The hexahedral cells of the mesh identified by their vertex indices
    pub cells: Vec<[usize; 8]>,
}

/// A point cloud in 3D
#[derive(Clone, Debug)]
pub struct PointCloud3d<R: Real> {
    /// Coordinates of all points in the point cloud
    pub points: Vec<Vector3<R>>,
}

/// A mesh with attached vertex or point data
#[derive(Clone, Debug)]
pub struct MeshWithPointData<MeshT, DataT> {
    /// The mesh geometry itself
    pub mesh: MeshT,
    /// Data attached to each vertex or point of the mesh
    pub data: Vec<DataT>,
}

impl<'a, MeshT: 'a, DataT> MeshWithPointData<MeshT, DataT>
where
    DataSet: From<&'a MeshT>,
    DataT: Real,
{
    pub fn to_dataset(&'a self) -> DataSet {
        let mut mesh_dataset = DataSet::from(&self.mesh);

        if let DataSet::UnstructuredGrid { ref mut data, .. } = &mut mesh_dataset {
            let attribute = Attribute::Scalars {
                num_comp: 1,
                lookup_table: None,
                data: self.data.clone().into(),
            };

            data.point.push((format!("density"), attribute));
        }

        mesh_dataset
    }
}

impl<'a, R> From<&'a TriMesh3d<R>> for DataSet
where
    R: Real,
{
    fn from(mesh: &'a TriMesh3d<R>) -> Self {
        let points = {
            let mut points: Vec<R> = Vec::new();
            points.reserve(mesh.vertices.len() * 3);
            for v in mesh.vertices.iter() {
                points.extend(v.as_slice());
            }
            points
        };

        let vertices = {
            let mut vertices = Vec::new();
            vertices.reserve(mesh.triangles.len() * (3 + 1));
            for triangle in mesh.triangles.iter() {
                vertices.push(3);
                vertices.extend(triangle.iter().copied().map(|i| i as u32));
            }
            vertices
        };

        let cell_types = vec![CellType::Triangle; mesh.triangles.len()];

        DataSet::UnstructuredGrid {
            points: points.into(),
            cells: Cells {
                num_cells: mesh.triangles.len() as u32,
                vertices,
            },
            cell_types,
            data: Attributes::new(),
        }
    }
}

impl<'a, R> From<&'a HexMesh3d<R>> for DataSet
where
    R: Real,
{
    fn from(mesh: &'a HexMesh3d<R>) -> Self {
        let points = {
            let mut points: Vec<R> = Vec::new();
            points.reserve(mesh.vertices.len() * 3);
            for v in mesh.vertices.iter() {
                points.extend(v.as_slice());
            }
            points
        };

        let vertices = {
            let mut vertices = Vec::new();
            vertices.reserve(mesh.cells.len() * (8 + 1));
            for cell in mesh.cells.iter() {
                vertices.push(8);
                vertices.extend(cell.iter().copied().map(|i| i as u32));
            }
            vertices
        };

        let cell_types = vec![CellType::Hexahedron; mesh.cells.len()];

        DataSet::UnstructuredGrid {
            points: points.into(),
            cells: Cells {
                num_cells: mesh.cells.len() as u32,
                vertices,
            },
            cell_types,
            data: Attributes::new(),
        }
    }
}

impl<'a, R> From<&'a PointCloud3d<R>> for DataSet
where
    R: Real,
{
    fn from(mesh: &'a PointCloud3d<R>) -> Self {
        let points = {
            let mut points: Vec<R> = Vec::new();
            points.reserve(mesh.points.len() * 3);
            for v in mesh.points.iter() {
                points.extend(v.as_slice());
            }
            points
        };

        let vertices = {
            let mut vertices = Vec::new();
            vertices.reserve(mesh.points.len() * (1 + 1));
            for (i, _) in mesh.points.iter().enumerate() {
                vertices.push(1 as u32);
                vertices.push(i as u32);
            }
            vertices
        };

        let cell_types = vec![CellType::Vertex; mesh.points.len()];

        DataSet::UnstructuredGrid {
            points: points.into(),
            cells: Cells {
                num_cells: mesh.points.len() as u32,
                vertices,
            },
            cell_types,
            data: Attributes::new(),
        }
    }
}