@@ -4,7 +4,7 @@ use std::option::Option;
4
4
5
5
use super :: { point:: Point , utilities:: make_uuid} ;
6
6
7
- #[ derive( Debug ) ]
7
+ #[ derive( Debug , PartialEq , Clone ) ]
8
8
pub struct Vertex < T , U > {
9
9
pub point : Point < T > ,
10
10
pub uuid : Uuid ,
@@ -36,6 +36,14 @@ impl<T, U> Vertex<T, U> {
36
36
data,
37
37
}
38
38
}
39
+
40
+ pub fn from_points ( points : Vec < Point < T > > ) -> Vec < Self > {
41
+ points. into_iter ( ) . map ( |p| Self :: new ( p) ) . collect ( )
42
+ }
43
+
44
+ pub fn into_hashmap ( vertices : Vec < Self > ) -> std:: collections:: HashMap < Uuid , Self > {
45
+ vertices. into_iter ( ) . map ( |v| ( v. uuid , v) ) . collect ( )
46
+ }
39
47
}
40
48
#[ cfg( test) ]
41
49
mod tests {
@@ -64,4 +72,52 @@ mod tests {
64
72
assert ! ( vertex. incident_cell. is_none( ) ) ;
65
73
assert ! ( vertex. data. is_none( ) ) ;
66
74
}
75
+
76
+ #[ test]
77
+ fn make_vertices_from_points ( ) {
78
+ let points = vec ! [
79
+ Point :: new( 1.0 , 2.0 , 3.0 ) ,
80
+ Point :: new( 4.0 , 5.0 , 6.0 ) ,
81
+ Point :: new( 7.0 , 8.0 , 9.0 ) ,
82
+ ] ;
83
+ let vertices: Vec < Vertex < f64 , Option < ( ) > > > = Vertex :: from_points ( points) ;
84
+ println ! ( "{:?}" , vertices) ;
85
+ assert_eq ! ( vertices. len( ) , 3 ) ;
86
+ assert_eq ! ( vertices[ 0 ] . point. x, 1.0 ) ;
87
+ assert_eq ! ( vertices[ 0 ] . point. y, 2.0 ) ;
88
+ assert_eq ! ( vertices[ 0 ] . point. z, 3.0 ) ;
89
+ assert_eq ! ( vertices[ 1 ] . point. x, 4.0 ) ;
90
+ assert_eq ! ( vertices[ 1 ] . point. y, 5.0 ) ;
91
+ assert_eq ! ( vertices[ 1 ] . point. z, 6.0 ) ;
92
+ assert_eq ! ( vertices[ 2 ] . point. x, 7.0 ) ;
93
+ assert_eq ! ( vertices[ 2 ] . point. y, 8.0 ) ;
94
+ assert_eq ! ( vertices[ 2 ] . point. z, 9.0 ) ;
95
+ }
96
+
97
+ #[ test]
98
+ fn make_hashmap_from_vec ( ) {
99
+ let points = vec ! [
100
+ Point :: new( 1.0 , 2.0 , 3.0 ) ,
101
+ Point :: new( 4.0 , 5.0 , 6.0 ) ,
102
+ Point :: new( 7.0 , 8.0 , 9.0 ) ,
103
+ ] ;
104
+ let mut vertices: Vec < Vertex < f64 , Option < ( ) > > > = Vertex :: from_points ( points) ;
105
+ let hashmap = Vertex :: into_hashmap ( vertices. clone ( ) ) ;
106
+ println ! ( "{:?}" , hashmap) ;
107
+ assert_eq ! ( hashmap. len( ) , 3 ) ;
108
+ for ( key, val) in hashmap. iter ( ) {
109
+ assert_eq ! ( * key, val. uuid) ;
110
+ }
111
+
112
+ let mut values: Vec < Vertex < f64 , Option < ( ) > > > = hashmap. into_values ( ) . collect ( ) ;
113
+ assert_eq ! ( values. len( ) , 3 ) ;
114
+
115
+ values. sort_by ( |a, b| a. uuid . cmp ( & b. uuid ) ) ;
116
+ vertices. sort_by ( |a, b| a. uuid . cmp ( & b. uuid ) ) ;
117
+ assert_eq ! ( values, vertices) ;
118
+
119
+ // Human readable output for cargo test -- --nocapture
120
+ println ! ( "values = {:?}" , values) ;
121
+ println ! ( "vertices = {:?}" , vertices) ;
122
+ }
67
123
}
0 commit comments