| 251 | } |
| 252 | |
| 253 | fn quad_grid(width: usize, height: usize, size_x: f64, size_y: f64) -> GeometryGraph<Undirected> { |
| 254 | let nodes = (width + 1) * (height + 1); |
| 255 | let edges = 2 * width * height - width - height; |
| 256 | let mut graph = GeometryGraph::<Undirected>::with_capacity(nodes, edges); |
| 257 | |
| 258 | // Add the nodes |
| 259 | for j in 0..=height { |
| 260 | for i in 0..=width { |
| 261 | let x = quad_i2x(i, size_x, 0.0); |
| 262 | let y = quad_j2y(j, size_y, 0.0); |
| 263 | let point = Point::new(x, y); |
| 264 | let index = graph.add_node(point); |
| 265 | let node_index = (width + 1) * j + i; |
| 266 | tracing::trace!("id={} node={point:?}", index.index()); |
| 267 | debug_assert_eq!(index.index(), node_index); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Add the edges |
| 272 | for j in 0..=height { |
| 273 | for i in 0..=width { |
| 274 | // As an implementation detail, the GeometryGraph gives nodes integer IDs in the order |
| 275 | // they were added. This is the index of the current node in the graph. |
| 276 | let current_index = (width + 1) * j + i; |
| 277 | tracing::trace!("adding neighbors for id={current_index}"); |
| 278 | |
| 279 | // Add the four neighbors, starting at the left and working clockwise |
| 280 | // |
| 281 | // 0--1--2 |
| 282 | // | | | |
| 283 | // 3--4--5 |
| 284 | // | | | |
| 285 | // 6--7--8 |
| 286 | if i > 0 { |
| 287 | let left = current_index - 1; |
| 288 | tracing::trace!("added left id={left}"); |
| 289 | graph.update_edge(current_index.into(), left.into(), ()); |
| 290 | } |
| 291 | if j < height { |
| 292 | let upper = current_index + width + 1; |
| 293 | tracing::trace!("added upper id={upper}"); |
| 294 | graph.update_edge(current_index.into(), upper.into(), ()); |
| 295 | } |
| 296 | if i < width { |
| 297 | let right = current_index + 1; |
| 298 | tracing::trace!("added right id={right}"); |
| 299 | graph.update_edge(current_index.into(), right.into(), ()); |
| 300 | } |
| 301 | if j > 0 { |
| 302 | let lower = current_index - width - 1; |
| 303 | tracing::trace!("added lower id={lower}"); |
| 304 | graph.update_edge(current_index.into(), lower.into(), ()); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | graph |
| 309 | } |
| 310 | |