- Timestamp:
- 2021-04-09T20:45:28+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/Shape.java
r16877 r17727 2 2 package org.openstreetmap.josm.data.imagery; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 4 import java.awt.Polygon; 6 5 import java.text.MessageFormat; 7 import java.util.A rrayList;6 import java.util.AbstractList; 8 7 import java.util.List; 9 8 import java.util.Objects; … … 13 12 import org.openstreetmap.gui.jmapviewer.Coordinate; 14 13 import org.openstreetmap.josm.data.coor.LatLon; 15 import org.openstreetmap.josm.data.osm.Node;16 14 import org.openstreetmap.josm.tools.CheckParameterUtil; 17 import org.openstreetmap.josm.tools.Geometry; 15 16 import static org.openstreetmap.josm.tools.I18n.tr; 18 17 19 18 /** … … 25 24 public class Shape { 26 25 27 private final List<Coordinate> coords = new ArrayList<>();26 private final Polygon coords; 28 27 29 28 public Shape(String asString, String separator) { … … 33 32 throw new IllegalArgumentException(MessageFormat.format("Even number of doubles expected in string, got {0}: {1}", 34 33 components.length, asString)); 34 int size = components.length / 2; 35 this.coords = new Polygon(new int[size], new int[size], 0); 35 36 for (int i = 0; i < components.length; i += 2) { 36 37 addPoint(components[i], components[i+1]); … … 42 43 */ 43 44 public Shape() { 45 coords = new Polygon(); 44 46 // shape contents can be set later with addPoint() 45 47 } … … 51 53 */ 52 54 public String encodeAsString(String separator) { 53 return coords.stream()55 return getPoints().stream() 54 56 .flatMap(c -> Stream.of(c.getLat(), c.getLon())) 55 57 .map(String::valueOf) … … 69 71 70 72 public List<Coordinate> getPoints() { 71 return coords; 73 return new AbstractList<Coordinate>() { 74 @Override 75 public Coordinate get(int index) { 76 double lat = coords.ypoints[index] / LatLon.MAX_SERVER_INV_PRECISION; 77 double lon = coords.xpoints[index] / LatLon.MAX_SERVER_INV_PRECISION; 78 return new Coordinate(lat, lon); 79 } 80 81 @Override 82 public int size() { 83 return coords.npoints; 84 } 85 }; 72 86 } 73 87 74 88 public boolean contains(LatLon latlon) { 75 if (latlon == null) 76 return false; 77 List<Node> nodes = coords.stream() 78 .map(c -> new Node(new LatLon(c.getLat(), c.getLon()))) 79 .collect(Collectors.toList()); 80 return Geometry.nodeInsidePolygon(new Node(latlon), nodes); 89 return coords.contains( 90 latlon.getX() * LatLon.MAX_SERVER_INV_PRECISION, 91 latlon.getY() * LatLon.MAX_SERVER_INV_PRECISION); 81 92 } 82 93 … … 103 114 } 104 115 105 coords.add(new Coordinate(LatLon.roundToOsmPrecision(lat), LatLon.roundToOsmPrecision(lon))); 116 coords.addPoint( 117 (int) (lon * LatLon.MAX_SERVER_INV_PRECISION), 118 (int) (lat * LatLon.MAX_SERVER_INV_PRECISION)); 106 119 } 107 120 108 121 @Override 109 122 public int hashCode() { 110 return Objects.hash( coords);123 return Objects.hash(getPoints()); 111 124 } 112 125 … … 116 129 if (obj == null || getClass() != obj.getClass()) return false; 117 130 Shape shape = (Shape) obj; 118 return Objects.equals(coords, shape.coords); 131 return Objects.equals(getPoints(), shape.getPoints()); 132 } 133 134 @Override 135 public String toString() { 136 return "Shape{coords=" + getPoints() + '}'; 119 137 } 120 138 }
Note:
See TracChangeset
for help on using the changeset viewer.