Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / curve / arc / Arc2D.java @ 47346

History | View | Annotate | Download (6.84 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts.primitive.curve.arc;
24

    
25
import com.vividsolutions.jts.geom.Coordinate;
26
import java.awt.geom.PathIterator;
27
import org.gvsig.fmap.geom.Geometry;
28
import org.gvsig.fmap.geom.GeometryException;
29
import org.gvsig.fmap.geom.aggregate.MultiLine;
30
import org.gvsig.fmap.geom.aggregate.MultiPoint;
31
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
32
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D;
33
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D;
35
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
36
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
37
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
38
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D;
39
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
40
import org.gvsig.fmap.geom.jts.util.JTSUtils;
41
import org.gvsig.fmap.geom.operation.GeometryOperationException;
42
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
43
import org.gvsig.fmap.geom.primitive.Line;
44
import org.gvsig.fmap.geom.primitive.Point;
45
import org.gvsig.fmap.geom.primitive.Polygon;
46

    
47

    
48

    
49
/**
50
 * @author fdiaz
51
 *
52
 */
53
public class Arc2D extends AbstractArc {
54

    
55
    /**
56
     *
57
     */
58
    private static final long serialVersionUID = 3414562338763885954L;
59

    
60
    /**
61
     * @param subtype
62
     */
63
    public Arc2D() {
64
        super(Geometry.SUBTYPES.GEOM2D);
65
    }
66

    
67
    /* (non-Javadoc)
68
     * @see org.gvsig.fmap.geom.primitive.Arc#setPoints(org.gvsig.fmap.geom.primitive.Point, double, double, double)
69
     */
70
    public void setPoints(Point center, double radius, double startAngle, double angleExt) {
71

    
72
        init = new Point2D(
73
                center.getX()+radius*Math.cos(startAngle),
74
                center.getY()+radius*Math.sin(startAngle)
75
                );
76
        
77
        double midAngle = startAngle+angleExt/2;
78
        middle = new Point2D(
79
                center.getX()+radius*Math.cos(midAngle),
80
                center.getY()+radius*Math.sin(midAngle)
81
                );
82
        
83
        end = new Point2D(
84
                center.getX()+radius*Math.cos(startAngle+angleExt),
85
                center.getY()+radius*Math.sin(startAngle+angleExt)
86
                );
87
        
88
        
89
}
90

    
91
    /* (non-Javadoc)
92
     * @see org.gvsig.fmap.geom.primitive.Arc#getCenterPoint()
93
     */
94
    public Point getCenterPoint() {
95
        ((PointJTS)init).getJTS();
96
        Point2D center = new Point2D(this.getProjection(), JTSUtils.getCircumcentre(init, middle, end));
97
        return center;
98
    }
99

    
100
    /* (non-Javadoc)
101
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
102
     */
103
    public Geometry cloneGeometry() {
104
        Arc2D other = new Arc2D();
105
        other.setProjection(this.getProjection());
106
        Point clonedInit = (Point)init.cloneGeometry();
107
        Point clonedMiddle = (Point)middle.cloneGeometry();
108
        Point clonedEnd = (Point)end.cloneGeometry();
109
        other.setPoints(clonedInit, clonedMiddle, clonedEnd);
110
        other.setProjection(this.getProjection());
111
        return other;
112
    }
113

    
114
    /*
115
     * (non-Javadoc)
116
     *
117
     * @see
118
     * org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#fixPoint(org
119
     * .gvsig.fmap.geom.primitive.Point)
120
     */
121
    @Override
122
    protected Point fixPoint(Point point) {
123
        if (point instanceof Point2D) {
124
            return point;
125
        } else {
126
            return new Point2D(point.getX(), point.getY());
127
        }
128
    }
129

    
130
    /* (non-Javadoc)
131
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
132
     */
133
    public com.vividsolutions.jts.geom.Geometry getJTS() {
134
        PathIterator pi = getPathIterator(null);
135
        ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
136

    
137
        double coords[] = new double[6];
138
        while (!pi.isDone()) {
139
            switch (pi.currentSegment(coords)) {
140
            case PathIterator.SEG_MOVETO:
141
                coordinates.add(new Coordinate(coords[0], coords[1]));
142
                break;
143
            case PathIterator.SEG_LINETO:
144
                coordinates.add(new Coordinate(coords[0], coords[1]));
145
                break;
146
            case PathIterator.SEG_QUADTO:
147
                coordinates.add(new Coordinate(coords[0], coords[1]));
148
                coordinates.add(new Coordinate(coords[2], coords[3]));
149
                break;
150
            case PathIterator.SEG_CUBICTO:
151
                coordinates.add(new Coordinate(coords[0], coords[1]));
152
                coordinates.add(new Coordinate(coords[2], coords[3]));
153
                coordinates.add(new Coordinate(coords[4], coords[5]));
154
                break;
155
            case PathIterator.SEG_CLOSE:
156
                coordinates.add(coordinates.get(0));
157
                break;
158
            }
159
            pi.next();
160
        }
161
        return JTSUtils.createJTSLineString(coordinates);
162
    }
163

    
164
    /* (non-Javadoc)
165
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
166
     */
167
    public MultiPoint toPoints() throws GeometryException {
168
        MultiPoint multiPoint = new MultiPoint2D();
169
        Coordinate[] coordinates = getJTS().getCoordinates();
170
        multiPoint.ensureCapacity(coordinates.length);
171
        for (int i = 0; i < coordinates.length; i++) {
172
            multiPoint.addPoint(new Point2D(this.getProjection(), coordinates[i]));
173
        }
174
        return multiPoint;
175
    }
176

    
177
    /* (non-Javadoc)
178
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
179
     */
180
    public MultiLine toLines() throws GeometryException {
181
        MultiLine multiLine = new MultiLine2D();
182
        Line line = new Line2D(getJTS().getCoordinates());
183
        multiLine.addPrimitive(line);
184
        return multiLine;
185
    }
186

    
187
    /* (non-Javadoc)
188
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
189
     */
190
    public MultiPolygon toPolygons() throws GeometryException {
191
        MultiPolygon multiPolygon = new MultiPolygon2D();
192
        Polygon polygon = new Polygon2D(getJTS().getCoordinates());
193
        multiPolygon.addPrimitive(polygon);
194
        return multiPolygon;
195
    }
196

    
197
}