svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src-test / org / gvsig / fmap / mapcontext / rendering / symbol / TestDrawMarkers.java @ 29313
History | View | Annotate | Download (8.11 KB)
1 |
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib��ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.fmap.mapcontext.rendering.symbol; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Dimension; |
45 |
import java.awt.Graphics2D; |
46 |
import java.awt.Rectangle; |
47 |
import java.awt.geom.AffineTransform; |
48 |
import java.awt.geom.Point2D; |
49 |
import java.awt.image.BufferedImage; |
50 |
import java.util.ArrayList; |
51 |
import java.util.Random; |
52 |
|
53 |
import junit.framework.TestCase; |
54 |
|
55 |
import org.gvsig.compat.CompatLocator; |
56 |
import org.gvsig.fmap.geom.GeometryLocator; |
57 |
import org.gvsig.fmap.geom.GeometryManager; |
58 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
59 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
60 |
import org.gvsig.fmap.mapcontext.rendering.symbols.IMarkerSymbol; |
61 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
/**
|
66 |
* Integration test to test that Marker symbols always draw in the same
|
67 |
* place respecting size constraints.
|
68 |
*
|
69 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
70 |
*/
|
71 |
public class TestDrawMarkers extends TestCase { |
72 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
73 |
private static final Logger logger = LoggerFactory.getLogger(TestDrawMarkers.class); |
74 |
private IMarkerSymbol[] symbols; |
75 |
private final Dimension sz = new Dimension(400, 400); |
76 |
private org.gvsig.fmap.geom.primitive.Point centerP = null; |
77 |
private static final int OUTTER_TOLERANCE = 1; |
78 |
private static final int INNER_TOLERANCE = 1; |
79 |
|
80 |
private static final double sizes[] = new double[] { |
81 |
200,
|
82 |
100,
|
83 |
50,
|
84 |
30,
|
85 |
16,
|
86 |
5,
|
87 |
3,
|
88 |
2,
|
89 |
// smaller sizes don't make any sense
|
90 |
|
91 |
}; |
92 |
|
93 |
|
94 |
|
95 |
protected void setUp() throws Exception { |
96 |
// get all the symbols in the Test Suite
|
97 |
super.setUp();
|
98 |
|
99 |
centerP = geomManager.createPoint(sz.width/2, sz.height/2, SUBTYPES.GEOM2D); |
100 |
|
101 |
ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
|
102 |
// Filter the marker ones
|
103 |
ArrayList symbols = new ArrayList(); |
104 |
|
105 |
for (int i = 0; i < allSymbols.length; i++) { |
106 |
if (allSymbols[i] instanceof IMarkerSymbol) { |
107 |
IMarkerSymbol sym = (IMarkerSymbol) allSymbols[i]; |
108 |
symbols.add(sym); |
109 |
|
110 |
} |
111 |
} |
112 |
this.symbols = (IMarkerSymbol[]) symbols.toArray(new IMarkerSymbol[symbols.size()]); |
113 |
} |
114 |
|
115 |
public void testDrawingSize() { |
116 |
for (int i = 0; i < symbols.length; i++) { |
117 |
for (int j = 0; j < sizes.length; j++) { |
118 |
// new blank buffered image
|
119 |
BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB); |
120 |
|
121 |
// the graphics for the image, so we can draw onto the buffered image
|
122 |
Graphics2D g = bi.createGraphics();
|
123 |
|
124 |
IMarkerSymbol testSymbol = symbols[i]; |
125 |
testSymbol.setColor(Color.YELLOW);
|
126 |
testSymbol.setSize(sizes[j]); |
127 |
String name = testSymbol.getClassName().substring(
|
128 |
testSymbol.getClassName().lastIndexOf('.')+1, |
129 |
testSymbol.getClassName().length()); |
130 |
|
131 |
testSymbol.draw(g, new AffineTransform(), centerP, null); |
132 |
|
133 |
Rectangle wrappingRect = new Rectangle( |
134 |
(int) (centerP.getX()-sizes[j]/2), |
135 |
(int) (centerP.getY()-sizes[j]/2), |
136 |
(int) (sizes[j]),
|
137 |
(int) (sizes[j]));
|
138 |
|
139 |
// if (testSymbol instanceof CharacterMarkerSymbol)
|
140 |
// continue;
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE )); |
146 |
assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" + |
147 |
"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
|
148 |
|
149 |
|
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
public void testDrawingOffset() { |
155 |
Random random = new Random(System.currentTimeMillis()); |
156 |
for (int i = 0; i < symbols.length; i++) { |
157 |
for (int j = 0; j < sizes.length; j++) { |
158 |
// new blank buffered image
|
159 |
BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB); |
160 |
|
161 |
|
162 |
// the graphics for the image, so we can draw onto the buffered image
|
163 |
Graphics2D g = bi.createGraphics();
|
164 |
|
165 |
IMarkerSymbol testSymbol = symbols[i]; |
166 |
testSymbol.setColor(Color.YELLOW);
|
167 |
testSymbol.setSize(sizes[j]); |
168 |
|
169 |
String name = testSymbol.getClassName().substring(
|
170 |
testSymbol.getClassName().lastIndexOf('.')+1, |
171 |
testSymbol.getClassName().length()); |
172 |
|
173 |
double xOffset = (random.nextDouble()-0.5)*sz.width; |
174 |
double yOffset = (random.nextDouble()-0.5)*sz.height; |
175 |
|
176 |
testSymbol.setOffset(new Point2D.Double(xOffset, yOffset)); |
177 |
|
178 |
Rectangle wrappingRect = new Rectangle( |
179 |
(int) ((centerP.getX()-sizes[j]/2) + xOffset), |
180 |
(int) ((centerP.getY()-sizes[j]/2) + yOffset), |
181 |
(int) (sizes[j]),
|
182 |
(int) (sizes[j]));
|
183 |
|
184 |
// if (testSymbol instanceof CharacterMarkerSymbol)
|
185 |
// continue;
|
186 |
|
187 |
|
188 |
|
189 |
testSymbol.draw(g, new AffineTransform(), centerP, null); |
190 |
assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE )); |
191 |
assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" + |
192 |
"\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
|
193 |
|
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
public void testDrawingRotation() { |
199 |
// TODO implement it
|
200 |
|
201 |
} |
202 |
|
203 |
private boolean fitsInsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) { |
204 |
int myTolerance = Math.min(tolerance, wrappingRect.width); |
205 |
Rectangle myRect;
|
206 |
if (myTolerance >= INNER_TOLERANCE) {
|
207 |
myRect = new Rectangle( |
208 |
wrappingRect.x+myTolerance, |
209 |
wrappingRect.y+myTolerance, |
210 |
wrappingRect.width-myTolerance-myTolerance, |
211 |
wrappingRect.height-myTolerance-myTolerance); |
212 |
} else {
|
213 |
myRect = new Rectangle(-1, -1, 0, 0); // just ensure it does not disturb the check |
214 |
} |
215 |
|
216 |
for (int i = 0; i < bi.getWidth(); i++) { |
217 |
for (int j = 0; j < bi.getHeight(); j++) { |
218 |
if (wrappingRect.contains(i,j)
|
219 |
&& !myRect.contains(i,j)) { |
220 |
|
221 |
if (bi.getRGB(i,j) != 0) |
222 |
return true; |
223 |
} |
224 |
} |
225 |
} |
226 |
return false; |
227 |
} |
228 |
|
229 |
private boolean isOutsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) { |
230 |
Rectangle myRect = new Rectangle( |
231 |
wrappingRect.x-tolerance, |
232 |
wrappingRect.y-tolerance, |
233 |
wrappingRect.width+tolerance+tolerance, |
234 |
wrappingRect.height+tolerance+tolerance); |
235 |
for (int i = 0; i < bi.getWidth(); i++) { |
236 |
for (int j = 0; j < bi.getHeight(); j++) { |
237 |
if (!myRect.contains(i,j))
|
238 |
continue;
|
239 |
|
240 |
if (bi.getRGB(i,j) != 0) return false; |
241 |
} |
242 |
} |
243 |
return true; |
244 |
} |
245 |
} |
246 |
|
247 |
/// paste this piece of code to produce some png files to check visually what is happening
|
248 |
//g.setColor(Color.BLUE);
|
249 |
//Rectangle myRect = new Rectangle(
|
250 |
// wrappingRect.x+INNER_TOLERANCE,
|
251 |
// wrappingRect.y+INNER_TOLERANCE,
|
252 |
// wrappingRect.width-INNER_TOLERANCE-INNER_TOLERANCE,
|
253 |
// wrappingRect.height-INNER_TOLERANCE-INNER_TOLERANCE);
|
254 |
//
|
255 |
//g.draw(myRect);
|
256 |
//g.setColor(Color.RED);
|
257 |
//g.draw(wrappingRect);
|
258 |
//try {
|
259 |
//
|
260 |
// File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
|
261 |
// if (!dstDir.exists()) dstDir.mkdir();
|
262 |
// ImageIO.write(bi, "png",
|
263 |
// new File(dstDir.getAbsoluteFile()+File.separator+
|
264 |
// name+"_size_"+sizes[j]
|
265 |
// +".png"));
|
266 |
//} catch (IOException e) {
|
267 |
// e.printStackTrace();
|
268 |
// fail();
|
269 |
//}
|
270 |
///
|