aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * @test aoqi@0: * @bug 7047069 aoqi@0: * @summary Array can dynamically change size when assigned to an object field aoqi@0: * aoqi@0: * @run main/othervm -Xbatch Test7047069 aoqi@0: */ aoqi@0: aoqi@0: import java.util.*; aoqi@0: import java.awt.geom.*; aoqi@0: aoqi@0: public class Test7047069 { aoqi@0: static boolean verbose; aoqi@0: aoqi@0: static final int GROW_SIZE = 24; // Multiple of cubic & quad curve size aoqi@0: aoqi@0: float squareflat; // Square of the flatness parameter aoqi@0: // for testing against squared lengths aoqi@0: aoqi@0: int limit; // Maximum number of recursion levels aoqi@0: aoqi@0: float hold[] = new float[14]; // The cache of interpolated coords aoqi@0: // Note that this must be long enough aoqi@0: // to store a full cubic segment and aoqi@0: // a relative cubic segment to avoid aoqi@0: // aliasing when copying the coords aoqi@0: // of a curve to the end of the array. aoqi@0: // This is also serendipitously equal aoqi@0: // to the size of a full quad segment aoqi@0: // and 2 relative quad segments. aoqi@0: aoqi@0: int holdEnd; // The index of the last curve segment aoqi@0: // being held for interpolation aoqi@0: aoqi@0: int holdIndex; // The index of the curve segment aoqi@0: // that was last interpolated. This aoqi@0: // is the curve segment ready to be aoqi@0: // returned in the next call to aoqi@0: // currentSegment(). aoqi@0: aoqi@0: int levels[]; // The recursion level at which aoqi@0: // each curve being held in storage aoqi@0: // was generated. aoqi@0: aoqi@0: int levelIndex; // The index of the entry in the aoqi@0: // levels array of the curve segment aoqi@0: // at the holdIndex aoqi@0: aoqi@0: public static void subdivide(float src[], int srcoff, aoqi@0: float left[], int leftoff, aoqi@0: float right[], int rightoff) aoqi@0: { aoqi@0: float x1 = src[srcoff + 0]; aoqi@0: float y1 = src[srcoff + 1]; aoqi@0: float ctrlx = src[srcoff + 2]; aoqi@0: float ctrly = src[srcoff + 3]; aoqi@0: float x2 = src[srcoff + 4]; aoqi@0: float y2 = src[srcoff + 5]; aoqi@0: if (left != null) { aoqi@0: left[leftoff + 0] = x1; aoqi@0: left[leftoff + 1] = y1; aoqi@0: } aoqi@0: if (right != null) { aoqi@0: right[rightoff + 4] = x2; aoqi@0: right[rightoff + 5] = y2; aoqi@0: } aoqi@0: x1 = (x1 + ctrlx) / 2f; aoqi@0: y1 = (y1 + ctrly) / 2f; aoqi@0: x2 = (x2 + ctrlx) / 2f; aoqi@0: y2 = (y2 + ctrly) / 2f; aoqi@0: ctrlx = (x1 + x2) / 2f; aoqi@0: ctrly = (y1 + y2) / 2f; aoqi@0: if (left != null) { aoqi@0: left[leftoff + 2] = x1; aoqi@0: left[leftoff + 3] = y1; aoqi@0: left[leftoff + 4] = ctrlx; aoqi@0: left[leftoff + 5] = ctrly; aoqi@0: } aoqi@0: if (right != null) { aoqi@0: right[rightoff + 0] = ctrlx; aoqi@0: right[rightoff + 1] = ctrly; aoqi@0: right[rightoff + 2] = x2; aoqi@0: right[rightoff + 3] = y2; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static double getFlatnessSq(float coords[], int offset) { aoqi@0: return Line2D.ptSegDistSq(coords[offset + 0], coords[offset + 1], aoqi@0: coords[offset + 4], coords[offset + 5], aoqi@0: coords[offset + 2], coords[offset + 3]); aoqi@0: } aoqi@0: aoqi@0: public Test7047069() { aoqi@0: this.squareflat = .0001f * .0001f; aoqi@0: holdIndex = hold.length - 6; aoqi@0: holdEnd = hold.length - 2; aoqi@0: hold[holdIndex + 0] = (float) (Math.random() * 100); aoqi@0: hold[holdIndex + 1] = (float) (Math.random() * 100); aoqi@0: hold[holdIndex + 2] = (float) (Math.random() * 100); aoqi@0: hold[holdIndex + 3] = (float) (Math.random() * 100); aoqi@0: hold[holdIndex + 4] = (float) (Math.random() * 100); aoqi@0: hold[holdIndex + 5] = (float) (Math.random() * 100); aoqi@0: levelIndex = 0; aoqi@0: this.limit = 10; aoqi@0: this.levels = new int[limit + 1]; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Ensures that the hold array can hold up to (want) more values. aoqi@0: * It is currently holding (hold.length - holdIndex) values. aoqi@0: */ aoqi@0: void ensureHoldCapacity(int want) { aoqi@0: if (holdIndex - want < 0) { aoqi@0: int have = hold.length - holdIndex; aoqi@0: int newsize = hold.length + GROW_SIZE; aoqi@0: float newhold[] = new float[newsize]; aoqi@0: System.arraycopy(hold, holdIndex, aoqi@0: newhold, holdIndex + GROW_SIZE, aoqi@0: have); aoqi@0: if (verbose) System.err.println("old hold = "+hold+"["+hold.length+"]"); aoqi@0: if (verbose) System.err.println("replacement hold = "+newhold+"["+newhold.length+"]"); aoqi@0: hold = newhold; aoqi@0: if (verbose) System.err.println("new hold = "+hold+"["+hold.length+"]"); aoqi@0: if (verbose) System.err.println("replacement hold still = "+newhold+"["+newhold.length+"]"); aoqi@0: holdIndex += GROW_SIZE; aoqi@0: holdEnd += GROW_SIZE; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private boolean next() { aoqi@0: if (holdIndex >= holdEnd) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: int level = levels[levelIndex]; aoqi@0: while (level < limit) { aoqi@0: if (getFlatnessSq(hold, holdIndex) < squareflat) { aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: ensureHoldCapacity(4); aoqi@0: subdivide(hold, holdIndex, aoqi@0: hold, holdIndex - 4, aoqi@0: hold, holdIndex); aoqi@0: holdIndex -= 4; aoqi@0: aoqi@0: // Now that we have subdivided, we have constructed aoqi@0: // two curves of one depth lower than the original aoqi@0: // curve. One of those curves is in the place of aoqi@0: // the former curve and one of them is in the next aoqi@0: // set of held coordinate slots. We now set both aoqi@0: // curves level values to the next higher level. aoqi@0: level++; aoqi@0: levels[levelIndex] = level; aoqi@0: levelIndex++; aoqi@0: levels[levelIndex] = level; aoqi@0: } aoqi@0: aoqi@0: // This curve segment is flat enough, or it is too deep aoqi@0: // in recursion levels to try to flatten any more. The aoqi@0: // two coordinates at holdIndex+4 and holdIndex+5 now aoqi@0: // contain the endpoint of the curve which can be the aoqi@0: // endpoint of an approximating line segment. aoqi@0: holdIndex += 4; aoqi@0: levelIndex--; aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public static void main(String argv[]) { aoqi@0: verbose = (argv.length > 0); aoqi@0: for (int i = 0; i < 100000; i++) { aoqi@0: Test7047069 st = new Test7047069(); aoqi@0: while (st.next()) {} aoqi@0: } aoqi@0: } aoqi@0: }