FlatteningPathIteratorpublic class FlatteningPathIterator extends Object implements PathIteratorThe FlatteningPathIterator class returns a flattened view of
another {@link PathIterator} object. Other {@link java.awt.Shape Shape}
classes can use this class to provide flattening behavior for their paths
without having to perform the interpolation calculations themselves. |
Fields Summary |
---|
static final int | GROW_SIZE | PathIterator | src | double | squareflat | int | limit | double[] | hold | double | curx | double | cury | double | movx | double | movy | int | holdType | int | holdEnd | int | holdIndex | int[] | levels | int | levelIndex | boolean | done |
Constructors Summary |
---|
public FlatteningPathIterator(PathIterator src, double flatness)Constructs a new FlatteningPathIterator object that
flattens a path as it iterates over it. The iterator does not
subdivide any curve read from the source iterator to more than
10 levels of subdivision which yields a maximum of 1024 line
segments per curve. // True when iteration is done
this(src, flatness, 10);
| public FlatteningPathIterator(PathIterator src, double flatness, int limit)Constructs a new FlatteningPathIterator object
that flattens a path as it iterates over it.
The limit parameter allows you to control the
maximum number of recursive subdivisions that the iterator
can make before it assumes that the curve is flat enough
without measuring against the flatness parameter.
The flattened iteration therefore never generates more than
a maximum of (2^limit) line segments per curve.
if (flatness < 0.0) {
throw new IllegalArgumentException("flatness must be >= 0");
}
if (limit < 0) {
throw new IllegalArgumentException("limit must be >= 0");
}
this.src = src;
this.squareflat = flatness * flatness;
this.limit = limit;
this.levels = new int[limit + 1];
// prime the first path segment
next(false);
|
Methods Summary |
---|
public int | currentSegment(float[] coords)Returns the coordinates and type of the current path segment in
the iteration.
The return value is the path segment type:
SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.
A float array of length 6 must be passed in and can be used to
store the coordinates of the point(s).
Each point is stored as a pair of float x,y coordinates.
SEG_MOVETO and SEG_LINETO types return one point,
and SEG_CLOSE does not return any points.
if (isDone()) {
throw new NoSuchElementException("flattening iterator out of bounds");
}
int type = holdType;
if (type != SEG_CLOSE) {
coords[0] = (float) hold[holdIndex + 0];
coords[1] = (float) hold[holdIndex + 1];
if (type != SEG_MOVETO) {
type = SEG_LINETO;
}
}
return type;
| public int | currentSegment(double[] coords)Returns the coordinates and type of the current path segment in
the iteration.
The return value is the path segment type:
SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.
A double array of length 6 must be passed in and can be used to
store the coordinates of the point(s).
Each point is stored as a pair of double x,y coordinates.
SEG_MOVETO and SEG_LINETO types return one point,
and SEG_CLOSE does not return any points.
if (isDone()) {
throw new NoSuchElementException("flattening iterator out of bounds");
}
int type = holdType;
if (type != SEG_CLOSE) {
coords[0] = hold[holdIndex + 0];
coords[1] = hold[holdIndex + 1];
if (type != SEG_MOVETO) {
type = SEG_LINETO;
}
}
return type;
| void | ensureHoldCapacity(int want)
if (holdIndex - want < 0) {
int have = hold.length - holdIndex;
int newsize = hold.length + GROW_SIZE;
double newhold[] = new double[newsize];
System.arraycopy(hold, holdIndex,
newhold, holdIndex + GROW_SIZE,
have);
hold = newhold;
holdIndex += GROW_SIZE;
holdEnd += GROW_SIZE;
}
| public double | getFlatness()Returns the flatness of this iterator.
return Math.sqrt(squareflat);
| public int | getRecursionLimit()Returns the recursion limit of this iterator.
return limit;
| public int | getWindingRule()Returns the winding rule for determining the interior of the
path.
return src.getWindingRule();
| public boolean | isDone()Tests if the iteration is complete.
return done;
| public void | next()Moves the iterator to the next segment of the path forwards
along the primary direction of traversal as long as there are
more points in that direction.
next(true);
| private void | next(boolean doNext)
int level;
if (holdIndex >= holdEnd) {
if (doNext) {
src.next();
}
if (src.isDone()) {
done = true;
return;
}
holdType = src.currentSegment(hold);
levelIndex = 0;
levels[0] = 0;
}
switch (holdType) {
case SEG_MOVETO:
case SEG_LINETO:
curx = hold[0];
cury = hold[1];
if (holdType == SEG_MOVETO) {
movx = curx;
movy = cury;
}
holdIndex = 0;
holdEnd = 0;
break;
case SEG_CLOSE:
curx = movx;
cury = movy;
holdIndex = 0;
holdEnd = 0;
break;
case SEG_QUADTO:
if (holdIndex >= holdEnd) {
// Move the coordinates to the end of the array.
holdIndex = hold.length - 6;
holdEnd = hold.length - 2;
hold[holdIndex + 0] = curx;
hold[holdIndex + 1] = cury;
hold[holdIndex + 2] = hold[0];
hold[holdIndex + 3] = hold[1];
hold[holdIndex + 4] = curx = hold[2];
hold[holdIndex + 5] = cury = hold[3];
}
level = levels[levelIndex];
while (level < limit) {
if (QuadCurve2D.getFlatnessSq(hold, holdIndex) < squareflat) {
break;
}
ensureHoldCapacity(4);
QuadCurve2D.subdivide(hold, holdIndex,
hold, holdIndex - 4,
hold, holdIndex);
holdIndex -= 4;
// Now that we have subdivided, we have constructed
// two curves of one depth lower than the original
// curve. One of those curves is in the place of
// the former curve and one of them is in the next
// set of held coordinate slots. We now set both
// curves level values to the next higher level.
level++;
levels[levelIndex] = level;
levelIndex++;
levels[levelIndex] = level;
}
// This curve segment is flat enough, or it is too deep
// in recursion levels to try to flatten any more. The
// two coordinates at holdIndex+4 and holdIndex+5 now
// contain the endpoint of the curve which can be the
// endpoint of an approximating line segment.
holdIndex += 4;
levelIndex--;
break;
case SEG_CUBICTO:
if (holdIndex >= holdEnd) {
// Move the coordinates to the end of the array.
holdIndex = hold.length - 8;
holdEnd = hold.length - 2;
hold[holdIndex + 0] = curx;
hold[holdIndex + 1] = cury;
hold[holdIndex + 2] = hold[0];
hold[holdIndex + 3] = hold[1];
hold[holdIndex + 4] = hold[2];
hold[holdIndex + 5] = hold[3];
hold[holdIndex + 6] = curx = hold[4];
hold[holdIndex + 7] = cury = hold[5];
}
level = levels[levelIndex];
while (level < limit) {
if (CubicCurve2D.getFlatnessSq(hold, holdIndex) < squareflat) {
break;
}
ensureHoldCapacity(6);
CubicCurve2D.subdivide(hold, holdIndex,
hold, holdIndex - 6,
hold, holdIndex);
holdIndex -= 6;
// Now that we have subdivided, we have constructed
// two curves of one depth lower than the original
// curve. One of those curves is in the place of
// the former curve and one of them is in the next
// set of held coordinate slots. We now set both
// curves level values to the next higher level.
level++;
levels[levelIndex] = level;
levelIndex++;
levels[levelIndex] = level;
}
// This curve segment is flat enough, or it is too deep
// in recursion levels to try to flatten any more. The
// two coordinates at holdIndex+6 and holdIndex+7 now
// contain the endpoint of the curve which can be the
// endpoint of an approximating line segment.
holdIndex += 6;
levelIndex--;
break;
}
|
|