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