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

History | View | Annotate | Download (6.73 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
import java.awt.Shape;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Ellipse2D;
31

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

    
49

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

    
56
    /**
57
     *
58
     */
59
    private static final long serialVersionUID = -6981603729327501715L;
60

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

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

    
79
    /* (non-Javadoc)
80
     * @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)
81
     */
82
    public void setPoints(Point p1, Point p2, Point p3) {
83
      this.center = new Point2D(JTSUtils.getCircumcentre(p1, p2, p3));
84
      this.radius = ((PointJTS)this.center).getJTS().distance(((PointJTS)p1).getJTS());
85
    }
86

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

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

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

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

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

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

    
169
    public Shape getShape(AffineTransform affineTransform) {
170
            double radious = this.getRadious();
171
            Point center = this.getCenter();
172
            java.awt.geom.Point2D origin = new java.awt.geom.Point2D.Double(
173
                    center.getX()-radious, 
174
                    center.getX()-radious
175
            );
176
            java.awt.geom.Point2D radiusTrans = new java.awt.geom.Point2D.Double(radious, radious);
177
            if( affineTransform != null ) {
178
                affineTransform.transform(origin, origin);
179
                affineTransform.transform(radiusTrans, radiusTrans);
180
            }
181
            Ellipse2D.Double ellipse = new Ellipse2D.Double(
182
                    origin.getX(),
183
                    origin.getY(), 
184
                    radiusTrans.getX(), 
185
                    radiusTrans.getY()
186
            );
187
            return ellipse;
188
    }
189
}