|
| 1 | +import 'dart:math'; |
| 2 | +import 'package:turf/helpers.dart'; |
| 3 | + |
| 4 | +/// Edge representation for the graph |
| 5 | +class Edge { |
| 6 | + final Position from; |
| 7 | + final Position to; |
| 8 | + bool visited = false; |
| 9 | + String? label; |
| 10 | + |
| 11 | + Edge(this.from, this.to); |
| 12 | + |
| 13 | + @override |
| 14 | + String toString() => '$from -> $to'; |
| 15 | + |
| 16 | + /// Get canonical edge key (ordered by coordinates) |
| 17 | + String get key { |
| 18 | + return from.toString().compareTo(to.toString()) <= 0 |
| 19 | + ? '${from.toString()}|${to.toString()}' |
| 20 | + : '${to.toString()}|${from.toString()}'; |
| 21 | + } |
| 22 | + |
| 23 | + /// Get the key as directed edge |
| 24 | + String get directedKey => '${from.toString()}|${to.toString()}'; |
| 25 | + |
| 26 | + /// Create a reversed edge |
| 27 | + Edge reversed() => Edge(to, from); |
| 28 | +} |
| 29 | + |
| 30 | +/// Helper class to associate an edge with its bearing |
| 31 | +class EdgeWithBearing { |
| 32 | + final Edge edge; |
| 33 | + final num bearing; |
| 34 | + |
| 35 | + EdgeWithBearing(this.edge, this.bearing); |
| 36 | +} |
| 37 | + |
| 38 | +/// Node in the graph, representing a vertex with its edges |
| 39 | +class Node { |
| 40 | + final Position position; |
| 41 | + final List<Edge> edges = []; |
| 42 | + |
| 43 | + Node(this.position); |
| 44 | + |
| 45 | + void addEdge(Edge edge) { |
| 46 | + edges.add(edge); |
| 47 | + } |
| 48 | + |
| 49 | + /// Get string representation for use as a map key |
| 50 | + String get key => position.toString(); |
| 51 | +} |
| 52 | + |
| 53 | +/// Graph representing a planar graph of edges and nodes |
| 54 | +class Graph { |
| 55 | + final Map<String, Node> nodes = {}; |
| 56 | + final Map<String, Edge> edges = {}; |
| 57 | + final Map<String, List<EdgeWithBearing>> edgesByVertex = {}; |
| 58 | + |
| 59 | + /// Add an edge to the graph |
| 60 | + void addEdge(Position from, Position to) { |
| 61 | + // Skip edges with identical start and end points |
| 62 | + if (from[0] == to[0] && from[1] == to[1]) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Create a canonical edge key to avoid duplicates |
| 67 | + final edgeKey = _createEdgeKey(from, to); |
| 68 | + |
| 69 | + // Skip duplicate edges |
| 70 | + if (edges.containsKey(edgeKey)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Create and store the edge |
| 75 | + final edge = Edge(from, to); |
| 76 | + edges[edgeKey] = edge; |
| 77 | + |
| 78 | + // Add from node if it doesn't exist |
| 79 | + final fromKey = from.toString(); |
| 80 | + if (!nodes.containsKey(fromKey)) { |
| 81 | + nodes[fromKey] = Node(from); |
| 82 | + } |
| 83 | + nodes[fromKey]!.addEdge(edge); |
| 84 | + |
| 85 | + // Add to node if it doesn't exist |
| 86 | + final toKey = to.toString(); |
| 87 | + if (!nodes.containsKey(toKey)) { |
| 88 | + nodes[toKey] = Node(to); |
| 89 | + } |
| 90 | + nodes[toKey]!.addEdge(Edge(to, from)); |
| 91 | + |
| 92 | + // Add to edge-by-vertex index for efficient lookup |
| 93 | + _addToEdgesByVertex(from, to); |
| 94 | + _addToEdgesByVertex(to, from); |
| 95 | + } |
| 96 | + |
| 97 | + /// Add edge to the index for efficient lookup by vertex |
| 98 | + void _addToEdgesByVertex(Position from, Position to) { |
| 99 | + final fromKey = from.toString(); |
| 100 | + if (!edgesByVertex.containsKey(fromKey)) { |
| 101 | + edgesByVertex[fromKey] = []; |
| 102 | + } |
| 103 | + |
| 104 | + // Calculate bearing for the edge |
| 105 | + final bearing = _calculateBearing(from, to); |
| 106 | + edgesByVertex[fromKey]!.add(EdgeWithBearing(Edge(from, to), bearing)); |
| 107 | + } |
| 108 | + |
| 109 | + /// Calculate bearing between two positions |
| 110 | + num _calculateBearing(Position start, Position end) { |
| 111 | + num lng1 = _degreesToRadians(start[0]!); |
| 112 | + num lng2 = _degreesToRadians(end[0]!); |
| 113 | + num lat1 = _degreesToRadians(start[1]!); |
| 114 | + num lat2 = _degreesToRadians(end[1]!); |
| 115 | + num a = sin(lng2 - lng1) * cos(lat2); |
| 116 | + num b = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lng2 - lng1); |
| 117 | + |
| 118 | + // Convert to azimuth (0-360°, clockwise from north) |
| 119 | + num bearing = _radiansToDegrees(atan2(a, b)); |
| 120 | + return (bearing % 360 + 360) % 360; // Normalize to 0-360 |
| 121 | + } |
| 122 | + |
| 123 | + /// Create a canonical edge key |
| 124 | + String _createEdgeKey(Position from, Position to) { |
| 125 | + final fromKey = from.toString(); |
| 126 | + final toKey = to.toString(); |
| 127 | + return fromKey.compareTo(toKey) < 0 ? '$fromKey|$toKey' : '$toKey|$fromKey'; |
| 128 | + } |
| 129 | + |
| 130 | + /// Convert degrees to radians |
| 131 | + num _degreesToRadians(num degrees) { |
| 132 | + return degrees * pi / 180; |
| 133 | + } |
| 134 | + |
| 135 | + /// Convert radians to degrees |
| 136 | + num _radiansToDegrees(num radians) { |
| 137 | + return radians * 180 / pi; |
| 138 | + } |
| 139 | +} |
0 commit comments