@@ -10,11 +10,20 @@ use crate::render_cells::Color;
10
10
use crate :: { Cell , IntersectionID , MapModel , RenderCells , RoadID , Shortcuts } ;
11
11
12
12
pub struct Neighbourhood {
13
+ // Immutable once created
13
14
pub interior_roads : HashSet < RoadID > ,
14
15
crosses : HashMap < RoadID , f64 > ,
15
16
pub border_intersections : HashSet < IntersectionID > ,
16
17
name : String ,
17
18
pub boundary_polygon : Polygon ,
19
+
20
+ // Updated after mutations
21
+ derived : Option < DerivedNeighbourhoodState > ,
22
+ }
23
+
24
+ struct DerivedNeighbourhoodState {
25
+ render_cells : RenderCells ,
26
+ shortcuts : Shortcuts ,
18
27
}
19
28
20
29
impl Neighbourhood {
@@ -60,32 +69,47 @@ impl Neighbourhood {
60
69
bail ! ( "No roads inside the boundary" ) ;
61
70
}
62
71
63
- Ok ( Self {
72
+ let mut n = Self {
64
73
interior_roads,
65
74
crosses,
66
75
border_intersections,
67
76
name,
68
77
boundary_polygon,
69
- } )
78
+ derived : None ,
79
+ } ;
80
+ n. after_edit ( map) ;
81
+ Ok ( n)
82
+ }
83
+
84
+ pub fn after_edit ( & mut self , map : & MapModel ) {
85
+ let cells = Cell :: find_all ( map, self ) ;
86
+ let render_cells = RenderCells :: new ( map, self , & cells) ;
87
+ let shortcuts = Shortcuts :: new ( map, self ) ;
88
+ self . derived = Some ( DerivedNeighbourhoodState {
89
+ render_cells,
90
+ shortcuts,
91
+ } ) ;
70
92
}
71
93
72
94
pub fn to_gj ( & self , map : & MapModel ) -> FeatureCollection {
73
95
let mut features = Vec :: new ( ) ;
74
96
97
+ let derived = self . derived . as_ref ( ) . unwrap ( ) ;
98
+
75
99
// Just one boundary
76
100
features. push ( map. boundaries . get ( & self . name ) . cloned ( ) . unwrap ( ) ) ;
77
101
78
- // TODO Decide how/where state lives
79
- let cells = Cell :: find_all ( map, self ) ;
80
- let render_cells = RenderCells :: new ( map, self , & cells) ;
81
- let shortcuts = Shortcuts :: new ( map, self ) ;
82
-
83
102
for r in & self . interior_roads {
84
103
let mut f = map. get_r ( * r) . to_gj ( & map. mercator ) ;
85
104
f. set_property ( "kind" , "interior_road" ) ;
86
105
f. set_property (
87
106
"shortcuts" ,
88
- shortcuts. count_per_road . get ( r) . cloned ( ) . unwrap_or ( 0 ) ,
107
+ derived
108
+ . shortcuts
109
+ . count_per_road
110
+ . get ( r)
111
+ . cloned ( )
112
+ . unwrap_or ( 0 ) ,
89
113
) ;
90
114
features. push ( f) ;
91
115
}
@@ -101,15 +125,16 @@ impl Neighbourhood {
101
125
features. push ( f) ;
102
126
}
103
127
104
- for ( polygons, color) in render_cells
128
+ for ( polygons, color) in derived
129
+ . render_cells
105
130
. polygons_per_cell
106
- . into_iter ( )
107
- . zip ( render_cells. colors )
131
+ . iter ( )
132
+ . zip ( derived . render_cells . colors . iter ( ) )
108
133
{
109
- let mut f = Feature :: from ( Geometry :: from ( & map. mercator . to_wgs84 ( & polygons) ) ) ;
134
+ let mut f = Feature :: from ( Geometry :: from ( & map. mercator . to_wgs84 ( polygons) ) ) ;
110
135
match color {
111
136
Color :: Disconnected => f. set_property ( "cell_color" , "disconnected" ) ,
112
- Color :: Cell ( idx) => f. set_property ( "cell_color" , idx) ,
137
+ Color :: Cell ( idx) => f. set_property ( "cell_color" , * idx) ,
113
138
}
114
139
features. push ( f) ;
115
140
}
0 commit comments