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
27
28
29
30 package org.openimaj.demos.irc;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.openimaj.content.animation.animator.LinearTimeBasedFloatValueAnimator;
37 import org.openimaj.content.animation.animator.ValueAnimator;
38 import org.openimaj.image.MBFImage;
39 import org.openimaj.image.colour.ColourSpace;
40 import org.openimaj.image.colour.RGBColour;
41 import org.openimaj.math.geometry.point.Point2d;
42 import org.openimaj.math.geometry.shape.Shape;
43 import org.openimaj.math.geometry.transforms.TransformUtilities;
44 import org.openimaj.text.geo.WorldPlace;
45 import org.openimaj.text.geo.WorldPolygons;
46 import org.openimaj.video.AnimatedVideo;
47
48 import Jama.Matrix;
49
50 public class WorldVis extends AnimatedVideo<MBFImage>{
51 class LabColourAnimator implements ValueAnimator<Float[]>{
52 private final LinearTimeBasedFloatValueAnimator l;
53 private final LinearTimeBasedFloatValueAnimator a;
54 private final LinearTimeBasedFloatValueAnimator b;
55 private final MBFImage buffer;
56
57 ColourSpace space = ColourSpace.RGB;
58 public LabColourAnimator(final Float[] start, final Float[] end, final long duration) {
59 final MBFImage startImg = new MBFImage(1,1,3);
60 startImg.setPixel(0, 0, start);
61 final MBFImage endImg = new MBFImage(1,1,3);
62 this.buffer = new MBFImage(1,1,3);
63 endImg.setPixel(0, 0, end);
64 final Float[] labstart = this.space.convertFromRGB(startImg).getPixel(0, 0);
65 final Float[] labend = this.space.convertFromRGB(endImg).getPixel(0, 0);
66 this.l = new LinearTimeBasedFloatValueAnimator(labstart[0], labend[0], duration);
67 this.a = new LinearTimeBasedFloatValueAnimator(labstart[1], labend[1], duration);
68 this.b = new LinearTimeBasedFloatValueAnimator(labstart[2], labend[2], duration);
69 }
70 @Override
71 public Float[] nextValue() {
72 this.buffer.setPixel(0, 0, new Float[]{this.l.nextValue(),this.a.nextValue(),this.b.nextValue()});
73 final Float[] retPix = this.space.convertToRGB(this.buffer).getPixel(0, 0);
74 return retPix;
75 }
76
77 @Override
78 public boolean hasFinished() {
79 return this.l.hasFinished();
80 }
81
82 @Override
83 public void reset() {
84 this.l.reset();
85 this.a.reset();
86 this.b.reset();
87 }
88
89 }
90 private static final Float[] SEA_COLOUR = new Float[]{135f/255f,206f/255f,250f/255f};
91 private static final Float[] LAND_COLOUR = new Float[]{238f/255f,232f/255f,170f/255f};
92 private final int w;
93 private final int h;
94 private final WorldPolygons worldPolys;
95 private final float scale;
96 private final Map<String,LabColourAnimator> activeCountries = new HashMap<String, LabColourAnimator>();
97 private final MBFImage img;
98
99
100
101
102
103 public WorldVis(final int w, final int h) {
104 super(new MBFImage(w,h,3));
105 this.w = w;
106 this.h = h;
107 this.worldPolys = new WorldPolygons();
108 this.scale = h/this.worldPolys.getBounds().height;
109 this.img = new MBFImage(w, h, 3);
110 }
111
112 private MBFImage create(){
113 this.img.fill(WorldVis.SEA_COLOUR);
114 final Point2d mid = this.img.getBounds().calculateCentroid();
115 Matrix trans = Matrix.identity(3, 3);
116 trans = trans.times(
117 TransformUtilities.scaleMatrixAboutPoint(
118 this.scale, this.scale, mid
119 )
120 );
121 trans = trans.times(
122 TransformUtilities.rotationMatrixAboutPoint(Math.PI, mid.getX(), mid.getY())
123 );
124 trans = trans.times(
125 TransformUtilities.translateMatrix(mid.getX(), mid.getY())
126 );
127 for (final WorldPlace wp : this.worldPolys.getShapes()) {
128
129 final List<Shape> shapes = wp.getShapes();
130 for (Shape s : shapes) {
131 s = s.clone();
132
133 s = s.transform(trans);
134 this.img.drawShape(s,3, RGBColour.RED);
135 if(this.activeCountries.containsKey(wp.getISOA2()))
136 {
137 this.img.drawShapeFilled(s, this.activeCountries.get(wp.getISOA2()).nextValue());
138 if(this.activeCountries.get(wp.getISOA2()).hasFinished())
139 {
140 this.activeCountries.remove(wp.getISOA2());
141 }
142 }else{
143 this.img.drawShapeFilled(s, WorldVis.LAND_COLOUR);
144 }
145 }
146 }
147 return this.img.flipX();
148 }
149
150 @Override
151 protected void updateNextFrame(final MBFImage frame) {
152 frame.internalAssign(this.create());
153
154 }
155
156 public void activate(final FreeGeoIPLocation geoip) {
157 this.activeCountries.put(
158 geoip.country_code.toLowerCase(),
159 new LabColourAnimator(RGBColour.RED, WorldVis.LAND_COLOUR, 1000)
160 );
161 }
162
163 }