mergeArtifactsBasedOnVerticalAlignment method

void mergeArtifactsBasedOnVerticalAlignment()

Merges connected artifacts based on specified thresholds.

This method iterates through the list of artifacts and merges those that are considered connected based on vertical and horizontal thresholds.

The algorithm works by:

  1. Sorting artifacts by their horizontal position
  2. Creating a working copy of the sorted artifacts
  3. For each artifact, checking if it should be merged with any subsequent artifacts
  4. If artifacts should be merged, combining them and removing the merged artifact

After processing, the band's artifacts list is replaced with the merged artifacts.

Implementation

void mergeArtifactsBasedOnVerticalAlignment() {
  final List<Artifact> mergedArtifacts = [];

  // First, sort artifacts by their horizontal position
  final List<Artifact> sortedArtifacts = List.from(artifacts);
  sortedArtifacts.sort(
    (a, b) => a.rectFound.left.compareTo(b.rectFound.left),
  );

  // Create a working copy of the artifacts list
  final List<Artifact> workingArtifacts = List.from(sortedArtifacts);

  for (int i = 0; i < workingArtifacts.length; i++) {
    final Artifact current = workingArtifacts[i];

    for (int j = i + 1; j < workingArtifacts.length; j++) {
      final Artifact next = workingArtifacts[j];

      // Check if these artifacts should be merged
      if (shouldMergeArtifacts(current, next)) {
        // current.debugPrintGrid();
        // next.debugPrintGrid();

        current.mergeArtifact(next);
        workingArtifacts.removeAt(j);
        j--; // Adjust index since we removed an artifact
      }
    }

    mergedArtifacts.add(current);
  }

  artifacts = mergedArtifacts;
}