Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / mapcontext / rendering / symbols / TestDrawMarkers.java @ 20984

History | View | Annotate | Download (7.68 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.symbols;
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.io.File;
51
import java.io.IOException;
52
import java.util.ArrayList;
53
import java.util.Random;
54

    
55
import javax.imageio.ImageIO;
56

    
57
import org.gvsig.fmap.core.shapes.FPoint2D;
58
import org.gvsig.fmap.mapcontext.rendering.symbols.IMarkerSymbol;
59
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
60

    
61

    
62
import junit.framework.TestCase;
63

    
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 IMarkerSymbol[] symbols;
73
        private final Dimension sz = new Dimension(400, 400);
74
        private final FPoint2D centerP = new FPoint2D(sz.width/2, sz.height/2);
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

    
92

    
93
        protected void setUp() throws Exception {
94
                // get all the symbols in the Test Suite
95
                super.setUp();
96

    
97
                ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
98
                // Filter the marker ones
99
                ArrayList symbols = new ArrayList();
100

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

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

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

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

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

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

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

    
135
//                                if (testSymbol instanceof CharacterMarkerSymbol)
136
//                                        continue;
137
                                
138
                                
139

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

    
145
                                
146
                        }
147
                }
148
        }
149

    
150
        public void testDrawingOffset() {
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 = new BufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
156

    
157
                                // the graphics for the image, so we can draw onto the buffered image
158
                                Graphics2D g = bi.createGraphics();
159

    
160
                                IMarkerSymbol testSymbol = symbols[i];
161
                                testSymbol.setColor(Color.YELLOW);
162
                                testSymbol.setSize(sizes[j]);
163

    
164
                                String name = testSymbol.getClassName().substring(
165
                                                testSymbol.getClassName().lastIndexOf('.')+1,
166
                                                testSymbol.getClassName().length());
167

    
168
                                double xOffset = (random.nextDouble()-0.5)*sz.width;
169
                                double yOffset = (random.nextDouble()-0.5)*sz.height;
170

    
171
                                testSymbol.setOffset(new Point2D.Double(xOffset, yOffset));
172

    
173
                                Rectangle wrappingRect = new Rectangle(
174
                                                (int) ((centerP.getX()-sizes[j]/2) + xOffset),
175
                                                (int) ((centerP.getY()-sizes[j]/2) + yOffset),
176
                                                (int) (sizes[j]),
177
                                                (int) (sizes[j]));
178

    
179
//                                if (testSymbol instanceof CharacterMarkerSymbol)
180
//                                        continue;
181
                                
182
                                
183

    
184
                                testSymbol.draw(g, new AffineTransform(), centerP, null);
185
                                assertFalse("fails sizing marker, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, wrappingRect, OUTTER_TOLERANCE ));
186
                                assertTrue("fails sizing marker, too small ("+name+", "+sizes[j]+"px) \n" +
187
                                                "\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, wrappingRect, INNER_TOLERANCE));
188

    
189
                        }
190
                }
191
        }
192
        
193
        public void testDrawingRotation() {
194
                // TODO implement it
195
                
196
        }
197

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

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

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

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

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

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