root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / GeneralPathX.java @ 11009
History | View | Annotate | Download (32.5 KB)
1 | 214 | fernando | /*
|
---|---|---|---|
2 | * Created on 10-jun-2004
|
||
3 | *
|
||
4 | * TODO To change the template for this generated file go to
|
||
5 | * Window - Preferences - Java - Code Generation - Code and Comments
|
||
6 | */
|
||
7 | 1100 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 | *
|
||
9 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
10 | *
|
||
11 | * This program is free software; you can redistribute it and/or
|
||
12 | * modify it under the terms of the GNU General Public License
|
||
13 | * as published by the Free Software Foundation; either version 2
|
||
14 | * of the License, or (at your option) any later version.
|
||
15 | *
|
||
16 | * This program is distributed in the hope that it will be useful,
|
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
19 | * GNU General Public License for more details.
|
||
20 | *
|
||
21 | * You should have received a copy of the GNU General Public License
|
||
22 | * along with this program; if not, write to the Free Software
|
||
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
24 | *
|
||
25 | * For more information, contact:
|
||
26 | *
|
||
27 | * Generalitat Valenciana
|
||
28 | * Conselleria d'Infraestructures i Transport
|
||
29 | * Av. Blasco Ib??ez, 50
|
||
30 | * 46010 VALENCIA
|
||
31 | * SPAIN
|
||
32 | *
|
||
33 | * +34 963862235
|
||
34 | * gvsig@gva.es
|
||
35 | * www.gvsig.gva.es
|
||
36 | *
|
||
37 | * or
|
||
38 | *
|
||
39 | * IVER T.I. S.A
|
||
40 | * Salamanca 50
|
||
41 | * 46005 Valencia
|
||
42 | * Spain
|
||
43 | *
|
||
44 | * +34 963163400
|
||
45 | * dac@iver.es
|
||
46 | */
|
||
47 | 214 | fernando | package com.iver.cit.gvsig.fmap.core; |
48 | |||
49 | /**
|
||
50 | * @author FJP
|
||
51 | *
|
||
52 | */
|
||
53 | /*
|
||
54 | * @(#)GeneralPathX.java 1.58 03/01/23
|
||
55 | *
|
||
56 | * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
|
||
57 | * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||
58 | */
|
||
59 | |||
60 | import java.awt.Shape; |
||
61 | import java.awt.geom.AffineTransform; |
||
62 | import java.awt.geom.FlatteningPathIterator; |
||
63 | import java.awt.geom.IllegalPathStateException; |
||
64 | import java.awt.geom.PathIterator; |
||
65 | import java.awt.geom.Point2D; |
||
66 | import java.awt.geom.Rectangle2D; |
||
67 | 2183 | fernando | import java.io.Serializable; |
68 | 6040 | fjp | import java.util.ArrayList; |
69 | 214 | fernando | |
70 | 885 | fjp | import org.cresques.cts.ICoordTrans; |
71 | |||
72 | 5923 | fjp | import sun.awt.geom.Crossings; |
73 | import sun.awt.geom.Curve; |
||
74 | |||
75 | import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
||
76 | 5747 | fjp | import com.vividsolutions.jts.algorithm.CGAlgorithms; |
77 | import com.vividsolutions.jts.geom.Coordinate; |
||
78 | import com.vividsolutions.jts.geom.CoordinateList; |
||
79 | import com.vividsolutions.jts.geom.CoordinateSequences; |
||
80 | import com.vividsolutions.jts.geom.impl.CoordinateArraySequence; |
||
81 | |||
82 | 214 | fernando | /**
|
83 | * The <code>GeneralPathX</code> class represents a geometric path
|
||
84 | * constructed from straight lines, and quadratic and cubic
|
||
85 | * (Bézier) curves. It can contain multiple subpaths.
|
||
86 | * <p>
|
||
87 | * The winding rule specifies how the interior of a path is
|
||
88 | * determined. There are two types of winding rules:
|
||
89 | * EVEN_ODD and NON_ZERO.
|
||
90 | * <p>
|
||
91 | * An EVEN_ODD winding rule means that enclosed regions
|
||
92 | * of the path alternate between interior and exterior areas as
|
||
93 | * traversed from the outside of the path towards a point inside
|
||
94 | * the region.
|
||
95 | * <p>
|
||
96 | * A NON_ZERO winding rule means that if a ray is
|
||
97 | * drawn in any direction from a given point to infinity
|
||
98 | * and the places where the path intersects
|
||
99 | * the ray are examined, the point is inside of the path if and only if
|
||
100 | * the number of times that the path crosses the ray from
|
||
101 | * left to right does not equal the number of times that the path crosses
|
||
102 | * the ray from right to left.
|
||
103 | * @version 1.58, 01/23/03
|
||
104 | * @author Jim Graham
|
||
105 | */
|
||
106 | 2183 | fernando | public class GeneralPathX implements Shape, Cloneable, Serializable { |
107 | 214 | fernando | /**
|
108 | * An even-odd winding rule for determining the interior of
|
||
109 | * a path.
|
||
110 | */
|
||
111 | public static final int WIND_EVEN_ODD = PathIterator.WIND_EVEN_ODD; |
||
112 | |||
113 | /**
|
||
114 | * A non-zero winding rule for determining the interior of a
|
||
115 | * path.
|
||
116 | */
|
||
117 | public static final int WIND_NON_ZERO = PathIterator.WIND_NON_ZERO; |
||
118 | |||
119 | // For code simplicity, copy these constants to our namespace
|
||
120 | // and cast them to byte constants for easy storage.
|
||
121 | private static final byte SEG_MOVETO = (byte) PathIterator.SEG_MOVETO; |
||
122 | private static final byte SEG_LINETO = (byte) PathIterator.SEG_LINETO; |
||
123 | private static final byte SEG_QUADTO = (byte) PathIterator.SEG_QUADTO; |
||
124 | private static final byte SEG_CUBICTO = (byte) PathIterator.SEG_CUBICTO; |
||
125 | private static final byte SEG_CLOSE = (byte) PathIterator.SEG_CLOSE; |
||
126 | |||
127 | byte[] pointTypes; |
||
128 | double[] pointCoords; |
||
129 | int numTypes;
|
||
130 | int numCoords;
|
||
131 | int windingRule;
|
||
132 | |||
133 | static final int INIT_SIZE = 20; |
||
134 | static final int EXPAND_MAX = 500; |
||
135 | 5747 | fjp | |
136 | private static final int curvesize[] = {2, 2, 4, 6, 0}; |
||
137 | 214 | fernando | |
138 | /**
|
||
139 | * Constructs a new <code>GeneralPathX</code> object.
|
||
140 | * If an operation performed on this path requires the
|
||
141 | * interior of the path to be defined then the default NON_ZERO
|
||
142 | * winding rule is used.
|
||
143 | * @see #WIND_NON_ZERO
|
||
144 | */
|
||
145 | public GeneralPathX() {
|
||
146 | 4835 | fjp | // this(WIND_NON_ZERO, INIT_SIZE, INIT_SIZE);
|
147 | this(WIND_EVEN_ODD, INIT_SIZE, INIT_SIZE);
|
||
148 | 214 | fernando | } |
149 | |||
150 | /**
|
||
151 | * Constructs a new <code>GeneralPathX</code> object with the specified
|
||
152 | * winding rule to control operations that require the interior of the
|
||
153 | * path to be defined.
|
||
154 | * @param rule the winding rule
|
||
155 | * @see #WIND_EVEN_ODD
|
||
156 | * @see #WIND_NON_ZERO
|
||
157 | */
|
||
158 | public GeneralPathX(int rule) { |
||
159 | this(rule, INIT_SIZE, INIT_SIZE);
|
||
160 | } |
||
161 | |||
162 | /**
|
||
163 | * Constructs a new <code>GeneralPathX</code> object with the specified
|
||
164 | * winding rule and the specified initial capacity to store path
|
||
165 | * coordinates. This number is an initial guess as to how many path
|
||
166 | * segments are in the path, but the storage is expanded
|
||
167 | * as needed to store whatever path segments are added to this path.
|
||
168 | * @param rule the winding rule
|
||
169 | * @param initialCapacity the estimate for the number of path segments
|
||
170 | * in the path
|
||
171 | * @see #WIND_EVEN_ODD
|
||
172 | * @see #WIND_NON_ZERO
|
||
173 | */
|
||
174 | public GeneralPathX(int rule, int initialCapacity) { |
||
175 | this(rule, initialCapacity, initialCapacity);
|
||
176 | } |
||
177 | |||
178 | /**
|
||
179 | * Constructs a new <code>GeneralPathX</code> object with the specified
|
||
180 | * winding rule and the specified initial capacities to store point types
|
||
181 | * and coordinates.
|
||
182 | * These numbers are an initial guess as to how many path segments
|
||
183 | * and how many points are to be in the path, but the
|
||
184 | * storage is expanded as needed to store whatever path segments are
|
||
185 | * added to this path.
|
||
186 | * @param rule the winding rule
|
||
187 | * @param initialTypes the estimate for the number of path segments
|
||
188 | * in the path
|
||
189 | * @param initialCapacity the estimate for the number of points
|
||
190 | * @see #WIND_EVEN_ODD
|
||
191 | * @see #WIND_NON_ZERO
|
||
192 | */
|
||
193 | GeneralPathX(int rule, int initialTypes, int initialCoords) { |
||
194 | setWindingRule(rule); |
||
195 | pointTypes = new byte[initialTypes]; |
||
196 | pointCoords = new double[initialCoords * 2]; |
||
197 | } |
||
198 | |||
199 | /**
|
||
200 | * Constructs a new <code>GeneralPathX</code> object from an arbitrary
|
||
201 | * {@link Shape} object.
|
||
202 | * All of the initial geometry and the winding rule for this path are
|
||
203 | * taken from the specified <code>Shape</code> object.
|
||
204 | * @param s the specified <code>Shape</code> object
|
||
205 | */
|
||
206 | public GeneralPathX(Shape s) { |
||
207 | 4835 | fjp | // this(WIND_NON_ZERO, INIT_SIZE, INIT_SIZE);
|
208 | this(WIND_EVEN_ODD, INIT_SIZE, INIT_SIZE);
|
||
209 | 214 | fernando | PathIterator pi = s.getPathIterator(null); |
210 | setWindingRule(pi.getWindingRule()); |
||
211 | append(pi, false);
|
||
212 | } |
||
213 | |||
214 | private void needRoom(int newTypes, int newCoords, boolean needMove) { |
||
215 | if (needMove && numTypes == 0) { |
||
216 | throw new IllegalPathStateException("missing initial moveto "+ |
||
217 | "in path definition");
|
||
218 | } |
||
219 | int size = pointCoords.length;
|
||
220 | if (numCoords + newCoords > size) {
|
||
221 | int grow = size;
|
||
222 | if (grow > EXPAND_MAX * 2) { |
||
223 | grow = EXPAND_MAX * 2;
|
||
224 | } |
||
225 | if (grow < newCoords) {
|
||
226 | grow = newCoords; |
||
227 | } |
||
228 | double[] arr = new double[size + grow]; |
||
229 | System.arraycopy(pointCoords, 0, arr, 0, numCoords); |
||
230 | pointCoords = arr; |
||
231 | } |
||
232 | size = pointTypes.length; |
||
233 | if (numTypes + newTypes > size) {
|
||
234 | int grow = size;
|
||
235 | if (grow > EXPAND_MAX) {
|
||
236 | grow = EXPAND_MAX; |
||
237 | } |
||
238 | if (grow < newTypes) {
|
||
239 | grow = newTypes; |
||
240 | } |
||
241 | byte[] arr = new byte[size + grow]; |
||
242 | System.arraycopy(pointTypes, 0, arr, 0, numTypes); |
||
243 | pointTypes = arr; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /**
|
||
248 | * Adds a point to the path by moving to the specified
|
||
249 | * coordinates.
|
||
250 | * @param x, y the specified coordinates
|
||
251 | */
|
||
252 | public synchronized void moveTo(double x, double y) { |
||
253 | if (numTypes > 0 && pointTypes[numTypes - 1] == SEG_MOVETO) { |
||
254 | pointCoords[numCoords - 2] = x;
|
||
255 | pointCoords[numCoords - 1] = y;
|
||
256 | } else {
|
||
257 | needRoom(1, 2, false); |
||
258 | pointTypes[numTypes++] = SEG_MOVETO; |
||
259 | pointCoords[numCoords++] = x; |
||
260 | pointCoords[numCoords++] = y; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /**
|
||
265 | * Adds a point to the path by drawing a straight line from the
|
||
266 | * current coordinates to the new specified coordinates.
|
||
267 | * @param x, y the specified coordinates
|
||
268 | */
|
||
269 | public synchronized void lineTo(double x, double y) { |
||
270 | needRoom(1, 2, true); |
||
271 | pointTypes[numTypes++] = SEG_LINETO; |
||
272 | pointCoords[numCoords++] = x; |
||
273 | pointCoords[numCoords++] = y; |
||
274 | } |
||
275 | |||
276 | /**
|
||
277 | * Adds a curved segment, defined by two new points, to the path by
|
||
278 | * drawing a Quadratic curve that intersects both the current
|
||
279 | * coordinates and the coordinates (x2, y2), using the
|
||
280 | * specified point (x1, y1) as a quadratic parametric control
|
||
281 | * point.
|
||
282 | * @param x1, y1 the coordinates of the first quadratic control
|
||
283 | * point
|
||
284 | * @param x2, y2 the coordinates of the final endpoint
|
||
285 | */
|
||
286 | public synchronized void quadTo(double x1, double y1, double x2, double y2) { |
||
287 | needRoom(1, 4, true); |
||
288 | pointTypes[numTypes++] = SEG_QUADTO; |
||
289 | pointCoords[numCoords++] = x1; |
||
290 | pointCoords[numCoords++] = y1; |
||
291 | pointCoords[numCoords++] = x2; |
||
292 | pointCoords[numCoords++] = y2; |
||
293 | } |
||
294 | |||
295 | /**
|
||
296 | * Adds a curved segment, defined by three new points, to the path by
|
||
297 | * drawing a Bézier curve that intersects both the current
|
||
298 | * coordinates and the coordinates (x3, y3), using the
|
||
299 | * specified points (x1, y1) and (x2, y2) as
|
||
300 | * Bézier control points.
|
||
301 | * @param x1, y1 the coordinates of the first Béezier
|
||
302 | * control point
|
||
303 | * @param x2, y2 the coordinates of the second Bézier
|
||
304 | * control point
|
||
305 | * @param x3, y3 the coordinates of the final endpoint
|
||
306 | */
|
||
307 | public synchronized void curveTo(double x1, double y1, |
||
308 | double x2, double y2, |
||
309 | double x3, double y3) { |
||
310 | needRoom(1, 6, true); |
||
311 | pointTypes[numTypes++] = SEG_CUBICTO; |
||
312 | pointCoords[numCoords++] = x1; |
||
313 | pointCoords[numCoords++] = y1; |
||
314 | pointCoords[numCoords++] = x2; |
||
315 | pointCoords[numCoords++] = y2; |
||
316 | pointCoords[numCoords++] = x3; |
||
317 | pointCoords[numCoords++] = y3; |
||
318 | } |
||
319 | |||
320 | /**
|
||
321 | * Closes the current subpath by drawing a straight line back to
|
||
322 | * the coordinates of the last <code>moveTo</code>. If the path is already
|
||
323 | * closed then this method has no effect.
|
||
324 | */
|
||
325 | public synchronized void closePath() { |
||
326 | if (numTypes == 0 || pointTypes[numTypes - 1] != SEG_CLOSE) { |
||
327 | needRoom(1, 0, true); |
||
328 | pointTypes[numTypes++] = SEG_CLOSE; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | 6002 | fjp | /**
|
333 | * Check if the first part is closed.
|
||
334 | * @return
|
||
335 | */
|
||
336 | 5747 | fjp | public boolean isClosed() |
337 | { |
||
338 | 7072 | caballero | PathIterator theIterator = getPathIterator(null, FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness); |
339 | 5923 | fjp | double[] theData = new double[6]; |
340 | double xFinal = 0; |
||
341 | double yFinal = 0; |
||
342 | double xIni = 0; |
||
343 | double yIni = 0; |
||
344 | boolean first = true; |
||
345 | 5747 | fjp | |
346 | 5923 | fjp | while (!theIterator.isDone()) {
|
347 | //while not done
|
||
348 | int theType = theIterator.currentSegment(theData);
|
||
349 | 5747 | fjp | |
350 | 5923 | fjp | switch (theType) {
|
351 | case PathIterator.SEG_MOVETO: |
||
352 | xIni = theData[0];
|
||
353 | yIni = theData[1];
|
||
354 | if (!first)
|
||
355 | { |
||
356 | break;
|
||
357 | } |
||
358 | first = false;
|
||
359 | break;
|
||
360 | |||
361 | case PathIterator.SEG_LINETO: |
||
362 | xFinal = theData[0];
|
||
363 | yFinal = theData[1];
|
||
364 | break;
|
||
365 | case PathIterator.SEG_CLOSE: |
||
366 | return true; |
||
367 | |||
368 | } //end switch
|
||
369 | |||
370 | theIterator.next(); |
||
371 | } |
||
372 | if ((xFinal == xIni) && (yFinal == yIni))
|
||
373 | return true; |
||
374 | return false; |
||
375 | |||
376 | |||
377 | |||
378 | // double xFinal = pointCoords[numCoords -2];
|
||
379 | // double yFinal = pointCoords[numCoords -1];
|
||
380 | // double xIni = pointCoords[0];
|
||
381 | // double yIni = pointCoords[1];
|
||
382 | //
|
||
383 | // if (pointTypes[numTypes-1] == SEG_CLOSE)
|
||
384 | // return true;
|
||
385 | // if ((xFinal == xIni) && (yFinal == yIni))
|
||
386 | // return true;
|
||
387 | // return false;
|
||
388 | |||
389 | 5747 | fjp | } |
390 | |||
391 | |||
392 | 214 | fernando | /**
|
393 | * Appends the geometry of the specified <code>Shape</code> object to the
|
||
394 | * path, possibly connecting the new geometry to the existing path
|
||
395 | * segments with a line segment.
|
||
396 | * If the <code>connect</code> parameter is <code>true</code> and the
|
||
397 | * path is not empty then any initial <code>moveTo</code> in the
|
||
398 | * geometry of the appended <code>Shape</code>
|
||
399 | * is turned into a <code>lineTo</code> segment.
|
||
400 | * If the destination coordinates of such a connecting <code>lineTo</code>
|
||
401 | * segment match the ending coordinates of a currently open
|
||
402 | * subpath then the segment is omitted as superfluous.
|
||
403 | * The winding rule of the specified <code>Shape</code> is ignored
|
||
404 | * and the appended geometry is governed by the winding
|
||
405 | * rule specified for this path.
|
||
406 | * @param s the <code>Shape</code> whose geometry is appended
|
||
407 | * to this path
|
||
408 | * @param connect a boolean to control whether or not to turn an
|
||
409 | * initial <code>moveTo</code> segment into a <code>lineTo</code>
|
||
410 | * segment to connect the new geometry to the existing path
|
||
411 | */
|
||
412 | public void append(Shape s, boolean connect) { |
||
413 | PathIterator pi = s.getPathIterator(null); |
||
414 | append(pi,connect); |
||
415 | } |
||
416 | |||
417 | /**
|
||
418 | * Appends the geometry of the specified
|
||
419 | * {@link PathIterator} object
|
||
420 | * to the path, possibly connecting the new geometry to the existing
|
||
421 | * path segments with a line segment.
|
||
422 | * If the <code>connect</code> parameter is <code>true</code> and the
|
||
423 | * path is not empty then any initial <code>moveTo</code> in the
|
||
424 | * geometry of the appended <code>Shape</code> is turned into a
|
||
425 | * <code>lineTo</code> segment.
|
||
426 | * If the destination coordinates of such a connecting <code>lineTo</code>
|
||
427 | * segment match the ending coordinates of a currently open
|
||
428 | * subpath then the segment is omitted as superfluous.
|
||
429 | * The winding rule of the specified <code>Shape</code> is ignored
|
||
430 | * and the appended geometry is governed by the winding
|
||
431 | * rule specified for this path.
|
||
432 | * @param pi the <code>PathIterator</code> whose geometry is appended to
|
||
433 | * this path
|
||
434 | * @param connect a boolean to control whether or not to turn an
|
||
435 | * initial <code>moveTo</code> segment into a <code>lineTo</code> segment
|
||
436 | * to connect the new geometry to the existing path
|
||
437 | */
|
||
438 | public void append(PathIterator pi, boolean connect) { |
||
439 | double coords[] = new double[6]; |
||
440 | while (!pi.isDone()) {
|
||
441 | switch (pi.currentSegment(coords)) {
|
||
442 | case SEG_MOVETO:
|
||
443 | if (!connect || numTypes < 1 || numCoords < 2) { |
||
444 | moveTo(coords[0], coords[1]); |
||
445 | break;
|
||
446 | } |
||
447 | if (pointTypes[numTypes - 1] != SEG_CLOSE && |
||
448 | pointCoords[numCoords - 2] == coords[0] && |
||
449 | pointCoords[numCoords - 1] == coords[1]) |
||
450 | { |
||
451 | // Collapse out initial moveto/lineto
|
||
452 | break;
|
||
453 | } |
||
454 | // NO BREAK;
|
||
455 | case SEG_LINETO:
|
||
456 | lineTo(coords[0], coords[1]); |
||
457 | break;
|
||
458 | case SEG_QUADTO:
|
||
459 | quadTo(coords[0], coords[1], |
||
460 | coords[2], coords[3]); |
||
461 | break;
|
||
462 | case SEG_CUBICTO:
|
||
463 | curveTo(coords[0], coords[1], |
||
464 | coords[2], coords[3], |
||
465 | coords[4], coords[5]); |
||
466 | break;
|
||
467 | case SEG_CLOSE:
|
||
468 | closePath(); |
||
469 | break;
|
||
470 | } |
||
471 | pi.next(); |
||
472 | connect = false;
|
||
473 | } |
||
474 | } |
||
475 | |||
476 | /**
|
||
477 | * Returns the fill style winding rule.
|
||
478 | * @return an integer representing the current winding rule.
|
||
479 | * @see #WIND_EVEN_ODD
|
||
480 | * @see #WIND_NON_ZERO
|
||
481 | * @see #setWindingRule
|
||
482 | */
|
||
483 | public synchronized int getWindingRule() { |
||
484 | return windingRule;
|
||
485 | } |
||
486 | |||
487 | /**
|
||
488 | * Sets the winding rule for this path to the specified value.
|
||
489 | * @param rule an integer representing the specified
|
||
490 | * winding rule
|
||
491 | * @exception <code>IllegalArgumentException</code> if
|
||
492 | * <code>rule</code> is not either
|
||
493 | * <code>WIND_EVEN_ODD</code> or
|
||
494 | * <code>WIND_NON_ZERO</code>
|
||
495 | * @see #WIND_EVEN_ODD
|
||
496 | * @see #WIND_NON_ZERO
|
||
497 | * @see #getWindingRule
|
||
498 | */
|
||
499 | public void setWindingRule(int rule) { |
||
500 | if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO) {
|
||
501 | throw new IllegalArgumentException("winding rule must be "+ |
||
502 | "WIND_EVEN_ODD or "+
|
||
503 | "WIND_NON_ZERO");
|
||
504 | } |
||
505 | windingRule = rule; |
||
506 | } |
||
507 | |||
508 | /**
|
||
509 | * Returns the coordinates most recently added to the end of the path
|
||
510 | * as a {@link Point2D} object.
|
||
511 | * @return a <code>Point2D</code> object containing the ending
|
||
512 | * coordinates of the path or <code>null</code> if there are no points
|
||
513 | * in the path.
|
||
514 | */
|
||
515 | public synchronized Point2D getCurrentPoint() { |
||
516 | if (numTypes < 1 || numCoords < 2) { |
||
517 | return null; |
||
518 | } |
||
519 | int index = numCoords;
|
||
520 | if (pointTypes[numTypes - 1] == SEG_CLOSE) { |
||
521 | loop: |
||
522 | for (int i = numTypes - 2; i > 0; i--) { |
||
523 | switch (pointTypes[i]) {
|
||
524 | case SEG_MOVETO:
|
||
525 | break loop;
|
||
526 | case SEG_LINETO:
|
||
527 | index -= 2;
|
||
528 | break;
|
||
529 | case SEG_QUADTO:
|
||
530 | index -= 4;
|
||
531 | break;
|
||
532 | case SEG_CUBICTO:
|
||
533 | index -= 6;
|
||
534 | break;
|
||
535 | case SEG_CLOSE:
|
||
536 | break;
|
||
537 | } |
||
538 | } |
||
539 | } |
||
540 | return new Point2D.Double(pointCoords[index - 2], |
||
541 | pointCoords[index - 1]);
|
||
542 | } |
||
543 | |||
544 | /**
|
||
545 | * Resets the path to empty. The append position is set back to the
|
||
546 | * beginning of the path and all coordinates and point types are
|
||
547 | * forgotten.
|
||
548 | */
|
||
549 | public synchronized void reset() { |
||
550 | numTypes = numCoords = 0;
|
||
551 | } |
||
552 | |||
553 | /**
|
||
554 | * Transforms the geometry of this path using the specified
|
||
555 | * {@link AffineTransform}.
|
||
556 | * The geometry is transformed in place, which permanently changes the
|
||
557 | * boundary defined by this object.
|
||
558 | * @param at the <code>AffineTransform</code> used to transform the area
|
||
559 | */
|
||
560 | public void transform(AffineTransform at) { |
||
561 | at.transform(pointCoords, 0, pointCoords, 0, numCoords / 2); |
||
562 | } |
||
563 | 885 | fjp | |
564 | public void reProject(ICoordTrans ct) |
||
565 | { |
||
566 | Point2D pt = new Point2D.Double(); |
||
567 | for (int i = 0; i < numCoords; i+=2) |
||
568 | { |
||
569 | pt.setLocation(pointCoords[i], pointCoords[i+1]);
|
||
570 | pt = ct.convert(pt,null);
|
||
571 | pointCoords[i] = pt.getX(); |
||
572 | pointCoords[i+1] = pt.getY();
|
||
573 | } |
||
574 | |||
575 | } |
||
576 | 214 | fernando | |
577 | 2859 | fjp | |
578 | 214 | fernando | /**
|
579 | * Returns a new transformed <code>Shape</code>.
|
||
580 | * @param at the <code>AffineTransform</code> used to transform a
|
||
581 | * new <code>Shape</code>.
|
||
582 | * @return a new <code>Shape</code>, transformed with the specified
|
||
583 | * <code>AffineTransform</code>.
|
||
584 | */
|
||
585 | public synchronized Shape createTransformedShape(AffineTransform at) { |
||
586 | GeneralPathX gp = (GeneralPathX) clone(); |
||
587 | if (at != null) { |
||
588 | gp.transform(at); |
||
589 | } |
||
590 | return gp;
|
||
591 | } |
||
592 | |||
593 | /**
|
||
594 | * Return the bounding box of the path.
|
||
595 | * @return a {@link java.awt.Rectangle} object that
|
||
596 | * bounds the current path.
|
||
597 | */
|
||
598 | public java.awt.Rectangle getBounds() {
|
||
599 | return getBounds2D().getBounds();
|
||
600 | } |
||
601 | |||
602 | /**
|
||
603 | * Returns the bounding box of the path.
|
||
604 | * @return a {@link Rectangle2D} object that
|
||
605 | * bounds the current path.
|
||
606 | */
|
||
607 | public synchronized Rectangle2D getBounds2D() { |
||
608 | double x1, y1, x2, y2;
|
||
609 | int i = numCoords;
|
||
610 | if (i > 0) { |
||
611 | y1 = y2 = pointCoords[--i]; |
||
612 | x1 = x2 = pointCoords[--i]; |
||
613 | while (i > 0) { |
||
614 | double y = pointCoords[--i];
|
||
615 | double x = pointCoords[--i];
|
||
616 | if (x < x1) x1 = x;
|
||
617 | if (y < y1) y1 = y;
|
||
618 | if (x > x2) x2 = x;
|
||
619 | if (y > y2) y2 = y;
|
||
620 | } |
||
621 | } else {
|
||
622 | x1 = y1 = x2 = y2 = 0.0f;
|
||
623 | } |
||
624 | return new Rectangle2D.Double(x1, y1, x2 - x1, y2 - y1); |
||
625 | } |
||
626 | |||
627 | /**
|
||
628 | * Tests if the specified coordinates are inside the boundary of
|
||
629 | * this <code>Shape</code>.
|
||
630 | * @param x, y the specified coordinates
|
||
631 | * @return <code>true</code> if the specified coordinates are inside this
|
||
632 | * <code>Shape</code>; <code>false</code> otherwise
|
||
633 | */
|
||
634 | public boolean contains(double x, double y) { |
||
635 | if (numTypes < 2) { |
||
636 | return false; |
||
637 | } |
||
638 | int cross = Curve.crossingsForPath(getPathIterator(null), x, y); |
||
639 | if (windingRule == WIND_NON_ZERO) {
|
||
640 | return (cross != 0); |
||
641 | } else {
|
||
642 | return ((cross & 1) != 0); |
||
643 | } |
||
644 | } |
||
645 | |||
646 | /**
|
||
647 | * Tests if the specified <code>Point2D</code> is inside the boundary
|
||
648 | * of this <code>Shape</code>.
|
||
649 | * @param p the specified <code>Point2D</code>
|
||
650 | * @return <code>true</code> if this <code>Shape</code> contains the
|
||
651 | * specified <code>Point2D</code>, <code>false</code> otherwise.
|
||
652 | */
|
||
653 | public boolean contains(Point2D p) { |
||
654 | return contains(p.getX(), p.getY());
|
||
655 | } |
||
656 | |||
657 | /**
|
||
658 | * Tests if the specified rectangular area is inside the boundary of
|
||
659 | * this <code>Shape</code>.
|
||
660 | * @param x, y the specified coordinates
|
||
661 | * @param w the width of the specified rectangular area
|
||
662 | * @param h the height of the specified rectangular area
|
||
663 | * @return <code>true</code> if this <code>Shape</code> contains
|
||
664 | * the specified rectangluar area; <code>false</code> otherwise.
|
||
665 | */
|
||
666 | public boolean contains(double x, double y, double w, double h) { |
||
667 | Crossings c = Crossings.findCrossings(getPathIterator(null),
|
||
668 | x, y, x+w, y+h); |
||
669 | return (c != null && c.covers(y, y+h)); |
||
670 | } |
||
671 | |||
672 | /**
|
||
673 | * Tests if the specified <code>Rectangle2D</code>
|
||
674 | * is inside the boundary of this <code>Shape</code>.
|
||
675 | * @param r a specified <code>Rectangle2D</code>
|
||
676 | * @return <code>true</code> if this <code>Shape</code> bounds the
|
||
677 | * specified <code>Rectangle2D</code>; <code>false</code> otherwise.
|
||
678 | */
|
||
679 | public boolean contains(Rectangle2D r) { |
||
680 | return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
|
||
681 | } |
||
682 | |||
683 | /**
|
||
684 | * Tests if the interior of this <code>Shape</code> intersects the
|
||
685 | * interior of a specified set of rectangular coordinates.
|
||
686 | * @param x, y the specified coordinates
|
||
687 | * @param w the width of the specified rectangular coordinates
|
||
688 | * @param h the height of the specified rectangular coordinates
|
||
689 | * @return <code>true</code> if this <code>Shape</code> and the
|
||
690 | * interior of the specified set of rectangular coordinates intersect
|
||
691 | * each other; <code>false</code> otherwise.
|
||
692 | */
|
||
693 | public boolean intersects(double x, double y, double w, double h) { |
||
694 | Crossings c = Crossings.findCrossings(getPathIterator(null),
|
||
695 | x, y, x+w, y+h); |
||
696 | return (c == null || !c.isEmpty()); |
||
697 | } |
||
698 | |||
699 | /**
|
||
700 | * Tests if the interior of this <code>Shape</code> intersects the
|
||
701 | * interior of a specified <code>Rectangle2D</code>.
|
||
702 | * @param r the specified <code>Rectangle2D</code>
|
||
703 | * @return <code>true</code> if this <code>Shape</code> and the interior
|
||
704 | * of the specified <code>Rectangle2D</code> intersect each
|
||
705 | * other; <code>false</code> otherwise.
|
||
706 | */
|
||
707 | public boolean intersects(Rectangle2D r) { |
||
708 | return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
|
||
709 | } |
||
710 | |||
711 | /**
|
||
712 | * Returns a <code>PathIterator</code> object that iterates along the
|
||
713 | * boundary of this <code>Shape</code> and provides access to the
|
||
714 | * geometry of the outline of this <code>Shape</code>.
|
||
715 | * The iterator for this class is not multi-threaded safe,
|
||
716 | * which means that this <code>GeneralPathX</code> class does not
|
||
717 | * guarantee that modifications to the geometry of this
|
||
718 | * <code>GeneralPathX</code> object do not affect any iterations of
|
||
719 | * that geometry that are already in process.
|
||
720 | * @param at an <code>AffineTransform</code>
|
||
721 | * @return a new <code>PathIterator</code> that iterates along the
|
||
722 | * boundary of this <code>Shape</code> and provides access to the
|
||
723 | * geometry of this <code>Shape</code>'s outline
|
||
724 | */
|
||
725 | public PathIterator getPathIterator(AffineTransform at) { |
||
726 | return new GeneralPathXIterator(this, at); |
||
727 | } |
||
728 | |||
729 | /**
|
||
730 | * Returns a <code>PathIterator</code> object that iterates along the
|
||
731 | * boundary of the flattened <code>Shape</code> and provides access to the
|
||
732 | * geometry of the outline of the <code>Shape</code>.
|
||
733 | * The iterator for this class is not multi-threaded safe,
|
||
734 | * which means that this <code>GeneralPathX</code> class does not
|
||
735 | * guarantee that modifications to the geometry of this
|
||
736 | * <code>GeneralPathX</code> object do not affect any iterations of
|
||
737 | * that geometry that are already in process.
|
||
738 | * @param at an <code>AffineTransform</code>
|
||
739 | * @param flatness the maximum distance that the line segments used to
|
||
740 | * approximate the curved segments are allowed to deviate
|
||
741 | * from any point on the original curve
|
||
742 | * @return a new <code>PathIterator</code> that iterates along the flattened
|
||
743 | * <code>Shape</code> boundary.
|
||
744 | */
|
||
745 | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
||
746 | return new FlatteningPathIterator(getPathIterator(at), flatness); |
||
747 | } |
||
748 | |||
749 | /**
|
||
750 | * Creates a new object of the same class as this object.
|
||
751 | *
|
||
752 | * @return a clone of this instance.
|
||
753 | * @exception OutOfMemoryError if there is not enough memory.
|
||
754 | * @see java.lang.Cloneable
|
||
755 | * @since 1.2
|
||
756 | */
|
||
757 | public Object clone() { |
||
758 | try {
|
||
759 | GeneralPathX copy = (GeneralPathX) super.clone();
|
||
760 | copy.pointTypes = (byte[]) pointTypes.clone(); |
||
761 | copy.pointCoords = (double[]) pointCoords.clone(); |
||
762 | return copy;
|
||
763 | } catch (CloneNotSupportedException e) { |
||
764 | // this shouldn't happen, since we are Cloneable
|
||
765 | throw new InternalError(); |
||
766 | } |
||
767 | } |
||
768 | |||
769 | GeneralPathX(int windingRule,
|
||
770 | byte[] pointTypes, |
||
771 | int numTypes,
|
||
772 | double[] pointCoords, |
||
773 | int numCoords) {
|
||
774 | |||
775 | // used to construct from native
|
||
776 | |||
777 | this.windingRule = windingRule;
|
||
778 | this.pointTypes = pointTypes;
|
||
779 | this.numTypes = numTypes;
|
||
780 | this.pointCoords = pointCoords;
|
||
781 | this.numCoords = numCoords;
|
||
782 | } |
||
783 | |||
784 | 5747 | fjp | /**
|
785 | 6040 | fjp | * Convertimos el path a puntos y luego le damos la vuelta.
|
786 | 5747 | fjp | */
|
787 | 214 | fernando | public void flip() |
788 | { |
||
789 | 7072 | caballero | PathIterator theIterator = getPathIterator(null, FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness); |
790 | 6040 | fjp | double[] theData = new double[6]; |
791 | Coordinate first = null;
|
||
792 | CoordinateList coordList = new CoordinateList();
|
||
793 | Coordinate c1; |
||
794 | GeneralPathX newGp = new GeneralPathX();
|
||
795 | ArrayList listOfParts = new ArrayList(); |
||
796 | while (!theIterator.isDone()) {
|
||
797 | //while not done
|
||
798 | int type = theIterator.currentSegment(theData);
|
||
799 | 5747 | fjp | switch (type)
|
800 | { |
||
801 | case SEG_MOVETO:
|
||
802 | 6040 | fjp | coordList = new CoordinateList();
|
803 | listOfParts.add(coordList); |
||
804 | c1= new Coordinate(theData[0], theData[1]); |
||
805 | coordList.add(c1, true);
|
||
806 | 5747 | fjp | break;
|
807 | case SEG_LINETO:
|
||
808 | 6040 | fjp | c1= new Coordinate(theData[0], theData[1]); |
809 | coordList.add(c1, true);
|
||
810 | 5747 | fjp | break;
|
811 | 6040 | fjp | |
812 | case SEG_CLOSE:
|
||
813 | coordList.add(coordList.getCoordinate(0));
|
||
814 | 5747 | fjp | break;
|
815 | |||
816 | } |
||
817 | 6040 | fjp | theIterator.next(); |
818 | } |
||
819 | |||
820 | for (int i=listOfParts.size()-1; i>=0; i--) |
||
821 | { |
||
822 | coordList = (CoordinateList) listOfParts.get(i); |
||
823 | Coordinate[] coords = coordList.toCoordinateArray();
|
||
824 | CoordinateArraySequence seq = new CoordinateArraySequence(coords);
|
||
825 | CoordinateSequences.reverse(seq); |
||
826 | coords = seq.toCoordinateArray(); |
||
827 | newGp.moveTo(coords[0].x, coords[0].y); |
||
828 | for (int j=1; j < coords.length; j++) |
||
829 | { |
||
830 | newGp.lineTo(coords[j].x, coords[j].y); |
||
831 | } |
||
832 | } |
||
833 | reset(); |
||
834 | append(newGp, false);
|
||
835 | 214 | fernando | } |
836 | 5747 | fjp | |
837 | /**
|
||
838 | * Use this function to ensure you get real polygons or holes
|
||
839 | 5893 | fjp | * En JTS, con bCCW = false obtienes un pol?gono exterior.
|
840 | * Nota: Solo se le da la vuelta (si es que lo necesita) al
|
||
841 | * pol?gono exterior. El resto, por ahora, no se tocan.
|
||
842 | * Si se necesita tenerlos en cuenta, habr?a que mirar
|
||
843 | * si est?n dentro del otro, y entonces revisar que tiene
|
||
844 | * un CCW contrario al exterior.
|
||
845 | 5747 | fjp | * @param bCCW true if you want the GeneralPath in CCW order
|
846 | 5893 | fjp | * @return true si se le ha dado la vuelta. (true if flipped)
|
847 | * TODO: TERMINAR ESTO!! NO EST? COMPLETO!! NO sirve para multipoligonos
|
||
848 | 5747 | fjp | */
|
849 | 5893 | fjp | public boolean ensureOrientation(boolean bCCW) { |
850 | 5747 | fjp | byte[] pointTypesAux = new byte[numTypes+1]; |
851 | double[] pointCoordsAux = new double[numCoords+2]; |
||
852 | int i;
|
||
853 | int pointIdx = 0; |
||
854 | |||
855 | 5893 | fjp | Coordinate c1, c2, c3; |
856 | 5747 | fjp | CoordinateList coordList = new CoordinateList();
|
857 | 5893 | fjp | CoordinateList firstList = new CoordinateList();
|
858 | boolean bFirstList = true; |
||
859 | Coordinate cInicio = null;
|
||
860 | 5747 | fjp | |
861 | for (i=0; i< numTypes; i++) |
||
862 | { |
||
863 | int type = pointTypes[i];
|
||
864 | |||
865 | switch (type)
|
||
866 | { |
||
867 | case SEG_MOVETO:
|
||
868 | c1= new Coordinate(pointCoords[pointIdx], pointCoords[pointIdx+1]); |
||
869 | 5893 | fjp | cInicio = c1; |
870 | 5747 | fjp | coordList.add(c1, true);
|
871 | 5893 | fjp | if (i>0) bFirstList = false; |
872 | if (bFirstList)
|
||
873 | { |
||
874 | firstList.add(c1,true);
|
||
875 | } |
||
876 | 5747 | fjp | break;
|
877 | case SEG_LINETO:
|
||
878 | c1= new Coordinate(pointCoords[pointIdx], pointCoords[pointIdx+1]); |
||
879 | coordList.add(c1, true);
|
||
880 | 5893 | fjp | if (bFirstList)
|
881 | { |
||
882 | firstList.add(c1,true);
|
||
883 | } |
||
884 | 5747 | fjp | break;
|
885 | case SEG_QUADTO:
|
||
886 | c1= new Coordinate(pointCoords[pointIdx], pointCoords[pointIdx+1]); |
||
887 | coordList.add(c1, true);
|
||
888 | 5893 | fjp | c2= new Coordinate(pointCoords[pointIdx+2], pointCoords[pointIdx+3]); |
889 | coordList.add(c2, true);
|
||
890 | if (bFirstList)
|
||
891 | { |
||
892 | firstList.add(c1,true);
|
||
893 | firstList.add(c2,true);
|
||
894 | } |
||
895 | |||
896 | 5747 | fjp | break;
|
897 | case SEG_CUBICTO:
|
||
898 | c1= new Coordinate(pointCoords[pointIdx], pointCoords[pointIdx+1]); |
||
899 | coordList.add(c1, true);
|
||
900 | 5893 | fjp | c2= new Coordinate(pointCoords[pointIdx+2], pointCoords[pointIdx+3]); |
901 | coordList.add(c2, true);
|
||
902 | c3= new Coordinate(pointCoords[pointIdx+4], pointCoords[pointIdx+5]); |
||
903 | coordList.add(c3, true);
|
||
904 | if (bFirstList)
|
||
905 | { |
||
906 | firstList.add(c1,true);
|
||
907 | firstList.add(c2,true);
|
||
908 | firstList.add(c3,true);
|
||
909 | } |
||
910 | 5747 | fjp | |
911 | break;
|
||
912 | case SEG_CLOSE:
|
||
913 | 5893 | fjp | coordList.add(cInicio, true);
|
914 | if (bFirstList)
|
||
915 | { |
||
916 | firstList.add(cInicio,true);
|
||
917 | } |
||
918 | 5747 | fjp | break;
|
919 | |||
920 | } |
||
921 | pointIdx += curvesize[type]; |
||
922 | } |
||
923 | // Guardamos el path dandole la vuelta
|
||
924 | Coordinate[] coords = coordList.toCoordinateArray();
|
||
925 | 5893 | fjp | boolean bFlipped = false; |
926 | 5747 | fjp | if (CGAlgorithms.isCCW(coords) != bCCW) // Le damos la vuelta |
927 | { |
||
928 | CoordinateArraySequence seq = new CoordinateArraySequence(coords);
|
||
929 | CoordinateSequences.reverse(seq); |
||
930 | coords = seq.toCoordinateArray(); |
||
931 | |||
932 | |||
933 | // En el primer punto metemos un moveto
|
||
934 | pointCoordsAux[0] = coords[0].x; |
||
935 | pointCoordsAux[1] = coords[0].y; |
||
936 | pointTypesAux[0] = SEG_MOVETO;
|
||
937 | int idx = 2; |
||
938 | i=0;
|
||
939 | int j=1; |
||
940 | for (int k=0; k < coords.length; k++) |
||
941 | { |
||
942 | pointCoordsAux[idx++] = coords[k].x; |
||
943 | pointCoordsAux[idx++] = coords[k].y; |
||
944 | int type = pointTypes[i++];
|
||
945 | pointIdx += curvesize[type]; |
||
946 | switch (type)
|
||
947 | { |
||
948 | case SEG_MOVETO:
|
||
949 | pointTypesAux[j] = SEG_LINETO; |
||
950 | break;
|
||
951 | case SEG_LINETO:
|
||
952 | pointTypesAux[j] = SEG_LINETO; |
||
953 | break;
|
||
954 | case SEG_QUADTO:
|
||
955 | pointTypesAux[j] = SEG_QUADTO; |
||
956 | break;
|
||
957 | case SEG_CUBICTO:
|
||
958 | pointTypesAux[j] = SEG_CUBICTO; |
||
959 | break;
|
||
960 | case SEG_CLOSE:
|
||
961 | // TODO: IMPLEMENTAR ESTO!!!
|
||
962 | break;
|
||
963 | |||
964 | } |
||
965 | j++; |
||
966 | |||
967 | } |
||
968 | |||
969 | pointTypes = pointTypesAux; |
||
970 | pointCoords = pointCoordsAux; |
||
971 | numCoords= numCoords+2;
|
||
972 | numTypes++; |
||
973 | 5893 | fjp | bFlipped = true;
|
974 | 5747 | fjp | |
975 | } |
||
976 | 5893 | fjp | return bFlipped;
|
977 | 5747 | fjp | } |
978 | |||
979 | 6002 | fjp | /**
|
980 | * Check if the first part is CCW.
|
||
981 | * @return
|
||
982 | */
|
||
983 | 5747 | fjp | public boolean isCCW() |
984 | { |
||
985 | int i;
|
||
986 | |||
987 | 7072 | caballero | PathIterator theIterator = getPathIterator(null, FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness); |
988 | 6002 | fjp | double[] theData = new double[6]; |
989 | 6040 | fjp | Coordinate first = null;
|
990 | 6002 | fjp | CoordinateList coordList = new CoordinateList();
|
991 | 5747 | fjp | Coordinate c1; |
992 | 6002 | fjp | boolean bFirst = true; |
993 | while (!theIterator.isDone()) {
|
||
994 | //while not done
|
||
995 | int type = theIterator.currentSegment(theData);
|
||
996 | 5747 | fjp | switch (type)
|
997 | { |
||
998 | case SEG_MOVETO:
|
||
999 | 6002 | fjp | c1= new Coordinate(theData[0], theData[1]); |
1000 | if (bFirst == false) // Ya tenemos la primera parte. |
||
1001 | break;
|
||
1002 | if (bFirst)
|
||
1003 | { |
||
1004 | bFirst=false;
|
||
1005 | 6040 | fjp | first = c1; |
1006 | 6002 | fjp | } |
1007 | 5747 | fjp | coordList.add(c1, true);
|
1008 | break;
|
||
1009 | case SEG_LINETO:
|
||
1010 | 6002 | fjp | c1= new Coordinate(theData[0], theData[1]); |
1011 | 5747 | fjp | coordList.add(c1, true);
|
1012 | break;
|
||
1013 | |||
1014 | } |
||
1015 | 6033 | fjp | theIterator.next(); |
1016 | 6002 | fjp | } |
1017 | 6040 | fjp | coordList.add(first, true);
|
1018 | 5747 | fjp | return CGAlgorithms.isCCW(coordList.toCoordinateArray());
|
1019 | |||
1020 | } |
||
1021 | |||
1022 | 214 | fernando | } |