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

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

    
34
import junit.framework.TestCase;
35

    
36
import org.gvsig.compat.CompatLocator;
37
import org.gvsig.fmap.geom.GeometryLocator;
38
import org.gvsig.fmap.geom.GeometryManager;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.exception.CreateGeometryException;
41
import org.gvsig.fmap.geom.primitive.Curve;
42
import org.gvsig.fmap.geom.primitive.GeneralPathX;
43
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
45
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48

    
49
/**
50
 * Integration test to test that Line symbols always draw in the same
51
 * place respecting size constraints.
52
 *
53
 * @author jaume dominguez faus - jaume.dominguez@iver.es
54
 */
55
public class TestDrawLines extends AbstractLibraryAutoInitTestCase {
56
        private GeometryManager geomManager;
57
        private static final Logger logger = LoggerFactory.getLogger(TestDrawLines.class);
58
        private ILineSymbol[] symbols;
59
        private final Dimension sz = new Dimension(401, 401);
60
        private Curve centerL;
61
        
62
        protected void doSetUp() throws Exception {
63
                geomManager = GeometryLocator.getGeometryManager();
64

    
65
                GeneralPathX gp = new GeneralPathX();
66
                gp.moveTo(0, sz.height / 2 );
67
                gp.lineTo(sz.width, sz.height /2 );
68

    
69
                try {
70
                        centerL = geomManager.createCurve(gp, SUBTYPES.GEOM2D);
71
                } catch (CreateGeometryException e) {
72
                        e.printStackTrace();
73
                }
74

    
75
                ISymbol[] allSymbols = TestISymbol.getNewSymbolInstances();
76
                // Filter the marker ones
77
                ArrayList symbols = new ArrayList();
78

    
79
                for (int i = 0; i < allSymbols.length; i++) {
80
                        if (allSymbols[i] instanceof ILineSymbol) {
81
                                ILineSymbol sym = (ILineSymbol) allSymbols[i];
82
                                symbols.add(sym);
83

    
84
                        }
85
                }
86
                this.symbols = (ILineSymbol[]) symbols.toArray(new ILineSymbol[symbols.size()]);
87
        }
88
        private static final double sizes[] = new double[] {
89
                300,
90
                250,
91
                225,
92
                200,
93
                100,
94
                50,
95
                30,
96
                15,
97
                5,
98
                3,
99
                2,
100
                1,
101
                // smaller sizes don't make any sense
102
        };
103

    
104
        private static final float INNER_TOLERANCE = 1F;
105
        private static final float OUTTER_TOLERANCE = 1F;
106

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

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

    
117
                                ILineSymbol testSymbol = symbols[i];
118
                                testSymbol.setLineColor(Color.YELLOW);
119
                                testSymbol.setLineWidth(sizes[j]);
120
                                String name = testSymbol.getClass().getName().substring(
121
                                                testSymbol.getClass().getName().lastIndexOf('.')+1,
122
                                                testSymbol.getClass().getName().length());
123

    
124
                                Point2D upperP1 = new Point2D.Double(0, centerL.getShape().getBounds().getY() - sizes[j]*0.5);
125
                                Point2D upperP2 = new Point2D.Double(centerL.getShape().getBounds().getWidth(), centerL.getShape().getBounds().getY() - sizes[j]*0.5);
126
                                GeneralPathX gpUp = new GeneralPathX();
127
                                gpUp.moveTo(upperP1.getX(), upperP1.getY());
128
                                gpUp.lineTo(upperP2.getX(), upperP2.getY());
129

    
130

    
131
                                GeneralPathX gpDown = new GeneralPathX();
132
                                Point2D lowerP1 = new Point2D.Double(0, centerL.getShape().getBounds().getY() + sizes[j]*0.5);
133
                                Point2D lowerP2 = new Point2D.Double(centerL.getShape().getBounds().getWidth(), centerL.getShape().getBounds().getY() + sizes[j]*0.5);
134
                                gpDown.moveTo(lowerP1.getX(), lowerP1.getY());
135
                                gpDown.lineTo(lowerP2.getX(), lowerP2.getY());
136

    
137

    
138
                                testSymbol.draw(g, new AffineTransform(), centerL, null, null);
139

    
140
                                /// per a borrar
141
//                                g.setStroke(new BasicStroke());
142
//                                g.setColor(Color.RED);
143
//
144
//                                g.draw(gpUp);
145
//
146
//                                g.setColor(Color.BLUE);
147
//                                g.draw(gpDown);
148
//
149
//                                try {
150
//
151
//                                        File dstDir = new File (System.getProperty("java.io.tmpdir")+"/prova-imatges/");
152
//                                        if (!dstDir.exists()) dstDir.mkdir();
153
//                                ImageIO.write(bi, "png",
154
//                                                new File(dstDir.getAbsoluteFile()+File.separator+
155
//                                                                        name+"_size_"+sizes[j]
156
//                                                                                            +".png"));
157
//                                } catch (IOException e) {
158
//                                        e.printStackTrace();
159
//                                        fail();
160
//                                }
161
                                /// fi per a borrar
162
                                assertFalse("fails sizing line, too big ("+name+", "+sizes[j]+"px)", isOutsideRect(bi, upperP1, lowerP1, OUTTER_TOLERANCE ));
163
                                assertTrue("fails sizing line, too small ("+name+", "+sizes[j]+"px) \n" +
164
                                                "\t - forgot to enable ANTIALIASING?", fitsInsideRect(bi, upperP1, lowerP1, INNER_TOLERANCE));
165

    
166
                        }
167
                }
168
        }
169

    
170
        private boolean isOutsideRect(BufferedImage bi, Point2D upper, Point2D lower, float outterTolerance) {
171
                for (int i = 0; i < bi.getWidth(); i++) {
172
                        for (int j = 0; j < bi.getHeight(); j++) {
173
                                if (j<upper.getY()-outterTolerance && j>lower.getY()+outterTolerance)
174
                                        if (bi.getRGB(i,j) != 0) {
175
                                                System.out.println("too big In pixel ("+i+", "+j+")");
176
                                                return true;
177
                                        }
178

    
179
                        }
180
                }
181
                return false;
182
        }
183

    
184
        private boolean fitsInsideRect(BufferedImage bi,Point2D upper, Point2D lower , float innerTolerance) {
185
                for (int i = 0; i < bi.getWidth(); i++) {
186
                        for (int j = 0; j < bi.getHeight(); j++) {
187
                                if (j<upper.getY()+innerTolerance && j>lower.getY()-innerTolerance)
188
                                        if (bi.getRGB(i,j) == 0) {
189
                                                System.out.println("does not fit big In pixel ("+i+", "+j+")");
190
                                                return false;
191
                                        }
192

    
193
                        }
194
                }
195
                return true;
196
        }
197

    
198
}