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 @ 44099
History | View | Annotate | Download (7.55 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 java.awt.geom.PathIterator; |
26 |
|
27 |
import com.vividsolutions.jts.geom.Coordinate; |
28 |
|
29 |
import org.gvsig.fmap.geom.Geometry; |
30 |
import org.gvsig.fmap.geom.GeometryException; |
31 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
32 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
33 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
34 |
import org.gvsig.fmap.geom.jts.aggregate.MultiLine2D; |
35 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D; |
36 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D; |
37 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D; |
38 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2D; |
39 |
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS; |
40 |
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D; |
41 |
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
42 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
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 |
double diameter = radius*2; |
73 |
double x;
|
74 |
double y;
|
75 |
double start;
|
76 |
double extent;
|
77 |
double angleOffset;
|
78 |
final double pi2 = Math.PI*2; |
79 |
|
80 |
angleOffset = 0;
|
81 |
if( angleExt<0 ) { |
82 |
angleOffset = pi2 + angleExt; |
83 |
angleExt = Math.abs(angleExt);
|
84 |
} |
85 |
x = center.getX() - radius; |
86 |
y = center.getY() - radius; |
87 |
|
88 |
if( angleExt > 0 && (angleExt % pi2) == 0 ) { |
89 |
start = 0;
|
90 |
extent = 360;
|
91 |
} else {
|
92 |
angleExt = angleExt % pi2; //Asumimos que aqui angleExt es siempre positivo
|
93 |
startAngle = Math.abs(startAngle) % pi2;
|
94 |
start = Math.toDegrees( pi2 - startAngle + angleOffset) ;
|
95 |
extent = - Math.toDegrees(pi2 - angleExt);
|
96 |
} |
97 |
|
98 |
|
99 |
double angleStart = Math.toRadians(-start); |
100 |
double initX = x + (Math.cos(angleStart) * 0.5 + 0.5) * diameter; |
101 |
double initY = y + (Math.sin(angleStart) * 0.5 + 0.5) * diameter; |
102 |
init = new Point2D(initX, initY); |
103 |
|
104 |
double angleEnd = Math.toRadians(-start - extent); |
105 |
double endX = x + (Math.cos(angleEnd) * 0.5 + 0.5) * diameter; |
106 |
double endY = y + (Math.sin(angleEnd) * 0.5 + 0.5) * diameter; |
107 |
end = new Point2D(endX, endY); |
108 |
|
109 |
double angleMiddle = Math.toRadians(-start - extent)/2; |
110 |
double middleX = x + (Math.cos(angleMiddle) * 0.5 + 0.5) * diameter; |
111 |
double middleY = y + (Math.sin(angleMiddle) * 0.5 + 0.5) * diameter; |
112 |
middle = new Point2D(middleX, middleY); |
113 |
} |
114 |
|
115 |
/* (non-Javadoc)
|
116 |
* @see org.gvsig.fmap.geom.primitive.Arc#getCenterPoint()
|
117 |
*/
|
118 |
public Point getCenterPoint() { |
119 |
((PointJTS)init).getJTS(); |
120 |
Point2D center = new Point2D(this.getProjection(), JTSUtils.getCircumcentre(init, middle, end)); |
121 |
return center;
|
122 |
} |
123 |
|
124 |
/* (non-Javadoc)
|
125 |
* @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
126 |
*/
|
127 |
public Geometry cloneGeometry() {
|
128 |
Arc2D arc2D = new Arc2D(); |
129 |
Point clonedInit = (Point)init.cloneGeometry(); |
130 |
Point clonedMiddle = (Point)middle.cloneGeometry(); |
131 |
Point clonedEnd = (Point)end.cloneGeometry(); |
132 |
arc2D.setPoints(clonedInit, clonedMiddle, clonedEnd); |
133 |
return arc2D;
|
134 |
} |
135 |
|
136 |
/*
|
137 |
* (non-Javadoc)
|
138 |
*
|
139 |
* @see
|
140 |
* org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#fixPoint(org
|
141 |
* .gvsig.fmap.geom.primitive.Point)
|
142 |
*/
|
143 |
@Override
|
144 |
protected Point fixPoint(Point point) { |
145 |
if (point instanceof Point2D) { |
146 |
return point;
|
147 |
} else {
|
148 |
return new Point2D(point.getX(), point.getY()); |
149 |
} |
150 |
} |
151 |
|
152 |
/* (non-Javadoc)
|
153 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
|
154 |
*/
|
155 |
public com.vividsolutions.jts.geom.Geometry getJTS() {
|
156 |
PathIterator pi = getPathIterator(null); |
157 |
ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
|
158 |
|
159 |
double coords[] = new double[6]; |
160 |
while (!pi.isDone()) {
|
161 |
switch (pi.currentSegment(coords)) {
|
162 |
case PathIterator.SEG_MOVETO: |
163 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
164 |
break;
|
165 |
case PathIterator.SEG_LINETO: |
166 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
167 |
break;
|
168 |
case PathIterator.SEG_QUADTO: |
169 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
170 |
coordinates.add(new Coordinate(coords[2], coords[3])); |
171 |
break;
|
172 |
case PathIterator.SEG_CUBICTO: |
173 |
coordinates.add(new Coordinate(coords[0], coords[1])); |
174 |
coordinates.add(new Coordinate(coords[2], coords[3])); |
175 |
coordinates.add(new Coordinate(coords[4], coords[5])); |
176 |
break;
|
177 |
case PathIterator.SEG_CLOSE: |
178 |
coordinates.add(coordinates.get(0));
|
179 |
break;
|
180 |
} |
181 |
pi.next(); |
182 |
} |
183 |
return JTSUtils.createJTSLineString(coordinates);
|
184 |
} |
185 |
|
186 |
/* (non-Javadoc)
|
187 |
* @see org.gvsig.fmap.geom.primitive.Line#toPoints()
|
188 |
*/
|
189 |
public MultiPoint toPoints() throws GeometryException { |
190 |
MultiPoint multiPoint = new MultiPoint2D();
|
191 |
Coordinate[] coordinates = getJTS().getCoordinates();
|
192 |
multiPoint.ensureCapacity(coordinates.length); |
193 |
for (int i = 0; i < coordinates.length; i++) { |
194 |
multiPoint.addPoint(new Point2D(this.getProjection(), coordinates[i])); |
195 |
} |
196 |
return multiPoint;
|
197 |
} |
198 |
|
199 |
/* (non-Javadoc)
|
200 |
* @see org.gvsig.fmap.geom.primitive.Line#toLines()
|
201 |
*/
|
202 |
public MultiLine toLines() throws GeometryException { |
203 |
MultiLine multiLine = new MultiLine2D();
|
204 |
Line line = new Line2D(getJTS().getCoordinates()); |
205 |
multiLine.addPrimitive(line); |
206 |
return multiLine;
|
207 |
} |
208 |
|
209 |
/* (non-Javadoc)
|
210 |
* @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
|
211 |
*/
|
212 |
public MultiPolygon toPolygons() throws GeometryException { |
213 |
MultiPolygon multiPolygon = new MultiPolygon2D();
|
214 |
Polygon polygon = new Polygon2D(getJTS().getCoordinates()); |
215 |
multiPolygon.addPrimitive(polygon); |
216 |
return multiPolygon;
|
217 |
} |
218 |
|
219 |
} |