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 @ 47346

History | View | Annotate | Download (6 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(p1.getProjection(), JTSUtils.getCircumcentre(p1, p2, p3));
86
      this.radius = ((PointJTS)this.center).getJTS().distance(((PointJTS)p1).getJTS());
87
      this.setProjection(this.center.getProjection());
88
    }
89

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

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

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

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

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

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