Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / test / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / TestDrawMarkers.java @ 34294

History | View | Annotate | Download (8.27 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.symbology.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 org.gvsig.compat.CompatLocator;
54
import org.gvsig.fmap.geom.GeometryLocator;
55
import org.gvsig.fmap.geom.GeometryManager;
56
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
57
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
58
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol;
59
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

    
63
/**
64
 * Integration test to test that Marker symbols always draw in the same
65
 * place respecting size constraints.
66
 *
67
 * @author jaume dominguez faus - jaume.dominguez@iver.es
68
 */
69
public class TestDrawMarkers extends AbstractLibraryAutoInitTestCase {
70
        private GeometryManager geomManager;
71
        private static final Logger logger = LoggerFactory.getLogger(TestDrawMarkers.class);
72
        private IMarkerSymbol[] symbols;
73
        private final Dimension sz = new Dimension(400, 400);
74
        private org.gvsig.fmap.geom.primitive.Point centerP = null;
75
        private static final int OUTTER_TOLERANCE = 1;
76
        private static final int INNER_TOLERANCE = 1;
77

    
78
        private static final double sizes[] = new double[] {
79
                200,
80
                100,
81
                50,
82
                30,
83
                16,
84
                5,
85
                3,
86
                2,
87
                // smaller sizes don't make any sense
88

    
89
        };
90

    
91
        protected void doSetUp() throws Exception {
92
                geomManager = GeometryLocator.getGeometryManager();
93

    
94
                centerP = geomManager.createPoint(sz.width/2, sz.height/2, SUBTYPES.GEOM2D);
95
                
96
                ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
97
                // Filter the marker ones
98
                ArrayList symbols = new ArrayList();
99

    
100
                for (int i = 0; i < allSymbols.length; i++) {
101
                        if (allSymbols[i] instanceof IMarkerSymbol) {
102
                                IMarkerSymbol sym = (IMarkerSymbol) allSymbols[i];
103
                                symbols.add(sym);
104

    
105
                        }
106
                }
107
                this.symbols = (IMarkerSymbol[]) symbols.toArray(new IMarkerSymbol[symbols.size()]);
108
        }
109

    
110
        public void testDrawingSize() {
111
                for (int i = 0; i < symbols.length; i++) {
112
                        for (int j = 0; j < sizes.length; j++) {
113
                                // new blank buffered image
114
                                BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
115

    
116
                                // the graphics for the image, so we can draw onto the buffered image
117
                                Graphics2D g = bi.createGraphics();
118

    
119
                                IMarkerSymbol testSymbol = symbols[i];
120
                                testSymbol.setColor(Color.YELLOW);
121
                                testSymbol.setSize(sizes[j]);
122
                                String name = testSymbol.getClass().getName().substring(
123
                                                testSymbol.getClass().getName().lastIndexOf('.')+1,
124
                                                testSymbol.getClass().getName().length());
125

    
126
                                testSymbol.draw(g, new AffineTransform(), centerP, null, null);
127

    
128
                                Rectangle wrappingRect = new Rectangle(
129
                                                (int) (centerP.getX()-sizes[j]/2),
130
                                                (int) (centerP.getY()-sizes[j]/2),
131
                                                (int) (sizes[j]),
132
                                                (int) (sizes[j]));
133

    
134
//                                if (testSymbol instanceof CharacterMarkerSymbol)
135
//                                        continue;
136

    
137

    
138

    
139

    
140
                                assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE ));
141
                                assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" +
142
                                                "\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
143

    
144

    
145
                        }
146
                }
147
        }
