Ignore:
Timestamp:
2012-01-30T11:42:14+01:00 (13 years ago)
Author:
larry0ua
Message:

'PicLayer - added possibility to edit transformation with 1 or 2 points (move or rect transformation)'

Location:
applications/editors/josm/plugins/piclayer
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/piclayer/build.xml

    r27627 r27662  
    2222-->
    2323<project name="PicLayer" default="dist" basedir=".">
    24     <property name="commit.message" value="PicLayer - fix #7319"/>
     24    <property name="commit.message" value="PicLayer - added possibility to edit transformation with 1 or 2 points (move or rect transformation)"/>
    2525    <property name="plugin.main.version" value="4669"/>
    2626    <!--
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java

    r27296 r27662  
    5252import org.openstreetmap.josm.plugins.piclayer.actions.transform.ShearPictureAction;
    5353import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction;
     54import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.RemovePointAction;
    5455import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.TransformPointAction;
    5556import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract;
     
    9798            MovePointAction movePointAction = new MovePointAction(newFrame);
    9899            TransformPointAction transformPointAction = new TransformPointAction(newFrame);
     100            RemovePointAction removePointAction = new RemovePointAction(newFrame);
    99101
    100102            RotatePictureAction rotatePictureAction = new RotatePictureAction(newFrame);
     
    109111            buttonList.add(picLayerActionButtonFactory(movePointAction));
    110112            buttonList.add(picLayerActionButtonFactory(transformPointAction));
     113            buttonList.add(picLayerActionButtonFactory(removePointAction));
    111114            buttonList.add(picLayerActionButtonFactory(rotatePictureAction));
    112115            buttonList.add(picLayerActionButtonFactory(scaleXYPictureAction));
     
    120123        }
    121124    }
    122    
     125
    123126    private IconToggleButton picLayerActionButtonFactory(MapMode action) {
    124127        IconToggleButton button = new IconToggleButton(action);
     
    134137        boolean oldPic = oldLayer instanceof PicLayerAbstract;
    135138        boolean newPic = newLayer instanceof PicLayerAbstract;
    136         // actually that should be not enough - JOSM should hide all buttons that are disabled for current layer!
    137         if (oldPic) { // leave picture layer            if (oldLayer != null)
     139
     140        if (oldPic) {
    138141            ((PicLayerAbstract)oldLayer).setDrawPoints(false);
    139142        }
     143
    140144        if (newPic) {
    141145                ((PicLayerAbstract)newLayer).setDrawPoints(true);
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/affine/TransformPointAction.java

    r27403 r27662  
    66import java.awt.geom.NoninvertibleTransformException;
    77import java.awt.geom.Point2D;
    8 
    9 import javax.swing.JOptionPane;
    108
    119import org.openstreetmap.josm.gui.MapFrame;
     
    2523                        Point2D pressed = currentLayer.transformPoint(e.getPoint());
    2624                        if (selectedPoint != null) {
    27                             if (currentLayer.getTransformer().getOriginPoints().size() < 3)
     25                            /*if (currentLayer.getTransformer().getOriginPoints().size() < 3)
    2826                                JOptionPane.showMessageDialog(null, tr("You should have 3 checkpoints to transform the image!"), tr("PicLayer"), JOptionPane.ERROR_MESSAGE, null);
    29                             else
     27                            else*/
     28                            {
    3029                                currentLayer.getTransformer().updatePair(selectedPoint, pressed);
     30                            }
    3131                        }
    3232
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/command/TransformCommand.java

    r27435 r27662  
    5050        layer.getTransformer().setTransform(transform.getTransform());
    5151        layer.getTransformer().setOriginPoints(transform.getOriginPoints());
     52        layer.getTransformer().setImagePosition(transform.getImagePosition());
    5253    }
    5354
     
    5657        transform.setOriginPoints(layer.getTransformer().getOriginPoints());
    5758        transform.setTransform(layer.getTransformer().getTransform());
     59        transform.setImagePosition(layer.getTransformer().getImagePosition());
    5860        return transform;
    5961    }
     
    7880
    7981        boolean changed = !(beforeTransform.getTransform().equals(afterTransform.getTransform())) ||
    80             !(beforeTransform.getOriginPoints().equals(afterTransform.getOriginPoints()));
     82            !(beforeTransform.getOriginPoints().equals(afterTransform.getOriginPoints())) ||
     83            !(beforeTransform.getImagePosition().equals(afterTransform.getImagePosition()));
    8184        if (changed && !alreadyAdded) {
    8285            Main.main.undoRedo.add(this);
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java

    r27403 r27662  
    7272
    7373    // Initial position of the image in the real world
    74     protected EastNorth initialImagePosition;
     74//    protected EastNorth initialImagePosition;
    7575
    7676    // Position of the image in the real world
    77     protected EastNorth imagePosition;
     77    //protected EastNorth imagePosition;
    7878
    7979    // The scale that was set on the map during image creation
     
    9696
    9797    // Keys for loading from old/new Properties
    98     private static final String INITIAL_POS_X = "INITIAL_POS_X";
    99     private static final String INITIAL_POS_Y = "INITIAL_POS_y";
    10098    private static final String POSITION_X = "POSITION_X";
    10199    private static final String POSITION_Y = "POSITION_Y";
     
    148146        // First, we initialize the calibration, so that createImage() can rely on it
    149147
     148        transformer = new PictureTransform();
     149
    150150        // If the map does not exist - we're screwed. We should not get into this situation in the first place!
    151151        if ( Main.map != null && Main.map.mapView != null ) {
     
    153153            EastNorth center = Main.map.mapView.getCenter();
    154154
    155             imagePosition = new EastNorth(center.east(), center.north());
    156             initialImagePosition = new EastNorth(imagePosition.east(), imagePosition.north());
     155//            imagePosition = new EastNorth(center.east(), center.north());
     156            transformer.setImagePosition(new EastNorth(center.east(), center.north()));
     157//            initialImagePosition = new EastNorth(imagePosition.east(), imagePosition.north());
    157158            // Initial scale at which the image was loaded
    158159            initialImageScale = Main.map.mapView.getDist100Pixel();
     
    168169        // Load image completely
    169170        (new ImageIcon(image)).getImage();
    170 
    171         transformer = new PictureTransform();
    172171
    173172        lookForCalibration();
     
    233232
    234233            //     This is now the offset in screen pixels
     234            EastNorth imagePosition = transformer.getImagePosition();
    235235            double pic_offset_x = (( imagePosition.east() - leftop.east() ) * pixel_per_en);
    236236            double pic_offset_y = (( leftop.north() - imagePosition.north() ) * pixel_per_en);
     
    348348            return;
    349349
    350         EastNorth center = imagePosition;
     350        EastNorth center = transformer.getImagePosition();
    351351        double w = image.getWidth(null);
    352352        double h = image.getHeight(null);
     
    382382        props.put(MATRIXm02, Double.toString(matrix[4]));
    383383        props.put(MATRIXm12, Double.toString(matrix[5]));
    384         props.put(POSITION_X, Double.toString(imagePosition.getX()));
    385         props.put(POSITION_Y, Double.toString(imagePosition.getY()));
     384        props.put(POSITION_X, Double.toString(transformer.getImagePosition().getX()));
     385        props.put(POSITION_Y, Double.toString(transformer.getImagePosition().getY()));
    386386        props.put(INITIAL_SCALE, Double.toString(initialImageScale));
    387387
     
    413413        double pos_y = Double.valueOf(props.getProperty(POSITION_Y, "0"));
    414414
    415         imagePosition = new EastNorth(pos_x, pos_y);
     415        EastNorth imagePosition = new EastNorth(pos_x, pos_y);
     416        transformer.setImagePosition(imagePosition);
     417
    416418        initialImageScale = Double.valueOf(props.getProperty(INITIAL_SCALE, "1")); //in_scale
    417419        if (props.containsKey(SCALEX)) {// old format
    418             double in_pos_x = Double.valueOf(props.getProperty(INITIAL_POS_X, "0"));
    419             double in_pos_y = Double.valueOf(props.getProperty(INITIAL_POS_Y, "0"));
     420            //double in_pos_x = Double.valueOf(props.getProperty(INITIAL_POS_X, "0"));
     421            //double in_pos_y = Double.valueOf(props.getProperty(INITIAL_POS_Y, "0"));
    420422            double angle = Double.valueOf(props.getProperty(ANGLE, "0"));
    421423            double scale_x = Double.valueOf(props.getProperty(SCALEX, "1"));
     
    423425            double shear_x = Double.valueOf(props.getProperty(SHEARX, "0"));
    424426            double shear_y = Double.valueOf(props.getProperty(SHEARY, "0"));
    425 
    426             initialImagePosition.setLocation(in_pos_x, in_pos_y);
    427427
    428428            // transform to matrix from these values - need testing
     
    462462            int w = image.getWidth(null);
    463463            int h = image.getHeight(null);
    464             imagePosition.setLocation(
     464            EastNorth imagePosition = new EastNorth(
    465465                    dx + w/2*sx + h/2*rx,
    466466                    dy + w/2*ry + h/2*sy
    467467            );
    468             initialImagePosition.setLocation(imagePosition);
     468//            initialImagePosition.setLocation(imagePosition);
    469469//            m_angle = 0;
    470470            double scalex = 100*sx*getMetersPerEasting(imagePosition);
     
    473473            double sheary = ry / sy;
    474474
     475            transformer.setImagePosition(imagePosition);
    475476            transformer.resetCalibration();
    476477            AffineTransform tr = transformer.getTransform();
     
    494495        double pixel_per_en = ( Main.map.mapView.getWidth() / 2.0 ) / ( center.east() - leftop.east() );
    495496
     497        EastNorth imageCenter = transformer.getImagePosition();
    496498        //     This is now the offset in screen pixels
    497         double pic_offset_x = (( imagePosition.east() - leftop.east() ) * pixel_per_en);
    498         double pic_offset_y = (( leftop.north() - imagePosition.north() ) * pixel_per_en); // something bad...
     499        double pic_offset_x = (( imageCenter.east() - leftop.east() ) * pixel_per_en);
     500        double pic_offset_y = (( leftop.north() - imageCenter.north() ) * pixel_per_en); // something bad...
    499501
    500502        AffineTransform pointTrans = AffineTransform.getTranslateInstance(pic_offset_x, pic_offset_y);
    501503
    502         double scalex = initialImageScale * pixel_per_en / getMetersPerEasting(imagePosition) / 100;
    503         double scaley = initialImageScale * pixel_per_en / getMetersPerNorthing(imagePosition) / 100;
     504        double scalex = initialImageScale * pixel_per_en / getMetersPerEasting(imageCenter) / 100;
     505        double scaley = initialImageScale * pixel_per_en / getMetersPerNorthing(imageCenter) / 100;
    504506
    505507        pointTrans.scale(scalex, scaley); // ok here
     
    515517     */
    516518    public void movePictureBy(double x, double y) {
    517         imagePosition = imagePosition.add(x, y);
    518         transformer.setModified();
     519        transformer.setImagePosition(transformer.getImagePosition().add(x, y));
    519520    }
    520521
     
    552553    public void resetCalibration() {
    553554        transformer.resetCalibration();
    554         imagePosition.setLocation(initialImagePosition);
    555555    }
    556556
     
    575575        return selected;
    576576    }
    577 
    578     public void saveTransformCommand() {
    579 
    580     }
    581577}
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerFromKML.java

    r27231 r27662  
    5757
    5858
    59         imagePosition.setLocation((en1.getX()+en2.getX())/2, (en1.getY()+en2.getY())/2);
     59        EastNorth imagePosition = new EastNorth((en1.getX()+en2.getX())/2, (en1.getY()+en2.getY())/2);
     60        transformer.setImagePosition(imagePosition);
     61
    6062        initialImageScale = 100*getMetersPerEasting(imagePosition);
    61         initialImagePosition.setLocation(imagePosition);
    6263
    6364        AffineTransform transform = AffineTransform.getScaleInstance((en1.getX()-en2.getX())/w, (en1.getY()-en2.getY())/h);
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java

    r27403 r27662  
    66import java.util.List;
    77
     8import org.openstreetmap.josm.data.coor.EastNorth;
     9
    810public class PictureTransform {
    911
    1012        private AffineTransform cachedTransform;
    11         private boolean modified = false;
     13        private EastNorth imagePosition;
     14
     15        public EastNorth getImagePosition() {
     16        return imagePosition;
     17    }
     18
     19    public void setImagePosition(EastNorth imagePosition) {
     20        this.imagePosition = imagePosition;
     21    }
     22
     23    private boolean modified = false;
    1224
    1325        private List<Point2D> originPoints;
     
    5062         */
    5163        public void updatePair(Point2D originPoint, Point2D desiredPoint) {
    52                 if (originPoints.size() < 3) // not enough information for creating transform - 3 points needed
    53                         return;
    54 
    5564                if (originPoint == null)
    5665                        return;
    5766
    58                 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);
    5996
    60                 for (Point2D origin : originPoints) {
    61                         if (origin.equals(originPoint))
    62                                 desiredPoints.add(desiredPoint);
    63                         else
    64                                 desiredPoints.add(origin);
     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;
    65105                }
    66                 trySolve(desiredPoints);
     106                default:
     107
     108                }
     109
    67110        }
     111
     112    private Point2D calculateTrianglePoint(Point2D d1, Point2D d2) {
     113        Point2D result;
     114        if (d1 instanceof Point2D.Double) {
     115            result = new Point2D.Double();
     116        } else {
     117            result = new Point2D.Float();
     118        }
     119        result.setLocation((d1.getX()+d2.getX()-d2.getY()+d1.getY())/2, (d1.getY()+d2.getY()+d2.getX()-d1.getX())/2);
     120        return result;
     121    }
    68122
    69123    private void trySolve(List<Point2D> desiredPoints) {
     
    92146        public void concatenateTransformPoint(AffineTransform transform, Point2D trans) {
    93147
    94         AffineTransform centered = AffineTransform.getTranslateInstance(trans.getX(), trans.getY());
    95         centered.concatenate(transform);
    96         centered.translate(-trans.getX(), -trans.getY());
    97         cachedTransform.concatenate(centered);
     148            if (trans != null) {
     149            AffineTransform centered = AffineTransform.getTranslateInstance(trans.getX(), trans.getY());
     150            centered.concatenate(transform);
     151            centered.translate(-trans.getX(), -trans.getY());
     152            cachedTransform.concatenate(centered);
     153            } else {
     154                cachedTransform.concatenate(transform);
     155            }
    98156
    99157
     
    129187        this.originPoints = new ArrayList<Point2D>(list);
    130188    }
     189
     190    public void removeOriginPoint(Point2D selectedPoint) {
     191        originPoints.remove(selectedPoint);
     192    }
    131193}
Note: See TracChangeset for help on using the changeset viewer.