Debian Codegraph @

src/dr/geo/VisualizeBrownianBridge2D.java

raw symbols SCIP
1 /*
2 * VisualizeBrownianBridge2D.java
3 *
4 * Copyright (c) 2002-2015 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.math.MathUtils;
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.Ellipse2D;
35 import java.awt.geom.GeneralPath;
36 import java.awt.geom.Point2D;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41 * @author Alexei Drummond
42 * @author Marc Suchard
43 */
44 public class VisualizeBrownianBridge2D extends JComponent {
45
46 MultivariateNormalDistribution mnd;
47
48 // some line segments in space-time
49 SpaceTime[] start, end;
50
51 SpaceTimeRejector rejector;
52
53 List<Shape> shapes;
54
55 Paint shapeColor = Color.GRAY;
56
57 Point2D topLeft;
58 Point2D bottomRight;
59
60 double scaleX;
61 double scaleY;
62
63
64 public VisualizeBrownianBridge2D() {
65
66 start = new SpaceTime[]{new SpaceTime(0, new double[]{0, 0})};
67 end = new SpaceTime[]{new SpaceTime(1, new double[]{1, 1})};
68
69 topLeft = new Point2D.Double(-0.2, -0.2);
70 bottomRight = new Point2D.Double(1.2, 1.2);
71
72 shapes = new ArrayList<Shape>();
73 shapes.add(new Ellipse2D.Double(0.25, 0.25, 0.4, 0.4));
74 shapes.add(new Ellipse2D.Double(0.5, 0.7, 0.2, 0.2));
75 shapes.add(new Ellipse2D.Double(0.8, 0.2, 0.15, 0.15));
76
77 rejector = new SpaceTimeRejector() {
78
79 ArrayList<Reject> rejects = new ArrayList<Reject>();
80
81 public boolean reject(double time, double[] space) {
82 double x = space[0];
83 double y = space[1];
84 for (Shape s : shapes) {
85 if (s.contains(x, y)) {
86 rejects.add(new Reject(0, time, space));
87 return true;
88 }
89 }
90 return false;
91 }
92
93 // removes all rejects
94 public void reset() {
95 rejects.clear();
96 }
97
98 public List<Reject> getRejects() {
99 return rejects;
100 }
101 };
102
103 mnd = new MultivariateNormalDistribution(new double[]{0.0}, new double[][]{{10, 0}, {0, 10}});
104 }
105
106 public void setShapeColor(Color c) {
107 shapeColor = c;
108 }
109
110 void computeScales() {
111 scaleX = getWidth() / (bottomRight.getX() - topLeft.getX());
112 scaleY = getHeight() / (bottomRight.getY() - topLeft.getY());
113 }
114
115 public void paintComponent(Graphics g) {
116
117 System.out.println("entering paintComponent()");
118
119 computeScales();
120
121 Graphics2D g2d = (Graphics2D) g;
122
123 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
124
125 g2d.setStroke(new BasicStroke(1.5f));
126
127 System.out.println("Painting shapes");
128 for (Shape s : shapes) {
129 System.out.print(".");
130 System.out.flush();
131 GeneralPath path = new GeneralPath(s);
132 path.transform(getFullTransform());
133 g2d.setPaint(shapeColor);
134 g2d.fill(path);
135 }
136
137 AffineTransform transform = getFullTransform();
138
139 for (int r = 0; r < getTrials(); r++) {
140 Color c = new Color((float) MathUtils.nextDouble(), (float) MathUtils.nextDouble(), (float) MathUtils.nextDouble());
141
142 for (int s = 0; s < start.length; s++) {
143 List<SpaceTime> points = null;
144 g2d.setPaint(c);
145 int topLevelRejects = -1;
146 while (points == null) {
147 topLevelRejects += 1;
148 points = MultivariateBrownianBridge.divideConquerBrownianBridge(mnd, start[s], end[s], getMaxDepth(), getMaxTries(), rejector);
149 }
150
151 GeneralPath path = new GeneralPath();
152 path.moveTo((float) points.get(0).getX(0), (float) points.get(0).getX(1));
153 //System.out.println(points.get(0));
154 for (int i = 1; i < points.size(); i++) {
155 path.lineTo((float) points.get(i).getX(0), (float) points.get(i).getX(1));
156 //System.out.println(points.get(i));
157 }
158
159 path.transform(getFullTransform());
160
161 g2d.draw(path);
162
163 g2d.setPaint(Color.black);
164
165 SpaceTime.paintDot(start[s], 3, transform, g2d);
166 SpaceTime.paintDot(end[s], 3, transform, g2d);
167
168 }
169
170 }
171
172
173 System.out.println("leaving paintComponent()");
174
175 }
176
177 AffineTransform getScale() {
178 return AffineTransform.getScaleInstance(scaleX, scaleY);
179 }
180
181 AffineTransform getTranslate() {
182 return AffineTransform.getTranslateInstance(-topLeft.getX(), -topLeft.getY());
183 }
184
185 AffineTransform getFullTransform() {
186 AffineTransform transform = getScale();
187 transform.concatenate(getTranslate());
188 return transform;
189 }
190
191 public static void main(String[] args) {
192
193 JFrame frame = new JFrame("Boulders");
194 frame.getContentPane().add(BorderLayout.CENTER, new VisualizeBrownianBridge2D());
195 frame.setSize(600, 600);
196 frame.setVisible(true);
197 }
198
199 public int getMaxDepth() {
200 return 10;
201 }
202
203 public int getTrials() {
204
205 return 10;
206 }
207
208 public int getMaxTries() {
209 return 10;
210 }
211 }