Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / AbstractLineSymbol.java @ 18621

History | View | Annotate | Download (9.42 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: AbstractLineSymbol.java 18621 2008-02-08 07:59:54Z jdominguez $
45
* $Log$
46
* Revision 1.14  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.13  2007/09/19 16:25:47  caballero
50
* FFrameLegend
51
*
52
* Revision 1.11  2007/09/18 14:50:31  caballero
53
* Leyendas sobre el Layout
54
*
55
* Revision 1.10  2007/09/17 09:33:47  jaume
56
* some multishapedsymbol bugs fixed
57
*
58
* Revision 1.9  2007/08/13 11:36:50  jvidal
59
* javadoc
60
*
61
* Revision 1.8  2007/07/18 06:54:34  jaume
62
* continuing with cartographic support
63
*
64
* Revision 1.7  2007/07/03 10:58:29  jaume
65
* first refactor on CartographicSupport
66
*
67
* Revision 1.6  2007/06/29 13:07:01  jaume
68
* +PictureLineSymbol
69
*
70
* Revision 1.5  2007/05/08 08:47:39  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.4  2007/03/20 16:00:00  jaume
74
* rename method
75
*
76
* Revision 1.3  2007/03/09 11:20:56  jaume
77
* Advanced symbology (start committing)
78
*
79
* Revision 1.2.2.3  2007/02/21 16:09:02  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2.2.2  2007/02/15 16:23:44  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.2.2.1  2007/02/09 07:47:04  jaume
86
* Isymbol moved
87
*
88
* Revision 1.2  2007/01/24 17:58:22  jaume
89
* new features and architecture error fixes
90
*
91
* Revision 1.1  2007/01/16 11:50:44  jaume
92
* *** empty log message ***
93
*
94
*
95
*/
96
package com.iver.cit.gvsig.fmap.core.symbols;
97

    
98
import java.awt.Color;
99
import java.awt.Graphics2D;
100
import java.awt.Rectangle;
101
import java.awt.geom.AffineTransform;
102
import java.awt.geom.PathIterator;
103
import java.awt.geom.Point2D;
104
import java.util.ArrayList;
105

    
106
import com.iver.cit.gvsig.fmap.ViewPort;
107
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
108
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
109
import com.iver.cit.gvsig.fmap.core.FShape;
110
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
111
import com.iver.cit.gvsig.fmap.core.IGeometry;
112
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
113
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
114
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
115

    
116
/**
117
 * AbstractLineSymbol is the class that implements the interface for line symbols.
118
 * It is considered as the father of all the XXXLineSymbols and will implement all the
119
 * methods that these classes had not developed (and correspond with one of the methods
120
 * of AbstractLineSymbol class).
121
 *
122
 *
123
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
124
 */
125
public abstract class AbstractLineSymbol extends AbstractSymbol implements ILineSymbol {
126
        private Color color = SymbologyFactory.DefaultSymbolColor;
127
        private ILineStyle lineStyle;
128

    
129

    
130
        public Color getColor() {
131
                return color;
132
        }
133

    
134
        public void setLineColor(Color color) {
135
                this.color = color;
136
        }
137

    
138
         public int getOnePointRgb() {
139
                return color.getRGB();
140
        }
141

    
142
        public final int getSymbolType() {
143
                return FShape.LINE;
144
        }
145

    
146
        public void getPixExtentPlus(FShape shp, float[] distances, ViewPort viewPort, int dpi) {
147
                float cs = (float) getCartographicSize(viewPort, dpi, shp);
148
                // TODO and add the line offset
149
                distances[0] = cs;
150
                distances[1] = cs;
151
        }
152

    
153
        public boolean isSuitableFor(IGeometry geom) {
154
                switch(geom.getGeometryType()) {
155
                // pasted from FSymbol
156
                case FShape.LINE:
157
                case FShape.LINE + FShape.Z:
158
                case FShape.ARC:
159
                case FShape.ARC + FShape.Z:
160
                        return true;
161
                }
162
                return false;
163
        }
164

    
165
        public ILineStyle getLineStyle() {
166
                if (lineStyle == null)
167
                        lineStyle = new SimpleLineStyle();
168
                return lineStyle;
169
        }
170

    
171
        public void setLineStyle(ILineStyle lineStyle) {
172
                this.lineStyle = lineStyle;
173
        }
174

    
175
        public int getAlpha() {
176
                return color.getAlpha();
177
        }
178

    
179
        public void setAlpha(int outlineAlpha) {
180
                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), outlineAlpha);
181
        }
182

    
183
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
184
                final int hGap = (int) (r.getWidth() * 0.1); // the left and right margins
185
                final int vPos = 1;                                                  // the top and bottom margins
186
                final int splitCount = 3;                                          // number of lines
187
                final int splitHSize = (r.width - hGap - hGap) / splitCount;
188
                int hPos = hGap;
189
                boolean swap = false;
190
                
191
                GeneralPathX gpx = new GeneralPathX();
192
                gpx.moveTo(r.x + hPos, r.y + r.height-vPos);
193

    
194
                for (int i = 0; i < splitCount; i++) {
195
                        swap = !swap;
196
                        gpx.lineTo(r.x + hPos + splitHSize, (swap ? vPos : r.height-vPos) + r.y);
197
                        hPos += splitHSize;
198
                }
199

    
200
                
201
                draw(g, new AffineTransform(), new FPolygon2D(gpx), null);
202
        }
203

    
204

    
205

    
206
        public void setCartographicSize(double cartographicSize, FShape shp) {
207
                getLineStyle().setLineWidth((float) cartographicSize);
208
        }
