This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 39162de7a8e0a9d5c018dd4b2578fd130455eeca Author: Martin Desruisseaux <[email protected]> AuthorDate: Tue Mar 26 15:37:28 2019 +0100 Add a GridExtent.expand(long...) method. --- .../org/apache/sis/coverage/grid/GridExtent.java | 26 +++++++++++++++++++++- .../apache/sis/coverage/grid/GridExtentTest.java | 12 ++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java index 4ebbe97..c441503 100644 --- a/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java +++ b/core/sis-raster/src/main/java/org/apache/sis/coverage/grid/GridExtent.java @@ -890,6 +890,29 @@ public class GridExtent implements Serializable { } /** + * Expands or shrinks this grid extent by the given amount of cells along each dimension. + * This method adds the given margins to the {@linkplain #getHigh(int) high coordinates} + * and subtracts the same margins to the {@linkplain #getLow(int) low coordinates}. + * + * @param margins amount of cells to add or subtract. + * @return a grid extent expanded by the given amount, or {@code this} if there is no change. + * @throws ArithmeticException if expanding this extent by the given margins overflows {@code long} capacity. + */ + public GridExtent expand(final long... margins) { + ArgumentChecks.ensureNonNull("margins", margins); + final int m = getDimension(); + final int length = Math.min(m, margins.length); + final GridExtent resize = new GridExtent(this); + final long[] c = resize.coordinates; + for (int i=0; i<length; i++) { + final long margin = margins[i]; + c[i] = Math.subtractExact(c[i], margin); + c[i+m] = Math.addExact(c[i+m], margin); + } + return Arrays.equals(c, coordinates) ? this : resize; + } + + /** * Sets the size of this grid extent to the given values. This method modifies grid coordinates as if they were multiplied * by (given size) / ({@linkplain #getSize(int) current size}), rounded toward zero and with the value farthest from zero * adjusted by ±1 for having a size exactly equals to the specified value. @@ -901,7 +924,8 @@ public class GridExtent implements Serializable { * If the array is longer, extra sizes are ignored.</p> * * @param sizes the new grid sizes for each dimension. - * @return a grid extent having the given sizes (may be {@code this}). + * @return a grid extent having the given sizes, or {@code this} if there is no change. + * @throws ArithmeticException if resizing this extent to the given size overflows {@code long} capacity. * * @see GridDerivation#resize(GridExtent, double...) */ diff --git a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java index 6687c45..386e0f7 100644 --- a/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java +++ b/core/sis-raster/src/test/java/org/apache/sis/coverage/grid/GridExtentTest.java @@ -139,6 +139,18 @@ public final strictfp class GridExtentTest extends TestCase { } /** + * Tests {@link GridExtent#expand(long...)}. + */ + @Test + public void testExpand() { + GridExtent extent = create3D(); + extent = extent.expand(20, -10); + assertExtentEquals(extent, 0, 80, 519); + assertExtentEquals(extent, 1, 210, 789); + assertExtentEquals(extent, 2, 40, 49); + } + + /** * Tests {@link GridExtent#resize(long...)}. */ @Test
