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 / polygon / Polygon3D.java @ 44612
History | View | Annotate | Download (11 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.polygon; |
24 |
|
25 |
import java.awt.geom.AffineTransform; |
26 |
import java.awt.geom.PathIterator; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import com.vividsolutions.jts.geom.Coordinate; |
32 |
import com.vividsolutions.jts.geom.GeometryFactory; |
33 |
import com.vividsolutions.jts.geomgraph.Position; |
34 |
import com.vividsolutions.jts.operation.buffer.BufferParameters; |
35 |
import com.vividsolutions.jts.operation.buffer.OffsetCurveBuilder; |
36 |
import java.awt.Shape; |
37 |
import java.awt.geom.GeneralPath; |
38 |
|
39 |
import org.cresques.cts.ICoordTrans; |
40 |
|
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.fmap.geom.GeometryException; |
43 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
44 |
import org.gvsig.fmap.geom.aggregate.MultiPolygon; |
45 |
import org.gvsig.fmap.geom.jts.GeometryJTS; |
46 |
import org.gvsig.fmap.geom.jts.aggregate.MultiLine3D; |
47 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3D; |
48 |
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
49 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.BaseLine3D; |
50 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3D; |
51 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
52 |
import org.gvsig.fmap.geom.jts.primitive.ring.Ring3D; |
53 |
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
54 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
55 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
56 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
57 |
import org.gvsig.fmap.geom.primitive.Line; |
58 |
import org.gvsig.fmap.geom.primitive.Polygon; |
59 |
import org.gvsig.fmap.geom.primitive.Ring; |
60 |
import org.gvsig.fmap.geom.primitive.SurfaceAppearance; |
61 |
|
62 |
|
63 |
|
64 |
/**
|
65 |
* @author fdiaz
|
66 |
*
|
67 |
*/
|
68 |
public class Polygon3D extends BaseLine3D implements Polygon { |
69 |
|
70 |
/**
|
71 |
*
|
72 |
*/
|
73 |
private static final long serialVersionUID = 5748137753517288500L; |
74 |
|
75 |
List<Ring> interiorRings = new ArrayList<Ring>(); |
76 |
|
77 |
/**
|
78 |
* @param subtype
|
79 |
*/
|
80 |
public Polygon3D() {
|
81 |
super(Geometry.TYPES.POLYGON);
|
82 |
} |
83 |
|
84 |
/**
|
85 |
*
|
86 |
*/
|
87 |
public Polygon3D(Coordinate[] coordinates) { |
88 |
super(Geometry.TYPES.POLYGON, coordinates);
|
89 |
closePrimitive(); |
90 |
} |
91 |
|
92 |
/* (non-Javadoc)
|
93 |
* @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
94 |
*/
|
95 |
public Polygon cloneGeometry() { |
96 |
Polygon3D cloned = new Polygon3D(cloneCoordinates().toCoordinateArray());
|
97 |
for (int i = 0; i < getNumInteriorRings(); i++){ |
98 |
cloned.addInteriorRing((Ring)getInteriorRing(i).cloneGeometry()); |
99 |
} |
100 |
return cloned;
|
101 |
} |
102 |
|
103 |
@Override
|
104 |
public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException { |
105 |
ArrayListCoordinateSequence coordinates2D = new ArrayListCoordinateSequence(coordinates.size());
|
106 |
for (Coordinate coordinate : this.coordinates) { |
107 |
coordinates2D.add(new Coordinate(coordinate.x, coordinate.y));
|
108 |
} |
109 |
Polygon2D poligon2D = new Polygon2D(coordinates2D);
|
110 |
for (int i = 0; i < getNumInteriorRings(); i++){ |
111 |
poligon2D.addInteriorRing((Ring)getInteriorRing(i).force2D()); |
112 |
} |
113 |
return poligon2D;
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
@SuppressWarnings("CloneDoesntCallSuperClone") |
118 |
public Polygon clone() throws CloneNotSupportedException { |
119 |
return this.cloneGeometry(); |
120 |
} |
121 |
|
122 |
/* (non-Javadoc)
|
123 |
* @see org.gvsig.fmap.geom.primitive.Surface#setSurfaceAppearance(org.gvsig.fmap.geom.primitive.SurfaceAppearance)
|
124 |
*/
|
125 |
public void setSurfaceAppearance(SurfaceAppearance app) { |
126 |
// TODO Auto-generated method stub
|
127 |
|
128 |
} |
129 |
|
130 |
/* (non-Javadoc)
|
131 |
* @see org.gvsig.fmap.geom.primitive.Surface#getSurfaceAppearance()
|
132 |
*/
|
133 |
public SurfaceAppearance getSurfaceAppearance() {
|
134 |
// TODO Auto-generated method stub
|
135 |
return null; |
136 |
} |
137 |
|
138 |
/* (non-Javadoc)
|
139 |
* @see org.gvsig.fmap.geom.primitive.Surface#getNumInteriorRings()
|
140 |
*/
|
141 |
public int getNumInteriorRings() { |
142 |
return this.interiorRings.size(); |
143 |
} |
144 |
|
145 |
/* (non-Javadoc)
|
146 |
* @see org.gvsig.fmap.geom.primitive.Surface#getInteriorRing(int)
|
147 |
*/
|
148 |
public Ring getInteriorRing(int index) { |
149 |
return interiorRings.get(index);
|
150 |
} |
151 |
|
152 |
/* (non-Javadoc)
|
153 |
* @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Ring)
|
154 |
*/
|
155 |
public void addInteriorRing(Ring ring) { |
156 |
interiorRings.add(ring); |
157 |
} |
158 |
|
159 |
/* (non-Javadoc)
|
160 |
* @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Line)
|
161 |
*/
|
162 |
public void addInteriorRing(Line line) { |
163 |
Ring3D ring = new Ring3D();
|
164 |
ring.ensureCapacity(line.getNumVertices()); |
165 |
for(int i=0; i<line.getNumVertices(); i++){ |
166 |
Point3D vertex = (Point3D)line.getVertex(i); |
167 |
ring.addVertex(new Point3D(vertex.getX(), vertex.getY(), vertex.getZ()));
|
168 |
} |
169 |
ring.closePrimitive(); |
170 |
interiorRings.add(ring); |
171 |
} |
172 |
|
173 |
/*
|
174 |
* (non-Javadoc)
|
175 |
*
|
176 |
* @see
|
177 |
* org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.
|
178 |
* geom.primitive.Polygon)
|
179 |
*/
|
180 |
public void addInteriorRing(Polygon polygon) { |
181 |
Ring3D ring = new Ring3D();
|
182 |
ring.ensureCapacity(polygon.getNumVertices()); |
183 |
for(int i=0; i<polygon.getNumVertices(); i++){ |
184 |
Point3D vertex = (Point3D)polygon.getVertex(i); |
185 |
ring.addVertex(new Point3D(vertex.getX(), vertex.getY(), vertex.getZ()));
|
186 |
} |
187 |
ring.closePrimitive(); |
188 |
interiorRings.add(ring); |
189 |
} |
190 |
|
191 |
/* (non-Javadoc)
|
192 |
* @see org.gvsig.fmap.geom.primitive.Surface#removeInteriorRing(int)
|
193 |
*/
|
194 |
public void removeInteriorRing(int index) { |
195 |
interiorRings.remove(index); |
196 |
} |
197 |
|
198 |
/* (non-Javadoc)
|
199 |
* @see org.gvsig.fmap.geom.primitive.Line#toLines()
|
200 |
*/
|
201 |
public MultiLine toLines() throws GeometryException { |
202 |
MultiLine multiLine = new MultiLine3D();
|
203 |
multiLine.addPrimitive(new Line3D(coordinates.toCoordinateArray()));
|
204 |
return multiLine;
|
205 |
} |
206 |
|
207 |
/* (non-Javadoc)
|
208 |
* @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
|
209 |
*/
|
210 |
public MultiPolygon toPolygons() throws GeometryException { |
211 |
MultiPolygon multiPolygon = new MultiPolygon3D();
|
212 |
multiPolygon.addPrimitive(this);
|
213 |
return multiPolygon;
|
214 |
} |
215 |
|
216 |
/* (non-Javadoc)
|
217 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
|
218 |
*/
|
219 |
public com.vividsolutions.jts.geom.Geometry getJTS() {
|
220 |
return JTSUtils.createJTSPolygon(coordinates, interiorRings);
|
221 |
} |
222 |
|
223 |
/*
|
224 |
* (non-Javadoc)
|
225 |
*
|
226 |
* @see
|
227 |
* org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
|
228 |
* )
|
229 |
*/
|
230 |
public PathIterator getPathIterator(AffineTransform at) { |
231 |
PolygonIterator pi = new PolygonIterator(this, at); |
232 |
return pi;
|
233 |
} |
234 |
|
235 |
/* (non-Javadoc)
|
236 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#ensureOrientation(boolean)
|
237 |
*/
|
238 |
public boolean ensureOrientation(boolean ccw) throws GeometryOperationNotSupportedException, GeometryOperationException { |
239 |
boolean result = super.ensureOrientation(ccw); |
240 |
for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
241 |
GeometryJTS ring = (GeometryJTS) iterator.next(); |
242 |
ring.ensureOrientation(!ccw); |
243 |
} |
244 |
return result;
|
245 |
} |
246 |
|
247 |
/* (non-Javadoc)
|
248 |
* @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#flip()
|
249 |
*/
|
250 |
@Override
|
251 |
public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException { |
252 |
super.flip();
|
253 |
for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
254 |
GeometryJTS ring = (GeometryJTS) iterator.next(); |
255 |
ring.flip(); |
256 |
} |
257 |
} |
258 |
|
259 |
/* (non-Javadoc)
|
260 |
* @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#transform(java.awt.geom.AffineTransform)
|
261 |
*/
|
262 |
@Override
|
263 |
public void transform(AffineTransform at) { |
264 |
super.transform(at);
|
265 |
for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
266 |
GeometryJTS ring = (GeometryJTS) iterator.next(); |
267 |
ring.transform(at); |
268 |
} |
269 |
} |
270 |
|
271 |
|
272 |
/* (non-Javadoc)
|
273 |
* @see org.gvsig.fmap.geom.Geometry#offset(double)
|
274 |
*/
|
275 |
public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
276 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
277 |
} |
278 |
|
279 |
/* (non-Javadoc)
|
280 |
* @see org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#reProject(org.cresques.cts.ICoordTrans)
|
281 |
*/
|
282 |
@Override
|
283 |
public void reProject(ICoordTrans ct) { |
284 |
super.reProject(ct);
|
285 |
for (Iterator<Ring> iterator = interiorRings.iterator(); iterator.hasNext();) { |
286 |
GeometryJTS ring = (GeometryJTS) iterator.next(); |
287 |
ring.reProject(ct); |
288 |
} |
289 |
if (coordinates.size()>=2 && !isClosed()) { |
290 |
closePrimitive(); |
291 |
} |
292 |
} |
293 |
|
294 |
@Override
|
295 |
public boolean equals(Object obj) { |
296 |
boolean res = super.equals(obj); |
297 |
if(res && obj instanceof Polygon3D){ |
298 |
Polygon3D other = (Polygon3D)obj; |
299 |
if(this.getNumVertices() != other.getNumVertices()){ |
300 |
return false; |
301 |
} |
302 |
for (int i = 0; i < this.getNumVertices(); i++) { |
303 |
Coordinate coordinate = this.coordinates.get(i);
|
304 |
Coordinate otherCoordinate = other.coordinates.get(i); |
305 |
if (otherCoordinate.getOrdinate(2) != coordinate.getOrdinate(2)) { |
306 |
return false; |
307 |
} |
308 |
} |
309 |
if(this.getNumInteriorRings() != other.getNumInteriorRings()){ |
310 |
return false; |
311 |
} |
312 |
for(int i=0; i < this.getNumInteriorRings(); i++){ |
313 |
if(!other.getInteriorRing(i).equals(this.getInteriorRing(i))){ |
314 |
return false; |
315 |
} |
316 |
} |
317 |
return true; |
318 |
} else {
|
319 |
return false; |
320 |
} |
321 |
} |
322 |
|
323 |
@Override
|
324 |
public Shape getShape(AffineTransform affineTransform) { |
325 |
return PolygonHelper.getShape(this, affineTransform); |
326 |
} |
327 |
} |