splitArtifactByColumns function

List<Artifact> splitArtifactByColumns(
  1. Artifact artifactToSplit,
  2. List<int> offsets
)

Splits the given matrix into multiple row matrices based on the provided row offsets.

Each row offset in offsets marks the start of a new split. The function returns a list of Artifact objects, where each matrix represents a horizontal slice of the original artifactToSplit matrix while maintaining its relative position.

Example:

Matrix input = Matrix(5, 5);
List<int> rowOffsets = [0, 2, 4]; // Splits at row indices 0, 2, and 4
List<Matrix> rowMatrices = Matrix.splitAsRows(input, rowOffsets);
  • artifactToSplit: The original matrix to split.
  • offsets: A list of row indices where splits should occur.
  • Returns: A list of matrices representing the split rows while preserving locationFound.

Implementation

List<Artifact> splitArtifactByColumns(
  final Artifact artifactToSplit,
  List<int> offsets,
) {
  List<Artifact> result = [];

  // Handle the first segment (from 0 to first offset)
  if (offsets.isNotEmpty && offsets[0] > 0) {
    Artifact firstSegment = Artifact(offsets[0], artifactToSplit.rows);

    // Copy the relevant columns
    for (int x = 0; x < offsets[0]; x++) {
      for (int y = 0; y < artifactToSplit.rows; y++) {
        firstSegment.cellSet(x, y, artifactToSplit.cellGet(x, y));
      }
    }

    // Set location properties
    firstSegment.locationFound = IntOffset(
      artifactToSplit.locationFound.x,
      artifactToSplit.locationFound.y,
    );

    firstSegment.locationAdjusted = IntOffset(
      artifactToSplit.locationAdjusted.x,
      artifactToSplit.locationAdjusted.y,
    );

    firstSegment.wasPartOfSplit = true;
    result.add(firstSegment);
  }

  // Handle middle segments and last segment
  for (int i = 0; i < offsets.length; i++) {
    int columnStart = offsets[i];
    int columnEnd = (i < offsets.length - 1)
        ? offsets[i + 1]
        : artifactToSplit.cols;

    // Skip if this segment has no width
    if (columnEnd <= columnStart) {
      continue;
    }

    // Create segment
    Artifact segment = Artifact(columnEnd - columnStart, artifactToSplit.rows);

    // Copy the relevant columns
    for (int x = columnStart; x < columnEnd; x++) {
      for (int y = 0; y < artifactToSplit.rows; y++) {
        segment.cellSet(x - columnStart, y, artifactToSplit.cellGet(x, y));
      }
    }

    // Set location properties
    segment.locationFound = IntOffset(
      artifactToSplit.locationFound.x + columnStart,
      artifactToSplit.locationFound.y,
    );

    segment.locationAdjusted = IntOffset(
      artifactToSplit.locationAdjusted.x + columnStart,
      artifactToSplit.locationAdjusted.y,
    );

    segment.wasPartOfSplit = true;
    result.add(segment);
  }

  return result;
}