*Sascha
Brawer, November 2003*

This document describes the GNU implementation of the class
`java.awt.geom.FlatteningPathIterator`

. It does
*not* describe how a programmer should use this class; please
refer to the generated API documentation for this purpose. Instead, it
is intended for maintenance programmers who want to understand the
implementation, for example because they want to extend the class or
fix a bug.

The algorithm uses a stack. Its allocation is delayed to the time
when the source path iterator actually returns the first curved
segment (either `SEG_QUADTO`

or `SEG_CUBICTO`

).
If the input path does not contain any curved segments, the value of
the `stack`

variable stays `null`

. In this quite
common case, the memory consumption is minimal.

`stack`

- The variable
`stack`

is a`double`

array that holds the start, control and end points of individual sub-segments. `recLevel`

- The variable
`recLevel`

holds how many recursive sub-divisions were needed to calculate a segment. The original curve has recursion level 0. For each sub-division, the corresponding recursion level is increased by one. `stackSize`

- Finally, the variable
`stackSize`

indicates how many sub-segments are stored on the stack.

The implementation separately processes each segment that the base iterator returns.

In the case of `SEG_CLOSE`

,
`SEG_MOVETO`

and `SEG_LINETO`

segments, the
implementation simply hands the segment to the consumer, without actually
doing anything.

Any `SEG_QUADTO`

and `SEG_CUBICTO`

segments
need to be flattened. Flattening is performed with a fixed-sized
stack, holding the coordinates of subdivided segments. When the base
iterator returns a `SEG_QUADTO`

and
`SEG_CUBICTO`

segments, it is recursively flattened as
follows:

- Intialization: Allocate memory for the stack (unless a
sufficiently large stack has been allocated previously). Push the
original quadratic or cubic curve onto the stack. Mark that segment as
having a
`recLevel`

of zero. - If the stack is empty, flattening the segment is complete, and the next segment is fetched from the base iterator.
- If the stack is not empty, pop a curve segment from the
stack.
- If its
`recLevel`

exceeds the recursion limit, hand the current segment to the consumer. - Calculate the squared flatness of the segment. If it smaller
than
`flatnessSq`

, hand the current segment to the consumer. - Otherwise, split the segment in two halves. Push the right half onto the stack. Then, push the left half onto the stack. Continue with step two.

- If its

The implementation is slightly complicated by the fact that
consumers *pull* the flattened segments from the
`FlatteningPathIterator`

. This means that we actually
cannot “hand the curent segment over to the consumer.”
But the algorithm is easier to understand if one assumes a
*push* paradigm.

The following example shows how a
`FlatteningPathIterator`

processes a
`SEG_QUADTO`

segment. It is (arbitrarily) assumed that the
recursion limit was set to 2.

A B C D E F G H `stack[0]`

— — S_{ll}.x— — — — — `stack[1]`

— — S_{ll}.y— — — — — `stack[2]`

— — C_{ll}.x— — — — — `stack[3]`

— — C_{ll}.y— — — — — `stack[4]`

— S_{l}.xE=_{ll}.xS_{lr}.xS_{lr}.x— S_{rl}.x— — `stack[5]`

— S_{l}.yE=_{ll}.xS_{lr}.yS_{lr}.y— S_{rl}.y— — `stack[6]`

— C_{l}.xC_{lr}.xC_{lr}.x— C_{rl}.x— — `stack[7]`

— C_{l}.yC_{lr}.yC_{lr}.y— C_{rl}.y— — `stack[8]`

S.xE=_{l}.xS_{r}.xE=_{lr}.xS_{r}.xE=_{lr}.xS_{r}.xS_{r}.xE=_{rl}.xS_{rr}.xS_{rr}.x— `stack[9]`

S.yE=_{l}.yS_{r}.yE=_{lr}.yS_{r}.yE=_{lr}.yS_{r}.yS_{r}.yE=_{rl}.yS_{rr}.yS_{rr}.y— `stack[10]`

C.xC_{r}.xC_{r}.xC_{r}.xC_{r}.xC_{rr}.xC_{rr}.x— `stack[11]`

C.yC_{r}.yC_{r}.yC_{r}.yC_{r}.yC_{rr}.yC_{rr}.y— `stack[12]`

E.xE_{r}.xE_{r}.xE_{r}.xE_{r}.xE_{rr}.xE_{rr}.x— `stack[13]`

E.yE_{r}.yE_{r}.yE_{r}.yE_{r}.yE_{rr}.yE_{rr}.x— `stackSize`

1 2 3 2 1 2 1 0 `recLevel[2]`

— — 2 — — — — — `recLevel[1]`

— 1 2 2 — 2 — — `recLevel[0]`

0 1 1 1 1 2 2 —

