You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/07/16 15:08:24 UTC

[sis] branch geoapi-4.0 updated: Test also slicing in other dimensions than the 2 first dimensions.

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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new ac4fe06  Test also slicing in other dimensions than the 2 first dimensions.
ac4fe06 is described below

commit ac4fe0607ff90405a1f0ff02a666e815d3e459f5
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Fri Jul 16 12:16:56 2021 +0200

    Test also slicing in other dimensions than the 2 first dimensions.
---
 .../apache/sis/coverage/grid/ImageRenderer.java    | 50 ++++++++++------------
 .../coverage/grid/BufferedGridCoverageTest.java    | 42 ++++++++++--------
 2 files changed, 48 insertions(+), 44 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
index 24b1b0e..0d58be9 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ImageRenderer.java
@@ -134,6 +134,8 @@ public class ImageRenderer {
     /**
      * Offset to add to {@link #buffer} offset for reaching the first sample value for the slice to render.
      * This is zero for a two-dimensional image, but may be greater for cube having more dimensions.
+     * Despite the "Z" letter in the field name, this field actually combines the offset for <em>all</em>
+     * dimensions other than X and Y.
      */
     private final long offsetZ;
 
@@ -318,39 +320,33 @@ public class ImageRenderer {
         offsetX = subtractExact(xmin, xcov);
         offsetY = subtractExact(ymin, ycov);
         /*
-         * If we are rendering a slice in a cube having more than 2 dimensions,
-         * there is a "global" offset to add for reachining the beginning of the slice.
+         * At this point, the RenderedImage properties have been computed as if the image was a single tile.
+         * Now compute `SampleModel` properties (the strides). Current version still assumes a single tile,
+         * but it could be changed in the future if we want to add tiling support. The following loop also
+         * computes a "global" offset to add for reachining the beginning of the slice if we are rendering
+         * a slice in a three-dimensional (or more) cube.
          */
-        if (dimension > GridCoverage2D.BIDIMENSIONAL) {
-            long base = 0, stride = 1;
-            for (int i=0; i<dimension; i++) {
-                if (i != xd && i != yd) {
-                    final long min = source.getLow(i);
-                    final long c = sliceExtent.getLow(i);
-                    if (c > min) {
-                        base = addExact(base, multiplyExact(stride, c - min));
-                    }
+        long stride         = 1;
+        long pixelStride    = 0;
+        long scanlineStride = 0;
+        long offsetZ        = 0;
+        for (int i=0; i<dimension; i++) {
+            if (i == xd) {
+                pixelStride = stride;
+            } else if (i == yd) {
+                scanlineStride = stride;
+            } else {
+                final long min = source.getLow(i);
+                final long c = sliceExtent.getLow(i);
+                if (c > min) {
+                    offsetZ = addExact(offsetZ, multiplyExact(stride, c - min));
                 }
-                stride = multiplyExact(stride, source.getSize(i));
             }
-            offsetZ = base;
-        } else {
-            offsetZ = 0;
-        }
-        /*
-         * At this point, the RenderedImage properties have been computed on the assumption
-         * that the returned image will be a single tile. Now compute SampleModel properties.
-         */
-        long pixelStride  = 1;
-        for (int i=0; i<xd; i++) {
-            pixelStride = multiplyExact(pixelStride, source.getSize(i));
-        }
-        long scanlineStride = pixelStride;
-        for (int i=xd; i<yd; i++) {
-            scanlineStride = multiplyExact(scanlineStride, source.getSize(i));
+            stride = multiplyExact(stride, source.getSize(i));
         }
         this.pixelStride    = toIntExact(pixelStride);
         this.scanlineStride = toIntExact(scanlineStride);
+        this.offsetZ        = offsetZ;
     }
 
     /**
diff --git a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/BufferedGridCoverageTest.java b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/BufferedGridCoverageTest.java
index 949fbcb..1efaf01 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/BufferedGridCoverageTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/coverage/grid/BufferedGridCoverageTest.java
@@ -30,7 +30,7 @@ import org.apache.sis.referencing.operation.transform.MathTransforms;
 import org.apache.sis.util.iso.Names;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.apache.sis.test.FeatureAssert.*;
 
 
 /**
@@ -77,8 +77,8 @@ public final strictfp class BufferedGridCoverageTest extends GridCoverage2DTest
      */
     @Test
     public void testMultidimensional() {
-        final int width  = 5;
-        final int height = 7;
+        final int width  = 4;
+        final int height = 3;
         final int nbTime = 3;
         final GridExtent extent = new GridExtent(null, null, new long[] {width, height, nbTime}, false);
         final GridGeometry domain = new GridGeometry(extent, PixelInCell.CELL_CENTER, MathTransforms.scale(2, 3, 5), null);
@@ -98,23 +98,31 @@ public final strictfp class BufferedGridCoverageTest extends GridCoverage2DTest
         /*
          * Verify a value in each temporal slice.
          */
-        GridExtent query = new GridExtent(null, null, new long[] {width, height, 0}, true);
-        RenderedImage slice = coverage.render(query);
-        assertSampleEquals(10, slice);
-
-        query = new GridExtent(null, new long[] {0,0,1}, new long[] {width, height, 1}, true);
-        slice = coverage.render(query);
-        assertSampleEquals(11, slice);
-
-        query = new GridExtent(null, new long[] {0,0,2}, new long[] {width, height, 2}, true);
-        slice = coverage.render(query);
-        assertSampleEquals(12, slice);
+        final int[] row10 = new int[width]; Arrays.fill(row10, 10);
+        final int[] row11 = new int[width]; Arrays.fill(row11, 11);
+        final int[] row12 = new int[width]; Arrays.fill(row12, 12);
+        assertRenderEqual(coverage, null,               new long[] {width, height, 0}, new int[][] {row10, row10, row10});
+        assertRenderEqual(coverage, new long[] {0,0,1}, new long[] {width, height, 1}, new int[][] {row11, row11, row11});
+        assertRenderEqual(coverage, new long[] {0,0,2}, new long[] {width, height, 2}, new int[][] {row12, row12, row12});
+        assertRenderEqual(coverage, null,               new long[] {width, 0, nbTime}, new int[][] {row10, row11, row12});
+        assertRenderEqual(coverage, null, new long[] {0, height, nbTime}, new int[][] {
+            {10, 10, 10},
+            {11, 11, 11},
+            {12, 12, 12}
+        });
     }
 
     /**
-     * Verifies that an arbitrary pixel taken from the given slice has a value equals to the expected value.
+     * Performs a {@link GridCoverage#render(GridExtent)} operation for the given region and
+     * verifies that pixels taken from the given slice have values equal to the expected values.
+     *
+     * @param coverage   the coverage on which to perform the render operation.
+     * @param low        lower grid coordinates, inclusive, or {@code null} for zeros.
+     * @param high       high grid coordinates, inclusives.
+     * @param expected   expected sample values.
      */
-    private static void assertSampleEquals(final int expected, final RenderedImage slice) {
-        assertEquals(expected, slice.getTile(0,0).getSample(1, 1, 0));
+    private static void assertRenderEqual(final GridCoverage coverage, final long[] low, final long[] high, final int[][] expected) {
+        final RenderedImage slice = coverage.render(new GridExtent(null, low, high, true));
+        assertValuesEqual(slice.getTile(0,0), 0, expected);
     }
 }