|
| 1 | +package VisualizationMap; |
| 2 | + |
| 3 | +import de.fhpotsdam.unfolding.geo.Location; |
| 4 | +import de.fhpotsdam.unfolding.marker.SimplePolygonMarker; |
| 5 | +import de.fhpotsdam.unfolding.utils.MapPosition; |
| 6 | + |
| 7 | +import java.text.DateFormat; |
| 8 | +import java.text.ParseException; |
| 9 | +import java.text.SimpleDateFormat; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Date; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | + |
| 17 | +import Visual.Building; |
| 18 | +import Visual.LatLong; |
| 19 | +import processing.core.PGraphics; |
| 20 | + |
| 21 | +/** |
| 22 | + * A marker used to represent a building on the map |
| 23 | + */ |
| 24 | +public final class BuildingMarker extends SimplePolygonMarker implements Comparable<BuildingMarker> { |
| 25 | + Building building; |
| 26 | + private Color c; |
| 27 | + BuildingMarker(Building b/*, Color c*/) { |
| 28 | + Objects.requireNonNull(b, "Building must be set"); |
| 29 | + building = b; |
| 30 | + List<Location> locations = new ArrayList<>(); |
| 31 | + for (LatLong l : b.outline()) { |
| 32 | + Location loc = new Location(l.longitude(), l.lat()); |
| 33 | + locations.add(loc); |
| 34 | + } |
| 35 | + this.addLocations(locations); |
| 36 | + c = new Color(255, 255, 255, 0); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public int compareTo(BuildingMarker other) { |
| 41 | + Objects.requireNonNull(other); |
| 42 | + return this.building.compareTo(other.building); |
| 43 | + } |
| 44 | + |
| 45 | + public void draw(PGraphics pg, List<MapPosition> mapPositions) { |
| 46 | + // marker |
| 47 | + pg.pushStyle(); |
| 48 | + pg.strokeWeight(2); |
| 49 | + pg.stroke(0, 0, 0); |
| 50 | + pg.fill(c.r, c.g, c.b, c.alpha); |
| 51 | + if (this.isSelected()) { |
| 52 | + pg.fill(255, 255, 255, 255); |
| 53 | + } |
| 54 | + pg.beginShape(); |
| 55 | + for (MapPosition mapPosition : mapPositions) { |
| 56 | + pg.vertex(mapPosition.x, mapPosition.y); |
| 57 | + } |
| 58 | + pg.endShape(); |
| 59 | + pg.popStyle(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Sets the fill color |
| 64 | + * @param c the new fill color |
| 65 | + */ |
| 66 | + public void newColor(Color c) { |
| 67 | + this.c = c; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Balances out all of the colors according to the current time, from min to max. |
| 72 | + * |
| 73 | + * @param markers the list of markers to balance out |
| 74 | + * @param time the time to balance with |
| 75 | + * @throws IllegalArgumentException if there are more than 255 markers |
| 76 | + */ |
| 77 | + static void balanceColors(ArrayList<BuildingMarker> markers, long time) { |
| 78 | + if (markers.size() > 255) { |
| 79 | + throw new IllegalArgumentException("Too many markers, can only balance a total" + |
| 80 | + "of 255 markers"); |
| 81 | + } |
| 82 | + for (BuildingMarker marker : markers) { |
| 83 | + marker.building.changeTime(time); |
| 84 | + } |
| 85 | + Collections.sort(markers); |
| 86 | + for (int i = 0; i < markers.size(); i++) { |
| 87 | + BuildingMarker bm = markers.get(i); |
| 88 | + if (bm.building.currentWattage() == 0) { |
| 89 | + bm.newColor(new Color(0, 128, 255, 225)); |
| 90 | + } else { |
| 91 | + int diff = 255 / markers.size(); |
| 92 | + bm.newColor(new Color(255, 255 - (diff * i), 0, 175)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Creates an array of doubles for last specified number of readings differing among the |
| 99 | + * specified number of times. |
| 100 | + * @param minTime the minimum point which there are no times past |
| 101 | + * @param timeDiff how much to offset the times by |
| 102 | + * @param numberOfReadings how many times to count down |
| 103 | + * @return the array of watts for the last specified times. |
| 104 | + */ |
| 105 | + double[] lastReadings(long minTime, long timeDiff, int numberOfReadings) { |
| 106 | + if (numberOfReadings < 1) { |
| 107 | + throw new IllegalArgumentException("There must be a number of readings"); |
| 108 | + } |
| 109 | + if (timeDiff < 1) { |
| 110 | + throw new IllegalArgumentException("Must have a positive integer for time difference"); |
| 111 | + } |
| 112 | + double[] result = new double[numberOfReadings]; |
| 113 | + for (int i = 0; i < numberOfReadings; i++) { |
| 114 | + long time = building.currentTime() - (timeDiff * i); |
| 115 | + if (time < minTime) { |
| 116 | + result[i] = -1.0; |
| 117 | + } else { |
| 118 | + result[i] = building.wattage(time); |
| 119 | + } |
| 120 | + } |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Identifies the maximum and minimum value in the array, and scales them down |
| 126 | + * to [0, 1] |
| 127 | + * @param toScale the array of doubles to scale |
| 128 | + * @return an array of values between [0, 1] representing the relative size to the max |
| 129 | + * and min value of the original array |
| 130 | + */ |
| 131 | + double[] scaleDoubles(double[] toScale) { |
| 132 | + double max = 0.0; |
| 133 | + double min = 0.0; |
| 134 | + if (toScale.length > 0) { |
| 135 | + max = toScale[0]; |
| 136 | + min = toScale[0]; |
| 137 | + } |
| 138 | + for (double d : toScale) { |
| 139 | + if (d > max) { |
| 140 | + max = d; |
| 141 | + } |
| 142 | + if (d < min) { |
| 143 | + min = d; |
| 144 | + } |
| 145 | + } |
| 146 | + max -= min; |
| 147 | + double[] result = new double[toScale.length]; |
| 148 | + if (max == 0) { |
| 149 | + return result; |
| 150 | + } |
| 151 | + for (int i = 0; i < toScale.length; i++) { |
| 152 | + result[i] = (toScale[i] - min) / max; |
| 153 | + } |
| 154 | + return result; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates the string array containing the last week worth of power readings |
| 159 | + * @param minTime the minimum time that there are no readings past. |
| 160 | + * @return the week's worth of readings in a string array. |
| 161 | + */ |
| 162 | + String[] lastWeek(long minTime) { |
| 163 | + String[] result = new String[7]; |
| 164 | + long currentTime = this.building.currentTime(); |
| 165 | + long timeDiff = 86400; |
| 166 | + SimpleDateFormat sdf = new SimpleDateFormat("MMMM d"); |
| 167 | + for (int i = 0; i < 7; i++) { |
| 168 | + if (i == 0) { |
| 169 | + String watt = Double.toString(building.currentWattage()); |
| 170 | + if (watt.length() > 9) { |
| 171 | + watt = watt.substring(0, 9); |
| 172 | + } |
| 173 | + watt += " Watts"; |
| 174 | + result[i] = "Today: " + watt; |
| 175 | + } else if ((currentTime - (timeDiff * i)) < minTime) { |
| 176 | + result[i] = "No such time: "; |
| 177 | + } else { |
| 178 | + String watt = Double.toString(building.wattage(currentTime - (timeDiff * i))); |
| 179 | + if (watt.length() > 9) { |
| 180 | + watt = watt.substring(0, 9); |
| 181 | + } |
| 182 | + watt += " Watts"; |
| 183 | + result[i] = sdf.format((currentTime - (timeDiff * i)) * 1000) + ": " + watt; |
| 184 | + } |
| 185 | + } |
| 186 | + return result; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Converts the given string date into unix time. |
| 191 | + * @param givenDate the string to parse. Must be in "mmddyyyy" |
| 192 | + * @return the unix time |
| 193 | + */ |
| 194 | + static long stringToUnix(String givenDate) { |
| 195 | + DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); |
| 196 | + try { |
| 197 | + Date date = dateFormat.parse(givenDate); |
| 198 | + long unixTime = date.getTime() / 1000; |
| 199 | + System.out.println(unixTime); |
| 200 | + return unixTime; |
| 201 | + } catch (ParseException e) { |
| 202 | + throw new IllegalArgumentException("String must be formatted as \"DAYMONTHYEAR in" + |
| 203 | + " numbers\""); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + static String unixTostring(long unix) { |
| 208 | + DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy 'at' hh:mm"); |
| 209 | + return dateFormat.format(unix * 1000); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Clamps the given int between [0, 255] |
| 214 | + * @param i the number to clamp |
| 215 | + * @return the clamped number |
| 216 | + */ |
| 217 | + private static int clamp(int i) { |
| 218 | + if (i > 255) { |
| 219 | + return 255; |
| 220 | + } else if (i < 0) { |
| 221 | + return 0; |
| 222 | + } else { |
| 223 | + return i; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | + /** |
| 229 | + * A class to hold rgb values. |
| 230 | + */ |
| 231 | + static class Color { |
| 232 | + public final int r; |
| 233 | + public final int g; |
| 234 | + public final int b; |
| 235 | + public final int alpha; |
| 236 | + |
| 237 | + Color(int r, int g, int b, int alpha) { |
| 238 | + this.r = clamp(r); |
| 239 | + this.g = clamp(g); |
| 240 | + this.b = clamp(b); |
| 241 | + this.alpha = clamp(alpha); |
| 242 | + } |
| 243 | + } |
| 244 | +} |
0 commit comments