148

    
149
        public void testDrawingOffset() {
150
                // TODO: uncomment this test and make it run successfully
151
//                Random random = new Random(System.currentTimeMillis());
152
//                for (int i = 0; i < symbols.length; i++) {
153
//                        for (int j = 0; j < sizes.length; j++) {
154
//                                // new blank buffered image
155
//                                BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
156
//
157
//
158
//                                // the graphics for the image, so we can draw onto the buffered image
159
//                                Graphics2D g = bi.createGraphics();
160
//
161
//                                IMarkerSymbol testSymbol = symbols[i];
162
//                                testSymbol.setColor(Color.YELLOW);
163
//                                testSymbol.setSize(sizes[j]);
164
//
165
//                                String name = testSymbol.getClass().getName().substring(
166
//                                                testSymbol.getClass().getName().lastIndexOf('.')+1,
167
//                                                testSymbol.getClass().getName().length());
168
//
169
//                                double xOffset = (random.nextDouble()-0.5)*sz.width;
170
//                                double yOffset = (random.nextDouble()-0.5)*sz.height;
171
//
172
//                                testSymbol.setOffset(new Point2D.Double(xOffset, yOffset));
173
//
174
//                                Rectangle wrappingRect = new Rectangle(
175
//                                                (int) ((centerP.getX()-sizes[j]/2) + xOffset),
176
//                                                (int) ((centerP.getY()-sizes[j]/2) + yOffset),
177
//                                                (int) (sizes[j]),
178
//                                                (int) (sizes[j]));
179
//
180
////                                if (testSymbol instanceof CharacterMarkerSymbol)
181
////                                        continue;
182
//
183
//
184
//
185
//                                testSymbol.draw(g, new AffineTransform(), centerP, null, null);
186
//                                assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE ));
187
//                                assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" +
188
//                                                "\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
189
//
190
//                        }
191
//                }
192
        }
193

    
194
        public void testDrawingRotation() {
195
                // TODO implement it
196

    
197
        }
198

    
199
        private boolean fitsInsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) {
200
                int myTolerance = Math.min(tolerance, wrappingRect.width);
201
                Rectangle myRect;
202
                if (myTolerance >= INNER_TOLERANCE) {
203
                        myRect = new Rectangle(
204
                                        wrappingRect.x+myTolerance,
205
                                        wrappingRect.y+myTolerance,
206
                                        wrappingRect.width-myTolerance-myTolerance,
207
                                        wrappingRect.height-myTolerance-myTolerance);
208
                } else {
209
                        myRect = new Rectangle(-1, -1, 0, 0); // just ensure it does not disturb the check
210
                }
211

    
212
                for (int i = 0; i < bi.getWidth(); i++) {
213
                        for (int j = 0; j < bi.getHeight(); j++) {
214
                                if (wrappingRect.contains(i,j)
215
                                                && !myRect.contains(i,j)) {
216

    
217
                                        if (bi.getRGB(i,j) != 0)
218
                                                return true;
219
                                }
220
                        }
221
                }
222
                return false;
223
        }
224

    
225
        private boolean isOutsideRect(BufferedImage bi, Rectangle wrappingRect, int tolerance) {
226
                Rectangle myRect = new Rectangle(
227
                                wrappingRect.x-tolerance,
228
                                wrappingRect.y-tolerance,
229
                                wrappingRect.width+tolerance+tolerance,
230
                                wrappingRect.height+tolerance+tolerance);
231
                for (int i = 0; i < bi.getWidth(); i++) {
232
                        for (int j = 0; j < bi.getHeight(); j++) {
233
                                if (!myRect.contains(i,j))
234
                                        continue;
235

    
236
                                if (bi.getRGB(i,j) != 0) return false;
237
                        }
238
                }
239
                return true;
240
        }
241
}
242

    
243
/// paste this piece of code to produce some png files to check visually what is happening
244
//g.setColor(Color.BLUE);
245
//Rectangle myRect = new Rectangle(
246
//                wrappingRect.x+INNER_TOLERANCE,
247
//                wrappingRect.y+INNER_TOLERANCE,
248
//                wrappingRect.width-INNER_TOLERANCE-INNER_TOLERANCE,
249
//                wrappingRect.height-INNER_TOLERANCE-INNER_TOLERANCE);
250
//
251
//g.draw(myRect);
252
//g.setColor(Color.RED);
253
//g.draw(wrappingRect);
254
//try {
255
//
256
//        File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
257
//        if (!dstDir.exists()) dstDir.mkdir();
258
//        ImageIO.write(bi, "png",
259
//                        new File(dstDir.getAbsoluteFile()+File.separator+
260
//                                        name+"_size_"+sizes[j]
261
//                                                            +".png"));
262
//} catch (IOException e) {
263
//        e.printStackTrace();
264
//        fail();
265
//}
266
///