| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.geo; |
| 27 |
|
| 28 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 29 |
|
| 30 |
import javax.swing.*; |
| 31 |
import java.awt.*; |
| 32 |
import java.awt.geom.AffineTransform; |
| 33 |
import java.awt.geom.Point2D; |
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class VisualizeKMLBrownianBridge extends VisualizeBrownianBridge2D { |
| 41 |
|
| 42 |
List<AbstractPolygon2D> polygons; |
| 43 |
List<Reject> rejects = new ArrayList<Reject>(); |
| 44 |
int MAX_DEPTH = 10; |
| 45 |
int MAX_TRIES = 20; |
| 46 |
int TRIALS = 3; |
| 47 |
|
| 48 |
Point2D brussels = new Point2D.Double(4.35, 50.85); |
| 49 |
Point2D amsterdam = new Point2D.Double(4.89, 52.37); |
| 50 |
Point2D berlin = new Point2D.Double(13.41, 52.52); |
| 51 |
Point2D rome = new Point2D.Double(12.48, 41.9); |
| 52 |
Point2D athens = new Point2D.Double(23.72, 37.98); |
| 53 |
Point2D paris = new Point2D.Double(2.35, 48.86); |
| 54 |
Point2D montepelier = new Point2D.Double(3.88, 43.61); |
| 55 |
Point2D munich = new Point2D.Double(11.58, 48.14); |
| 56 |
Point2D bern = new Point2D.Double(7.45, 46.95); |
| 57 |
|
| 58 |
|
| 59 |
Color[] depthColor = new Color[]{Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta}; |
| 60 |
|
| 61 |
public VisualizeKMLBrownianBridge(String kmlFileName) { |
| 62 |
|
| 63 |
polygons = Polygon2D.readKMLFile(kmlFileName); |
| 64 |
|
| 65 |
System.out.println("Read " + polygons.size() + " polygons"); |
| 66 |
|
| 67 |
start = new SpaceTime[]{ |
| 68 |
new SpaceTime(0, amsterdam), |
| 69 |
new SpaceTime(0, amsterdam), |
| 70 |
new SpaceTime(1, bern), |
| 71 |
new SpaceTime(1, bern), |
| 72 |
|
| 73 |
}; |
| 74 |
end = new SpaceTime[]{ |
| 75 |
new SpaceTime(2, montepelier), |
| 76 |
new SpaceTime(1, bern), |
| 77 |
new SpaceTime(2, rome), |
| 78 |
new SpaceTime(2, athens) |
| 79 |
}; |
| 80 |
|
| 81 |
topLeft = new Point2D.Double(-5, 28); |
| 82 |
bottomRight = new Point2D.Double(25, 57); |
| 83 |
|
| 84 |
System.out.println("Converting polygons to shapes"); |
| 85 |
shapes = new ArrayList<Shape>(); |
| 86 |
for (AbstractPolygon2D p : polygons) { |
| 87 |
shapes.add(p.getShape()); |
| 88 |
System.out.print("."); |
| 89 |
System.out.flush(); |
| 90 |
} |
| 91 |
System.out.println(); |
| 92 |
|
| 93 |
rejector = new SpaceTimeRejector() { |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public boolean reject(double time, double[] space) { |
| 98 |
double x = space[0]; |
| 99 |
double y = space[1]; |
| 100 |
for (Shape s : shapes) { |
| 101 |
if (s.contains(x, y)) { |
| 102 |
rejects.add(new Reject(0, time, space)); |
| 103 |
return true; |
| 104 |
} |
| 105 |
} |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public void reset() { |
| 111 |
rejects.clear(); |
| 112 |
} |
| 113 |
|
| 114 |
public List<Reject> getRejects() { |
| 115 |
return rejects; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
}; |
| 127 |
|
| 128 |
mnd = new MultivariateNormalDistribution(new double[]{0.0}, new double[][]{{0.15, 0}, {0, 0.15}}); |
| 129 |
|
| 130 |
shapeColor = Color.BLACK; |
| 131 |
|
| 132 |
for (int i = 0; i < start.length; i++) { |
| 133 |
if (rejector.reject(start[i].time, start[i].space) || rejector.reject(end[i].time, end[i].space)) { |
| 134 |
throw new RuntimeException("Start or end in water"); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
for (int i = 0; i < depthColor.length; i++) { |
| 139 |
depthColor[i] = new Color(depthColor[i].getRed(), depthColor[i].getGreen(), depthColor[i].getBlue(), 128); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public int getMaxDepth() { |
| 144 |
return MAX_DEPTH; |
| 145 |
} |
| 146 |
|
| 147 |
public int getMaxTries() { |
| 148 |
return MAX_TRIES; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
public int getTrials() { |
| 153 |
return TRIALS; |
| 154 |
} |
| 155 |
|
| 156 |
public void paintComponent(Graphics g) { |
| 157 |
super.paintComponent(g); |
| 158 |
|
| 159 |
AffineTransform transform = getFullTransform(); |
| 160 |
for (Reject r : rejects) { |
| 161 |
g.setColor(depthColor[0]); |
| 162 |
SpaceTime.paintDot(new SpaceTime(r.getTime(), r.getSpace()), 2, transform, (Graphics2D) g); |
| 163 |
} |
| 164 |
rejector.reset(); |
| 165 |
} |
| 166 |
|
| 167 |
AffineTransform getTranslate() { |
| 168 |
return AffineTransform.getTranslateInstance(-topLeft.getX(), -bottomRight.getY()); |
| 169 |
} |
| 170 |
|
| 171 |
AffineTransform getScale() { |
| 172 |
return AffineTransform.getScaleInstance(scaleX, -scaleY); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
public static void main(String[] args) { |
| 177 |
|
| 178 |
JFrame frame = new JFrame("Europe"); |
| 179 |
frame.getContentPane().add(BorderLayout.CENTER, new VisualizeKMLBrownianBridge(args[0])); |
| 180 |
frame.setSize(900, 900); |
| 181 |
frame.setVisible(true); |
| 182 |
} |
| 183 |
|
| 184 |
} |