Ignore:
Timestamp:
2014-03-24T22:24:40+01:00 (10 years ago)
Author:
donvip
Message:

[josm_piclayer] code cleanup, https link to josm website

Location:
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java

    r29809 r30356  
    118118
    119119    private IconToggleButton picLayerActionButtonFactory(MapMode action) {
    120         IconToggleButton button = new IconToggleButton(action);
    121         button.setAutoHideDisabledButton(true);
    122         return button;
     120        IconToggleButton button = new IconToggleButton(action);
     121        button.setAutoHideDisabledButton(true);
     122        return button;
    123123    }
    124124
     
    136136
    137137        if (newPic) {
    138                 ((PicLayerAbstract)newLayer).setDrawPoints(true);
     138            ((PicLayerAbstract)newLayer).setDrawPoints(true);
    139139        }
    140140    }
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/GenericPicTransformAction.java

    r29595 r30356  
    2020public abstract class GenericPicTransformAction extends MapMode implements MouseListener, MouseMotionListener {
    2121
    22         protected boolean isDragging = false;
    23         protected PicLayerAbstract currentLayer = null;
    24         protected Point2D selectedPoint = null;
    25         protected EastNorth prevEastNorth = null;
    26         protected Point2D prevMousePoint = null;
    27         protected TransformCommand currentCommand = null;
    28         private String actionName;
     22    protected boolean isDragging = false;
     23    protected PicLayerAbstract currentLayer = null;
     24    protected Point2D selectedPoint = null;
     25    protected EastNorth prevEastNorth = null;
     26    protected Point2D prevMousePoint = null;
     27    protected TransformCommand currentCommand = null;
     28    private String actionName;
    2929
    30         public GenericPicTransformAction(String name, String actionName, String iconName,
    31                         String tooltip, Shortcut shortcut, MapFrame mapFrame, Cursor cursor) {
    32                 super(name, iconName, tooltip, shortcut, mapFrame, cursor);
    33                 this.actionName = actionName;
    34         }
     30    public GenericPicTransformAction(String name, String actionName, String iconName,
     31            String tooltip, Shortcut shortcut, MapFrame mapFrame, Cursor cursor) {
     32        super(name, iconName, tooltip, shortcut, mapFrame, cursor);
     33        this.actionName = actionName;
     34    }
    3535
    36         public GenericPicTransformAction(String name, String actionName, String iconName,
    37                         String tooltip, MapFrame mapFrame, Cursor cursor) {
    38                 super(name, iconName, tooltip, mapFrame, cursor);
     36    public GenericPicTransformAction(String name, String actionName, String iconName,
     37            String tooltip, MapFrame mapFrame, Cursor cursor) {
     38        super(name, iconName, tooltip, mapFrame, cursor);
    3939        this.actionName = actionName;
    40         }
     40    }
    4141
    42         @Override
    43         public void enterMode() {
    44             super.enterMode();
    45             Main.map.mapView.addMouseListener(this);
    46             Main.map.mapView.addMouseMotionListener(this);
    47         }
     42    @Override
     43    public void enterMode() {
     44        super.enterMode();
     45        Main.map.mapView.addMouseListener(this);
     46        Main.map.mapView.addMouseMotionListener(this);
     47    }
    4848
    49         @Override
    50         public void exitMode() {
    51             super.exitMode();
    52             Main.map.mapView.removeMouseListener(this);
    53             Main.map.mapView.removeMouseMotionListener(this);
    54         }
     49    @Override
     50    public void exitMode() {
     51        super.exitMode();
     52        Main.map.mapView.removeMouseListener(this);
     53        Main.map.mapView.removeMouseMotionListener(this);
     54    }
    5555
    56         @Override
    57         public void mousePressed(MouseEvent e) {
    58             // Start action
    59             if ( Main.map.mapView.getActiveLayer() instanceof PicLayerAbstract ) {
    60                 currentLayer = (PicLayerAbstract)Main.map.mapView.getActiveLayer();
     56    @Override
     57    public void mousePressed(MouseEvent e) {
     58        // Start action
     59        if ( Main.map.mapView.getActiveLayer() instanceof PicLayerAbstract ) {
     60            currentLayer = (PicLayerAbstract)Main.map.mapView.getActiveLayer();
    6161
    62                 if ( currentLayer != null && e.getButton() == MouseEvent.BUTTON1 ) {
    63                     requestFocusInMapView();
    64                     isDragging = true;
    65                     prevMousePoint = new Point(e.getPoint());
    66                     prevEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
    67                     // try to find and fill selected point if possible
    68                     selectedPoint = currentLayer.findSelectedPoint(e.getPoint());
    69                     currentCommand = new TransformCommand(currentLayer, actionName);
    70                 }
    71             }
    72         }
     62            if ( currentLayer != null && e.getButton() == MouseEvent.BUTTON1 ) {
     63                requestFocusInMapView();
     64                isDragging = true;
     65                prevMousePoint = new Point(e.getPoint());
     66                prevEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
     67                // try to find and fill selected point if possible
     68                selectedPoint = currentLayer.findSelectedPoint(e.getPoint());
     69                currentCommand = new TransformCommand(currentLayer, actionName);
     70            }
     71        }
     72    }
    7373
    74         @Override
     74    @Override
    7575    public void mouseDragged(MouseEvent e) {
    7676        // Call action performing
    7777        if(isDragging && currentLayer != null) {
    78                 doAction(e);
     78            doAction(e);
    7979            prevMousePoint = new Point(e.getPoint());
    8080            prevEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
     
    8383    }
    8484
    85         protected abstract void doAction(MouseEvent e);
     85    protected abstract void doAction(MouseEvent e);
    8686
    87         @Override
    88         public void mouseReleased(MouseEvent e) {
    89             // End action
    90             isDragging = false;
    91             if (currentCommand != null)
    92                 currentCommand.addIfChanged();
    93         }
     87    @Override
     88    public void mouseReleased(MouseEvent e) {
     89        // End action
     90        isDragging = false;
     91        if (currentCommand != null)
     92            currentCommand.addIfChanged();
     93    }
    9494
    95         @Override
    96         public boolean layerIsSupported(Layer l) {
    97                 return l instanceof PicLayerAbstract;
    98         }
     95    @Override
     96    public boolean layerIsSupported(Layer l) {
     97        return l instanceof PicLayerAbstract;
     98    }
    9999
    100         protected void updateDrawPoints(boolean value) {
    101             Layer active = Main.map.mapView.getActiveLayer();
     100    protected void updateDrawPoints(boolean value) {
     101        Layer active = Main.map.mapView.getActiveLayer();
    102102        if (active instanceof PicLayerAbstract) {
    103103            ((PicLayerAbstract)active).setDrawPoints(value);
    104104        }
    105105        Main.map.mapView.repaint();
    106         }
     106    }
    107107
    108108}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/ResetCalibrationAction.java

    r27403 r30356  
    1313public class ResetCalibrationAction extends JosmAction {
    1414
    15         private PicLayerAbstract layer;
    16         public ResetCalibrationAction(PicLayerAbstract layer, PictureTransform transformer) {
    17                 super(tr("Reset Calibration"), null, tr("Reset calibration"), null, false);
    18                 this.layer = layer;
    19         }
     15    private PicLayerAbstract layer;
     16    public ResetCalibrationAction(PicLayerAbstract layer, PictureTransform transformer) {
     17        super(tr("Reset Calibration"), null, tr("Reset calibration"), null, false);
     18        this.layer = layer;
     19    }
    2020
    21         @Override
    22         public void actionPerformed(ActionEvent arg0) {
    23             TransformCommand currentCommand = new TransformCommand(layer, tr("Calibration reset"));
    24                 layer.resetCalibration();
    25                 currentCommand.addIfChanged();
    26         }
     21    @Override
     22    public void actionPerformed(ActionEvent arg0) {
     23        TransformCommand currentCommand = new TransformCommand(layer, tr("Calibration reset"));
     24        layer.resetCalibration();
     25        currentCommand.addIfChanged();
     26    }
    2727}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/newlayer/NewLayerFromFileAction.java

    r27231 r30356  
    134134                    KMLReader kml = new KMLReader(file);
    135135                    kml.process();
    136                     JOptionPane.showMessageDialog(null, tr("KML calibration is in beta stage and may produce incorrectly calibrated layers!\nPlease use http://josm.openstreetmap.de/ticket/5451 to upload your KMLs that were calibrated incorrectly."));
     136                    JOptionPane.showMessageDialog(null, tr("KML calibration is in beta stage and may produce incorrectly calibrated layers!\n"+
     137                    "Please use {0} to upload your KMLs that were calibrated incorrectly.",
     138                    "https://josm.openstreetmap.de/ticket/5451"));
    137139                    for (KMLGroundOverlay overlay : kml.getGroundOverlays()) {
    138140                        //TODO: zoom to whole picture, not only the last
    139141                        addNewLayerFromKML(file, overlay, newLayerPos);
    140142                    }
    141 
    142143                } else {
    143                 addNewLayerFromFile(file, newLayerPos, fc.getSelectedFiles().length == 1);
    144 
     144                    addNewLayerFromFile(file, newLayerPos, fc.getSelectedFiles().length == 1);
     145                }
    145146            }
    146147        }
    147148    }
    148 }
    149149
    150150    private void addNewLayerFromFile(File file, int newLayerPos, boolean isZoomToLayer) {
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/MovePictureAction.java

    r27403 r30356  
    4444    }
    4545
    46         @Override
    47         protected void doAction(MouseEvent e) {
    48                 EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
     46    @Override
     47    protected void doAction(MouseEvent e) {
     48        EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
    4949        currentLayer.movePictureBy(
    5050            eastNorth.east() - prevEastNorth.east(),
    5151            eastNorth.north() - prevEastNorth.north()
    5252        );
    53         }
     53    }
    5454
    5555}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/RotatePictureAction.java

    r27403 r30356  
    4444    }
    4545
    46         @Override
    47         protected void doAction(MouseEvent e) {
    48                 double factor;
     46    @Override
     47    protected void doAction(MouseEvent e) {
     48        double factor;
    4949        if ( ( e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK ) != 0 ) {
    5050            factor = Main.pref.getDouble("piclayer.rotatefactors.high_precision", 100.0);
     
    5454        }
    5555        currentLayer.rotatePictureBy( ( e.getY() - prevMousePoint.getY() ) / factor );
    56         }
     56    }
    5757}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ScalePictureActionAbstract.java

    r27403 r30356  
    3535public abstract class ScalePictureActionAbstract extends GenericPicTransformAction {
    3636
    37         /**
     37    /**
    3838     * Constructor
    3939     */
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ShearPictureAction.java

    r27403 r30356  
    3939public class ShearPictureAction extends GenericPicTransformAction {
    4040
    41         /**
     41    /**
    4242     * Constructor
    4343     */
     
    4646    }
    4747
    48         @Override
    49         protected void doAction(MouseEvent e) {
     48    @Override
     49    protected void doAction(MouseEvent e) {
    5050        EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
    5151        currentLayer.shearPictureBy(
     
    5353            1000* (eastNorth.north() - prevEastNorth.north())
    5454        );
    55         }
     55    }
    5656
    5757}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/affine/MovePointAction.java

    r27403 r30356  
    1515public class MovePointAction extends GenericPicTransformAction {
    1616
    17         public MovePointAction(MapFrame frame) {
     17    public MovePointAction(MapFrame frame) {
    1818        super(tr("PicLayer Move point"), tr("Point added/moved"), "movepoint", tr("Drag or create point on the picture"), frame, ImageProvider.getCursor("crosshair", null));
    19         }
     19    }
    2020
    21         @Override
    22         protected void doAction(MouseEvent e) {
    23                 try {
    24                         Point2D pressed = currentLayer.transformPoint(e.getPoint());
    25                         if (selectedPoint != null) {
    26                                 currentLayer.getTransformer().replaceOriginPoint(selectedPoint, pressed);
    27                                 selectedPoint = pressed;
    28                         }
    29                 } catch (NoninvertibleTransformException e1) {
    30                         e1.printStackTrace();
    31                 }
    32         }
     21    @Override
     22    protected void doAction(MouseEvent e) {
     23        try {
     24            Point2D pressed = currentLayer.transformPoint(e.getPoint());
     25            if (selectedPoint != null) {
     26                currentLayer.getTransformer().replaceOriginPoint(selectedPoint, pressed);
     27                selectedPoint = pressed;
     28            }
     29        } catch (NoninvertibleTransformException e1) {
     30            e1.printStackTrace();
     31        }
     32    }
    3333
    34         @Override
    35         public void mouseClicked(MouseEvent e) {
    36                 if (currentLayer == null)
    37                         return;
     34    @Override
     35    public void mouseClicked(MouseEvent e) {
     36        if (currentLayer == null)
     37            return;
    3838
    39                 try {
    40                         Point2D pressed = currentLayer.transformPoint(e.getPoint());
    41                         if (selectedPoint == null)
    42                                 currentLayer.getTransformer().addOriginPoint(pressed);
     39        try {
     40            Point2D pressed = currentLayer.transformPoint(e.getPoint());
     41            if (selectedPoint == null)
     42                currentLayer.getTransformer().addOriginPoint(pressed);
    4343
    44                         currentCommand.addIfChanged();
    45                 } catch (NoninvertibleTransformException e1) {
    46                         e1.printStackTrace();
    47                 }
    48         }
     44            currentCommand.addIfChanged();
     45        } catch (NoninvertibleTransformException e1) {
     46            e1.printStackTrace();
     47        }
     48    }
    4949
    50         @Override
    51         public void enterMode() {
    52             super.enterMode();
    53             updateDrawPoints(true);
    54         }
     50    @Override
     51    public void enterMode() {
     52        super.enterMode();
     53        updateDrawPoints(true);
     54    }
    5555
    56         @Override
    57         public void exitMode() {
    58             super.exitMode();
    59             updateDrawPoints(false);
    60         }
     56    @Override
     57    public void exitMode() {
     58        super.exitMode();
     59        updateDrawPoints(false);
     60    }
    6161}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/affine/TransformPointAction.java

    r27662 r30356  
    1414public class TransformPointAction extends GenericPicTransformAction {
    1515
    16         public TransformPointAction(MapFrame frame) {
    17                 super(tr("PicLayer Transform point"), tr("Point transformed"), "transformpoint", tr("Transform point on the picture"), frame, ImageProvider.getCursor("crosshair", null));
    18         }
     16    public TransformPointAction(MapFrame frame) {
     17        super(tr("PicLayer Transform point"), tr("Point transformed"), "transformpoint", tr("Transform point on the picture"), frame, ImageProvider.getCursor("crosshair", null));
     18    }
    1919
    20         @Override
    21         protected void doAction(MouseEvent e) {
    22                 try {
    23                         Point2D pressed = currentLayer.transformPoint(e.getPoint());
    24                         if (selectedPoint != null) {
    25                             /*if (currentLayer.getTransformer().getOriginPoints().size() < 3)
    26                                 JOptionPane.showMessageDialog(null, tr("You should have 3 checkpoints to transform the image!"), tr("PicLayer"), JOptionPane.ERROR_MESSAGE, null);
    27                             else*/
    28                             {
    29                                 currentLayer.getTransformer().updatePair(selectedPoint, pressed);
    30                             }
    31                         }
     20    @Override
     21    protected void doAction(MouseEvent e) {
     22        try {
     23            Point2D pressed = currentLayer.transformPoint(e.getPoint());
     24            if (selectedPoint != null) {
     25                /*if (currentLayer.getTransformer().getOriginPoints().size() < 3)
     26                    JOptionPane.showMessageDialog(null, tr("You should have 3 checkpoints to transform the image!"), tr("PicLayer"), JOptionPane.ERROR_MESSAGE, null);
     27                else*/
     28                {
     29                    currentLayer.getTransformer().updatePair(selectedPoint, pressed);
     30                }
     31            }
    3232
    33                         currentCommand.addIfChanged();
    34                 } catch (NoninvertibleTransformException e1) {
    35                         e1.printStackTrace();
    36                 }
    37         }
     33            currentCommand.addIfChanged();
     34        } catch (NoninvertibleTransformException e1) {
     35            e1.printStackTrace();
     36        }
     37    }
    3838    @Override
    3939    public void enterMode() {
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/Matrix3D.java

    r27120 r30356  
    77
    88class Matrix3D {
    9         double[][] a;
    10        
    11         public Matrix3D() {
    12                 a = new double[3][]; a[0] = new double[3]; a[1] = new double[3]; a[2] = new double[3];
    13         }
    14        
    15         public Matrix3D(PictureTransform pictureTransform, double b11, double b12, double b13, double b21, double b22, double b23, double b31, double b32, double b33) {
    16                 this();
    17                
    18                 a[0][0] = b11; a[0][1] = b12; a[0][2] = b13;
    19                 a[1][0] = b21; a[1][1] = b22; a[1][2] = b23;
    20                 a[2][0] = b31; a[2][1] = b32; a[2][2] = b33;
    21         }
    22        
    23         public Matrix3D(List<? extends Point2D> list) {
    24                 this();
    25                
    26                 for(int i=0; i<3; i++) {
    27                         Point2D p = list.get(i);
    28                         a[0][i] = p.getX();
    29                         a[1][i] = p.getY();
    30                         a[2][i] = 1;
    31                 }
    32         }
     9    double[][] a;
     10   
     11    public Matrix3D() {
     12        a = new double[3][]; a[0] = new double[3]; a[1] = new double[3]; a[2] = new double[3];
     13    }
     14   
     15    public Matrix3D(PictureTransform pictureTransform, double b11, double b12, double b13, double b21, double b22, double b23, double b31, double b32, double b33) {
     16        this();
     17       
     18        a[0][0] = b11; a[0][1] = b12; a[0][2] = b13;
     19        a[1][0] = b21; a[1][1] = b22; a[1][2] = b23;
     20        a[2][0] = b31; a[2][1] = b32; a[2][2] = b33;
     21    }
     22   
     23    public Matrix3D(List<? extends Point2D> list) {
     24        this();
     25       
     26        for(int i=0; i<3; i++) {
     27            Point2D p = list.get(i);
     28            a[0][i] = p.getX();
     29            a[1][i] = p.getY();
     30            a[2][i] = 1;
     31        }
     32    }
    3333
    34         public Matrix3D multiply(Matrix3D m) {
    35                 Matrix3D result = new Matrix3D();
    36                 for(int i=0; i<3; i++)
    37                         for (int j=0; j<3; j++) {
    38                                 double sum = 0;
    39                                 for (int k=0; k<3; k++)
    40                                         sum += a[i][k]*m.a[k][j];
    41                                 result.a[i][j] = sum;
    42                         }                               
    43                 return result;
    44         }
    45        
    46         private double determinant() {
    47                 return a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])
    48                         +a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);
    49         }
    50        
    51         public Matrix3D inverse() throws NoSolutionException {
    52                 Matrix3D invert = new Matrix3D();
    53                 double det = determinant();
    54                 if (Math.abs(det) <= Double.MIN_VALUE)
    55                         throw new NoSolutionException("Determinant = 0");
    56                
    57                 double s = 1/det;
    58                
    59             invert.a[0][0] = (s) * (a[1][1] * a[2][2] - a[1][2] * a[2][1]);
    60             invert.a[1][0] = (s) * (a[1][2] * a[2][0] - a[1][0] * a[2][2]);
    61             invert.a[2][0] = (s) * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
    62             invert.a[0][1] = (s) * (a[0][2] * a[2][1] - a[0][1] * a[2][2]);
    63             invert.a[1][1] = (s) * (a[0][0] * a[2][2] - a[0][2] * a[2][0]);
    64             invert.a[2][1] = (s) * (a[0][1] * a[2][0] - a[0][0] * a[2][1]);
    65             invert.a[0][2] = (s) * (a[0][1] * a[1][2] - a[0][2] * a[1][1]);
    66             invert.a[1][2] = (s) * (a[0][2] * a[1][0] - a[0][0] * a[1][2]);
    67             invert.a[2][2] = (s) * (a[0][0] * a[1][1] - a[0][1] * a[1][0]);
    68            
    69             return invert;
    70         }
    71        
    72         public AffineTransform toAffineTransform() throws NoSolutionException {
    73                 if (!(Math.abs(a[2][0]) <= 1e-2 && Math.abs(a[2][1]) <= 1e-2 && Math.abs(a[2][2]-1) <= 1e-2))
    74                         throw new NoSolutionException("Resulted matrix is not AF");
    75                 return new AffineTransform(a[0][0], a[1][0], a[0][1], a[1][1], a[0][2], a[1][2]);
    76         }
     34    public Matrix3D multiply(Matrix3D m) {
     35        Matrix3D result = new Matrix3D();
     36        for(int i=0; i<3; i++)
     37            for (int j=0; j<3; j++) {
     38                double sum = 0;
     39                for (int k=0; k<3; k++)
     40                    sum += a[i][k]*m.a[k][j];
     41                result.a[i][j] = sum;
     42            }               
     43        return result;
     44    }
     45   
     46    private double determinant() {
     47        return a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])
     48               +a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);
     49    }
     50   
     51    public Matrix3D inverse() throws NoSolutionException {
     52        Matrix3D invert = new Matrix3D();
     53        double det = determinant();
     54        if (Math.abs(det) <= Double.MIN_VALUE)
     55            throw new NoSolutionException("Determinant = 0");
     56       
     57        double s = 1/det;
     58       
     59        invert.a[0][0] = (s) * (a[1][1] * a[2][2] - a[1][2] * a[2][1]);
     60        invert.a[1][0] = (s) * (a[1][2] * a[2][0] - a[1][0] * a[2][2]);
     61        invert.a[2][0] = (s) * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
     62        invert.a[0][1] = (s) * (a[0][2] * a[2][1] - a[0][1] * a[2][2]);
     63        invert.a[1][1] = (s) * (a[0][0] * a[2][2] - a[0][2] * a[2][0]);
     64        invert.a[2][1] = (s) * (a[0][1] * a[2][0] - a[0][0] * a[2][1]);
     65        invert.a[0][2] = (s) * (a[0][1] * a[1][2] - a[0][2] * a[1][1]);
     66        invert.a[1][2] = (s) * (a[0][2] * a[1][0] - a[0][0] * a[1][2]);
     67        invert.a[2][2] = (s) * (a[0][0] * a[1][1] - a[0][1] * a[1][0]);
     68       
     69        return invert;
     70    }
     71   
     72    public AffineTransform toAffineTransform() throws NoSolutionException {
     73        if (!(Math.abs(a[2][0]) <= 1e-2 && Math.abs(a[2][1]) <= 1e-2 && Math.abs(a[2][2]-1) <= 1e-2))
     74            throw new NoSolutionException("Resulted matrix is not AF");
     75        return new AffineTransform(a[0][0], a[1][0], a[0][1], a[1][1], a[0][2], a[1][2]);
     76    }
    7777}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/NoSolutionException.java

    r27120 r30356  
    22
    33class NoSolutionException extends Exception {
    4        
    5         public NoSolutionException(String message) {
    6                 super(message);
    7         }
     4   
     5    public NoSolutionException(String message) {
     6        super(message);
     7    }
    88
    9         private static final long serialVersionUID = 3170170664858078930L;
     9    private static final long serialVersionUID = 3170170664858078930L;
    1010}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java

    r27662 r30356  
    1010public class PictureTransform {
    1111
    12         private AffineTransform cachedTransform;
    13         private EastNorth imagePosition;
     12    private AffineTransform cachedTransform;
     13    private EastNorth imagePosition;
    1414
    15         public EastNorth getImagePosition() {
     15    public EastNorth getImagePosition() {
    1616        return imagePosition;
    1717    }
     
    2323    private boolean modified = false;
    2424
    25         private List<Point2D> originPoints;
     25    private List<Point2D> originPoints;
    2626
    27         public PictureTransform() {
    28                 cachedTransform = new AffineTransform();
    29                 originPoints = new ArrayList<Point2D>(3);
    30         }
     27    public PictureTransform() {
     28        cachedTransform = new AffineTransform();
     29        originPoints = new ArrayList<Point2D>(3);
     30    }
    3131
    32         public AffineTransform getTransform() {
    33                 return cachedTransform;
    34         }
     32    public AffineTransform getTransform() {
     33        return cachedTransform;
     34    }
    3535
    36         private AffineTransform solveEquation(List<Point2D> desiredPoints) throws NoSolutionException {
    37                 Matrix3D X = new Matrix3D(originPoints);
    38                 Matrix3D Y = new Matrix3D(desiredPoints);
    39                 Matrix3D result = Y.multiply(X.inverse());
     36    private AffineTransform solveEquation(List<Point2D> desiredPoints) throws NoSolutionException {
     37        Matrix3D X = new Matrix3D(originPoints);
     38        Matrix3D Y = new Matrix3D(desiredPoints);
     39        Matrix3D result = Y.multiply(X.inverse());
    4040
    41                 return result.toAffineTransform();
    42         }
     41        return result.toAffineTransform();
     42    }
    4343
    44         public void addOriginPoint(Point2D originPoint) {
    45                 if (originPoints.size() < 3)
    46                         originPoints.add(originPoint);
    47         }
     44    public void addOriginPoint(Point2D originPoint) {
     45        if (originPoints.size() < 3)
     46            originPoints.add(originPoint);
     47    }
    4848
    49         public void resetCalibration() {
    50                 originPoints.clear();
    51                 modified = false;
    52                 cachedTransform = new AffineTransform();
    53         }
     49    public void resetCalibration() {
     50        originPoints.clear();
     51        modified = false;
     52        cachedTransform = new AffineTransform();
     53    }
    5454
    55         /**
    56         * updates pair of points (suppose that other pairs are (origin=>origin) points are the same),
    57         * solves equation,
    58         * applies transform matrix to the existing cachedTransform
    59         *
    60         * @param originPoint - should be one of origin points, otherwise - no transform applied
    61         * @param desiredPoint - new place for the point
    62         */
    63         public void updatePair(Point2D originPoint, Point2D desiredPoint) {
    64                 if (originPoint == null)
    65                         return;
     55    /**
     56    * updates pair of points (suppose that other pairs are (origin=>origin) points are the same),
     57    * solves equation,
     58    * applies transform matrix to the existing cachedTransform
     59    *
     60    * @param originPoint - should be one of origin points, otherwise - no transform applied
     61    * @param desiredPoint - new place for the point
     62    */
     63    public void updatePair(Point2D originPoint, Point2D desiredPoint) {
     64        if (originPoint == null)
     65            return;
    6666
    67                 switch (originPoints.size()) {
    68                 case 1: {
    69                     cachedTransform.concatenate(AffineTransform.getTranslateInstance(desiredPoint.getX()-originPoint.getX(),
    70                             desiredPoint.getY()-originPoint.getY()));
    71                     break;
    72                 }
    73                 case 2: {
    74                     // find triangle and move it
    75                     List<Point2D> desiredPoints = new ArrayList<Point2D>(3);
    76                     Point2D o1 = originPoints.get(0);
    77                     Point2D o2 = originPoints.get(1);
    78                     Point2D d1, d2;
    79                     if (o2 == originPoint) {
    80                         d2 = desiredPoint;
    81                         d1 = (Point2D) o1.clone();
    82                     } else {
    83                         d1 = desiredPoint;
    84                         d2 = (Point2D) o2.clone();
    85                     }
    86                     Point2D o3 = calculateTrianglePoint(o1, o2);
    87                     Point2D d3 = calculateTrianglePoint(d1, d2);
    88                     originPoints.add(o3);
    89                     desiredPoints.add(d1); desiredPoints.add(d2); desiredPoints.add(d3);
    90                     trySolve(desiredPoints);
    91                     originPoints.remove(2);
    92                     break;
    93                 }
    94                 case 3: {
    95                     List<Point2D> desiredPoints = new ArrayList<Point2D>(3);
     67        switch (originPoints.size()) {
     68        case 1: {
     69            cachedTransform.concatenate(AffineTransform.getTranslateInstance(desiredPoint.getX()-originPoint.getX(),
     70                    desiredPoint.getY()-originPoint.getY()));
     71            break;
     72        }
     73        case 2: {
     74            // find triangle and move it
     75            List<Point2D> desiredPoints = new ArrayList<Point2D>(3);
     76            Point2D o1 = originPoints.get(0);
     77            Point2D o2 = originPoints.get(1);
     78            Point2D d1, d2;
     79            if (o2 == originPoint) {
     80                d2 = desiredPoint;
     81                d1 = (Point2D) o1.clone();
     82            } else {
     83                d1 = desiredPoint;
     84                d2 = (Point2D) o2.clone();
     85            }
     86            Point2D o3 = calculateTrianglePoint(o1, o2);
     87            Point2D d3 = calculateTrianglePoint(d1, d2);
     88            originPoints.add(o3);
     89            desiredPoints.add(d1); desiredPoints.add(d2); desiredPoints.add(d3);
     90            trySolve(desiredPoints);
     91            originPoints.remove(2);
     92            break;
     93        }
     94        case 3: {
     95            List<Point2D> desiredPoints = new ArrayList<Point2D>(3);
    9696
    97                 for (Point2D origin : originPoints) {
    98                     if (origin.equals(originPoint))
    99                         desiredPoints.add(desiredPoint);
    100                     else
    101                         desiredPoints.add(origin);
    102                 }
    103                 trySolve(desiredPoints);
    104                 break;
    105                 }
    106                 default:
     97            for (Point2D origin : originPoints) {
     98                if (origin.equals(originPoint))
     99                    desiredPoints.add(desiredPoint);
     100                else
     101                    desiredPoints.add(origin);
     102            }
     103            trySolve(desiredPoints);
     104            break;
     105        }
     106        default:
    107107
    108                 }
     108        }
    109109
    110         }
     110    }
    111111
    112112    private Point2D calculateTrianglePoint(Point2D d1, Point2D d2) {
     
    133133    }
    134134
    135         public void replaceOriginPoint(Point2D originPoint, Point2D newOriginPoint) {
    136                 if (originPoint == null || newOriginPoint == null)
    137                         return;
     135    public void replaceOriginPoint(Point2D originPoint, Point2D newOriginPoint) {
     136        if (originPoint == null || newOriginPoint == null)
     137            return;
    138138
    139                 int index = originPoints.indexOf(originPoint);
    140                 if (index < 0)
    141                         return;
     139        int index = originPoints.indexOf(originPoint);
     140        if (index < 0)
     141            return;
    142142
    143                 originPoints.set(index, newOriginPoint);
    144         }
     143        originPoints.set(index, newOriginPoint);
     144    }
    145145
    146         public void concatenateTransformPoint(AffineTransform transform, Point2D trans) {
     146    public void concatenateTransformPoint(AffineTransform transform, Point2D trans) {
    147147
    148             if (trans != null) {
     148        if (trans != null) {
    149149            AffineTransform centered = AffineTransform.getTranslateInstance(trans.getX(), trans.getY());
    150150            centered.concatenate(transform);
    151151            centered.translate(-trans.getX(), -trans.getY());
    152152            cachedTransform.concatenate(centered);
    153             } else {
    154                 cachedTransform.concatenate(transform);
    155             }
     153        } else {
     154            cachedTransform.concatenate(transform);
     155        }
    156156
    157157
    158                 for (int i = 0; i < originPoints.size(); i++) {
    159                         Point2D point = originPoints.get(i);
    160                         transform.transform(point, point);
    161                 }
    162                 modified = true;
    163         }
     158        for (int i = 0; i < originPoints.size(); i++) {
     159            Point2D point = originPoints.get(i);
     160            transform.transform(point, point);
     161        }
     162        modified = true;
     163    }
    164164
    165         public boolean isModified() {
    166             return modified;
    167         }
     165    public boolean isModified() {
     166        return modified;
     167    }
    168168
    169169    public void setModified() {
Note: See TracChangeset for help on using the changeset viewer.