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 / surface / circle / BaseCircle2D.java @ 42384

History | View | Annotate | Download (5.91 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.surface.circle;
24

    
25
import java.awt.geom.PathIterator;
26

    
27
import com.vividsolutions.jts.geom.Coordinate;
28

    
29
import java.awt.Shape;
30
import java.awt.geom.AffineTransform;
31
import java.awt.geom.Ellipse2D;
32
import java.awt.geom.Point2D.Double;
33

    
34
import org.gvsig.fmap.geom.GeometryException;
35
import org.gvsig.fmap.geom.aggregate.MultiLine;
36
import org.gvsig.fmap.geom.aggregate.MultiPoint;
37
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
38
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D;
39
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D;
40
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D;
41
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D;
42
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
43
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
44
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D;
45
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
46
import org.gvsig.fmap.geom.jts.util.JTSUtils;
47
import org.gvsig.fmap.geom.primitive.Line;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.fmap.geom.primitive.Polygon;
50

    
51

    
52
/**
53
 * @author fdiaz
54
 *
55
 */
56
public abstract class BaseCircle2D extends AbstractCircle {
57

    
58
    /**
59
     *
60
     */
61
    private static final long serialVersionUID = -6981603729327501715L;
62

    
63
    /**
64
     * @param type
65
     * @param subtype
66
     * @param center
67
     * @param radius
68
     */
69
    public BaseCircle2D(int type, int subtype, Point center, double radius) {
70
        super(type, subtype, center, radius);
71
    }
72

    
73
    /**
74
     * @param circle
75
     * @param geom2d
76
     */
77
    public BaseCircle2D(int type, int subtype) {
78
        super(type,subtype);
79
    }
80

    
81
    /* (non-Javadoc)
82
     * @see org.gvsig.fmap.geom.primitive.Circle#setPoints(org.gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point)
83
     */
84
    public void setPoints(Point p1, Point p2, Point p3) {
85
      this.center = new Point2D(JTSUtils.getCircumcentre(p1, p2, p3));
86
      this.radius = ((PointJTS)this.center).getJTS().distance(((PointJTS)p1).getJTS());
87
    }
88

    
89
    /* (non-Javadoc)
90
     * @see org.gvsig.fmap.geom.jts.primitive.surface.circle.AbstractCircle#fixPoint(org.gvsig.fmap.geom.primitive.Point)
91
     */
92
    @Override
93
    protected Point fixPoint(Point point) {
94
        if (point instanceof Point2D) {
95
            return point;
96
        } else {
97
            return new Point2D(point.getX(), point.getY());
98
        }
99
    }
100

    
101
    /**
102
     * @return
103
     */
104
    protected ArrayListCoordinateSequence getJTSCoordinates() {
105
        PathIterator pi = getPathIterator(null);
106
        ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
107

    
108
        double coords[] = new double[6];
109
        while (!pi.isDone()) {
110
            switch (pi.currentSegment(coords)) {
111
            case PathIterator.SEG_MOVETO:
112
                coordinates.add(new Coordinate(coords[0], coords[1]));
113
                break;
114
            case PathIterator.SEG_LINETO:
115
                coordinates.add(new Coordinate(coords[0], coords[1]));
116
                break;
117
            case PathIterator.SEG_QUADTO:
118
                coordinates.add(new Coordinate(coords[0], coords[1]));
119
                coordinates.add(new Coordinate(coords[2], coords[3]));
120
                break;
121
            case PathIterator.SEG_CUBICTO:
122
                coordinates.add(new Coordinate(coords[0], coords[1]));
123
                coordinates.add(new Coordinate(coords[2], coords[3]));
124
                coordinates.add(new Coordinate(coords[4], coords[5]));
125
                break;
126
            case PathIterator.SEG_CLOSE:
127
                coordinates.add(coordinates.get(0));
128
                break;
129
            }
130
            pi.next();
131
        }
132
        if(!coordinates.get(0).equals(coordinates.get(coordinates.size()-1))){
133
            coordinates.add((Coordinate)coordinates.get(0).clone());
134
        }
135
        return coordinates;
136
    }
137

    
138
    /* (non-Javadoc)
139
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
140
     */
141
    public MultiPoint toPoints() throws GeometryException {
142
        MultiPoint multiPoint = new MultiPoint2D();
143
        Coordinate[] coordinates = getJTS().getCoordinates();
144
        multiPoint.ensureCapacity(coordinates.length);
145
        for (int i = 0; i < coordinates.length; i++) {
146
            multiPoint.addPoint(new Point2D(coordinates[i]));
147
        }
148
        return multiPoint;
149
    }
150

    
151
    /* (non-Javadoc)
152
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
153
     */
154
    public MultiLine toLines() throws GeometryException {
155
        MultiLine multiLine = new MultiLine2D();
156
        Line line = new Line2D(getJTS().getCoordinates());
157
        multiLine.addPrimitive(line);
158
        return multiLine;
159
    }
160

    
161
    /* (non-Javadoc)
162
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
163
     */
164
    public MultiPolygon toPolygons() throws GeometryException {
165
        MultiPolygon multiPolygon = new MultiPolygon2D();
166
        Polygon polygon = new Polygon2D(getJTS().getCoordinates());
167
        multiPolygon.addPrimitive(polygon);
168
        return multiPolygon;
169
    }
170
}