Ticket #18908: patch_normalize_sequence.patch

File patch_normalize_sequence.patch, 7.3 KB (added by francois2, 5 years ago)

Normalize the direction of a sequence of images

  • photoadjust/.classpath

     
    11<?xml version="1.0" encoding="UTF-8"?>
    22<classpath>
    33        <classpathentry kind="src" path="src"/>
     4        <classpathentry kind="src" output="buildtest" path="test/unit">
     5                <attributes>
     6                        <attribute name="test" value="true"/>
     7                </attributes>
     8        </classpathentry>
    49        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
     10        <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
    511        <classpathentry combineaccessrules="false" kind="src" path="/JOSM"/>
     12        <classpathentry kind="lib" path="../00_core_test_lib/jmockit.jar"/>
    613        <classpathentry kind="output" path="build"/>
    714</classpath>
  • photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/PhotoAdjustPlugin.java

     
    88import java.util.List;
    99
    1010import org.openstreetmap.josm.gui.MainApplication;
    1111import org.openstreetmap.josm.gui.MapFrame;
    1212import org.openstreetmap.josm.gui.layer.Layer;
    1313import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
     
    3637    public PhotoAdjustPlugin(PluginInformation info) {
    3738        super(info);
    3839        GeoImageLayer.registerMenuAddition(new UntaggedGeoImageLayerAction());
     40        GeoImageLayer.registerMenuAddition(new SetDirectionToASequence());
    3941        PhotoPropertyEditor.init();
  • photoadjust/src/org/openstreetmap/josm/plugins/photoadjust/SetDirectionToASequence.java

             initAdapters();
         }
     
     
     1package org.openstreetmap.josm.plugins.photoadjust;
     2
     3import static org.openstreetmap.josm.tools.I18n.tr;
     4
     5import java.awt.Component;
     6import java.awt.event.ActionEvent;
     7import java.util.List;
     8
     9import javax.swing.AbstractAction;
     10import javax.swing.JMenuItem;
     11
     12import org.openstreetmap.josm.data.ImageData;
     13import org.openstreetmap.josm.data.coor.LatLon;
     14import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
     15import org.openstreetmap.josm.gui.layer.Layer;
     16import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
     17import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
     18import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry;
     19
     20public class SetDirectionToASequence extends AbstractAction implements LayerAction {
     21
     22    public SetDirectionToASequence() {
     23        super(tr("Set every image to look at the next one in the sequence"), null);
     24    }
     25
     26    @Override
     27    public void actionPerformed(ActionEvent arg0) {
     28        GeoImageLayer layer = getSelectedLayer();
     29
     30        List<ImageEntry> images = layer.getImageData().getImages();
     31        normalizeSequence(images, layer.getImageData());
     32    }
     33
     34    public void normalizeSequence(List<ImageEntry> images, ImageData data) {
     35        Double lastDirection = null;
     36        for (int i = 0; i < images.size(); i++) {
     37                ImageEntry entry = images.get(i);
     38                if (i + 1 < images.size()) {
     39                        lastDirection = changeDirection(entry, images.get(i + 1), data);
     40                }
     41                if (lastDirection != null) {
     42                        data.updateImageDirection(entry, lastDirection);
     43                }
     44        }
     45    }
     46   
     47    private static GeoImageLayer getSelectedLayer() {
     48        return (GeoImageLayer)LayerListDialog.getInstance().getModel()
     49                .getSelectedLayers().get(0);
     50    }
     51
     52    private Double changeDirection(ImageEntry photo, ImageEntry next, ImageData data) {
     53        final LatLon photoLL = photo.getPos();
     54        if (photoLL == null) {
     55            // Direction cannot be set if image doesn't have a position.
     56            return null;
     57        }
     58        final LatLon mouseLL = next.getPos();
     59        // The projection doesn't matter here.
     60        double direction = photoLL.bearing(mouseLL) * 360.0 / 2.0 / Math.PI;
     61        if (direction < 0.0) {
     62            direction += 360.0;
     63        } else if (direction >= 360.0) {
     64            direction -= 360.0;
     65        }
     66        return direction;
     67    }
     68
     69    @Override
     70    public boolean supportLayers(List<Layer> layers) {
     71        return layers.size() == 1 && layers.get(0) instanceof GeoImageLayer;
     72    }
     73
     74    @Override
     75    public Component createMenuComponent() {
     76        return new JMenuItem(this);
     77    }
     78}
  • photoadjust/test/unit/org/openstreetmap/josm/plugins/photoadjust/SetDirectionToASequenceTest.java

     
     1package org.openstreetmap.josm.plugins.photoadjust;
     2
     3import java.io.File;
     4import java.util.ArrayList;
     5
     6import org.junit.Test;
     7import org.openstreetmap.josm.data.ImageData;
     8import org.openstreetmap.josm.data.coor.CachedLatLon;
     9import org.openstreetmap.josm.gui.layer.geoimage.ImageEntry;
     10
     11import mockit.Delegate;
     12import mockit.Expectations;
     13import mockit.Mocked;
     14
     15public class SetDirectionToASequenceTest {
     16
     17        @Test
     18        public void testChangeDirectionToASequence() {
     19                SetDirectionToASequence setdirection = new SetDirectionToASequence();
     20               
     21                ArrayList<ImageEntry> list = new ArrayList<>();
     22        ImageEntry first = new ImageEntry(new File("test1"));
     23        ImageEntry last = new ImageEntry(new File("test3"));
     24        ImageEntry middle = new ImageEntry(new File("test2"));
     25        list.add(first);
     26        list.add(middle);
     27        list.add(last);
     28       
     29        ImageData data = new ImageData(list);
     30       
     31        new Expectations(first) {{
     32            first.getPos(); result = new CachedLatLon(0, 0);
     33        }};
     34       
     35        new Expectations(middle) {{
     36            middle.getPos(); result = new CachedLatLon(0, 1);
     37        }};
     38       
     39        new Expectations(last) {{
     40            last.getPos(); result = new CachedLatLon(0, 0);
     41        }};
     42       
     43        new Expectations(data) {{
     44            data.updateImageDirection(first, 90);
     45            data.updateImageDirection(middle, 270);
     46            data.updateImageDirection(last, 270);
     47        }};
     48       
     49                setdirection.normalizeSequence(list, data);
     50        }
     51       
     52        @Test
     53        public void testChangeDirectionWithNullPosition() {
     54                SetDirectionToASequence setdirection = new SetDirectionToASequence();
     55               
     56                ArrayList<ImageEntry> list = new ArrayList<>();
     57        ImageEntry first = new ImageEntry(new File("test1"));
     58        ImageEntry last = new ImageEntry(new File("test3"));
     59        list.add(first);
     60        list.add(last);
     61       
     62        ImageData data = new ImageData(list);
     63       
     64        new Expectations(first) {{
     65            first.getPos(); result = null;
     66        }};
     67       
     68                setdirection.normalizeSequence(list, data);
     69        }
     70
     71}