Statistics
| Revision:

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

History | View | Annotate | Download (7.04 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.Dimension;
27
import java.awt.Graphics2D;
28
import java.awt.geom.AffineTransform;
29
import java.awt.geom.Point2D;
30
import java.awt.image.BufferedImage;
31
import java.io.File;
32
import java.io.IOException;
33
import java.util.ArrayList;
34

    
35
import javax.imageio.ImageIO;
36

    
37
import junit.framework.TestCase;
38

    
39
import org.gvsig.compat.CompatLocator;
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.geom.GeometryLocator;
42
import org.gvsig.fmap.geom.GeometryManager;
43
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
44
import org.gvsig.fmap.geom.exception.CreateGeometryException;
45
import org.gvsig.fmap.geom.primitive.GeneralPathX;
46
import org.gvsig.fmap.mapcontext.MapContext;
47
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
48
import org.gvsig.symbology.fmap.mapcontext.rendering.AllTests;
49
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
50
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54

    
55
/**
56
 * Integration test to test that Fill symbols always draw in the same
57
 * place respecting size constraints.
58
 *
59
 * @author jaume dominguez faus - jaume.dominguez@iver.es
60
 */
61
public class TestDrawFills extends AbstractLibraryAutoInitTestCase {
62
        private GeometryManager geomManager;
63
        private static final Logger logger = LoggerFactory.getLogger(TestDrawFills.class);
64
        private final Dimension sz = new Dimension(400, 400);
65
        private org.gvsig.fmap.geom.primitive.Point centerP = null;
66
        private static final int OUTTER_TOLERANCE = 1;
67
        private static final int INNER_TOLERANCE = 1;
68
        private IFillSymbol[] symbols;
69
        private IDrawFillSymbol[] drawFillSymbols;
70
        private static ArrayList classesToTest;
71

    
72
        private static final double sizes[] = new double[] {
73
                200,
74
                100,
75
                50,
76
                30,
77
                16,
78
                5,
79
                3,
80
                2,
81
                // smaller sizes don't make any sense
82

    
83
        };
84

    
85

    
86
        private static ArrayList getClassesToTest() {
87
                if (classesToTest == null) {
88
                        classesToTest = new ArrayList();
89
//                        TestDrawFills.addSymbolToTest(DrawLineFillSymbol.class);
90
//                        TestDrawFills.addSymbolToTest(DrawPictureFillSymbol.class);
91
                }
92

    
93
                return classesToTest;
94
        }
95

    
96
        public static void addSymbolToTest(Class symbolClass) {
97
        try {
98
            IDrawFillSymbol sym = (IDrawFillSymbol) symbolClass.newInstance();
99
        } catch (InstantiationException e) {
100
            // TODO Auto-generated catch block
101
            fail("Instantiating class, cannot test a non-instantiable symbol");
102
        } catch (IllegalAccessException e) {
103
            // TODO Auto-generated catch block
104
            fail("Class not instantiable");
105
        } catch (ClassCastException ccEx) {
106
            fail("Cannot test a non symbol class");
107
        }
108
        getClassesToTest().add(symbolClass);
109
    }
110

    
111
        protected void doSetUp() throws Exception {
112
                geomManager = GeometryLocator.getGeometryManager();
113
                centerP = geomManager.createPoint(sz.width/2, sz.height/2, SUBTYPES.GEOM2D);
114
                
115
                this.drawFillSymbols = new IDrawFillSymbol[getClassesToTest().size()];
116

    
117
                for (int i = 0; i < drawFillSymbols.length; i++) {
118
                        drawFillSymbols[i] = (IDrawFillSymbol) ((Class) getClassesToTest().get(i)).newInstance();
119
                }
120

    
121
                TestISymbol.addSymbols();
122
                ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
123
                // Filter the marker ones
124
                ArrayList symbols = new ArrayList();
125

    
126
                for (int i = 0; i < allSymbols.length; i++) {
127
                        if (allSymbols[i] instanceof IFillSymbol) {
128
                                IFillSymbol sym = (IFillSymbol) allSymbols[i];
129
                                symbols.add(sym);
130

    
131
                        }
132
                }
133
                this.symbols = (IFillSymbol[]) symbols.toArray(new IFillSymbol[symbols.size()]);
134

    
135

    
136

    
137
        }
138

    
139
        public void testDraw() {
140
                MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION);
141

    
142
        }
143

    
144

    
145
        public final void testNullColorAvoidsFilling() throws CreateGeometryException {
146

    
147
                for (int i = 0; i < symbols.length; i++) {
148

    
149
                        for (int s = 0; s < sizes.length; s++) {
150

    
151
                                BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width,sz.height, BufferedImage.TYPE_INT_ARGB);
152
                                // the graphics for the image, so we can draw onto the buffered image
153
                                Graphics2D g = bi.createGraphics();
154

    
155
                                IFillSymbol newSymbol = symbols[i];
156
                                newSymbol.setFillColor(null);
157
                                newSymbol.setOutline(null);
158

    
159

    
160

    
161

    
162
                                String name = newSymbol.getClass().getName().substring(
163
                                                newSymbol.getClass().getName().lastIndexOf('.')+1,
164
                                                newSymbol.getClass().getName().length());
165

    
166

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

    
170

    
171

    
172
                                GeneralPathX gpx = new GeneralPathX();
173

    
174
                                gpx.moveTo((float)firstPoint.getX(),(float) firstPoint.getY());
175
                                gpx.lineTo((float)lastPoint.getX(),(float) firstPoint.getY());
176
                                gpx.lineTo((float)lastPoint.getX(),(float) lastPoint.getY());
177
                                gpx.lineTo((float)firstPoint.getX(),(float) lastPoint.getY());
178
                                gpx.lineTo((float)firstPoint.getX(), (float)firstPoint.getY());
179
                                gpx.closePath();
180

    
181

    
182
                                for (int j = 0; j < drawFillSymbols.length; j++) {
183
                                        if (drawFillSymbols[j].isSuitableFor(newSymbol)) {
184
                                                newSymbol = drawFillSymbols[j].makeSymbolTransparent(newSymbol);
185
                                                break;
186
                                        }
187
                                }
188

    
189

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

    
192
                                try {
193

    
194
                                        File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
195
                                        if (!dstDir.exists()) dstDir.mkdir();
196
                                        ImageIO.write(bi, "png",
197
                                                        new File(dstDir.getAbsoluteFile()+File.separator+
198
                                                                        name+"_size_"+sizes[s]
199
                                                                                            +".png"));
200
                                } catch (IOException e) {
201
                                        e.printStackTrace();
202
                                        fail();
203
                                }
204

    
205
                                for (int j = 0; j < bi.getWidth(); j++) {
206
                                        for (int k = 0; k < bi.getHeight(); k++) {
207
                                                if (isInsideShape(geomManager.createSurface(gpx, SUBTYPES.GEOM2D), j, k)) {
208
                                                        assertEquals("Failed, the fill is being painted when the color is null",bi.getRGB(j, k),0);
209

    
210
                                                }
211
                                        }
212

    
213
                                }
214
                        }
215
                }
216
        }
217

    
218

    
219
        private boolean isInsideShape(Geometry geom, int x, int y) {
220
                Point2D p = new Point2D.Float(x,y);
221
                if(geom.getShape().contains(p))return true;
222
                return false;
223
        }
224

    
225

    
226
}