Remove the farthest entries from node center and return for reinsertion.
(tree: &mut RTree, node_idx: usize)
| 141 | |
| 142 | /// Remove the farthest entries from node center and return for reinsertion. |
| 143 | fn forced_reinsert(tree: &mut RTree, node_idx: usize) -> Vec<RTreeEntry> { |
| 144 | let reinsert_count = if tree.nodes[node_idx].is_leaf() { |
| 145 | REINSERT_COUNT_LEAF |
| 146 | } else { |
| 147 | 0 |
| 148 | }; |
| 149 | if reinsert_count == 0 { |
| 150 | return Vec::new(); |
| 151 | } |
| 152 | |
| 153 | let center_lng = (tree.nodes[node_idx].bbox.min_lng + tree.nodes[node_idx].bbox.max_lng) / 2.0; |
| 154 | let center_lat = (tree.nodes[node_idx].bbox.min_lat + tree.nodes[node_idx].bbox.max_lat) / 2.0; |
| 155 | |
| 156 | if let NodeKind::Leaf { entries } = &mut tree.nodes[node_idx].kind { |
| 157 | entries.sort_by(|a, b| { |
| 158 | let da = dist_sq_center(center_lng, center_lat, &a.bbox); |
| 159 | let db = dist_sq_center(center_lng, center_lat, &b.bbox); |
| 160 | db.partial_cmp(&da).unwrap_or(std::cmp::Ordering::Equal) |
| 161 | }); |
| 162 | let removed: Vec<RTreeEntry> = entries.drain(..reinsert_count).collect(); |
| 163 | tree.nodes[node_idx].recompute_bbox(); |
| 164 | removed |
| 165 | } else { |
| 166 | Vec::new() |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | fn dist_sq_center(lng: f64, lat: f64, bbox: &BoundingBox) -> f64 { |
| 171 | let cx = (bbox.min_lng + bbox.max_lng) / 2.0; |
no test coverage detected