| 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.*; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Random; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class VisualizeSpaceTimeSim2D extends JComponent { |
| 41 |
|
| 42 |
MultivariateNormalDistribution mnd; |
| 43 |
|
| 44 |
|
| 45 |
SpaceTime start; |
| 46 |
|
| 47 |
SpaceTimeRejector rejector; |
| 48 |
|
| 49 |
List<Shape> shapes; |
| 50 |
|
| 51 |
Paint shapeColor = Color.GRAY; |
| 52 |
|
| 53 |
Point2D topLeft; |
| 54 |
Point2D bottomRight; |
| 55 |
|
| 56 |
double scaleX; |
| 57 |
double scaleY; |
| 58 |
|
| 59 |
SpaceTimeSimulator sim; |
| 60 |
Random random = new Random(); |
| 61 |
|
| 62 |
boolean drawPath = false; |
| 63 |
boolean drawPoints = false; |
| 64 |
boolean drawFinalPoint = true; |
| 65 |
|
| 66 |
boolean paintDensity = true; |
| 67 |
|
| 68 |
int latticeWidth = 100; |
| 69 |
int latticeHeight = 100; |
| 70 |
|
| 71 |
public static final int POINT_SIZE = 3; |
| 72 |
public static final int steps = 100; |
| 73 |
public static final double dt = 0.01; |
| 74 |
|
| 75 |
public VisualizeSpaceTimeSim2D(final Rectangle2D bounds, final List<Shape> obstructions) { |
| 76 |
|
| 77 |
start = new SpaceTime(0, new double[]{0, 0}); |
| 78 |
|
| 79 |
double x1 = bounds.getMinX(); |
| 80 |
double y1 = bounds.getMinY(); |
| 81 |
double x2 = bounds.getMaxX(); |
| 82 |
double y2 = bounds.getMaxY(); |
| 83 |
|
| 84 |
topLeft = new Point2D.Double(x1, y1); |
| 85 |
bottomRight = new Point2D.Double(x2, y2); |
| 86 |
|
| 87 |
this.shapes = obstructions; |
| 88 |
|
| 89 |
rejector = new SpaceTimeRejector() { |
| 90 |
|
| 91 |
ArrayList<Reject> rejects = new ArrayList<Reject>(); |
| 92 |
|
| 93 |
public boolean reject(double time, double[] space) { |
| 94 |
Point2D p = new Point2D.Double(space[0], space[1]); |
| 95 |
|
| 96 |
if (!bounds.contains(p)) return true; |
| 97 |
|
| 98 |
for (Shape s : shapes) { |
| 99 |
if (s.contains(p)) { |
| 100 |
|
| 101 |
rejects.add(new Reject(0, time, space)); |
| 102 |
return true; |
| 103 |
} |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public void reset() { |
| 110 |
rejects.clear(); |
| 111 |
} |
| 112 |
|
| 113 |
public List<Reject> getRejects() { |
| 114 |
return rejects; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
mnd = new MultivariateNormalDistribution(new double[]{0.0}, new double[][]{{10, 0}, {0, 10}}); |
| 119 |
sim = new SpaceTimeSimulator(mnd); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
public void setShapeColor(Color c) { |
| 124 |
shapeColor = c; |
| 125 |
} |
| 126 |
|
| 127 |
void computeScales() { |
| 128 |
scaleX = getWidth() / (bottomRight.getX() - topLeft.getX()); |
| 129 |
scaleY = getHeight() / (bottomRight.getY() - topLeft.getY()); |
| 130 |
} |
| 131 |
|
| 132 |
public void paintComponent(Graphics g) { |
| 133 |
|
| 134 |
System.out.println("entering paintComponent()"); |
| 135 |
|
| 136 |
computeScales(); |
| 137 |
|
| 138 |
Graphics2D g2d = (Graphics2D) g; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
g2d.setStroke(new BasicStroke(1.5f)); |
| 143 |
|
| 144 |
System.out.println("Painting shapes"); |
| 145 |
for (Shape s : shapes) { |
| 146 |
System.out.print("."); |
| 147 |
System.out.flush(); |
| 148 |
GeneralPath path = new GeneralPath(s); |
| 149 |
path.transform(getFullTransform()); |
| 150 |
g2d.setPaint(shapeColor); |
| 151 |
g2d.fill(path); |
| 152 |
} |
| 153 |
|
| 154 |
AffineTransform transform = getFullTransform(); |
| 155 |
|
| 156 |
List<SpaceTime> points; |
| 157 |
for (int r = 0; r < getTrials(); r++) { |
| 158 |
Color c = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256), 128); |
| 159 |
g.setColor(c); |
| 160 |
|
| 161 |
if (drawPoints || drawPath) { |
| 162 |
points = sim.simulatePath(start, rejector, dt, steps); |
| 163 |
} else { |
| 164 |
points = new ArrayList<SpaceTime>(); |
| 165 |
points.add(sim.simulate(start, rejector, dt, steps)); |
| 166 |
} |
| 167 |
|
| 168 |
if (drawPoints) { |
| 169 |
for (SpaceTime s : points) { |
| 170 |
SpaceTime.paintDot(s, POINT_SIZE, transform, g2d); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if (drawPath) { |
| 175 |
GeneralPath path = new GeneralPath(); |
| 176 |
path.moveTo((float) points.get(0).getX(0), (float) points.get(0).getX(1)); |
| 177 |
|
| 178 |
for (int i = 1; i < points.size(); i++) { |
| 179 |
path.lineTo((float) points.get(i).getX(0), (float) points.get(i).getX(1)); |
| 180 |
|
| 181 |
} |
| 182 |
path.transform(getFullTransform()); |
| 183 |
g2d.draw(path); |
| 184 |
} |
| 185 |
|
| 186 |
if (drawFinalPoint) { |
| 187 |
SpaceTime.paintDot(points.get(points.size() - 1), POINT_SIZE, transform, g2d); |
| 188 |
} |
| 189 |
|
| 190 |
} |
| 191 |
g2d.setPaint(Color.black); |
| 192 |
SpaceTime.paintDot(start, POINT_SIZE, transform, g2d); |
| 193 |
|
| 194 |
System.out.println("leaving paintComponent()"); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
AffineTransform getScale() { |
| 199 |
return AffineTransform.getScaleInstance(scaleX, scaleY); |
| 200 |
} |
| 201 |
|
| 202 |
AffineTransform getTranslate() { |
| 203 |
return AffineTransform.getTranslateInstance(-topLeft.getX(), -topLeft.getY()); |
| 204 |
} |
| 205 |
|
| 206 |
AffineTransform getFullTransform() { |
| 207 |
AffineTransform transform = getScale(); |
| 208 |
transform.concatenate(getTranslate()); |
| 209 |
return transform; |
| 210 |
} |
| 211 |
|
| 212 |
public static void main(String[] args) { |
| 213 |
|
| 214 |
ArrayList<Shape> shapes = new ArrayList<Shape>(); |
| 215 |
shapes.add(new Ellipse2D.Double(0.15, 0.15, 0.3, 0.3)); |
| 216 |
shapes.add(new Ellipse2D.Double(0.5, 0.7, 0.2, 0.2)); |
| 217 |
shapes.add(new Ellipse2D.Double(0.8, 0.2, 0.15, 0.15)); |
| 218 |
|
| 219 |
Rectangle2D bounds = new Rectangle2D.Double(-0.2, -0.2, 1.4, 1.4); |
| 220 |
|
| 221 |
JFrame frame = new JFrame("Boulders"); |
| 222 |
frame.getContentPane().add(BorderLayout.CENTER, new VisualizeSpaceTimeSim2D(bounds, shapes)); |
| 223 |
frame.setSize(700, 700); |
| 224 |
frame.setVisible(true); |
| 225 |
} |
| 226 |
|
| 227 |
public int getTrials() { |
| 228 |
|
| 229 |
return 20000; |
| 230 |
} |
| 231 |
} |