getEdgesForWaypointList method

List<WaypointEdge> getEdgesForWaypointList(
  1. List<String> waypointUuids
)

Constructs the full path of edges by connecting a sequence of specified waypoints. Finds the path between each consecutive pair in the input list using BFS, allowing for intermediate waypoints. It then attempts to find a path from the last waypoint back to the first to close the loop. Returns the concatenated list of all edges in the full path. If the loop cannot be closed, it returns the path connecting the specified waypoints in order, without the closing segment. Returns an empty list only if any of the initial required segments (e.g., wp1->wp2, wp2->wp3) cannot be found.

Implementation

List<WaypointEdge> getEdgesForWaypointList(List<String> waypointUuids) {
  final List<WaypointEdge> fullPathEdges = [];

  if (waypointUuids.length < 2) {
    // Not enough points to form even a single segment.
    return fullPathEdges;
  }

  // Iterate through the required waypoint sequence (0->1, 1->2, ..., n-2 -> n-1)
  for (int i = 0; i < waypointUuids.length - 1; i++) {
    final String startSegmentUuid = waypointUuids[i];
    final String endSegmentUuid = waypointUuids[i + 1];

    // Find the path edges for this specific segment using BFS
    final List<WaypointEdge>? segmentEdges = _findPathEdgesBfs(
      startSegmentUuid,
      endSegmentUuid,
    );

    if (segmentEdges != null) {
      // Successfully found path for this segment, add its edges
      fullPathEdges.addAll(segmentEdges);
    } else {
      return []; // Indicate failure for the whole sequence
    }
  }

  // --- Attempt to close the loop ---
  // Find path from the last waypoint back to the first waypoint
  final String lastWaypointUuid = waypointUuids.last;
  final String firstWaypointUuid = waypointUuids.first;

  final List<WaypointEdge>? closingSegmentEdges = _findPathEdgesBfs(
    lastWaypointUuid,
    firstWaypointUuid,
  );

  if (closingSegmentEdges != null) {
    // Successfully found closing path segment, add its edges
    fullPathEdges.addAll(closingSegmentEdges);
  } else {
    // No action needed here, just don't add the non-existent edges.
  }
  // --- End of loop closing attempt ---
  return fullPathEdges;
}