Debian Codegraph @

src/dr/geo/VisualizeKMLNumericalProbs.java

raw symbols SCIP
1 /*
2 * VisualizeKMLNumericalProbs.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.geo;
27
28 import dr.app.gui.ColorFunction;
29 import dr.math.distributions.MultivariateNormalDistribution;
30
31 import javax.swing.*;
32 import java.awt.*;
33 import java.awt.geom.AffineTransform;
34 import java.awt.geom.GeneralPath;
35 import java.awt.geom.Point2D;
36 import java.awt.geom.Rectangle2D;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41 * @author Alexei Drummond
42 */
43 public class VisualizeKMLNumericalProbs extends JComponent {
44
45 List<AbstractPolygon2D> polygons;
46
47 Point2D brussels = new Point2D.Double(4.35, 50.85);
48 Point2D amsterdam = new Point2D.Double(4.89, 52.37);
49 Point2D berlin = new Point2D.Double(13.41, 52.52);
50 Point2D rome = new Point2D.Double(12.48, 41.9);
51 Point2D naples = new Point2D.Double(14.283, 40.85);
52
53 Point2D athens = new Point2D.Double(23.72, 37.98);
54 Point2D paris = new Point2D.Double(2.35, 48.86);
55 Point2D montepelier = new Point2D.Double(3.88, 43.61);
56 Point2D munich = new Point2D.Double(11.58, 48.14);
57 Point2D bern = new Point2D.Double(7.45, 46.95);
58
59 Point2D start, end;
60 Point2D topLeft;
61 Point2D bottomRight;
62 List<Shape> shapes;
63 SpaceTimeRejector rejector;
64 MultivariateNormalDistribution D;
65 NumericalSpaceTimeProbs2D probs;
66
67 ColorFunction cf;
68
69 double scaleX;
70 double scaleY;
71
72 boolean ABSORBING = true;
73
74 public VisualizeKMLNumericalProbs(String kmlFileName) {
75
76 polygons = Polygon2D.readKMLFile(kmlFileName);
77
78 System.out.println("Read " + polygons.size() + " polygons");
79
80 start = bern;
81 end = rome;
82
83 topLeft = new Point2D.Double(-5, 28);
84 bottomRight = new Point2D.Double(25, 57);
85
86 System.out.println("Converting polygons to shapes");
87 shapes = new ArrayList<Shape>();
88 for (AbstractPolygon2D p : polygons) {
89 shapes.add(p.getShape());
90 System.out.print(".");
91 System.out.flush();
92 }
93 System.out.println();
94
95 Rectangle2D bounds = new Rectangle2D.Double(topLeft.getX(), topLeft.getY(), bottomRight.getX() - topLeft.getX(), bottomRight.getY() - topLeft.getY());
96
97 final SpaceTimeRejector boundsRejector = SpaceTimeRejector.Utils.createSimpleBounds2D(bounds);
98
99 rejector = new SpaceTimeRejector() {
100
101 // private boolean stop = false;
102
103 public boolean reject(double time, double[] space) {
104
105 if (boundsRejector.reject(time, space)) return true;
106
107 double x = space[0];
108 double y = space[1];
109 for (Shape s : shapes) {
110 if (s.contains(x, y)) {
111 return true;
112 }
113 }
114 return false;
115 }
116
117 // removes all rejects
118 public void reset() {
119 }
120
121 public List<Reject> getRejects() {
122 return null;
123 }
124 };
125
126 cf = new ColorFunction(
127 new Color[]{Color.white, Color.blue, Color.magenta, Color.red},
128 new float[]{0.0f, 0.10f, 0.20f, 1.0f}
129 );
130
131 D = new MultivariateNormalDistribution(new double[]{0.0}, new double[][]{{1, 0}, {0, 1}});
132
133 if (rejector.reject(0.0, new double[]{start.getX(), start.getY()})) {
134 throw new RuntimeException("The start position was rejected!");
135 }
136
137 //probs = new NumericalSpaceTimeProbs2D(50, 50, 50, 50, 1, bounds, D, boundsRejector);
138 probs = new NumericalSpaceTimeProbs2D(50, 50, 50, 50, 1, bounds, D, rejector);
139
140 System.out.println("Populating...");
141 if (ABSORBING) {
142 int successes = probs.populateAbsorbing(start, 2000000);
143 System.out.println(successes + " paths simulated successfully simulated");
144 } else {
145 probs.populate(start, 25000, false);
146 }
147 System.out.println("Finished populating...");
148 }
149
150 void computeScales() {
151 scaleX = getWidth() / (bottomRight.getX() - topLeft.getX());
152 scaleY = getHeight() / (bottomRight.getY() - topLeft.getY());
153 }
154
155
156 public void paintComponent(Graphics g) {
157
158 System.out.println("entering paintComponent()");
159
160 computeScales();
161
162 Graphics2D g2d = (Graphics2D) g;
163
164 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
165
166 g2d.setStroke(new BasicStroke(1.5f));
167
168 int sx = probs.x(start.getX());
169 int sy = probs.y(start.getY());
170 int t = 49;
171
172 double maxCount1 = probs.maxCount[sx][sy][t];
173 System.out.println("start max count = " + maxCount1);
174
175
176 AffineTransform transform = getFullTransform();
177
178 System.out.println("Painting lattice probs");
179 for (int i = 0; i < probs.latticeWidth; i++) {
180 for (int j = 0; j < probs.latticeHeight; j++) {
181
182 float I1 = (float) probs.r(sx, sy, i, j, t);
183
184 Rectangle2D rect = new Rectangle2D.Double(i * probs.dx + probs.minx, j * probs.dy + probs.miny, probs.dx, probs.dy);
185
186 g.setColor(cf.getColor(I1));
187 g2d.fill(transform.createTransformedShape(rect));
188 g.setColor(Color.black);
189 g2d.draw(transform.createTransformedShape(rect));
190 }
191 }
192
193
194 System.out.println("Painting shapes");
195 for (Shape s : shapes) {
196 System.out.print(".");
197 System.out.flush();
198 GeneralPath path = new GeneralPath(s);
199 path.transform(transform);
200 g2d.setPaint(Color.BLACK);
201 g2d.fill(path);
202 }
203
204 g2d.setColor(Color.yellow);
205 SpaceTime.paintDot(new SpaceTime(0, start), 4, transform, g2d);
206
207 g2d.setColor(Color.green);
208 SpaceTime.paintDot(new SpaceTime(0, end), 4, transform, g2d);
209
210 int ex = probs.x(end.getX());
211 int ey = probs.y(end.getY());
212
213 String message = "p=" + probs.p(sx, sy, ex, ey, t) + " r=" + probs.r(sx, sy, ex, ey, t) + " c=" + probs.counts[sx][sy][ex][ey][t];
214
215 g2d.setColor(Color.yellow);
216 g2d.drawString(message, 20, getHeight() - 20);
217 }
218
219 AffineTransform getFullTransform() {
220 AffineTransform transform = getScale();
221 transform.concatenate(getTranslate());
222 return transform;
223 }
224
225 AffineTransform getTranslate() {
226 return AffineTransform.getTranslateInstance(-topLeft.getX(), -bottomRight.getY());
227 }
228
229 AffineTransform getScale() {
230 return AffineTransform.getScaleInstance(scaleX, -scaleY);
231 }
232
233
234 public static void main(String[] args) {
235
236 JFrame frame = new JFrame("Europe");
237 frame.getContentPane().add(BorderLayout.CENTER, new VisualizeKMLNumericalProbs(args[0]));
238 frame.setSize(900, 900);
239 frame.setVisible(true);
240 }
241
242 }