mergeArtifact method
Merges the current artifact with another artifact.
This method combines the current artifact with the provided artifact, creating a new, larger artifact that encompasses both.
Parameters:
toMerge: The Artifact to be merged with the current artifact.
The merging process involves:
- Creating a new rectangle that encompasses both artifacts.
- Creating a new matrix (grid) large enough to contain both artifacts' data.
- Copying the data from both artifacts into the new matrix.
- Updating the current artifact's matrix and rectangle to reflect the merged state.
Note: This method modifies the current artifact in-place.
Implementation
void mergeArtifact(final Artifact toMerge) {
// Create a new rectangle that encompasses both artifacts
final IntRect newRect = IntRect.fromLTRB(
min(rectFound.left, toMerge.rectFound.left),
min(rectFound.top, toMerge.rectFound.top),
max(rectFound.right, toMerge.rectFound.right),
max(rectFound.bottom, toMerge.rectFound.bottom),
);
// Create a new grid that can fit both artifacts
final Artifact newGrid = Artifact(newRect.width, newRect.height);
// Copy both grids onto the new grid with correct offsets
copyArtifactGrid(
this,
newGrid,
(rectFound.left - newRect.left),
(rectFound.top - newRect.top),
);
copyArtifactGrid(
toMerge,
newGrid,
(toMerge.rectFound.left - newRect.left),
(toMerge.rectFound.top - newRect.top),
);
// Update this artifact with the merged data
setGrid(newGrid._matrix, newGrid.cols);
}