209

    
210
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
211
                double oldSize = getLineWidth();
212
                setCartographicSize(getCartographicSize(
213
                                                                viewPort,
214
                                                                dpi,
215
                                                                shp),
216
                                                        shp);
217
                return oldSize;
218
        }
219

    
220
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
221
                return CartographicSupportToolkit.
222
                                        getCartographicLength(this,
223
                                                                                  getLineWidth(),
224
                                                                                  viewPort,
225
                                                                                  dpi);
226
        }
227

    
228
        public static FShape offsetFShape(FShape shp, double offset) {
229
                FShape offsetFShape = null;
230
                if (shp != null) {
231
                        if (offset == 0)
232
                                return shp;
233
                        
234
                        ArrayList<Point2D[]> segments = new ArrayList<Point2D[]>();
235
                        GeneralPathX gpx = new GeneralPathX(shp);
236
                        PathIterator it = gpx.getPathIterator(null);
237
                        double[] data = new double[6];
238
                        Point2D segmentIni = null;
239
                        Point2D segmentEnd = null;
240
                        while (!it.isDone()) {
241
                                switch (it.currentSegment(data)) {
242
                                case PathIterator.SEG_MOVETO:
243
                                        segmentEnd = new Point2D.Double(
244
                                                        data[0], data[1]);
245
                                        break;
246

    
247
                                case PathIterator.SEG_LINETO:
248
                                        segmentEnd = segmentIni;
249
                                        segmentIni = new Point2D.Double(
250
                                                        data[0], data[1]);
251
                                        
252
                                        segments.add(getParallel(segmentIni, segmentEnd, offset));
253
                                        break;
254

    
255
                                case PathIterator.SEG_QUADTO:
256
                                        break;
257

    
258
                                case PathIterator.SEG_CUBICTO:
259
                                        break;
260

    
261
                                case PathIterator.SEG_CLOSE:
262
                                        break;
263
                                }
264
                        }
265
                        
266
                }
267
                
268
                
269
                return offsetFShape;
270
        }
271
        
272
        private static Point2D[] getParallel(Point2D p1, Point2D p2, double distance) {
273
                Point2D[] pParallel=new Point2D[2];
274
                pParallel[0]=getPerpendicularPoint(p1,p2,p1,distance);
275
                pParallel[1]=getPerpendicularPoint(p1,p2,p2,distance);
276
                return pParallel;
277
        }
278

    
279
        /**
280
         * Obtiene el punto que se encuentra a una distancia 'dist' de la recta
281
         * p1-p2 y se encuentra en la recta perpendicular que pasa por perpPoint
282
         *
283
         * @param p1 Punto de la recta p1-p2
284
         * @param p2 Punto de la recta p1-p2
285
         * @param perpPoint Punto de la recta perpendicular
286
         * @param dist Distancia del punto que se quiere obtener a la recta p1-p2
287
         *
288
         * @return DOCUMENT ME!
289
         */
290
        private static Point2D getPerpendicularPoint(Point2D p1, Point2D p2,
291
                Point2D perpPoint, double dist) {
292
                Point2D[] p = getPerpendicular(p1, p2, perpPoint);
293
                Point2D unit = getUnitVector(p[0], p[1]);
294

    
295
                return new Point2D.Double(perpPoint.getX() + (unit.getX() * dist),
296
                        perpPoint.getY() + (unit.getY() * dist));
297
        }
298
        
299
        
300
        /**
301
         * Obtiene un par de puntos que definen la recta perpendicular a p1-p2 que
302
         * pasa por el punto perp
303
         *
304
         * @param p1 punto de la recta p1-p2
305
         * @param p2 punto de la recta p1-p2
306
         * @param perp Punto por el que pasa la recta perpendicular, debe ser
307
         *                   distinto a p2
308
         *
309
         * @return Array con dos puntos que definen la recta resultante
310
         */
311
        private static Point2D[] getPerpendicular(Point2D p1, Point2D p2,
312
                Point2D perp) {
313
                if ((p2.getY() - p1.getY()) == 0) {
314
                        return new Point2D[] {
315
                                new Point2D.Double(perp.getX(), 0),
316
                                new Point2D.Double(perp.getX(), 1)
317
                        };
318
                }
319

    
320
                //Pendiente de la recta perpendicular
321
                double m = (p1.getX() - p2.getX()) / (p2.getY() - p1.getY());
322

    
323
                //b de la funcion de la recta perpendicular
324
                double b = perp.getY() - (m * perp.getX());
325

    
326
                //Obtenemos un par de puntos
327
                Point2D[] res = new Point2D[2];
328

    
329
                res[0] = new Point2D.Double(0, (m * 0) + b);
330
                res[1] = new Point2D.Double(1000, (m * 1000) + b);
331

    
332
                return res;
333
        }
334
        
335
        /**
336
         * Devuelve un vector unitario en forma de punto a partir de dos puntos.
337
         *
338
         * @param p1 punto origen.
339
         * @param p2 punto destino.
340
         *
341
         * @return vector unitario.
342
         */
343
        private static Point2D getUnitVector(Point2D p1, Point2D p2) {
344
                Point2D paux = new Point2D.Double(p2.getX() - p1.getX(),
345
                                p2.getY() - p1.getY());
346
                double v = Math.sqrt(Math.pow(paux.getX(), 2d) +
347
                                Math.pow(paux.getY(), 2d));
348
                paux = new Point2D.Double(paux.getX() / v, paux.getY() / v);
349

    
350
                return paux;
351
        }
352
}