Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / impl / DrawGeneralPathXIterator.java @ 40559

History | View | Annotate | Download (5.96 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.fmap.geom.impl;
25

    
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28

    
29
import org.gvsig.fmap.geom.primitive.GeneralPathX;
30
import org.gvsig.fmap.geom.primitive.Point;
31

    
32
/**
33
 * AWT {@link PathIterator} to draw a {@link GeneralPathX} object.
34
 * 
35
 * While iterating the {@link GeneralPathX) each point coordinates are 
36
 * converted to int. If a segment is a PathIterator#SEG_LINETO and the 
37
 * integer coordinates are the same as the previous point ones, the point 
38
 * is ignored and the next one is looked for.
39
 * 
40
 * @author gvSIG Team
41
 */
42
public class DrawGeneralPathXIterator implements PathIterator {
43

    
44
    protected int typeIdx = -1;
45
    protected int pointIdx = 0;
46
    protected GeneralPathX path;
47
    protected AffineTransform affine;
48

    
49
    /**
50
     * This array keeps the "real" coordinates (double precision) which
51
     * will afterwards be rounded to int. It is final and created with
52
     * its maximum possible size. This does not have a performance cost
53
     * because only relevant indices are read/written (also when applying
54
     * an affine transformation)
55
     */
56
    private final double[] currentPointRealCoords = new double[] { 0, 0, 0, 0, 0, 0 };
57
    
58
    /**
59
     * This array keeps the rounded (int) coordinates.
60
     * It is final and created with
61
     * its maximum possible size. This does not have a performance cost
62
     * because only relevant indices are read/written.
63
     */
64
    private final int[] currentPointCoords = new int[] { 0, 0, 0, 0, 0, 0 };
65
    
66
    private final int[] previousPointCoords = new int[] { 0, 0 };
67

    
68
    private int currentType = -1;
69
    private int previousType = -1;
70

    
71
    private static final int curvesize[] = { 1, 1, 2, 3, 0 };
72

    
73
    private final int numTypes;
74
    private final int numPoints;
75

    
76
    /**
77
     * Constructs an iterator given a GeneralPathX.
78
     * 
79
     * @see GeneralPathX#getPathIterator
80
     */
81
    public DrawGeneralPathXIterator(GeneralPathX path) {
82
        this(path, null);
83
    }
84

    
85
    /**
86
     * Constructs an iterator given a GeneralPathX and an optional
87
     * AffineTransform.
88
     * 
89
     * @see GeneralPathX#getPathIterator
90
     */
91
    public DrawGeneralPathXIterator(GeneralPathX path, AffineTransform at) {
92
        this.path = path;
93
        this.affine = at;
94
        // Go to the first segment
95
        this.pointIdx = 0;
96
        this.numPoints = path.getNumCoords();
97
        this.typeIdx = 0;
98
        this.numTypes = path.getNumTypes();
99
        if (numTypes > 0) {
100
            currentType = path.getTypeAt(typeIdx);
101
            readCurrentSegmentCoordsAsInt();
102
            previousType = currentType;
103
            previousPointCoords[0] = currentPointCoords[0];
104
            previousPointCoords[1] = currentPointCoords[1];
105
        }
106
    }
107

    
108
    public int getWindingRule() {
109
        return path.getWindingRule();
110
    }
111

    
112
    public boolean isDone() {
113
        return typeIdx >= numTypes;
114
    }
115

    
116
    public void next() {
117
        do {
118
            typeIdx++;
119
            pointIdx += curvesize[currentType];
120
            if (typeIdx < numTypes) {
121
                currentType = path.getTypeAt(typeIdx);
122
                if (SEG_CLOSE != currentType) {
123
                    readCurrentSegmentCoordsAsInt();
124
                }
125
            } else {
126
                // We have reached past the last segment
127
                return;
128
            }
129
        } while (isSameSegment());
130

    
131
        previousType = currentType;
132
        previousPointCoords[0] = currentPointCoords[0];
133
        previousPointCoords[1] = currentPointCoords[1];
134
    }
135

    
136
    private final boolean isSameSegment() {
137
        return SEG_LINETO == currentType
138
            && (SEG_LINETO == previousType || SEG_MOVETO == previousType)
139
            && currentPointCoords[0] == previousPointCoords[0]
140
            && currentPointCoords[1] == previousPointCoords[1];
141
    }
142

    
143
    private void readCurrentSegmentCoordsAsInt() {
144
        int currentSize = curvesize[currentType];
145

    
146
        for (int i = 0; i < currentSize; i++) {
147
            readPoint(i * 2, pointIdx + i);
148
        }
149

    
150
        if (affine != null && !affine.isIdentity()) {
151
            affine.transform(currentPointRealCoords, 0, currentPointRealCoords,
152
                0, currentSize);
153
        }
154

    
155
        for (int i = 0; i < currentSize * 2; i++) {
156
            currentPointCoords[i] = (int) currentPointRealCoords[i];
157
        }
158
    }
159

    
160
    private void readPoint(int currentCoordsPos, int currentPointIdx) {
161
        Point point = path.getPointAt(currentPointIdx);
162
        currentPointRealCoords[currentCoordsPos] = point.getX();
163
        currentPointRealCoords[currentCoordsPos + 1] = point.getY();
164
    }
165

    
166
    public int currentSegment(float[] coords) {
167
        if (SEG_CLOSE != currentType) {
168
            int size = curvesize[currentType] * 2;
169

    
170
            for (int i = 0; i < size; i++) {
171
                coords[i] = currentPointCoords[i];
172
            }
173
        }
174
        return currentType;
175
    }
176

    
177
    public int currentSegment(double[] coords) {
178
        int size = curvesize[currentType] * 2;
179

    
180
        for (int i = 0; i < size; i++) {
181
            coords[i] = currentPointCoords[i];
182
        }
183
        return currentType;
184
    }
185
}