001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.geometry.jts;
017:
018: import java.awt.geom.AffineTransform;
019: import java.awt.geom.PathIterator;
020:
021: import com.vividsolutions.jts.geom.CoordinateSequence;
022: import com.vividsolutions.jts.geom.LineString;
023:
024: /**
025: * A path iterator for the LiteShape class, specialized to iterate over
026: * LineString object.
027: *
028: * @author Andrea Aime
029: * @author simone giannecchini
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/geometry/jts/LineIterator2.java $
031: * @version $Id: LineIterator2.java 25075 2007-04-09 19:20:46Z desruisseaux $
032: */
033: public final class LineIterator2 implements PathIterator {
034:
035: private double[] allCoords;
036:
037: /** Transform applied on the coordinates during iteration */
038: private AffineTransform at;
039:
040: /** Current line coordinate */
041: private int currentCoord = 0;
042: private int actualCoords; // numb coordinates
043:
044: /** True when the iteration is terminated */
045: private boolean done = false;
046:
047: public LineIterator2() {
048:
049: }
050:
051: /**
052: * @see java.awt.geom.PathIterator#currentSegment(float[])
053: */
054: public int currentSegment(float[] coords) {
055: if (currentCoord == 0) {
056: coords[0] = (float) allCoords[0];
057: coords[1] = (float) allCoords[1];
058: if (at != null)
059: at.transform(coords, 0, coords, 0, 1);
060: return SEG_MOVETO;
061: } else {
062: coords[0] = (float) allCoords[currentCoord * 2];
063: coords[1] = (float) allCoords[currentCoord * 2 + 1];
064: if (at != null)
065: at.transform(coords, 0, coords, 0, 1);
066: return SEG_LINETO;
067: }
068: }
069:
070: /**
071: * @param ls
072: * @param at
073: *
074: */
075: public void init(LineString ls, AffineTransform at) {
076:
077: if ((at == null) || (at.isIdentity())) {
078: this .at = null;
079: } else {
080: this .at = at;
081: }
082:
083: CoordinateSequence coordinates = ls.getCoordinateSequence();
084: if (coordinates instanceof LiteCoordinateSequence) {
085: //array already there for us
086: allCoords = ((LiteCoordinateSequence) coordinates)
087: .getXYArray();
088: actualCoords = coordinates.size();
089: } else {
090: //build the array
091: actualCoords = coordinates.size();
092: allCoords = new double[actualCoords * 2];
093: for (int t = 0; t < actualCoords; t++) {
094: allCoords[t * 2] = coordinates.getOrdinate(t, 0);
095: allCoords[t * 2 + 1] = coordinates.getOrdinate(t, 1);
096: }
097: }
098:
099: done = false;
100: currentCoord = 0;
101: }
102:
103: /**
104: * Returns the winding rule for determining the interior of the path.
105: *
106: * @return the winding rule.
107: *
108: * @see #WIND_EVEN_ODD
109: * @see #WIND_NON_ZERO
110: */
111: public int getWindingRule() {
112: return WIND_NON_ZERO;
113: }
114:
115: /**
116: * Tests if the iteration is complete.
117: *
118: * @return <code>true</code> if all the segments have been read;
119: * <code>false</code> otherwise.
120: */
121: public boolean isDone() {
122: return done;
123: }
124:
125: /**
126: * Moves the iterator to the next segment of the path forwards along the
127: * primary direction of traversal as long as there are more points in that
128: * direction.
129: */
130: public void next() {
131: if (currentCoord == (actualCoords - 1)) {
132: done = true;
133: } else {
134: currentCoord++;
135: }
136: }
137:
138: /**
139: * @see java.awt.geom.PathIterator#currentSegment(double[])
140: */
141: public int currentSegment(double[] coords) {
142: float[] fco = new float[6];
143: int result = currentSegment(fco);
144: coords[0] = fco[0];
145: coords[1] = fco[1];
146: coords[2] = fco[2];
147: coords[3] = fco[3];
148: return result;
149: }
150:
151: }
|