- The data structures are initialized as follows.
- The segment’s end point
*E*, control point*C*, and start point*S*are pushed onto the stack. - Currently, the curve in the stack would be approximated by one
single straight line segment (
*S*–*E*). Therefore,`stackSize`

is set to 1. - This single straight line segment is approximating the original
curve, which can be seen as the result of zero recursive
splits. Therefore,
`recLevel[0]`

is set to zero.

- The segment’s end point
- The algorithm proceeds by taking the topmost curve segment
(
*S*–*C*–*E*) from the stack.- The recursion level of this segment (stored in
`recLevel[0]`

) is zero, which is smaller than the limit 2. - The method
`java.awt.geom.QuadCurve2D.getFlatnessSq`

is called to calculate the squared flatness. - For the sake of argument, we assume that the squared flatness is
exceeding the threshold stored in
`flatnessSq`

. Thus, the curve segment*S*–*C*–*E*gets subdivided into a left and a right half, namely*S*–_{l}*C*–_{l}*E*and_{l}*S*–_{r}*C*–_{r}*E*. Both halves are pushed onto the stack, so the left half is now on top._{r}

The left half starts at the same point as the original curve, so*S*has the same coordinates as_{l}*S*. Similarly, the end point of the right half and of the original curve are identical (*E*=_{r}*E*). More interestingly, the left half ends where the right half starts. Because*E*=_{l}*S*, their coordinates need to be stored only once, which amounts to saving 16 bytes (two_{r}`double`

values) for each iteration.

- The recursion level of this segment (stored in
- Again, the topmost curve segment (
*S*–_{l}*C*–_{l}*E*) is taken from the stack._{l}- The recursion level of this segment (stored in
`recLevel[1]`

) is 1, which is smaller than the limit 2. - The method
`java.awt.geom.QuadCurve2D.getFlatnessSq`

is called to calculate the squared flatness. - Assuming that the segment is still not considered
flat enough, it gets subdivided into a left
(
*S*–_{ll}*C*–_{ll}*E*) and a right (_{ll}*S*–_{lr}*C*–_{lr}*E*) half._{lr}

- The recursion level of this segment (stored in
- The topmost curve segment (
*S*–_{ll}*C*–_{ll}*E*) is popped from the stack._{ll}- The recursion level of this segment (stored in
`recLevel[2]`

) is 2, which is*not*smaller than the limit 2. Therefore, a`SEG_LINETO`

(from*S*to_{ll}*E*) is passed to the consumer._{ll}

- The recursion level of this segment (stored in
- The topmost curve segment (
*S*–_{lr}*C*–_{lr}*E*) is popped from the stack._{lr}- The recursion level of this segment (stored in
`recLevel[1]`

) is 2, which is*not*smaller than the limit 2. Therefore, a`SEG_LINETO`

(from*S*to_{lr}*E*) is passed to the consumer._{lr}

- The recursion level of this segment (stored in
- The algorithm proceeds by taking the topmost curve segment
(
*S*–_{r}*C*–_{r}*E*) from the stack._{r}- The recursion level of this segment (stored in
`recLevel[0]`

) is 1, which is smaller than the limit 2. - The method
`java.awt.geom.QuadCurve2D.getFlatnessSq`

is called to calculate the squared flatness. - For the sake of argument, we again assume that the squared
flatness is exceeding the threshold stored in
`flatnessSq`

. Thus, the curve segment (*S*–_{r}*C*–_{r}*E*) is subdivided into a left and a right half, namely_{r}*S*–_{rl}*C*–_{rl}*E*and_{rl}*S*–_{rr}*C*–_{rr}*E*. Both halves are pushed onto the stack._{rr}

- The recursion level of this segment (stored in
- The topmost curve segment (
*S*–_{rl}*C*–_{rl}*E*) is popped from the stack._{rl}- The recursion level of this segment (stored in
`recLevel[2]`

) is 2, which is*not*smaller than the limit 2. Therefore, a`SEG_LINETO`

(from*S*to_{rl}*E*) is passed to the consumer._{rl}

- The recursion level of this segment (stored in
- The topmost curve segment (
*S*–_{rr}*C*–_{rr}*E*) is popped from the stack._{rr}- The recursion level of this segment (stored in
`recLevel[2]`

) is 2, which is*not*smaller than the limit 2. Therefore, a`SEG_LINETO`

(from*S*to_{rr}*E*) is passed to the consumer._{rr}

- The recursion level of this segment (stored in
- The stack is now empty. The FlatteningPathIterator will fetch the next segment from the base iterator, and process it.

In order to split the most recently pushed segment, the
`subdivideQuadratic()`

method passes `stack`

directly to
`QuadCurve2D.subdivide(double[],int,double[],int,double[],int)`

.
Because the stack grows towards the beginning of the array, no data
needs to be copied around: `subdivide`

will directly store
the result into the stack, which will have the contents shown to the
right.