001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.validation.util; 003 004import java.awt.geom.Point2D; 005import java.util.ArrayList; 006import java.util.Collections; 007import java.util.HashSet; 008import java.util.List; 009import java.util.Map; 010import java.util.Set; 011 012import org.openstreetmap.josm.data.coor.EastNorth; 013import org.openstreetmap.josm.data.osm.Node; 014import org.openstreetmap.josm.data.osm.Way; 015import org.openstreetmap.josm.data.validation.OsmValidator; 016import org.openstreetmap.josm.tools.CheckParameterUtil; 017 018/** 019 * Utility class 020 * 021 * @author frsantos 022 */ 023public final class ValUtil { 024 025 private ValUtil() { 026 // Hide default constructor for utils classes 027 } 028 029 /** 030 * Returns the start and end cells of a way. 031 * @param w The way 032 * @param cellWays The map with all cells 033 * @return A list with all the cells the way starts or ends 034 */ 035 public static List<List<Way>> getWaysInCell(Way w, Map<Point2D, List<Way>> cellWays) { 036 if (w.getNodesCount() == 0) 037 return Collections.emptyList(); 038 039 Node n1 = w.getNode(0); 040 Node n2 = w.getNode(w.getNodesCount() - 1); 041 042 List<List<Way>> cells = new ArrayList<>(2); 043 Set<Point2D> cellNodes = new HashSet<>(); 044 Point2D cell; 045 046 // First, round coordinates 047 // CHECKSTYLE.OFF: SingleSpaceSeparator 048 long x0 = Math.round(n1.getEastNorth().east() * OsmValidator.griddetail); 049 long y0 = Math.round(n1.getEastNorth().north() * OsmValidator.griddetail); 050 long x1 = Math.round(n2.getEastNorth().east() * OsmValidator.griddetail); 051 long y1 = Math.round(n2.getEastNorth().north() * OsmValidator.griddetail); 052 // CHECKSTYLE.ON: SingleSpaceSeparator 053 054 // Start of the way 055 cell = new Point2D.Double(x0, y0); 056 cellNodes.add(cell); 057 List<Way> ways = cellWays.get(cell); 058 if (ways == null) { 059 ways = new ArrayList<>(); 060 cellWays.put(cell, ways); 061 } 062 cells.add(ways); 063 064 // End of the way 065 cell = new Point2D.Double(x1, y1); 066 if (!cellNodes.contains(cell)) { 067 cellNodes.add(cell); 068 ways = cellWays.get(cell); 069 if (ways == null) { 070 ways = new ArrayList<>(); 071 cellWays.put(cell, ways); 072 } 073 cells.add(ways); 074 } 075 076 // Then floor coordinates, in case the way is in the border of the cell. 077 // CHECKSTYLE.OFF: SingleSpaceSeparator 078 x0 = (long) Math.floor(n1.getEastNorth().east() * OsmValidator.griddetail); 079 y0 = (long) Math.floor(n1.getEastNorth().north() * OsmValidator.griddetail); 080 x1 = (long) Math.floor(n2.getEastNorth().east() * OsmValidator.griddetail); 081 y1 = (long) Math.floor(n2.getEastNorth().north() * OsmValidator.griddetail); 082 // CHECKSTYLE.ON: SingleSpaceSeparator 083 084 // Start of the way 085 cell = new Point2D.Double(x0, y0); 086 if (!cellNodes.contains(cell)) { 087 cellNodes.add(cell); 088 ways = cellWays.get(cell); 089 if (ways == null) { 090 ways = new ArrayList<>(); 091 cellWays.put(cell, ways); 092 } 093 cells.add(ways); 094 } 095 096 // End of the way 097 cell = new Point2D.Double(x1, y1); 098 if (!cellNodes.contains(cell)) { 099 cellNodes.add(cell); 100 ways = cellWays.get(cell); 101 if (ways == null) { 102 ways = new ArrayList<>(); 103 cellWays.put(cell, ways); 104 } 105 cells.add(ways); 106 } 107 return cells; 108 } 109 110 /** 111 * Returns the coordinates of all cells in a grid that a line between 2 nodes intersects with. 112 * 113 * @param n1 The first node. 114 * @param n2 The second node. 115 * @param gridDetail The detail of the grid. Bigger values give smaller 116 * cells, but a bigger number of them. 117 * @return A list with the coordinates of all cells 118 * @throws IllegalArgumentException if n1 or n2 is {@code null} or without coordinates 119 */ 120 public static List<Point2D> getSegmentCells(Node n1, Node n2, double gridDetail) { 121 CheckParameterUtil.ensureParameterNotNull(n1, "n1"); 122 CheckParameterUtil.ensureParameterNotNull(n1, "n2"); 123 return getSegmentCells(n1.getEastNorth(), n2.getEastNorth(), gridDetail); 124 } 125 126 /** 127 * Returns the coordinates of all cells in a grid that a line between 2 nodes intersects with. 128 * 129 * @param en1 The first EastNorth. 130 * @param en2 The second EastNorth. 131 * @param gridDetail The detail of the grid. Bigger values give smaller 132 * cells, but a bigger number of them. 133 * @return A list with the coordinates of all cells 134 * @throws IllegalArgumentException if en1 or en2 is {@code null} 135 * @since 6869 136 */ 137 public static List<Point2D> getSegmentCells(EastNorth en1, EastNorth en2, double gridDetail) { 138 CheckParameterUtil.ensureParameterNotNull(en1, "en1"); 139 CheckParameterUtil.ensureParameterNotNull(en2, "en2"); 140 List<Point2D> cells = new ArrayList<>(); 141 double x0 = en1.east() * gridDetail; 142 double x1 = en2.east() * gridDetail; 143 double y0 = en1.north() * gridDetail + 1; 144 double y1 = en2.north() * gridDetail + 1; 145 146 if (x0 > x1) { 147 // Move to 1st-4th cuadrants 148 double aux; 149 aux = x0; x0 = x1; x1 = aux; 150 aux = y0; y0 = y1; y1 = aux; 151 } 152 153 double dx = x1 - x0; 154 double dy = y1 - y0; 155 long stepY = y0 <= y1 ? 1 : -1; 156 long gridX0 = (long) Math.floor(x0); 157 long gridX1 = (long) Math.floor(x1); 158 long gridY0 = (long) Math.floor(y0); 159 long gridY1 = (long) Math.floor(y1); 160 161 long maxSteps = (gridX1 - gridX0) + Math.abs(gridY1 - gridY0) + 1; 162 while ((gridX0 <= gridX1 && (gridY0 - gridY1)*stepY <= 0) && maxSteps-- > 0) { 163 cells.add(new Point2D.Double(gridX0, gridY0)); 164 165 // Is the cross between the segment and next vertical line nearer than the cross with next horizontal line? 166 // Note: segment line formula: y=dy/dx(x-x1)+y1 167 // Note: if dy < 0, must use *bottom* line. If dy > 0, must use upper line 168 double scanY = dy/dx * (gridX0 + 1 - x1) + y1 + (dy < 0 ? -1 : 0); 169 double scanX = dx/dy * (gridY0 + (dy < 0 ? 0 : 1)*stepY - y1) + x1; 170 171 double distX = Math.pow(gridX0 + 1 - x0, 2) + Math.pow(scanY - y0, 2); 172 double distY = Math.pow(scanX - x0, 2) + Math.pow(gridY0 + stepY - y0, 2); 173 174 if (distX < distY) { 175 gridX0 += 1; 176 } else { 177 gridY0 += stepY; 178 } 179 } 180 return cells; 181 } 182}