Source Code Cross Referenced for FlatteningPathIterator.java in  » 6.0-JDK-Core » AWT » java » awt » geom » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » AWT » java.awt.geom 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-1998 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.awt.geom;
027
028        import java.util.*;
029
030        /**
031         * The <code>FlatteningPathIterator</code> class returns a flattened view of
032         * another {@link PathIterator} object.  Other {@link java.awt.Shape Shape} 
033         * classes can use this class to provide flattening behavior for their paths
034         * without having to perform the interpolation calculations themselves.
035         *
036         * @version 1.6 06/29/98
037         * @author Jim Graham
038         */
039        public class FlatteningPathIterator implements  PathIterator {
040            static final int GROW_SIZE = 24; // Multiple of cubic & quad curve size
041
042            PathIterator src; // The source iterator
043
044            double squareflat; // Square of the flatness parameter
045            // for testing against squared lengths
046
047            int limit; // Maximum number of recursion levels
048
049            double hold[] = new double[14]; // The cache of interpolated coords
050            // Note that this must be long enough
051            // to store a full cubic segment and
052            // a relative cubic segment to avoid
053            // aliasing when copying the coords
054            // of a curve to the end of the array.
055            // This is also serendipitously equal
056            // to the size of a full quad segment
057            // and 2 relative quad segments.
058
059            double curx, cury; // The ending x,y of the last segment
060
061            double movx, movy; // The x,y of the last move segment
062
063            int holdType; // The type of the curve being held
064            // for interpolation
065
066            int holdEnd; // The index of the last curve segment
067            // being held for interpolation
068
069            int holdIndex; // The index of the curve segment
070            // that was last interpolated.  This
071            // is the curve segment ready to be
072            // returned in the next call to
073            // currentSegment().
074
075            int levels[]; // The recursion level at which
076            // each curve being held in storage
077            // was generated.
078
079            int levelIndex; // The index of the entry in the
080            // levels array of the curve segment
081            // at the holdIndex
082
083            boolean done; // True when iteration is done
084
085            /**
086             * Constructs a new <code>FlatteningPathIterator</code> object that 
087             * flattens a path as it iterates over it.  The iterator does not 
088             * subdivide any curve read from the source iterator to more than 
089             * 10 levels of subdivision which yields a maximum of 1024 line 
090             * segments per curve.
091             * @param src the original unflattened path being iterated over
092             * @param flatness the maximum allowable distance between the
093             * control points and the flattened curve
094             */
095            public FlatteningPathIterator(PathIterator src, double flatness) {
096                this (src, flatness, 10);
097            }
098
099            /**
100             * Constructs a new <code>FlatteningPathIterator</code> object 
101             * that flattens a path as it iterates over it.
102             * The <code>limit</code> parameter allows you to control the
103             * maximum number of recursive subdivisions that the iterator
104             * can make before it assumes that the curve is flat enough
105             * without measuring against the <code>flatness</code> parameter.
106             * The flattened iteration therefore never generates more than
107             * a maximum of <code>(2^limit)</code> line segments per curve.
108             * @param src the original unflattened path being iterated over
109             * @param flatness the maximum allowable distance between the
110             * control points and the flattened curve
111             * @param limit the maximum number of recursive subdivisions
112             * allowed for any curved segment
113             * @exception <code>IllegalArgumentException</code> if 
114             * 		<code>flatness</code> or <code>limit</code>
115             *		is less than zero
116             */
117            public FlatteningPathIterator(PathIterator src, double flatness,
118                    int limit) {
119                if (flatness < 0.0) {
120                    throw new IllegalArgumentException("flatness must be >= 0");
121                }
122                if (limit < 0) {
123                    throw new IllegalArgumentException("limit must be >= 0");
124                }
125                this .src = src;
126                this .squareflat = flatness * flatness;
127                this .limit = limit;
128                this .levels = new int[limit + 1];
129                // prime the first path segment
130                next(false);
131            }
132
133            /**
134             * Returns the flatness of this iterator.
135             * @return the flatness of this <code>FlatteningPathIterator</code>.
136             */
137            public double getFlatness() {
138                return Math.sqrt(squareflat);
139            }
140
141            /**
142             * Returns the recursion limit of this iterator.
143             * @return the recursion limit of this 
144             * <code>FlatteningPathIterator</code>.
145             */
146            public int getRecursionLimit() {
147                return limit;
148            }
149
150            /**
151             * Returns the winding rule for determining the interior of the
152             * path.
153             * @return the winding rule of the original unflattened path being
154             * iterated over.
155             * @see PathIterator#WIND_EVEN_ODD
156             * @see PathIterator#WIND_NON_ZERO
157             */
158            public int getWindingRule() {
159                return src.getWindingRule();
160            }
161
162            /**
163             * Tests if the iteration is complete.
164             * @return <code>true</code> if all the segments have
165             * been read; <code>false</code> otherwise.
166             */
167            public boolean isDone() {
168                return done;
169            }
170
171            /*
172             * Ensures that the hold array can hold up to (want) more values.
173             * It is currently holding (hold.length - holdIndex) values.
174             */
175            void ensureHoldCapacity(int want) {
176                if (holdIndex - want < 0) {
177                    int have = hold.length - holdIndex;
178                    int newsize = hold.length + GROW_SIZE;
179                    double newhold[] = new double[newsize];
180                    System.arraycopy(hold, holdIndex, newhold, holdIndex
181                            + GROW_SIZE, have);
182                    hold = newhold;
183                    holdIndex += GROW_SIZE;
184                    holdEnd += GROW_SIZE;
185                }
186            }
187
188            /**
189             * Moves the iterator to the next segment of the path forwards
190             * along the primary direction of traversal as long as there are
191             * more points in that direction.
192             */
193            public void next() {
194                next(true);
195            }
196
197            private void next(boolean doNext) {
198                int level;
199
200                if (holdIndex >= holdEnd) {
201                    if (doNext) {
202                        src.next();
203                    }
204                    if (src.isDone()) {
205                        done = true;
206                        return;
207                    }
208                    holdType = src.currentSegment(hold);
209                    levelIndex = 0;
210                    levels[0] = 0;
211                }
212
213                switch (holdType) {
214                case SEG_MOVETO:
215                case SEG_LINETO:
216                    curx = hold[0];
217                    cury = hold[1];
218                    if (holdType == SEG_MOVETO) {
219                        movx = curx;
220                        movy = cury;
221                    }
222                    holdIndex = 0;
223                    holdEnd = 0;
224                    break;
225                case SEG_CLOSE:
226                    curx = movx;
227                    cury = movy;
228                    holdIndex = 0;
229                    holdEnd = 0;
230                    break;
231                case SEG_QUADTO:
232                    if (holdIndex >= holdEnd) {
233                        // Move the coordinates to the end of the array.
234                        holdIndex = hold.length - 6;
235                        holdEnd = hold.length - 2;
236                        hold[holdIndex + 0] = curx;
237                        hold[holdIndex + 1] = cury;
238                        hold[holdIndex + 2] = hold[0];
239                        hold[holdIndex + 3] = hold[1];
240                        hold[holdIndex + 4] = curx = hold[2];
241                        hold[holdIndex + 5] = cury = hold[3];
242                    }
243
244                    level = levels[levelIndex];
245                    while (level < limit) {
246                        if (QuadCurve2D.getFlatnessSq(hold, holdIndex) < squareflat) {
247                            break;
248                        }
249
250                        ensureHoldCapacity(4);
251                        QuadCurve2D.subdivide(hold, holdIndex, hold,
252                                holdIndex - 4, hold, holdIndex);
253                        holdIndex -= 4;
254
255                        // Now that we have subdivided, we have constructed
256                        // two curves of one depth lower than the original
257                        // curve.  One of those curves is in the place of
258                        // the former curve and one of them is in the next
259                        // set of held coordinate slots.  We now set both
260                        // curves level values to the next higher level.
261                        level++;
262                        levels[levelIndex] = level;
263                        levelIndex++;
264                        levels[levelIndex] = level;
265                    }
266
267                    // This curve segment is flat enough, or it is too deep
268                    // in recursion levels to try to flatten any more.  The
269                    // two coordinates at holdIndex+4 and holdIndex+5 now
270                    // contain the endpoint of the curve which can be the
271                    // endpoint of an approximating line segment.
272                    holdIndex += 4;
273                    levelIndex--;
274                    break;
275                case SEG_CUBICTO:
276                    if (holdIndex >= holdEnd) {
277                        // Move the coordinates to the end of the array.
278                        holdIndex = hold.length - 8;
279                        holdEnd = hold.length - 2;
280                        hold[holdIndex + 0] = curx;
281                        hold[holdIndex + 1] = cury;
282                        hold[holdIndex + 2] = hold[0];
283                        hold[holdIndex + 3] = hold[1];
284                        hold[holdIndex + 4] = hold[2];
285                        hold[holdIndex + 5] = hold[3];
286                        hold[holdIndex + 6] = curx = hold[4];
287                        hold[holdIndex + 7] = cury = hold[5];
288                    }
289
290                    level = levels[levelIndex];
291                    while (level < limit) {
292                        if (CubicCurve2D.getFlatnessSq(hold, holdIndex) < squareflat) {
293                            break;
294                        }
295
296                        ensureHoldCapacity(6);
297                        CubicCurve2D.subdivide(hold, holdIndex, hold,
298                                holdIndex - 6, hold, holdIndex);
299                        holdIndex -= 6;
300
301                        // Now that we have subdivided, we have constructed
302                        // two curves of one depth lower than the original
303                        // curve.  One of those curves is in the place of
304                        // the former curve and one of them is in the next
305                        // set of held coordinate slots.  We now set both
306                        // curves level values to the next higher level.
307                        level++;
308                        levels[levelIndex] = level;
309                        levelIndex++;
310                        levels[levelIndex] = level;
311                    }
312
313                    // This curve segment is flat enough, or it is too deep
314                    // in recursion levels to try to flatten any more.  The
315                    // two coordinates at holdIndex+6 and holdIndex+7 now
316                    // contain the endpoint of the curve which can be the
317                    // endpoint of an approximating line segment.
318                    holdIndex += 6;
319                    levelIndex--;
320                    break;
321                }
322            }
323
324            /**
325             * Returns the coordinates and type of the current path segment in
326             * the iteration.
327             * The return value is the path segment type:
328             * SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.
329             * A float array of length 6 must be passed in and can be used to
330             * store the coordinates of the point(s).
331             * Each point is stored as a pair of float x,y coordinates.
332             * SEG_MOVETO and SEG_LINETO types return one point,
333             * and SEG_CLOSE does not return any points.
334             * @param coords an array that holds the data returned from
335             * this method
336             * @return the path segment type of the current path segment.
337             * @exception <code>NoSuchElementException</code> if there
338             *		are no more elements in the flattening path to be
339             *		returned.
340             * @see PathIterator#SEG_MOVETO
341             * @see PathIterator#SEG_LINETO
342             * @see PathIterator#SEG_CLOSE
343             */
344            public int currentSegment(float[] coords) {
345                if (isDone()) {
346                    throw new NoSuchElementException(
347                            "flattening iterator out of bounds");
348                }
349                int type = holdType;
350                if (type != SEG_CLOSE) {
351                    coords[0] = (float) hold[holdIndex + 0];
352                    coords[1] = (float) hold[holdIndex + 1];
353                    if (type != SEG_MOVETO) {
354                        type = SEG_LINETO;
355                    }
356                }
357                return type;
358            }
359
360            /**
361             * Returns the coordinates and type of the current path segment in
362             * the iteration.
363             * The return value is the path segment type:
364             * SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.
365             * A double array of length 6 must be passed in and can be used to
366             * store the coordinates of the point(s).
367             * Each point is stored as a pair of double x,y coordinates.
368             * SEG_MOVETO and SEG_LINETO types return one point,
369             * and SEG_CLOSE does not return any points.
370             * @param coords an array that holds the data returned from
371             * this method
372             * @return the path segment type of the current path segment.
373             * @exception <code>NoSuchElementException</code> if there
374             *          are no more elements in the flattening path to be
375             *          returned.
376             * @see PathIterator#SEG_MOVETO
377             * @see PathIterator#SEG_LINETO
378             * @see PathIterator#SEG_CLOSE
379             */
380            public int currentSegment(double[] coords) {
381                if (isDone()) {
382                    throw new NoSuchElementException(
383                            "flattening iterator out of bounds");
384                }
385                int type = holdType;
386                if (type != SEG_CLOSE) {
387                    coords[0] = hold[holdIndex + 0];
388                    coords[1] = hold[holdIndex + 1];
389                    if (type != SEG_MOVETO) {
390                        type = SEG_LINETO;
391                    }
392                }
393                return type;
394            }
395        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.