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