View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 	 * @param w
101 	 * @param h
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 }