Changeset 30356 in osm for applications/editors/josm/plugins/piclayer/src
- Timestamp:
- 2014-03-24T22:24:40+01:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer
- Files:
-
- 13 edited
-
PicLayerPlugin.java (modified) (2 diffs)
-
actions/GenericPicTransformAction.java (modified) (2 diffs)
-
actions/ResetCalibrationAction.java (modified) (1 diff)
-
actions/newlayer/NewLayerFromFileAction.java (modified) (1 diff)
-
actions/transform/MovePictureAction.java (modified) (1 diff)
-
actions/transform/RotatePictureAction.java (modified) (2 diffs)
-
actions/transform/ScalePictureActionAbstract.java (modified) (1 diff)
-
actions/transform/ShearPictureAction.java (modified) (3 diffs)
-
actions/transform/affine/MovePointAction.java (modified) (1 diff)
-
actions/transform/affine/TransformPointAction.java (modified) (1 diff)
-
transform/Matrix3D.java (modified) (1 diff)
-
transform/NoSolutionException.java (modified) (1 diff)
-
transform/PictureTransform.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java
r29809 r30356 118 118 119 119 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; 123 123 } 124 124 … … 136 136 137 137 if (newPic) { 138 ((PicLayerAbstract)newLayer).setDrawPoints(true);138 ((PicLayerAbstract)newLayer).setDrawPoints(true); 139 139 } 140 140 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/GenericPicTransformAction.java
r29595 r30356 20 20 public abstract class GenericPicTransformAction extends MapMode implements MouseListener, MouseMotionListener { 21 21 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; 29 29 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 } 35 35 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); 39 39 this.actionName = actionName; 40 }40 } 41 41 42 @Override43 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 } 48 48 49 @Override50 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 } 55 55 56 @Override57 public void mousePressed(MouseEvent e) {58 // Start action59 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(); 61 61 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 possible68 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 } 73 73 74 @Override74 @Override 75 75 public void mouseDragged(MouseEvent e) { 76 76 // Call action performing 77 77 if(isDragging && currentLayer != null) { 78 doAction(e);78 doAction(e); 79 79 prevMousePoint = new Point(e.getPoint()); 80 80 prevEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY()); … … 83 83 } 84 84 85 protected abstract void doAction(MouseEvent e);85 protected abstract void doAction(MouseEvent e); 86 86 87 @Override88 public void mouseReleased(MouseEvent e) {89 // End action90 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 } 94 94 95 @Override96 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 } 99 99 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(); 102 102 if (active instanceof PicLayerAbstract) { 103 103 ((PicLayerAbstract)active).setDrawPoints(value); 104 104 } 105 105 Main.map.mapView.repaint(); 106 }106 } 107 107 108 108 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/ResetCalibrationAction.java
r27403 r30356 13 13 public class ResetCalibrationAction extends JosmAction { 14 14 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 } 20 20 21 @Override22 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 } 27 27 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/newlayer/NewLayerFromFileAction.java
r27231 r30356 134 134 KMLReader kml = new KMLReader(file); 135 135 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")); 137 139 for (KMLGroundOverlay overlay : kml.getGroundOverlays()) { 138 140 //TODO: zoom to whole picture, not only the last 139 141 addNewLayerFromKML(file, overlay, newLayerPos); 140 142 } 141 142 143 } else { 143 addNewLayerFromFile(file, newLayerPos, fc.getSelectedFiles().length == 1); 144 144 addNewLayerFromFile(file, newLayerPos, fc.getSelectedFiles().length == 1); 145 } 145 146 } 146 147 } 147 148 } 148 }149 149 150 150 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 44 44 } 45 45 46 @Override47 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()); 49 49 currentLayer.movePictureBy( 50 50 eastNorth.east() - prevEastNorth.east(), 51 51 eastNorth.north() - prevEastNorth.north() 52 52 ); 53 }53 } 54 54 55 55 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/RotatePictureAction.java
r27403 r30356 44 44 } 45 45 46 @Override47 protected void doAction(MouseEvent e) {48 double factor;46 @Override 47 protected void doAction(MouseEvent e) { 48 double factor; 49 49 if ( ( e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK ) != 0 ) { 50 50 factor = Main.pref.getDouble("piclayer.rotatefactors.high_precision", 100.0); … … 54 54 } 55 55 currentLayer.rotatePictureBy( ( e.getY() - prevMousePoint.getY() ) / factor ); 56 }56 } 57 57 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ScalePictureActionAbstract.java
r27403 r30356 35 35 public abstract class ScalePictureActionAbstract extends GenericPicTransformAction { 36 36 37 /**37 /** 38 38 * Constructor 39 39 */ -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/ShearPictureAction.java
r27403 r30356 39 39 public class ShearPictureAction extends GenericPicTransformAction { 40 40 41 /**41 /** 42 42 * Constructor 43 43 */ … … 46 46 } 47 47 48 @Override49 protected void doAction(MouseEvent e) {48 @Override 49 protected void doAction(MouseEvent e) { 50 50 EastNorth eastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY()); 51 51 currentLayer.shearPictureBy( … … 53 53 1000* (eastNorth.north() - prevEastNorth.north()) 54 54 ); 55 }55 } 56 56 57 57 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/affine/MovePointAction.java
r27403 r30356 15 15 public class MovePointAction extends GenericPicTransformAction { 16 16 17 public MovePointAction(MapFrame frame) {17 public MovePointAction(MapFrame frame) { 18 18 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 } 20 20 21 @Override22 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 } 33 33 34 @Override35 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; 38 38 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); 43 43 44 currentCommand.addIfChanged();45 } catch (NoninvertibleTransformException e1) {46 e1.printStackTrace();47 }48 }44 currentCommand.addIfChanged(); 45 } catch (NoninvertibleTransformException e1) { 46 e1.printStackTrace(); 47 } 48 } 49 49 50 @Override51 public void enterMode() {52 super.enterMode();53 updateDrawPoints(true);54 }50 @Override 51 public void enterMode() { 52 super.enterMode(); 53 updateDrawPoints(true); 54 } 55 55 56 @Override57 public void exitMode() {58 super.exitMode();59 updateDrawPoints(false);60 }56 @Override 57 public void exitMode() { 58 super.exitMode(); 59 updateDrawPoints(false); 60 } 61 61 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/affine/TransformPointAction.java
r27662 r30356 14 14 public class TransformPointAction extends GenericPicTransformAction { 15 15 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 } 19 19 20 @Override21 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 } 32 32 33 currentCommand.addIfChanged();34 } catch (NoninvertibleTransformException e1) {35 e1.printStackTrace();36 }37 }33 currentCommand.addIfChanged(); 34 } catch (NoninvertibleTransformException e1) { 35 e1.printStackTrace(); 36 } 37 } 38 38 @Override 39 39 public void enterMode() { -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/Matrix3D.java
r27120 r30356 7 7 8 8 class 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 } 33 33 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 } 77 77 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/NoSolutionException.java
r27120 r30356 2 2 3 3 class NoSolutionException extends Exception { 4 5 public NoSolutionException(String message) {6 super(message);7 }4 5 public NoSolutionException(String message) { 6 super(message); 7 } 8 8 9 private static final long serialVersionUID = 3170170664858078930L;9 private static final long serialVersionUID = 3170170664858078930L; 10 10 } -
applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java
r27662 r30356 10 10 public class PictureTransform { 11 11 12 private AffineTransform cachedTransform;13 private EastNorth imagePosition;12 private AffineTransform cachedTransform; 13 private EastNorth imagePosition; 14 14 15 public EastNorth getImagePosition() {15 public EastNorth getImagePosition() { 16 16 return imagePosition; 17 17 } … … 23 23 private boolean modified = false; 24 24 25 private List<Point2D> originPoints;25 private List<Point2D> originPoints; 26 26 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 } 31 31 32 public AffineTransform getTransform() {33 return cachedTransform;34 }32 public AffineTransform getTransform() { 33 return cachedTransform; 34 } 35 35 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()); 40 40 41 return result.toAffineTransform();42 }41 return result.toAffineTransform(); 42 } 43 43 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 } 48 48 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 } 54 54 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 cachedTransform59 *60 * @param originPoint - should be one of origin points, otherwise - no transform applied61 * @param desiredPoint - new place for the point62 */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; 66 66 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 it75 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); 96 96 97 for (Point2D origin : originPoints) {98 if (origin.equals(originPoint))99 desiredPoints.add(desiredPoint);100 else101 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: 107 107 108 }108 } 109 109 110 }110 } 111 111 112 112 private Point2D calculateTrianglePoint(Point2D d1, Point2D d2) { … … 133 133 } 134 134 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; 138 138 139 int index = originPoints.indexOf(originPoint);140 if (index < 0)141 return;139 int index = originPoints.indexOf(originPoint); 140 if (index < 0) 141 return; 142 142 143 originPoints.set(index, newOriginPoint);144 }143 originPoints.set(index, newOriginPoint); 144 } 145 145 146 public void concatenateTransformPoint(AffineTransform transform, Point2D trans) {146 public void concatenateTransformPoint(AffineTransform transform, Point2D trans) { 147 147 148 if (trans != null) {148 if (trans != null) { 149 149 AffineTransform centered = AffineTransform.getTranslateInstance(trans.getX(), trans.getY()); 150 150 centered.concatenate(transform); 151 151 centered.translate(-trans.getX(), -trans.getY()); 152 152 cachedTransform.concatenate(centered); 153 } else {154 cachedTransform.concatenate(transform);155 }153 } else { 154 cachedTransform.concatenate(transform); 155 } 156 156 157 157 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 } 164 164 165 public boolean isModified() {166 return modified;167 }165 public boolean isModified() { 166 return modified; 167 } 168 168 169 169 public void setModified() {
Note:
See TracChangeset
for help on using the changeset viewer.
