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
use cgmath::{Vector3, InnerSpace}; use ideal::{Id, IdVec}; use ideal::vec::IdsIter; pub struct VertexData { position: Vector3<f64>, edges: Vec<Edge>, cells: [Cell; 3], } pub struct EdgeData { vertices: (Vertex, Vertex), cells: (Cell, Cell), } pub struct CellData { vertices: Vec<Vertex>, edges: Vec<Edge>, } #[derive(Default)] pub struct Diagram { vertices: IdVec<VertexData>, edges: IdVec<EdgeData>, cells: IdVec<CellData>, } impl Diagram { pub fn add_vertex(&mut self, position: Vector3<f64>, cells: [Cell; 3]) -> Vertex { let vertex = self.vertices.push(VertexData { position: position, edges: Vec::new(), cells: cells, }); for &cell in &cells { self.cells[cell].vertices.push(vertex); } vertex } pub fn vertices(&self) -> IdsIter<VertexData> { self.vertices.ids() } pub fn vertex_position(&self, vertex: Vertex) -> Vector3<f64> { self.vertices[vertex].position } pub fn vertex_edges(&self, vertex: Vertex) -> &[Edge] { &self.vertices[vertex].edges } pub fn vertex_cells(&self, vertex: Vertex) -> &[Cell] { &self.vertices[vertex].cells } pub fn vertex_neighbors(&self, vertex: Vertex) -> Vec<Vertex> { self.vertex_edges(vertex) .iter() .map(|&edge| self.other_edge_vertex(edge, vertex)) .collect() } pub fn add_edge(&mut self, vertex0: Vertex, vertex1: Vertex) -> Edge { let edge = self.edges.push(EdgeData { vertices: (vertex0, vertex1), cells: (Cell::invalid(), Cell::invalid()) }); self.vertices[vertex0].edges.push(edge); self.vertices[vertex1].edges.push(edge); edge } pub fn edges(&self) -> IdsIter<EdgeData> { self.edges.ids() } pub fn edge_vertices(&self, edge: Edge) -> (Vertex, Vertex) { self.edges[edge].vertices } pub fn edge_cells(&self, edge: Edge) -> (Cell, Cell) { self.edges[edge].cells } pub fn set_edge_cells(&mut self, edge: Edge, cell0: Cell, cell1: Cell) { self.edges[edge].cells = (cell0, cell1); self.cells[cell0].edges.push(edge); self.cells[cell1].edges.push(edge); } pub fn other_edge_vertex(&self, edge: Edge, vertex: Vertex) -> Vertex { let (vertex0, vertex1) = self.edge_vertices(edge); if vertex == vertex0 { vertex1 } else if vertex == vertex1 { vertex0 } else { Vertex::invalid() } } pub fn other_edge_cell(&self, edge: Edge, cell: Cell) -> Cell { let (cell0, cell1) = self.edge_cells(edge); if cell == cell0 { cell1 } else if cell == cell1 { cell0 } else { Cell::invalid() } } pub fn add_cell(&mut self) -> Cell { self.cells.push(CellData { vertices: Vec::new(), edges: Vec::new(), }) } pub fn cells(&self) -> IdsIter<CellData> { self.cells.ids() } pub fn center(&self, cell: Cell) -> Vector3<f64> { let mut center = Vector3::new(0.0, 0.0, 0.0); let cell_vertices = self.cell_vertices(cell); for vertex in cell_vertices { center += self.vertex_position(*vertex); } center.normalize() } pub fn cell_vertices(&self, cell: Cell) -> &[Vertex] { &self.cells[cell].vertices } pub fn cell_edges(&self, cell: Cell) -> &[Edge] { &self.cells[cell].edges } pub fn cell_neighbors(&self, cell: Cell) -> Vec<Cell> { self.cell_edges(cell) .iter() .map(|&edge| self.other_edge_cell(edge, cell)) .collect() } pub fn clear(&mut self) { self.vertices.clear(); self.edges.clear(); self.cells.clear(); } } pub type Vertex = Id<VertexData>; pub type Edge = Id<EdgeData>; pub type Cell = Id<CellData>;