Statistics
| Revision:

root / 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 / TestDrawFills.java @ 34294

History | View | Annotate | Download (7.28 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.Dimension;
44
import java.awt.Graphics2D;
45
import java.awt.geom.AffineTransform;
46
import java.awt.geom.Point2D;
47
import java.awt.image.BufferedImage;
48
import java.io.File;
49
import java.io.IOException;
50
import java.util.ArrayList;
51

    
52
import javax.imageio.ImageIO;
53

    
54
import junit.framework.TestCase;
55

    
56
import org.gvsig.compat.CompatLocator;
57
import org.gvsig.fmap.geom.Geometry;
58
import org.gvsig.fmap.geom.GeometryLocator;
59
import org.gvsig.fmap.geom.GeometryManager;
60
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
61
import org.gvsig.fmap.geom.exception.CreateGeometryException;
62
import org.gvsig.fmap.geom.primitive.GeneralPathX;
63
import org.gvsig.fmap.mapcontext.MapContext;
64
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
65
import org.gvsig.symbology.fmap.mapcontext.rendering.AllTests;
66
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
67
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
68
import org.slf4j.Logger;
69
import org.slf4j.LoggerFactory;
70

    
71

    
72
/**
73
 * Integration test to test that Fill symbols always draw in the same
74
 * place respecting size constraints.
75
 *
76
 * @author jaume dominguez faus - jaume.dominguez@iver.es
77
 */
78
public class TestDrawFills extends AbstractLibraryAutoInitTestCase {
79
        private GeometryManager geomManager;
80
        private static final Logger logger = LoggerFactory.getLogger(TestDrawFills.class);
81
        private final Dimension sz = new Dimension(400, 400);
82
        private org.gvsig.fmap.geom.primitive.Point centerP = null;
83
        private static final int OUTTER_TOLERANCE = 1;
84
        private static final int INNER_TOLERANCE = 1;
85
        private IFillSymbol[] symbols;
86
        private IDrawFillSymbol[] drawFillSymbols;
87
        private static ArrayList classesToTest;
88

    
89
        private static final double sizes[] = new double[] {
90
                200,
91
                100,
92
                50,
93
                30,
94
                16,
95
                5,
96
                3,
97
                2,
98
                // smaller sizes don't make any sense
99

    
100
        };
101

    
102

    
103
        private static ArrayList getClassesToTest() {
104
                if (classesToTest == null) {
105
                        classesToTest = new ArrayList();
106
//                        TestDrawFills.addSymbolToTest(DrawLineFillSymbol.class);
107
//                        TestDrawFills.addSymbolToTest(DrawPictureFillSymbol.class);
108
                }
109

    
110
                return classesToTest;
111
        }
112

    
113
        public static void addSymbolToTest(Class symbolClass) {
114
        try {
115
            IDrawFillSymbol sym = (IDrawFillSymbol) symbolClass.newInstance();
116
        } catch (InstantiationException e) {
117
            // TODO Auto-generated catch block
118
            fail("Instantiating class, cannot test a non-instantiable symbol");
119
        } catch (IllegalAccessException e) {
120
            // TODO Auto-generated catch block
121
            fail("Class not instantiable");
122
        } catch (ClassCastException ccEx) {
123
            fail("Cannot test a non symbol class");
124
        }
125
        getClassesToTest().add(symbolClass);
126
    }
127

    
128
        protected void doSetUp() throws Exception {
129
                geomManager = GeometryLocator.getGeometryManager();
130
                centerP = geomManager.createPoint(sz.width/2, sz.height/2, SUBTYPES.GEOM2D);
131
                
132
                this.drawFillSymbols = new IDrawFillSymbol[getClassesToTest().size()];
133

    
134
                for (int i = 0; i < drawFillSymbols.length; i++) {
135
                        drawFillSymbols[i] = (IDrawFillSymbol) ((Class) getClassesToTest().get(i)).newInstance();
136
                }
137

    
138
                TestISymbol.addSymbols();
139
                ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
140
                // Filter the marker ones
141
                ArrayList symbols = new ArrayList();
142

    
143
                for (int i = 0; i < allSymbols.length; i++) {
144
                        if (allSymbols[i] instanceof IFillSymbol) {
145
                                IFillSymbol sym = (IFillSymbol) allSymbols[i];
146
                                symbols.add(sym);
147

    
148
                        }
149
                }
150
                this.symbols = (IFillSymbol[]) symbols.toArray(new IFillSymbol[symbols.size()]);
151

    
152

    
153

    
154
        }
155

    
156
        public void testDraw() {
157
                MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION);
158

    
159
        }
160

    
161

    
162
        public final void testNullColorAvoidsFilling() throws CreateGeometryException {
163

    
164
                for (int i = 0; i < symbols.length; i++) {
165

    
166
                        for (int s = 0; s < sizes.length; s++) {
167

    
168
                                BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width,sz.height, BufferedImage.TYPE_INT_ARGB);
169
                                // the graphics for the image, so we can draw onto the buffered image
170
                                Graphics2D g = bi.createGraphics();
171

    
172
                                IFillSymbol newSymbol = symbols[i];
173
                                newSymbol.setFillColor(null);
174
                                newSymbol.setOutline(null);
175

    
176

    
177

    
178

    
179
                                String name = newSymbol.getClass().getName().substring(
180
                                                newSymbol.getClass().getName().lastIndexOf('.')+1,
181
                                                newSymbol.getClass().getName().length());
182

    
183

    
184
                                Point2D firstPoint = new Point2D.Float((float)(centerP.getX()-sizes[s]/2-3),(float)(centerP.getY()-sizes[s]/2-3));
185
                                Point2D lastPoint = new Point2D.Float((float)(centerP.getX()+sizes[s]/2+3),(float)(centerP.getY()+sizes[s]/2+3));
186

    
187

    
188

    
189
                                GeneralPathX gpx = new GeneralPathX();
190

    
191
                                gpx.moveTo((float)firstPoint.getX(),(float) firstPoint.getY());
192
                                gpx.lineTo((float)lastPoint.getX(),(float) firstPoint.getY());
193
                                gpx.lineTo((float)lastPoint.getX(),(float) lastPoint.getY());
194
                                gpx.lineTo((float)firstPoint.getX(),(float) lastPoint.getY());
195
                                gpx.lineTo((float)firstPoint.getX(), (float)firstPoint.getY());
196
                                gpx.closePath();
197

    
198

    
199
                                for (int j = 0; j < drawFillSymbols.length; j++) {
200
                                        if (drawFillSymbols[j].isSuitableFor(newSymbol)) {
201
                                                newSymbol = drawFillSymbols[j].makeSymbolTransparent(newSymbol);
202
                                                break;
203
                                        }
204
                                }
205

    
206

    
207
                                newSymbol.draw(g, new AffineTransform(), geomManager.createSurface(gpx, SUBTYPES.GEOM2D), null, null);
208

    
209
                                try {
210

    
211
                                        File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
212
                                        if (!dstDir.exists()) dstDir.mkdir();
213
                                        ImageIO.write(bi, "png",
214
                                                        new File(dstDir.getAbsoluteFile()+File.separator+
215
                                                                        name+"_size_"+sizes[s]
216
                                                                                            +".png"));
217
                                } catch (IOException e) {
218
                                        e.printStackTrace();
219
                                        fail();
220
                                }
221

    
222
                                for (int j = 0; j < bi.getWidth(); j++) {
223
                                        for (int k = 0; k < bi.getHeight(); k++) {
224
                                                if (isInsideShape(geomManager.createSurface(gpx, SUBTYPES.GEOM2D), j, k)) {
225
                                                        assertEquals("Failed, the fill is being painted when the color is null",bi.getRGB(j, k),0);
226

    
227
                                                }
228
                                        }
229

    
230
                                }
231
                        }
232
                }
233
        }
234

    
235

    
236
        private boolean isInsideShape(Geometry geom, int x, int y) {
237
                Point2D p = new Point2D.Float(x,y);
238
                if(geom.contains(p))return true;
239
                return false;
240
        }
241

    
242

    
243
}