Changeset 21174 in osm for applications/editors/josm/plugins/routes/src/org
- Timestamp:
- 2010-05-08T11:09:41+02:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/RelationEditMode.java
r20427 r21174 62 62 @Override 63 63 public void mouseClicked(MouseEvent e) { 64 if (Main.main.getCurrentDataSet() == null) 65 return; 66 64 67 Way way = Main.map.mapView.getNearestWay(e.getPoint()); 65 66 68 Collection<Relation> selectedRelations = Main.main.getCurrentDataSet().getSelectedRelations(); 67 69 -
applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/paint/AbstractLinePainter.java
r16428 r21174 1 1 package org.openstreetmap.josm.plugins.routes.paint; 2 2 3 import java.awt.Graphics2D; 4 import java.awt.Point; 5 import java.awt.Rectangle; 6 import java.awt.geom.GeneralPath; 3 7 import java.awt.geom.Line2D; 4 8 import java.awt.geom.Point2D; 9 import java.util.List; 10 11 import org.openstreetmap.josm.Main; 12 import org.openstreetmap.josm.data.osm.Node; 13 import org.openstreetmap.josm.data.osm.visitor.paint.LineClip; 14 import org.openstreetmap.josm.gui.MapView; 15 import org.openstreetmap.josm.gui.NavigatableComponent; 5 16 6 17 public abstract class AbstractLinePainter implements PathPainter { 7 18 8 19 // Following two method copied from http://blog.persistent.info/2004/03/java-lineline-intersections.html 9 20 protected boolean getLineLineIntersection(Line2D.Double l1, 10 21 Line2D.Double l2, 11 Point 2D.Doubleintersection)22 Point intersection) 12 23 { 13 24 double x1 = l1.getX1(), y1 = l1.getY1(), … … 15 26 x3 = l2.getX1(), y3 = l2.getY1(), 16 27 x4 = l2.getX2(), y4 = l2.getY2(); 17 18 double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3)*(y2 - y1)); 19 intersection.x = x1 + ua * (x2 - x1); 20 intersection.y = y1 + ua * (y2 - y1); 21 28 double dx1 = x2 - x1; 29 double dx2 = x4 - x3; 30 double dy1 = y2 - y1; 31 double dy2 = y4 - y3; 22 32 23 /* intersection.x = det(det(x1, y1, x2, y2), x1 - x2, 24 det(x3, y3, x4, y4), x3 - x4)/ 25 det(x1 - x2, y1 - y2, x3 - x4, y3 - y4); 26 intersection.y = det(det(x1, y1, x2, y2), y1 - y2, 27 det(x3, y3, x4, y4), y3 - y4)/ 28 det(x1 - x2, y1 - y2, x3 - x4, y3 - y4);*/ 33 double ua = (dx2 * (y1 - y3) - dy2 * (x1 - x3)) / (dy2 * dx1 - dx2 * dy1); 34 35 if (Math.abs(dy2 * dx1 - dx2 * dy1) < 0.0001) { 36 intersection.x = (int)l1.x2; 37 intersection.y = (int)l1.y2; 38 return false; 39 } else { 40 intersection.x = (int)(x1 + ua * (x2 - x1)); 41 intersection.y = (int)(y1 + ua * (y2 - y1)); 42 } 43 44 if (intersection.x > 600) { 45 System.out.println(); 46 } 29 47 30 48 return true; … … 36 54 } 37 55 38 protected Point 2DshiftPoint(Point2D p1, Point2D p2, double shift) {56 protected Point shiftPoint(Point2D p1, Point2D p2, double shift) { 39 57 double dx = p2.getX() - p1.getX(); 40 58 double dy = p2.getY() - p1.getY(); 41 59 42 60 // Perpendicular vector 43 61 double ndx = -dy; 44 62 double ndy = dx; 45 63 46 64 // Normalize 47 65 double length = Math.sqrt(ndx * ndx + ndy * ndy); 48 66 ndx = ndx / length; 49 67 ndy = ndy / length; 50 51 return new Point 2D.Double(p1.getX() + shift * ndx, p1.getY() + shift * ndy);68 69 return new Point((int)(p1.getX() + shift * ndx), (int)(p1.getY() + shift * ndy)); 52 70 } 53 71 54 72 protected Line2D.Double shiftLine(Point2D p1, Point2D p2, double shift) { 55 73 double dx = p2.getX() - p1.getX(); … … 58 76 Point2D point1 = shiftPoint(p1, p2, shift); 59 77 Point2D point2 = new Point2D.Double(point1.getX() + dx, point1.getY() + dy); 60 78 61 79 return new Line2D.Double( 62 80 point1, point2); 63 81 } 64 82 83 protected GeneralPath getPath(Graphics2D g, MapView mapView, List<Node> nodes, double shift) { 84 85 GeneralPath path = new GeneralPath(); 86 87 if (nodes.size() < 2) { 88 return path; 89 } 90 91 Point p1 = null; 92 Point p2 = null; 93 Point p3 = null; 94 Point lastPoint = null; 95 96 for (Node n: nodes) { 97 Point p = mapView.getPoint(n); 98 99 if (!p.equals(p3)) { 100 p1 = p2; 101 p2 = p3; 102 p3 = p; 103 } else { 104 continue; 105 } 106 107 p = null; 108 if (p2 != null) { 109 if (p1 == null) { 110 p = shiftPoint(p2, p3, shift); 111 } else { 112 Line2D.Double line1 = shiftLine(p1, p2, shift); 113 Line2D.Double line2 = shiftLine(p2, p3, shift); 114 115 /*path.moveTo((float)line1.x1, (float)line1.y1); 116 path.lineTo((float)line1.x2, (float)line1.y2); 117 path.moveTo((float)line2.x1, (float)line2.y1); 118 path.lineTo((float)line2.x2, (float)line2.y2);*/ 119 120 p = new Point(); 121 if (!getLineLineIntersection(line1, line2, p)) { 122 p = null; 123 } else { 124 int dx = p.x - p2.x; 125 int dy = p.y - p2.y; 126 int distance = (int)Math.sqrt(dx * dx + dy * dy); 127 if (distance > 10) { 128 p.x = p2.x + dx / (distance / 10); 129 p.y = p2.y + dy / (distance / 10); 130 } 131 } 132 } 133 } 134 135 if (p != null && lastPoint != null) { 136 drawSegment(g, mapView, path, lastPoint, p); 137 } 138 if (p != null) { 139 lastPoint = p; 140 } 141 } 142 143 if (p2 != null && p3 != null && lastPoint != null) { 144 p3 = shiftPoint(p3, p2, -shift); 145 drawSegment(g, mapView, path, lastPoint, p3); 146 } 147 148 return path; 149 } 150 151 private void drawSegment(Graphics2D g, NavigatableComponent nc, GeneralPath path, Point p1, Point p2) { 152 boolean drawIt = false; 153 if (Main.isOpenjdk) { 154 /** 155 * Work around openjdk bug. It leads to drawing artefacts when zooming in a lot. (#4289, #4424) 156 * (It looks like int overflow when clipping.) We do custom clipping. 157 */ 158 Rectangle bounds = g.getClipBounds(); 159 bounds.grow(100, 100); // avoid arrow heads at the border 160 LineClip clip = new LineClip(); 161 drawIt = clip.cohenSutherland(p1.x, p1.y, p2.x, p2.y, bounds.x, bounds.y, bounds.x+bounds.width, bounds.y+bounds.height); 162 if (drawIt) { 163 p1 = clip.getP1(); 164 p2 = clip.getP2(); 165 } 166 } else { 167 drawIt = isSegmentVisible(nc, p1, p2); 168 } 169 if (drawIt) { 170 /* draw segment line */ 171 path.moveTo(p1.x, p1.y); 172 path.lineTo(p2.x, p2.y); 173 } 174 } 175 176 private boolean isSegmentVisible(NavigatableComponent nc, Point p1, Point p2) { 177 if ((p1.x < 0) && (p2.x < 0)) return false; 178 if ((p1.y < 0) && (p2.y < 0)) return false; 179 if ((p1.x > nc.getWidth()) && (p2.x > nc.getWidth())) return false; 180 if ((p1.y > nc.getHeight()) && (p2.y > nc.getHeight())) return false; 181 return true; 182 } 183 184 65 185 } -
applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/paint/NarrowLinePainter.java
r16428 r21174 3 3 import java.awt.BasicStroke; 4 4 import java.awt.Graphics2D; 5 import java.awt.Point;6 import java.awt.geom.GeneralPath;7 import java.awt.geom.Line2D;8 import java.awt.geom.Point2D;9 5 import java.util.BitSet; 10 6 import java.util.List; … … 17 13 18 14 public class NarrowLinePainter extends AbstractLinePainter { 19 15 20 16 private static final float LINE_WIDTH = 5; 21 17 private final RouteLayer layer; 22 18 23 19 public NarrowLinePainter(RouteLayer layer) { 24 20 this.layer = layer; … … 28 24 List<Node> nodes = way.getNodes(); 29 25 BitSet routes = way.getRoutes(); 30 26 31 27 if (nodes.size() < 2) { 32 28 return; 33 29 } 34 30 35 31 //double totalWidth = LINE_WIDTH + (colors.size() - 1) * 4; 36 32 //double width = totalWidth / colors.size(); … … 38 34 double width = LINE_WIDTH; 39 35 double shift = - (LINE_WIDTH * routes.cardinality()) / 2 + width / 2; 40 36 41 37 for (int k=0; k<routes.length(); k++) { 42 38 43 39 if (!routes.get(k)) { 44 40 continue; 45 41 } 46 42 47 43 RouteDefinition route = layer.getRoutes().get(k); 48 44 49 45 g.setColor(route.getColor()); 50 46 g.setStroke(new BasicStroke((float) width)); 51 52 GeneralPath path = new GeneralPath();53 Point2D start = shiftPoint(mapView.getPoint(nodes.get(0).getEastNorth()),54 mapView.getPoint(nodes.get(1).getEastNorth()), shift);55 path.moveTo((float)start.getX(), (float)start.getY());56 for (int i=1; i<nodes.size() - 1; i++) {57 Point p1 = mapView.getPoint(nodes.get(i - 1).getEastNorth());58 Point p2 = mapView.getPoint(nodes.get(i).getEastNorth());59 Point p3 = mapView.getPoint(nodes.get(i + 1).getEastNorth());60 47 48 g.draw(getPath(g, mapView, nodes, shift)); 61 49 62 Line2D.Double line1 = shiftLine(p1, p2, shift);63 Line2D.Double line2 = shiftLine(p2, p3, shift);64 65 Point2D.Double intersection = new Point2D.Double();66 getLineLineIntersection(line1, line2, intersection);67 if (!Double.isNaN(intersection.getX()) && !Double.isNaN(intersection.getY())) {68 path.lineTo((float)intersection.getX(), (float)intersection.getY());69 }70 }71 Point2D stop = shiftPoint(mapView.getPoint(nodes.get(nodes.size() - 1).getEastNorth()),72 mapView.getPoint(nodes.get(nodes.size() - 2).getEastNorth()), -shift);73 path.lineTo((float)stop.getX(), (float)stop.getY());74 g.draw(path);75 76 50 shift += width + 2; 77 51 } -
applications/editors/josm/plugins/routes/src/org/openstreetmap/josm/plugins/routes/paint/WideLinePainter.java
r16428 r21174 4 4 import java.awt.Color; 5 5 import java.awt.Graphics2D; 6 import java.awt.Point;7 import java.awt.geom.GeneralPath;8 import java.awt.geom.Line2D;9 import java.awt.geom.Point2D;10 6 import java.util.BitSet; 11 7 import java.util.List; … … 18 14 19 15 public class WideLinePainter extends AbstractLinePainter { 20 16 21 17 private static final float LINE_WIDTH = 10; 22 18 private final RouteLayer layer; 23 19 24 20 public WideLinePainter(RouteLayer layer) { 25 21 this.layer = layer; … … 29 25 List<Node> nodes = way.getNodes(); 30 26 BitSet routes = way.getRoutes(); 31 27 32 28 if (nodes.size() < 2) { 33 29 return; 34 30 } 35 31 36 32 double totalWidth = LINE_WIDTH + (routes.size() - 1) * 4; 37 33 double width = totalWidth / routes.cardinality(); 38 34 double shift = -totalWidth / 2 + width / 2; 39 35 40 36 for (int k=0; k<routes.length(); k++) { 41 37 42 38 if (!routes.get(k)) { 43 39 continue; 44 40 } 45 41 46 42 RouteDefinition route = layer.getRoutes().get(k); 47 43 48 44 Color color = route.getColor(); 49 45 g.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue(), 100)); 50 46 g.setStroke(new BasicStroke((float) width)); 51 52 GeneralPath path = new GeneralPath();53 Point2D start = shiftPoint(mapView.getPoint(nodes.get(0).getEastNorth()),54 mapView.getPoint(nodes.get(1).getEastNorth()), shift);55 path.moveTo((float)start.getX(), (float)start.getY());56 for (int i=1; i<nodes.size() - 1; i++) {57 Point p1 = mapView.getPoint(nodes.get(i - 1).getEastNorth());58 Point p2 = mapView.getPoint(nodes.get(i).getEastNorth());59 Point p3 = mapView.getPoint(nodes.get(i + 1).getEastNorth());60 47 48 g.draw(getPath(g, mapView, nodes, shift)); 61 49 62 Line2D.Double line1 = shiftLine(p1, p2, shift);63 Line2D.Double line2 = shiftLine(p2, p3, shift);64 65 Point2D.Double intersection = new Point2D.Double();66 getLineLineIntersection(line1, line2, intersection);67 if (!Double.isNaN(intersection.getX()) && !Double.isNaN(intersection.getY())) {68 path.lineTo((float)intersection.getX(), (float)intersection.getY());69 }70 }71 Point2D stop = shiftPoint(mapView.getPoint(nodes.get(nodes.size() - 1).getEastNorth()),72 mapView.getPoint(nodes.get(nodes.size() - 2).getEastNorth()), -shift);73 path.lineTo((float)stop.getX(), (float)stop.getY());74 g.draw(path);75 76 50 shift += width; 77 51 }
Note:
See TracChangeset
for help on using the changeset viewer.