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 @ 47432
History | View | Annotate | Download (5.87 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 com.vividsolutions.jts.geom.Coordinate; |
26 |
import java.awt.geom.PathIterator; |
27 |
import org.gvsig.fmap.geom.GeometryException; |
28 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
29 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
30 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
31 |
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D; |
32 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D; |
33 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D; |
34 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D; |
35 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2D; |
36 |
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS; |
37 |
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D; |
38 |
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
39 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
40 |
import org.gvsig.fmap.geom.primitive.Line; |
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.fmap.geom.primitive.Polygon; |
43 |
|
44 |
|
45 |
/**
|
46 |
* @author fdiaz
|
47 |
*
|
48 |
*/
|
49 |
public abstract class BaseCircle2D extends AbstractCircle { |
50 |
|
51 |
/**
|
52 |
*
|
53 |
*/
|
54 |
private static final long serialVersionUID = -6981603729327501715L; |
55 |
|
56 |
/**
|
57 |
* @param type
|
58 |
* @param subtype
|
59 |
* @param center
|
60 |
* @param radius
|
61 |
*/
|
62 |
public BaseCircle2D(int type, int subtype, Point center, double radius) { |
63 |
super(type, subtype, center, radius);
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* @param circle
|
68 |
* @param geom2d
|
69 |
*/
|
70 |
public BaseCircle2D(int type, int subtype) { |
71 |
super(type,subtype);
|
72 |
} |
73 |
|
74 |
/* (non-Javadoc)
|
75 |
* @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)
|
76 |
*/
|
77 |
public void setPoints(Point p1, Point p2, Point p3) { |
78 |
this.center = new Point2D(p1.getProjection(), JTSUtils.getCircumcentre(p1, p2, p3)); |
79 |
this.radius = ((PointJTS)this.center).getJTS().distance(((PointJTS)p1).getJTS()); |
80 |
this.setProjection(this.center.getProjection()); |
81 |
} |
82 |
|
83 |
/* (non-Javadoc)
|
84 |
* @see org.gvsig.fmap.geom.jts.primitive.surface.circle.AbstractCircle#fixPoint(org.gvsig.fmap.geom.primitive.Point)
|
85 |
*/
|
86 |
@Override
|
87 |
protected Point fixPoint(Point point) { |
88 |
if (point instanceof Point2D) { |
89 |
return point;
|
90 |
} else {
|
91 |
return new Point2D(point.getX(), point.getY()); |
92 |
} |
93 |
} |
94 |
|
95 |
/**
|
96 |
* @return
|
97 |
*/
|
98 |
protected ArrayListCoordinateSequence getJTSCoordinates() {
|
99 |
PathIterator pi = getPathIterator(null); |
100 |
ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
|
101 |
|
102 |
double coords[] = new double[6]; |
103 |
while (!pi.isDone()) {
|
104 |
switch (pi.currentSegment(coords)) {
|
105 |
case PathIterator.SEG_MOVETO: |
106 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
107 |
break;
|
108 |
case PathIterator.SEG_LINETO: |
109 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
110 |
break;
|
111 |
case PathIterator.SEG_QUADTO: |
112 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
113 |
coordinates.add(new Coordinate(coords[2], coords[3])); |
114 |
break;
|
115 |
case PathIterator.SEG_CUBICTO: |
116 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
117 |
coordinates.add(new Coordinate(coords[2], coords[3])); |
118 |
coordinates.add(new Coordinate(coords[4], coords[5])); |
119 |
break;
|
120 |
case PathIterator.SEG_CLOSE: |
121 |
coordinates.add(coordinates.get(0));
|
122 |
break;
|
123 |
} |
124 |
pi.next(); |
125 |
} |
126 |
if(!coordinates.get(0).equals(coordinates.get(coordinates.size()-1))){ |
127 |
coordinates.add((Coordinate)coordinates.get(0).clone());
|
128 |
} |
129 |
return coordinates;
|
130 |
} |
131 |
|
132 |
/* (non-Javadoc)
|
133 |
* @see org.gvsig.fmap.geom.primitive.Line#toPoints()
|
134 |
*/
|
135 |
public MultiPoint toPoints() throws GeometryException { |
136 |
MultiPoint multiPoint = new MultiPoint2D();
|
137 |
Coordinate[] coordinates = getJTS().getCoordinates();
|
138 |
multiPoint.ensureCapacity(coordinates.length); |
139 |
for (int i = 0; i < coordinates.length; i++) { |
140 |
multiPoint.addPoint(new Point2D(this.getProjection(), coordinates[i])); |
141 |
} |
142 |
return multiPoint;
|
143 |
} |
144 |
|
145 |
/* (non-Javadoc)
|
146 |
* @see org.gvsig.fmap.geom.primitive.Line#toLines()
|
147 |
*/
|
148 |
public MultiLine toLines() throws GeometryException { |
149 |
MultiLine multiLine = new MultiLine2D();
|
150 |
Line line = new Line2D(getJTS().getCoordinates()); |
151 |
multiLine.addPrimitive(line); |
152 |
return multiLine;
|
153 |
} |
154 |
|
155 |
/* (non-Javadoc)
|
156 |
* @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
|
157 |
*/
|
158 |
public MultiPolygon toPolygons() throws GeometryException { |
159 |
MultiPolygon multiPolygon = new MultiPolygon2D();
|
160 |
Polygon polygon = new Polygon2D(getJTS().getCoordinates()); |
161 |
multiPolygon.addPrimitive(polygon); |
162 |
return multiPolygon;
|
163 |
} |
164 |
} |