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