sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/02: Add a visual test for `org.apache.sis.internal.processing.image.Isolines`.
Date Fri, 18 Dec 2020 15:55:54 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch visual-test
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 6162dbc2bf64a9841bb0672c3d4e02ed335f7b36
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Dec 18 16:54:22 2020 +0100

    Add a visual test for `org.apache.sis.internal.processing.image.Isolines`.
---
 pom.xml                                            |   2 +-
 src/main/java/org/apache/sis/swing/ZoomPane.java   |   7 ++
 .../org/apache/sis/test/visual/DesktopPane.java    |  13 +-
 .../org/apache/sis/test/visual/IsolinesView.java   | 140 +++++++++++++++++++++
 .../org/apache/sis/test/visual/Visualization.java  |   3 +-
 .../java/org/apache/sis/swing/ZoomPaneTest.java    |   6 +-
 6 files changed, 160 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index b5d6a81..463807f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -79,7 +79,7 @@
   <dependencies>
     <dependency>
       <groupId>org.apache.sis.core</groupId>
-      <artifactId>sis-referencing</artifactId>
+      <artifactId>sis-feature</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
diff --git a/src/main/java/org/apache/sis/swing/ZoomPane.java b/src/main/java/org/apache/sis/swing/ZoomPane.java
index 070ff44..511d158 100644
--- a/src/main/java/org/apache/sis/swing/ZoomPane.java
+++ b/src/main/java/org/apache/sis/swing/ZoomPane.java
@@ -632,6 +632,13 @@ public abstract class ZoomPane extends JComponent implements DeformableViewer
{
     private Paint magnifierBorder = DEFAULT_MAGNIFIER_BORDER;
 
     /**
+     * Creates a new zoom pane allowing all actions.
+     */
+    public ZoomPane() {
+        this(UNIFORM_SCALE | ROTATE | TRANSLATE_X | TRANSLATE_Y | RESET | DEFAULT_ZOOM);
+    }
+
+    /**
      * Constructs a {@code ZoomPane}.
      *
      * @param allowedActions
diff --git a/src/main/java/org/apache/sis/test/visual/DesktopPane.java b/src/main/java/org/apache/sis/test/visual/DesktopPane.java
index d46136f..744dbeb 100644
--- a/src/main/java/org/apache/sis/test/visual/DesktopPane.java
+++ b/src/main/java/org/apache/sis/test/visual/DesktopPane.java
@@ -109,7 +109,7 @@ final class DesktopPane extends JDesktopPane {
         frame.setJMenuBar(menuBar);
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.setContentPane(this);
-        frame.setSize(1000, 600);
+        frame.setSize(1500, 1000);
         frame.setLocationRelativeTo(null);                          // Put at screen center.
         frame.setVisible(true);
     }
@@ -133,9 +133,14 @@ final class DesktopPane extends JDesktopPane {
      * Shows the widget created by the given test case.
      */
     private void show(final Visualization testCase) {
-        final int n = testCase.numTests;
-        for (int i=0; i<n; i++) {
-            show(testCase.create(i), i, n);
+        try {
+            final int n = testCase.numTests;
+            for (int i=0; i<n; i++) {
+                show(testCase.create(i), i, n);
+            }
+        } catch (Exception e) {
+            // Not acceptable for a real application, but this widget is only for testing
purpose.
+            e.printStackTrace();
         }
     }
 
diff --git a/src/main/java/org/apache/sis/test/visual/IsolinesView.java b/src/main/java/org/apache/sis/test/visual/IsolinesView.java
new file mode 100644
index 0000000..d8d946b
--- /dev/null
+++ b/src/main/java/org/apache/sis/test/visual/IsolinesView.java
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.test.visual;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Random;
+import java.awt.Color;
+import java.awt.BasicStroke;
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
+import java.awt.Shape;
+import java.awt.geom.Rectangle2D;
+import java.awt.geom.AffineTransform;
+import java.awt.image.WritableRaster;
+import java.awt.image.BufferedImage;
+import javax.swing.JComponent;
+import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.swing.ZoomPane;
+import org.apache.sis.internal.processing.image.Isolines;
+
+
+/**
+ * Generate an image with synthetic mounts and draw isolines on that image.
+ * This allows a visual check of {@link Isolines} results.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.1
+ * @since   1.1
+ * @module
+ */
+public final class IsolinesView extends Visualization {
+    /**
+     * Shows a random image with isolines on it.
+     *
+     * @param  args  ignored.
+     */
+    public static void main(final String[] args) {
+        new IsolinesView().show();
+    }
+
+    /**
+     * Desired size of the image to test.
+     */
+    private final int width, height;
+
+    /**
+     * Colors to use for drawing isolines.
+     */
+    private final Color[] colors;
+
+    /**
+     * Creates a new viewer for {@link Isolines}.
+     */
+    public IsolinesView() {
+        super(Isolines.class, 4);
+        width  = 800;
+        height = 600;
+        colors = new Color[] {
+            Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.ORANGE, Color.RED
+        };
+    }
+
+    /**
+     * Creates a widget showing a random image with isolines on it.
+     * The widget uses {@link ZoomPane}.
+     *
+     * @param  index  a sequence number for the isoline window.
+     * @return a widget showing isolines.
+     * @throws TransformException if an error occurred while computing isolines.
+     */
+    @Override
+    protected JComponent create(final int index) throws TransformException {
+        final BufferedImage image = createImage(index * 1000);
+        final List<Shape> shapes = new ArrayList<>();
+        for (final Isolines isolines : Isolines.generate(image, new double[][] {{0x20, 0x40,
0x60, 0x80, 0xA0, 0xC0, 0xE0}}, 0, null)) {
+            shapes.addAll(isolines.polylines().values());
+        }
+        final ZoomPane pane = new ZoomPane() {
+            /** Requests a window of the size of the image to show. */
+            @Override public Rectangle2D getArea() {
+                return new Rectangle(width, height);
+            }
+
+            /** Paints isolines on top of the image. */
+            @Override protected void paintComponent(final Graphics2D graphics) {
+                graphics.transform(zoom);
+                graphics.drawRenderedImage(image, new AffineTransform());
+                graphics.setStroke(new BasicStroke(0));
+                int count = 0;
+                for (final Shape shape : shapes) {
+                    graphics.setColor(colors[count++ % colors.length]);
+                    graphics.draw(shape);
+                }
+            }
+        };
+        pane.setPaintingWhileAdjusting(true);
+        return pane.createScrollPane();
+    }
+
+    /**
+     * Creates a grayscale image of given size with random mounts.
+     */
+    private BufferedImage createImage(final int seed) {
+        final Random         random = new Random(seed);
+        final BufferedImage  image  = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
+        final WritableRaster raster = image.getRaster();
+        for (int i=0; i<10; i++) {
+            final int centerX = random.nextInt(width);
+            final int centerY = random.nextInt(height);
+            final double magnitude = 255d / (i+1);
+            final double fx = (i+1) / (width  * -40 * (random.nextDouble() + 0.5));
+            final double fy = (i+1) / (height * -40 * (random.nextDouble() + 0.5));
+            for (int y=0; y<height; y++) {
+                for (int x=0; x<width; x++) {
+                    final int dx = x - centerX;
+                    final int dy = y - centerY;
+                    double value = magnitude * Math.exp(dx*dx*fx + dy*dy*fy);
+                    value += raster.getSample(x, y, 0);
+                    raster.setSample(x, y, 0, (int) Math.min(255, Math.round(value)));
+                }
+            }
+        }
+        return image;
+    }
+}
diff --git a/src/main/java/org/apache/sis/test/visual/Visualization.java b/src/main/java/org/apache/sis/test/visual/Visualization.java
index 310928a..63ccb82 100644
--- a/src/main/java/org/apache/sis/test/visual/Visualization.java
+++ b/src/main/java/org/apache/sis/test/visual/Visualization.java
@@ -62,9 +62,10 @@ public abstract class Visualization {
      * Creates a widget showing the object to test.
      *
      * @param  index  index of test occurrence, from 0 inclusive to the value given at construction
time, exclusive.
+     * @throws Exception if an error occurred while computing data for the widget.
      * @return a widget showing the object to test.
      */
-    protected abstract JComponent create(int index);
+    protected abstract JComponent create(int index) throws Exception;
 
     /**
      * Creates and shows a widget visualizing the object to test.
diff --git a/src/test/java/org/apache/sis/swing/ZoomPaneTest.java b/src/test/java/org/apache/sis/swing/ZoomPaneTest.java
index f0becba..a21f90c 100644
--- a/src/test/java/org/apache/sis/swing/ZoomPaneTest.java
+++ b/src/test/java/org/apache/sis/swing/ZoomPaneTest.java
@@ -56,11 +56,7 @@ public final strictfp class ZoomPaneTest extends TestCase {
     protected JComponent create(final int index) {
         final Rectangle rect = new Rectangle(100,200,100,93);
         final Polygon   poly = new Polygon(new int[] {125,175,150}, new int[] {225,225,268},
3);
-        final ZoomPane  pane = new ZoomPane(
-                ZoomPane.UNIFORM_SCALE | ZoomPane.ROTATE      |
-                ZoomPane.TRANSLATE_X   | ZoomPane.TRANSLATE_Y |
-                ZoomPane.RESET         | ZoomPane.DEFAULT_ZOOM)
-        {
+        final ZoomPane  pane = new ZoomPane() {
             @Override public Rectangle2D getArea() {
                 return rect;
             }


Mime
View raw message