< prev index next >

modules/javafx.graphics/src/main/java/com/sun/marlin/DDasher.java

Print this page

        

*** 41,56 **** */ public final class DDasher implements DPathConsumer2D, MarlinConst { static final int REC_LIMIT = 4; static final double ERR = 0.01d; ! static final double MIN_T_INC = 1d / (1 << REC_LIMIT); // More than 24 bits of mantissa means we can no longer accurately // measure the number of times cycled through the dash array so we // punt and override the phase to just be 0 past that point. ! static final double MAX_CYCLES = 16000000d; private DPathConsumer2D out; private double[] dash; private int dashLen; private double startPhase; --- 41,56 ---- */ public final class DDasher implements DPathConsumer2D, MarlinConst { static final int REC_LIMIT = 4; static final double ERR = 0.01d; ! static final double MIN_T_INC = 1.0d / (1 << REC_LIMIT); // More than 24 bits of mantissa means we can no longer accurately // measure the number of times cycled through the dash array so we // punt and override the phase to just be 0 past that point. ! static final double MAX_CYCLES = 16000000.0d; private DPathConsumer2D out; private double[] dash; private int dashLen; private double startPhase;
*** 114,148 **** this.out = out; // Normalize so 0 <= phase < dash[0] int sidx = 0; dashOn = true; ! double sum = 0d; for (double d : dash) { sum += d; } double cycles = phase / sum; ! if (phase < 0d) { if (-cycles >= MAX_CYCLES) { ! phase = 0d; } else { int fullcycles = FloatMath.floor_int(-cycles); if ((fullcycles & dash.length & 1) != 0) { dashOn = !dashOn; } phase += fullcycles * sum; ! while (phase < 0d) { if (--sidx < 0) { sidx = dash.length - 1; } phase += dash[sidx]; dashOn = !dashOn; } } } else if (phase > 0) { if (cycles >= MAX_CYCLES) { ! phase = 0d; } else { int fullcycles = FloatMath.floor_int(cycles); if ((fullcycles & dash.length & 1) != 0) { dashOn = !dashOn; } --- 114,148 ---- this.out = out; // Normalize so 0 <= phase < dash[0] int sidx = 0; dashOn = true; ! double sum = 0.0d; for (double d : dash) { sum += d; } double cycles = phase / sum; ! if (phase < 0.0d) { if (-cycles >= MAX_CYCLES) { ! phase = 0.0d; } else { int fullcycles = FloatMath.floor_int(-cycles); if ((fullcycles & dash.length & 1) != 0) { dashOn = !dashOn; } phase += fullcycles * sum; ! while (phase < 0.0d) { if (--sidx < 0) { sidx = dash.length - 1; } phase += dash[sidx]; dashOn = !dashOn; } } } else if (phase > 0) { if (cycles >= MAX_CYCLES) { ! phase = 0.0d; } else { int fullcycles = FloatMath.floor_int(cycles); if ((fullcycles & dash.length & 1) != 0) { dashOn = !dashOn; }
*** 175,185 **** * clean up before reusing this instance */ void dispose() { if (DO_CLEAN_DIRTY) { // Force zero-fill dirty arrays: ! Arrays.fill(curCurvepts, 0d); } // Return arrays: if (recycleDashes) { dash = dashes_ref.putArray(dash); } --- 175,185 ---- * clean up before reusing this instance */ void dispose() { if (DO_CLEAN_DIRTY) { // Force zero-fill dirty arrays: ! Arrays.fill(curCurvepts, 0.0d); } // Return arrays: if (recycleDashes) { dash = dashes_ref.putArray(dash); }
*** 294,307 **** public void lineTo(double x1, double y1) { double dx = x1 - x0; double dy = y1 - y0; double len = dx*dx + dy*dy; ! if (len == 0d) { return; } ! len = Math.sqrt(len); // The scaling factors needed to get the dx and dy of the // transformed dash segments. final double cx = dx / len; final double cy = dy / len; --- 294,307 ---- public void lineTo(double x1, double y1) { double dx = x1 - x0; double dy = y1 - y0; double len = dx*dx + dy*dy; ! if (len == 0.0d) { return; } ! len = Math.sqrt(len); // The scaling factors needed to get the dx and dy of the // transformed dash segments. final double cx = dx / len; final double cy = dy / len;
*** 322,342 **** // Advance phase within current dash segment phase += len; // TODO: compare double values using epsilon: if (len == leftInThisDashSegment) { ! phase = 0d; idx = (idx + 1) % dashLen; dashOn = !dashOn; } return; } dashdx = _dash[idx] * cx; dashdy = _dash[idx] * cy; ! if (phase == 0d) { _curCurvepts[0] = x0 + dashdx; _curCurvepts[1] = y0 + dashdy; } else { p = leftInThisDashSegment / _dash[idx]; _curCurvepts[0] = x0 + p * dashdx; --- 322,342 ---- // Advance phase within current dash segment phase += len; // TODO: compare double values using epsilon: if (len == leftInThisDashSegment) { ! phase = 0.0d; idx = (idx + 1) % dashLen; dashOn = !dashOn; } return; } dashdx = _dash[idx] * cx; dashdy = _dash[idx] * cy; ! if (phase == 0.0d) { _curCurvepts[0] = x0 + dashdx; _curCurvepts[1] = y0 + dashdy; } else { p = leftInThisDashSegment / _dash[idx]; _curCurvepts[0] = x0 + p * dashdx;
*** 347,357 **** len -= leftInThisDashSegment; // Advance to next dash segment idx = (idx + 1) % dashLen; dashOn = !dashOn; ! phase = 0d; } } // shared instance in DDasher private final LengthIterator li = new LengthIterator(); --- 347,357 ---- len -= leftInThisDashSegment; // Advance to next dash segment idx = (idx + 1) % dashLen; dashOn = !dashOn; ! phase = 0.0d; } } // shared instance in DDasher private final LengthIterator li = new LengthIterator();
*** 364,397 **** } li.initializeIterationOnCurve(curCurvepts, type); // initially the current curve is at curCurvepts[0...type] int curCurveoff = 0; ! double lastSplitT = 0d; double t; double leftInThisDashSegment = dash[idx] - phase; ! while ((t = li.next(leftInThisDashSegment)) < 1d) { ! if (t != 0d) { ! DHelpers.subdivideAt((t - lastSplitT) / (1d - lastSplitT), curCurvepts, curCurveoff, curCurvepts, 0, curCurvepts, type, type); lastSplitT = t; goTo(curCurvepts, 2, type); curCurveoff = type; } // Advance to next dash segment idx = (idx + 1) % dashLen; dashOn = !dashOn; ! phase = 0d; leftInThisDashSegment = dash[idx]; } goTo(curCurvepts, curCurveoff+2, type); phase += li.lastSegLen(); if (phase >= dash[idx]) { ! phase = 0d; idx = (idx + 1) % dashLen; dashOn = !dashOn; } // reset LengthIterator: li.reset(); --- 364,397 ---- } li.initializeIterationOnCurve(curCurvepts, type); // initially the current curve is at curCurvepts[0...type] int curCurveoff = 0; ! double lastSplitT = 0.0d; double t; double leftInThisDashSegment = dash[idx] - phase; ! while ((t = li.next(leftInThisDashSegment)) < 1.0d) { ! if (t != 0.0d) { ! DHelpers.subdivideAt((t - lastSplitT) / (1.0d - lastSplitT), curCurvepts, curCurveoff, curCurvepts, 0, curCurvepts, type, type); lastSplitT = t; goTo(curCurvepts, 2, type); curCurveoff = type; } // Advance to next dash segment idx = (idx + 1) % dashLen; dashOn = !dashOn; ! phase = 0.0d; leftInThisDashSegment = dash[idx]; } goTo(curCurvepts, curCurveoff+2, type); phase += li.lastSegLen(); if (phase >= dash[idx]) { ! phase = 0.0d; idx = (idx + 1) % dashLen; dashOn = !dashOn; } // reset LengthIterator: li.reset();
*** 442,452 **** private int recLevel; private boolean done; // the lengths of the lines of the control polygon. Only its first // curveType/2 - 1 elements are valid. This is an optimization. See ! // next for more detail. private final double[] curLeafCtrlPolyLengths = new double[3]; LengthIterator() { this.recCurveStack = new double[REC_LIMIT + 1][8]; this.sides = new Side[REC_LIMIT]; --- 442,452 ---- private int recLevel; private boolean done; // the lengths of the lines of the control polygon. Only its first // curveType/2 - 1 elements are valid. This is an optimization. See ! // next() for more detail. private final double[] curLeafCtrlPolyLengths = new double[3]; LengthIterator() { this.recCurveStack = new double[REC_LIMIT + 1][8]; this.sides = new Side[REC_LIMIT];
*** 467,506 **** // keep data dirty // as it appears not useful to reset data: if (DO_CLEAN_DIRTY) { final int recLimit = recCurveStack.length - 1; for (int i = recLimit; i >= 0; i--) { ! Arrays.fill(recCurveStack[i], 0d); } Arrays.fill(sides, Side.LEFT); ! Arrays.fill(curLeafCtrlPolyLengths, 0d); ! Arrays.fill(nextRoots, 0d); ! Arrays.fill(flatLeafCoefCache, 0d); ! flatLeafCoefCache[2] = -1d; } } void initializeIterationOnCurve(double[] pts, int type) { // optimize arraycopy (8 values faster than 6 = type): System.arraycopy(pts, 0, recCurveStack[0], 0, 8); this.curveType = type; this.recLevel = 0; ! this.lastT = 0d; ! this.lenAtLastT = 0d; ! this.nextT = 0d; ! this.lenAtNextT = 0d; goLeft(); // initializes nextT and lenAtNextT properly ! this.lenAtLastSplit = 0d; if (recLevel > 0) { this.sides[0] = Side.LEFT; this.done = false; } else { // the root of the tree is a leaf so we're done. this.sides[0] = Side.RIGHT; this.done = true; } ! this.lastSegLen = 0d; } // 0 == false, 1 == true, -1 == invalid cached value. private int cachedHaveLowAcceleration = -1; --- 467,506 ---- // keep data dirty // as it appears not useful to reset data: if (DO_CLEAN_DIRTY) { final int recLimit = recCurveStack.length - 1; for (int i = recLimit; i >= 0; i--) { ! Arrays.fill(recCurveStack[i], 0.0d); } Arrays.fill(sides, Side.LEFT); ! Arrays.fill(curLeafCtrlPolyLengths, 0.0d); ! Arrays.fill(nextRoots, 0.0d); ! Arrays.fill(flatLeafCoefCache, 0.0d); ! flatLeafCoefCache[2] = -1.0d; } } void initializeIterationOnCurve(double[] pts, int type) { // optimize arraycopy (8 values faster than 6 = type): System.arraycopy(pts, 0, recCurveStack[0], 0, 8); this.curveType = type; this.recLevel = 0; ! this.lastT = 0.0d; ! this.lenAtLastT = 0.0d; ! this.nextT = 0.0d; ! this.lenAtNextT = 0.0d; goLeft(); // initializes nextT and lenAtNextT properly ! this.lenAtLastSplit = 0.0d; if (recLevel > 0) { this.sides[0] = Side.LEFT; this.done = false; } else { // the root of the tree is a leaf so we're done. this.sides[0] = Side.RIGHT; this.done = true; } ! this.lastSegLen = 0.0d; } // 0 == false, 1 == true, -1 == invalid cached value. private int cachedHaveLowAcceleration = -1;
*** 540,560 **** // caches the coefficients of the current leaf in its flattened // form (see inside next() for what that means). The cache is // invalid when it's third element is negative, since in any // valid flattened curve, this would be >= 0. ! private final double[] flatLeafCoefCache = new double[]{0d, 0d, -1d, 0d}; // returns the t value where the remaining curve should be split in // order for the left subdivided curve to have length len. If len // is >= than the length of the uniterated curve, it returns 1. double next(final double len) { final double targetLength = lenAtLastSplit + len; while (lenAtNextT < targetLength) { if (done) { lastSegLen = lenAtNextT - lenAtLastSplit; ! return 1d; } goToNextLeaf(); } lenAtLastSplit = targetLength; final double leaflen = lenAtNextT - lenAtLastT; --- 540,560 ---- // caches the coefficients of the current leaf in its flattened // form (see inside next() for what that means). The cache is // invalid when it's third element is negative, since in any // valid flattened curve, this would be >= 0. ! private final double[] flatLeafCoefCache = new double[]{0.0d, 0.0d, -1.0d, 0.0d}; // returns the t value where the remaining curve should be split in // order for the left subdivided curve to have length len. If len // is >= than the length of the uniterated curve, it returns 1. double next(final double len) { final double targetLength = lenAtLastSplit + len; while (lenAtNextT < targetLength) { if (done) { lastSegLen = lenAtNextT - lenAtLastSplit; ! return 1.0d; } goToNextLeaf(); } lenAtLastSplit = targetLength; final double leaflen = lenAtNextT - lenAtLastT;
*** 567,589 **** // left with a, b, c which define a 1D Bezier curve. We then // solve this to get the parameter of the original leaf that // gives us the desired length. final double[] _flatLeafCoefCache = flatLeafCoefCache; ! if (_flatLeafCoefCache[2] < 0d) { ! double x = 0d + curLeafCtrlPolyLengths[0], ! y = x + curLeafCtrlPolyLengths[1]; if (curveType == 8) { double z = y + curLeafCtrlPolyLengths[2]; ! _flatLeafCoefCache[0] = 3d * (x - y) + z; ! _flatLeafCoefCache[1] = 3d * (y - 2d * x); ! _flatLeafCoefCache[2] = 3d * x; _flatLeafCoefCache[3] = -z; } else if (curveType == 6) { ! _flatLeafCoefCache[0] = 0d; ! _flatLeafCoefCache[1] = y - 2d * x; ! _flatLeafCoefCache[2] = 2d * x; _flatLeafCoefCache[3] = -y; } } double a = _flatLeafCoefCache[0]; double b = _flatLeafCoefCache[1]; --- 567,589 ---- // left with a, b, c which define a 1D Bezier curve. We then // solve this to get the parameter of the original leaf that // gives us the desired length. final double[] _flatLeafCoefCache = flatLeafCoefCache; ! if (_flatLeafCoefCache[2] < 0.0d) { ! double x = curLeafCtrlPolyLengths[0], ! y = x + curLeafCtrlPolyLengths[1]; if (curveType == 8) { double z = y + curLeafCtrlPolyLengths[2]; ! _flatLeafCoefCache[0] = 3.0d * (x - y) + z; ! _flatLeafCoefCache[1] = 3.0d * (y - 2.0d * x); ! _flatLeafCoefCache[2] = 3.0d * x; _flatLeafCoefCache[3] = -z; } else if (curveType == 6) { ! _flatLeafCoefCache[0] = 0.0d; ! _flatLeafCoefCache[1] = y - 2.0d * x; ! _flatLeafCoefCache[2] = 2.0d * x; _flatLeafCoefCache[3] = -y; } } double a = _flatLeafCoefCache[0]; double b = _flatLeafCoefCache[1];
*** 591,610 **** double d = t * _flatLeafCoefCache[3]; // we use cubicRootsInAB here, because we want only roots in 0, 1, // and our quadratic root finder doesn't filter, so it's just a // matter of convenience. ! int n = DHelpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0d, 1d); if (n == 1 && !Double.isNaN(nextRoots[0])) { t = nextRoots[0]; } } // t is relative to the current leaf, so we must make it a valid parameter // of the original curve. t = t * (nextT - lastT) + lastT; ! if (t >= 1d) { ! t = 1d; done = true; } // even if done = true, if we're here, that means targetLength // is equal to, or very, very close to the total length of the // curve, so lastSegLen won't be too high. In cases where len --- 591,610 ---- double d = t * _flatLeafCoefCache[3]; // we use cubicRootsInAB here, because we want only roots in 0, 1, // and our quadratic root finder doesn't filter, so it's just a // matter of convenience. ! int n = DHelpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0d, 1.0d); if (n == 1 && !Double.isNaN(nextRoots[0])) { t = nextRoots[0]; } } // t is relative to the current leaf, so we must make it a valid parameter // of the original curve. t = t * (nextT - lastT) + lastT; ! if (t >= 1.0d) { ! t = 1.0d; done = true; } // even if done = true, if we're here, that means targetLength // is equal to, or very, very close to the total length of the // curve, so lastSegLen won't be too high. In cases where len
*** 647,663 **** } // go to the leftmost node from the current node. Return its length. private void goLeft() { double len = onLeaf(); ! if (len >= 0d) { lastT = nextT; lenAtLastT = lenAtNextT; nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC; lenAtNextT += len; // invalidate caches ! flatLeafCoefCache[2] = -1d; cachedHaveLowAcceleration = -1; } else { DHelpers.subdivide(recCurveStack[recLevel], 0, recCurveStack[recLevel+1], 0, recCurveStack[recLevel], 0, curveType); --- 647,663 ---- } // go to the leftmost node from the current node. Return its length. private void goLeft() { double len = onLeaf(); ! if (len >= 0.0d) { lastT = nextT; lenAtLastT = lenAtNextT; nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC; lenAtNextT += len; // invalidate caches ! flatLeafCoefCache[2] = -1.0d; cachedHaveLowAcceleration = -1; } else { DHelpers.subdivide(recCurveStack[recLevel], 0, recCurveStack[recLevel+1], 0, recCurveStack[recLevel], 0, curveType);
*** 669,679 **** // this is a bit of a hack. It returns -1 if we're not on a leaf, and // the length of the leaf if we are on a leaf. private double onLeaf() { double[] curve = recCurveStack[recLevel]; ! double polyLen = 0d; double x0 = curve[0], y0 = curve[1]; for (int i = 2; i < curveType; i += 2) { final double x1 = curve[i], y1 = curve[i+1]; final double len = DHelpers.linelen(x0, y0, x1, y1); --- 669,679 ---- // this is a bit of a hack. It returns -1 if we're not on a leaf, and // the length of the leaf if we are on a leaf. private double onLeaf() { double[] curve = recCurveStack[recLevel]; ! double polyLen = 0.0d; double x0 = curve[0], y0 = curve[1]; for (int i = 2; i < curveType; i += 2) { final double x1 = curve[i], y1 = curve[i+1]; final double len = DHelpers.linelen(x0, y0, x1, y1);
*** 685,697 **** final double lineLen = DHelpers.linelen(curve[0], curve[1], curve[curveType-2], curve[curveType-1]); if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) { ! return (polyLen + lineLen) / 2d; } ! return -1d; } } @Override public void curveTo(double x1, double y1, --- 685,697 ---- final double lineLen = DHelpers.linelen(curve[0], curve[1], curve[curveType-2], curve[curveType-1]); if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) { ! return (polyLen + lineLen) / 2.0d; } ! return -1.0d; } } @Override public void curveTo(double x1, double y1,
< prev index next >