source: josm/trunk/src/org/openstreetmap/josm/data/validation/util/ValUtil.java@ 11852

Last change on this file since 11852 was 11852, checked in by Don-vip, 7 years ago

sonar - squid:S1444 - "public static" fields should be constant

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.util;
3
4import java.awt.geom.Point2D;
5import java.util.ArrayList;
6import java.util.Collections;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.data.validation.OsmValidator;
16import org.openstreetmap.josm.tools.CheckParameterUtil;
17
18/**
19 * Utility class
20 *
21 * @author frsantos
22 */
23public final class ValUtil {
24
25 private ValUtil() {
26 // Hide default constructor for utils classes
27 }
28
29 /**
30 * Returns the start and end cells of a way.
31 * @param w The way
32 * @param cellWays The map with all cells
33 * @return A list with all the cells the way starts or ends
34 */
35 public static List<List<Way>> getWaysInCell(Way w, Map<Point2D, List<Way>> cellWays) {
36 if (w.getNodesCount() == 0)
37 return Collections.emptyList();
38
39 Node n1 = w.getNode(0);
40 Node n2 = w.getNode(w.getNodesCount() - 1);
41
42 List<List<Way>> cells = new ArrayList<>(2);
43 Set<Point2D> cellNodes = new HashSet<>();
44 Point2D cell;
45 double griddetail = OsmValidator.getGridDetail();
46
47 // First, round coordinates
48 // CHECKSTYLE.OFF: SingleSpaceSeparator
49 long x0 = Math.round(n1.getEastNorth().east() * griddetail);
50 long y0 = Math.round(n1.getEastNorth().north() * griddetail);
51 long x1 = Math.round(n2.getEastNorth().east() * griddetail);
52 long y1 = Math.round(n2.getEastNorth().north() * griddetail);
53 // CHECKSTYLE.ON: SingleSpaceSeparator
54
55 // Start of the way
56 cell = new Point2D.Double(x0, y0);
57 cellNodes.add(cell);
58 List<Way> ways = cellWays.get(cell);
59 if (ways == null) {
60 ways = new ArrayList<>();
61 cellWays.put(cell, ways);
62 }
63 cells.add(ways);
64
65 // End of the way
66 cell = new Point2D.Double(x1, y1);
67 if (!cellNodes.contains(cell)) {
68 cellNodes.add(cell);
69 ways = cellWays.get(cell);
70 if (ways == null) {
71 ways = new ArrayList<>();
72 cellWays.put(cell, ways);
73 }
74 cells.add(ways);
75 }
76
77 // Then floor coordinates, in case the way is in the border of the cell.
78 // CHECKSTYLE.OFF: SingleSpaceSeparator
79 x0 = (long) Math.floor(n1.getEastNorth().east() * griddetail);
80 y0 = (long) Math.floor(n1.getEastNorth().north() * griddetail);
81 x1 = (long) Math.floor(n2.getEastNorth().east() * griddetail);
82 y1 = (long) Math.floor(n2.getEastNorth().north() * griddetail);
83 // CHECKSTYLE.ON: SingleSpaceSeparator
84
85 // Start of the way
86 cell = new Point2D.Double(x0, y0);
87 if (!cellNodes.contains(cell)) {
88 cellNodes.add(cell);
89 ways = cellWays.get(cell);
90 if (ways == null) {
91 ways = new ArrayList<>();
92 cellWays.put(cell, ways);
93 }
94 cells.add(ways);
95 }
96
97 // End of the way
98 cell = new Point2D.Double(x1, y1);
99 if (!cellNodes.contains(cell)) {
100 cellNodes.add(cell);
101 ways = cellWays.get(cell);
102 if (ways == null) {
103 ways = new ArrayList<>();
104 cellWays.put(cell, ways);
105 }
106 cells.add(ways);
107 }
108 return cells;
109 }
110
111 /**
112 * Returns the coordinates of all cells in a grid that a line between 2 nodes intersects with.
113 *
114 * @param n1 The first node.
115 * @param n2 The second node.
116 * @param gridDetail The detail of the grid. Bigger values give smaller
117 * cells, but a bigger number of them.
118 * @return A list with the coordinates of all cells
119 * @throws IllegalArgumentException if n1 or n2 is {@code null} or without coordinates
120 */
121 public static List<Point2D> getSegmentCells(Node n1, Node n2, double gridDetail) {
122 CheckParameterUtil.ensureParameterNotNull(n1, "n1");
123 CheckParameterUtil.ensureParameterNotNull(n1, "n2");
124 return getSegmentCells(n1.getEastNorth(), n2.getEastNorth(), gridDetail);
125 }
126
127 /**
128 * Returns the coordinates of all cells in a grid that a line between 2 nodes intersects with.
129 *
130 * @param en1 The first EastNorth.
131 * @param en2 The second EastNorth.
132 * @param gridDetail The detail of the grid. Bigger values give smaller
133 * cells, but a bigger number of them.
134 * @return A list with the coordinates of all cells
135 * @throws IllegalArgumentException if en1 or en2 is {@code null}
136 * @since 6869
137 */
138 public static List<Point2D> getSegmentCells(EastNorth en1, EastNorth en2, double gridDetail) {
139 CheckParameterUtil.ensureParameterNotNull(en1, "en1");
140 CheckParameterUtil.ensureParameterNotNull(en2, "en2");
141 List<Point2D> cells = new ArrayList<>();
142 double x0 = en1.east() * gridDetail;
143 double x1 = en2.east() * gridDetail;
144 double y0 = en1.north() * gridDetail + 1;
145 double y1 = en2.north() * gridDetail + 1;
146
147 if (x0 > x1) {
148 // Move to 1st-4th cuadrants
149 double aux;
150 aux = x0; x0 = x1; x1 = aux;
151 aux = y0; y0 = y1; y1 = aux;
152 }
153
154 double dx = x1 - x0;
155 double dy = y1 - y0;
156 long stepY = y0 <= y1 ? 1 : -1;
157 long gridX0 = (long) Math.floor(x0);
158 long gridX1 = (long) Math.floor(x1);
159 long gridY0 = (long) Math.floor(y0);
160 long gridY1 = (long) Math.floor(y1);
161
162 long maxSteps = (gridX1 - gridX0) + Math.abs(gridY1 - gridY0) + 1;
163 while ((gridX0 <= gridX1 && (gridY0 - gridY1)*stepY <= 0) && maxSteps-- > 0) {
164 cells.add(new Point2D.Double(gridX0, gridY0));
165
166 // Is the cross between the segment and next vertical line nearer than the cross with next horizontal line?
167 // Note: segment line formula: y=dy/dx(x-x1)+y1
168 // Note: if dy < 0, must use *bottom* line. If dy > 0, must use upper line
169 double scanY = dy/dx * (gridX0 + 1 - x1) + y1 + (dy < 0 ? -1 : 0);
170 double scanX = dx/dy * (gridY0 + (dy < 0 ? 0 : 1)*stepY - y1) + x1;
171
172 double distX = Math.pow(gridX0 + 1 - x0, 2) + Math.pow(scanY - y0, 2);
173 double distY = Math.pow(scanX - x0, 2) + Math.pow(gridY0 + stepY - y0, 2);
174
175 if (distX < distY) {
176 gridX0 += 1;
177 } else {
178 gridY0 += stepY;
179 }
180 }
181 return cells;
182 }
183}
Note: See TracBrowser for help on using the repository browser.