Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src-test / org / gvsig / fmap / mapcontext / rendering / symbol / TestDrawLines.java @ 29313

History | View | Annotate | Download (6.65 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.symbol;
42

    
43
import java.awt.Color;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.geom.AffineTransform;
47
import java.awt.geom.Point2D;
48
import java.awt.image.BufferedImage;
49
import java.util.ArrayList;
50

    
51
import junit.framework.TestCase;
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.geom.exception.CreateGeometryException;
58
import org.gvsig.fmap.geom.primitive.Curve;
59
import org.gvsig.fmap.geom.primitive.GeneralPathX;
60
import org.gvsig.fmap.geom.util.UtilFunctions;
61
import org.gvsig.fmap.mapcontext.rendering.symbols.ILineSymbol;
62
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
63
import org.slf4j.Logger;
64
import org.slf4j.LoggerFactory;
65

    
66
/**
67
 * Integration test to test that Line symbols always draw in the same
68
 * place respecting size constraints.
69
 *
70
 * @author jaume dominguez faus - jaume.dominguez@iver.es
71
 */
72
public class TestDrawLines extends TestCase {
73
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
74
        private static final Logger logger = LoggerFactory.getLogger(TestDrawLines.class);
75
        private ILineSymbol[] symbols;
76
        private final Dimension sz = new Dimension(401, 401);
77
        private Curve centerL;
78

    
79
        {
80
                GeneralPathX gp = new GeneralPathX();
81
                gp.moveTo(0, sz.height / 2 );
82
                gp.lineTo(sz.width, sz.height /2 );
83

    
84
                try {
85
                        centerL = geomManager.createCurve(gp, SUBTYPES.GEOM2D);
86
                } catch (CreateGeometryException e) {
87
                        e.printStackTrace();
88
                }
89
        }
90
        private static final double sizes[] = new double[] {
91
                300,
92
                250,
93
                225,
94
                200,
95
                100,
96
                50,
97
                30,
98
                15,
99
                5,
100
                3,
101
                2,
102
                1,
103
                // smaller sizes don't make any sense
104
        };
105

    
106
        private static final float INNER_TOLERANCE = 1F;
107
        private static final float OUTTER_TOLERANCE = 1F;
108

    
109
        protected void setUp() throws Exception {
110
                // get all the symbols in the Test Suite
111
                super.setUp();
112

    
113
                ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
114
                // Filter the marker ones
115
                ArrayList symbols = new ArrayList();
116

    
117
                for (int i = 0; i < allSymbols.length; i++) {
118
                        if (allSymbols[i] instanceof ILineSymbol) {
119
                                ILineSymbol sym = (ILineSymbol) allSymbols[i];
120
                                symbols.add(sym);
121

    
122
                        }
123
                }
124
                this.symbols = (ILineSymbol[]) symbols.toArray(new ILineSymbol[symbols.size()]);
125
        }
126

    
127
        public void testDrawingSize() {
128
                for (int i = 0; i < symbols.length; i++) {
129
                        for (int j = 0; j < sizes.length; j++) {
130
                                // new blank buffered image
131
                                
132
                                BufferedImage bi = CompatLocator.getGraphicsUtils().createBufferedImage(sz.width, sz.height, BufferedImage.TYPE_INT_ARGB);
133

    
134
                                // the graphics for the image, so we can draw onto the buffered image
135
                                Graphics2D g = bi.createGraphics();
136

    
137
                                ILineSymbol testSymbol = symbols[i];
138
                                testSymbol.setLineColor(Color.YELLOW);
139
                                testSymbol.setLineWidth(sizes[j]);
140
                                String name = testSymbol.getClassName().substring(
141
                                                testSymbol.getClassName().lastIndexOf('.')+1,
142
                                                testSymbol.getClassName().length());
143

    
144
                                Point2D upperP1 = new Point2D.Double(0, centerL.getBounds().getY() - sizes[j]*0.5);
145
                                Point2D upperP2 = new Point2D.Double(centerL.getBounds().getWidth(), centerL.getBounds().getY() - sizes[j]*0.5);
146
                                GeneralPathX gpUp = new GeneralPathX();
147
                                gpUp.moveTo(upperP1.getX(), upperP1.getY());
148
                                gpUp.lineTo(upperP2.getX(), upperP2.getY());
149

    
150

    
151
                                GeneralPathX gpDown = new GeneralPathX();
152
                                Point2D lowerP1 = new Point2D.Double(0, centerL.getBounds().getY() + sizes[j]*0.5);
153
                                Point2D lowerP2 = new Point2D.Double(centerL.getBounds().getWidth(), centerL.getBounds().getY() + sizes[j]*0.5);
154
                                gpDown.moveTo(lowerP1.getX(), lowerP1.getY());
155
                                gpDown.lineTo(lowerP2.getX(), lowerP2.getY());
156

    
157

    
158
                                testSymbol.draw(g, new AffineTransform(), centerL, null);
159

    
160
                                /// per a borrar
161
//                                g.setStroke(new BasicStroke());
162
//                                g.setColor(Color.RED);
163
//
164
//                                g.draw(gpUp);
165
//
166
//                                g.setColor(Color.BLUE);
167
//                                g.draw(gpDown);
168
//
169
//                                try {
170
//
171
//                                        File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
172
//                                        if (!dstDir.exists()) dstDir.mkdir();
173
//                                ImageIO.write(bi, "png",
174
//                                                new File(dstDir.getAbsoluteFile()+File.separator+
175
//                                                                        name+"_size_"+sizes[j]
176
//                                                                                            +".png"));
177
//                                } catch (IOException e) {
178
//                                        e.printStackTrace();
179
//                                        fail();
180
//                                }
181
                                /// fi per a borrar
182
                                assertFalse("fails sizing line, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, upperP1, lowerP1, OUTTER_TOLERANCE ));
183
                                assertTrue("fails sizing line, too small ("+name+", "+sizes[j]+"px) \n" +
184
                                                "\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, upperP1, lowerP1, INNER_TOLERANCE));
185

    
186
                        }
187
                }
188
        }
189

    
190
        private boolean isOutsideRect(BufferedImage bi, Point2D upper, Point2D lower, float outterTolerance) {
191
                for (int i = 0; i < bi.getWidth(); i++) {
192
                        for (int j = 0; j < bi.getHeight(); j++) {
193
                                if (j<upper.getY()-outterTolerance && j>lower.getY()+outterTolerance)
194
                                        if (bi.getRGB(i,j) != 0) {
195
                                                System.out.println("too big In pixel ("+i+", "+j+")");
196
                                                return true;
197
                                        }
198

    
199
                        }
200
                }
201
                return false;
202
        }
203

    
204
        private boolean fitsInsideRect(BufferedImage bi,Point2D upper, Point2D lower , float innerTolerance) {
205
                for (int i = 0; i < bi.getWidth(); i++) {
206
                        for (int j = 0; j < bi.getHeight(); j++) {
207
                                if (j<upper.getY()+innerTolerance && j>lower.getY()-innerTolerance)
208
                                        if (bi.getRGB(i,j) == 0) {
209
                                                System.out.println("does not fit big In pixel ("+i+", "+j+")");
210
                                                return false;
211
                                        }
212

    
213
                        }
214
                }
215
                return true;
216
        }
217

    
218
}