Changeset 30701 in osm for applications/editors/josm/plugins/ElevationProfile/src
- Timestamp:
- 2014-10-04T17:28:45+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/grid/EleVertex.java
r30344 r30701 10 10 11 11 public class EleVertex { 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 12 private static final int NPOINTS = 3; 13 private static final double MIN_DIST = 90; 14 15 private double avrgEle = Double.NaN; 16 private double area = Double.NaN; 17 private final EleCoordinate[] points = new EleCoordinate[NPOINTS]; 18 19 public EleVertex(EleCoordinate p1, EleCoordinate p2, EleCoordinate p3) { 20 points[0] = p1; 21 points[1] = p2; 22 points[2] = p3; 23 24 // compute elevation 25 double z = 0D; 26 boolean eleValid = true; 27 for (EleCoordinate point : points) { 28 if (ElevationHelper.isValidElevation(p1.getEle())) { 29 z += point.getEle(); 30 } else { 31 eleValid = false; 32 break; 33 } 34 } 35 36 if (eleValid) { 37 avrgEle = z / NPOINTS; 38 } else { 39 avrgEle = ElevationHelper.NO_ELEVATION; 40 } 41 42 // compute the (approx.!) area of the vertex using heron's formula 43 double a = p1.greatCircleDistance(p2); 44 double b = p2.greatCircleDistance(p3); 45 double c = p1.greatCircleDistance(p3); 46 47 double s = (a + b + c) / 2D; 48 double sq = s * (s - a) * (s - b) * (s - c); 49 area = Math.sqrt(sq); 50 } 51 52 public List<EleVertex> divide() { 53 TriangleEdge[] edges = new TriangleEdge[NPOINTS]; 54 55 int k = 0; 56 for (int i = 0; i < points.length; i++) { 57 EleCoordinate c1 = points[i]; 58 59 for (int j = i + 1; j < points.length; j++) { 60 EleCoordinate c2 = points[j]; 61 62 edges[k++] = new TriangleEdge(i, j, c1.greatCircleDistance(c2)); 63 } 64 } 65 66 /* 67 67 for (int i = 0; i < edges.length; i++) { 68 68 TriangleEdge triangleEdge = edges[i]; … … 70 70 }*/ 71 71 72 73 74 75 76 77 78 79 80 81 82 83 72 // sort by distance 73 Arrays.sort(edges); 74 // pick the longest edge 75 TriangleEdge longest = edges[0]; 76 77 78 //System.out.println("Longest " + longest); 79 EleCoordinate pI = points[longest.getI()]; 80 EleCoordinate pJ = points[longest.getJ()]; 81 EleCoordinate pK = points[longest.getK()]; 82 EleCoordinate newP = getMid(pI, pJ); 83 /* 84 84 System.out.println(pI); 85 85 System.out.println(pJ); 86 86 System.out.println(pK); 87 87 System.out.println(newP); 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 88 */ 89 List<EleVertex> res = new ArrayList<EleVertex>(); 90 res.add(new EleVertex(pI, pK, newP)); 91 res.add(new EleVertex(pJ, pK, newP)); 92 93 return res; 94 } 95 96 /** 97 * Checks if vertex requires further processing or is finished. Currently this 98 * method returns <code>true</code>, if the average deviation is < 5m 99 * 100 * @return true, if is finished 101 */ 102 public boolean isFinished() { 103 /*double z = 0D; 104 104 double avrgEle = getEle(); 105 105 106 106 for (EleCoordinate point : points) { 107 107 z += (avrgEle - point.getEle()) * (avrgEle - point.getEle()); 108 } 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 108 }*/ 109 110 // TODO: Check for proper limit 111 return /*z < 75 || */getArea() < (30 * 30); // = 3 * 25 112 } 113 114 /** 115 * Gets the approximate area of this vertex in square meters. 116 * 117 * @return the area 118 */ 119 public double getArea() { 120 return area; 121 } 122 123 /** 124 * Gets the (linear interpolated) mid point of c1 and c2. 125 * 126 * @param c1 the first coordinate 127 * @param c2 the second coordinate 128 * @return the mid point 129 */ 130 public EleCoordinate getMid(EleCoordinate c1, EleCoordinate c2) { 131 double x = (c1.getX() + c2.getX()) / 2.0; 132 double y = (c1.getY() + c2.getY()) / 2.0; 133 134 double z = (c1.getEle() + c2.getEle()) / 2.0; 135 if (c1.greatCircleDistance(c2) > MIN_DIST) { 136 double hgtZ = ElevationHelper.getSrtmElevation(new LatLon(y, x)); 137 138 if (ElevationHelper.isValidElevation(hgtZ)) { 139 z = hgtZ; 140 } 141 } 142 143 return new EleCoordinate(y, x, z); 144 } 145 146 /** 147 * Gets the coordinate for the given index. 148 * 149 * @param index the index between 0 and NPOINTS: 150 * @return the elevation coordinate instance 151 * @throws IllegalArgumentException, if index is invalid 152 */ 153 public EleCoordinate get(int index) { 154 if (index < 0 || index >= NPOINTS) throw new IllegalArgumentException("Invalid index: " + index); 155 156 return points[index]; 157 } 158 159 /** 160 * Gets the average elevation of this vertex. 161 * 162 * @return the ele 163 */ 164 public double getEle() { 165 166 return avrgEle; 167 } 168 169 @Override 170 public String toString() { 171 return "EleVertex [avrgEle=" + avrgEle + ", area=" + area + ", points=" 172 + Arrays.toString(points) + "]"; 173 } 174 175 176 177 178 class TriangleEdge implements Comparable<TriangleEdge> { 179 private final int i; 180 private final int j; 181 private final double dist; 182 183 public TriangleEdge(int i, int j, double dist) { 184 super(); 185 this.i = i; 186 this.j = j; 187 this.dist = dist; 188 } 189 190 public int getI() { 191 return i; 192 } 193 194 public int getJ() { 195 return j; 196 } 197 198 public int getK() { 199 if (i == 0) { 200 return j == 1 ? 2 : 1; 201 } else if (i == 1) { 202 return j == 0 ? 2 : 0; 203 } else { 204 return j == 0 ? 1 : 0; 205 } 206 } 207 208 public double getDist() { 209 return dist; 210 } 211 212 @Override 213 public int compareTo(TriangleEdge o) { 214 return (int) (o.getDist() - dist); 215 } 216 217 @Override 218 public String toString() { 219 return "TriangleEdge [i=" + i + ", j=" + j + ", dist=" + dist + "]"; 220 } 221 } 222 222 }
Note:
See TracChangeset
for help on using the changeset viewer.