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